Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import wikipedia
|
| 3 |
+
|
| 4 |
+
wikipedia.set_lang("en")
|
| 5 |
+
|
| 6 |
+
def science_bot(user_input):
|
| 7 |
+
topic = user_input.lower().strip()
|
| 8 |
+
for prefix in ["what is", "define", "explain", "tell me about"]:
|
| 9 |
+
if topic.startswith(prefix):
|
| 10 |
+
topic = topic.replace(prefix, "").strip()
|
| 11 |
+
|
| 12 |
+
try:
|
| 13 |
+
return wikipedia.summary(topic, sentences=3)
|
| 14 |
+
except wikipedia.exceptions.DisambiguationError as e:
|
| 15 |
+
return f"⚠️ That topic is ambiguous. Try one of these:\n{', '.join(e.options[:5])}"
|
| 16 |
+
except wikipedia.exceptions.PageError:
|
| 17 |
+
try:
|
| 18 |
+
return wikipedia.summary(topic.title(), sentences=3)
|
| 19 |
+
except:
|
| 20 |
+
try:
|
| 21 |
+
return wikipedia.summary(f"{topic} (physics)", sentences=3)
|
| 22 |
+
except:
|
| 23 |
+
return f"❌ I couldn't find anything on '{topic}'. Try a different science topic."
|
| 24 |
+
|
| 25 |
+
demo = gr.Interface(
|
| 26 |
+
fn=science_bot,
|
| 27 |
+
inputs=gr.Textbox(lines=1, placeholder="Ask a science question..."),
|
| 28 |
+
outputs=gr.Textbox(lines=8, label="🧠 Science Answer"),
|
| 29 |
+
title="🔬 SciWhiz: Science Chatbot",
|
| 30 |
+
description="Ask me science questions. I’ll answer using Wikipedia!"
|
| 31 |
+
)
|
| 32 |
+
|
| 33 |
+
demo.launch()
|