Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,7 +5,7 @@ import time
|
|
| 5 |
from transformers import pipeline
|
| 6 |
|
| 7 |
|
| 8 |
-
classifier = pipeline("zero-shot-classification")
|
| 9 |
|
| 10 |
def get_wikipedia_facts(topic):
|
| 11 |
if not topic.strip():
|
|
@@ -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()
|
|
@@ -61,24 +61,21 @@ def get_wikipedia_facts(topic):
|
|
| 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 |
-
|
| 81 |
-
|
|
|
|
|
|
|
| 82 |
top_label = classification['labels'][0]
|
| 83 |
top_score = classification['scores'][0]
|
| 84 |
classification_text = f"Category: {top_label} (confidence: {top_score:.2f})"
|
|
@@ -89,7 +86,6 @@ def get_wikipedia_facts(topic):
|
|
| 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"
|
|
@@ -99,16 +95,17 @@ 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
|
| 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])
|
|
@@ -117,5 +114,6 @@ if __name__ == "__main__":
|
|
| 117 |
demo.launch()
|
| 118 |
|
| 119 |
|
|
|
|
| 120 |
|
| 121 |
|
|
|
|
| 5 |
from transformers import pipeline
|
| 6 |
|
| 7 |
|
| 8 |
+
classifier = pipeline("zero-shot-classification", model="valhalla/distilbart-mnli-12-3")
|
| 9 |
|
| 10 |
def get_wikipedia_facts(topic):
|
| 11 |
if not topic.strip():
|
|
|
|
| 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()
|
|
|
|
| 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 |
facts = random.sample(sentences, min(3, len(sentences)))
|
| 70 |
facts = [fact if fact.endswith(".") else fact + "." for fact in facts]
|
| 71 |
facts_text = "\n\n".join(f"💡 {fact}" for fact in facts)
|
| 72 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
|
| 74 |
+
image_url = page.get("thumbnail", {}).get("source", None)
|
| 75 |
+
|
| 76 |
+
|
| 77 |
+
labels = ["history", "science", "technology", "art", "geography", "biology", "music", "sports", "politics"]
|
| 78 |
+
classification = classifier(topic, labels)
|
| 79 |
top_label = classification['labels'][0]
|
| 80 |
top_score = classification['scores'][0]
|
| 81 |
classification_text = f"Category: {top_label} (confidence: {top_score:.2f})"
|
|
|
|
| 86 |
print("Error:", e)
|
| 87 |
return "Oops! Something went wrong while fetching your facts.", None, None
|
| 88 |
|
|
|
|
| 89 |
random_topics = [
|
| 90 |
"cats", "space", "chocolate", "Egypt", "Leonardo da Vinci",
|
| 91 |
"volcanoes", "Tokyo", "honeybees", "quantum physics", "orcas"
|
|
|
|
| 95 |
topic = random.choice(random_topics)
|
| 96 |
return get_wikipedia_facts(topic)
|
| 97 |
|
| 98 |
+
|
| 99 |
with gr.Blocks() as demo:
|
| 100 |
+
gr.Markdown("## 🤖 Wikipedia Facts + Image + Topic Classification (Fast CPU Version)")
|
| 101 |
|
| 102 |
with gr.Row():
|
| 103 |
topic_input = gr.Textbox(placeholder="Enter a topic like 'koalas' or 'Eiffel Tower'")
|
| 104 |
surprise_button = gr.Button("🎲 Surprise me!")
|
| 105 |
|
| 106 |
+
facts_output = gr.Textbox(label="📚 3 Wikipedia Facts")
|
| 107 |
+
image_output = gr.Image(label="🖼️ Image from Wikipedia")
|
| 108 |
+
classification_output = gr.Textbox(label="🧠 Topic Classification")
|
| 109 |
|
| 110 |
topic_input.submit(get_wikipedia_facts, inputs=topic_input, outputs=[facts_output, image_output, classification_output])
|
| 111 |
surprise_button.click(surprise_topic, inputs=None, outputs=[facts_output, image_output, classification_output])
|
|
|
|
| 114 |
demo.launch()
|
| 115 |
|
| 116 |
|
| 117 |
+
|
| 118 |
|
| 119 |
|