Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -25,7 +25,7 @@ def get_wikipedia_facts(topic):
|
|
| 25 |
}
|
| 26 |
|
| 27 |
try:
|
| 28 |
-
|
| 29 |
search_response = requests.get(search_url, params=search_params, headers=headers)
|
| 30 |
time.sleep(0.3)
|
| 31 |
search_response.raise_for_status()
|
|
@@ -37,7 +37,7 @@ def get_wikipedia_facts(topic):
|
|
| 37 |
|
| 38 |
best_title = search_hits[0]["title"]
|
| 39 |
|
| 40 |
-
|
| 41 |
extract_params = {
|
| 42 |
"action": "query",
|
| 43 |
"format": "json",
|
|
@@ -61,5 +61,61 @@ def get_wikipedia_facts(topic):
|
|
| 61 |
if not extract_text:
|
| 62 |
return f"Sorry, no extract found for '{topic}'.", None, None
|
| 63 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
|
| 65 |
|
|
|
|
| 25 |
}
|
| 26 |
|
| 27 |
try:
|
| 28 |
+
|
| 29 |
search_response = requests.get(search_url, params=search_params, headers=headers)
|
| 30 |
time.sleep(0.3)
|
| 31 |
search_response.raise_for_status()
|
|
|
|
| 37 |
|
| 38 |
best_title = search_hits[0]["title"]
|
| 39 |
|
| 40 |
+
|
| 41 |
extract_params = {
|
| 42 |
"action": "query",
|
| 43 |
"format": "json",
|
|
|
|
| 61 |
if not extract_text:
|
| 62 |
return f"Sorry, no extract found for '{topic}'.", None, None
|
| 63 |
|
| 64 |
+
|
| 65 |
+
sentences = [s.strip() for s in extract_text.replace("\n", " ").split(". ") if s.strip()]
|
| 66 |
+
if not sentences:
|
| 67 |
+
return f"Sorry, no facts available for '{topic}'.", None, None
|
| 68 |
+
|
| 69 |
+
|
| 70 |
+
facts = random.sample(sentences, min(3, len(sentences)))
|
| 71 |
+
facts = [fact if fact.endswith(".") else fact + "." for fact in facts]
|
| 72 |
+
facts_text = "\n\n".join(f"💡 {fact}" for fact in facts)
|
| 73 |
+
|
| 74 |
+
# Get Wikipedia image URL
|
| 75 |
+
image_url = None
|
| 76 |
+
if "thumbnail" in page:
|
| 77 |
+
image_url = page["thumbnail"].get("source")
|
| 78 |
+
|
| 79 |
+
|
| 80 |
+
candidate_labels = ["history", "science", "technology", "art", "geography", "biology", "music", "sports", "politics"]
|
| 81 |
+
classification = classifier(topic, candidate_labels)
|
| 82 |
+
top_label = classification['labels'][0]
|
| 83 |
+
top_score = classification['scores'][0]
|
| 84 |
+
classification_text = f"Category: {top_label} (confidence: {top_score:.2f})"
|
| 85 |
+
|
| 86 |
+
return facts_text, image_url, classification_text
|
| 87 |
+
|
| 88 |
+
except Exception as e:
|
| 89 |
+
print("Error:", e)
|
| 90 |
+
return "Oops! Something went wrong while fetching your facts.", None, None
|
| 91 |
+
|
| 92 |
+
|
| 93 |
+
random_topics = [
|
| 94 |
+
"cats", "space", "chocolate", "Egypt", "Leonardo da Vinci",
|
| 95 |
+
"volcanoes", "Tokyo", "honeybees", "quantum physics", "orcas"
|
| 96 |
+
]
|
| 97 |
+
|
| 98 |
+
def surprise_topic(_):
|
| 99 |
+
topic = random.choice(random_topics)
|
| 100 |
+
return get_wikipedia_facts(topic)
|
| 101 |
+
|
| 102 |
+
with gr.Blocks() as demo:
|
| 103 |
+
gr.Markdown("## 🤖 Wikipedia Facts + Image + Topic Classification\nEnter a topic or click 'Surprise me!'")
|
| 104 |
+
|
| 105 |
+
with gr.Row():
|
| 106 |
+
topic_input = gr.Textbox(placeholder="Enter a topic like 'koalas' or 'Eiffel Tower'")
|
| 107 |
+
surprise_button = gr.Button("🎲 Surprise me!")
|
| 108 |
+
|
| 109 |
+
facts_output = gr.Textbox(label="3 Wikipedia Facts")
|
| 110 |
+
image_output = gr.Image(label="Image from Wikipedia")
|
| 111 |
+
classification_output = gr.Textbox(label="Topic Classification")
|
| 112 |
+
|
| 113 |
+
topic_input.submit(get_wikipedia_facts, inputs=topic_input, outputs=[facts_output, image_output, classification_output])
|
| 114 |
+
surprise_button.click(surprise_topic, inputs=None, outputs=[facts_output, image_output, classification_output])
|
| 115 |
+
|
| 116 |
+
if __name__ == "__main__":
|
| 117 |
+
demo.launch()
|
| 118 |
+
|
| 119 |
+
|
| 120 |
|
| 121 |
|