Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,22 +4,22 @@ import random
|
|
| 4 |
import time
|
| 5 |
from transformers import pipeline
|
| 6 |
|
| 7 |
-
# Load zero-shot classification
|
| 8 |
classifier = pipeline("zero-shot-classification", model="valhalla/distilbart-mnli-12-3")
|
| 9 |
|
| 10 |
-
#
|
| 11 |
random_topics = [
|
| 12 |
"cats", "space", "chocolate", "Egypt", "Leonardo da Vinci",
|
| 13 |
"volcanoes", "Tokyo", "honeybees", "quantum physics", "orcas"
|
| 14 |
]
|
| 15 |
|
| 16 |
-
# Main function
|
| 17 |
def get_wikipedia_facts(topic):
|
| 18 |
if not topic.strip():
|
| 19 |
return "Please enter a topic or use 'Surprise me!'", None, None
|
| 20 |
|
| 21 |
headers = {
|
| 22 |
-
"User-Agent": "RandomFactApp/3.0"
|
| 23 |
}
|
| 24 |
|
| 25 |
search_url = "https://en.wikipedia.org/w/api.php"
|
|
@@ -32,16 +32,18 @@ def get_wikipedia_facts(topic):
|
|
| 32 |
}
|
| 33 |
|
| 34 |
try:
|
|
|
|
| 35 |
search_response = requests.get(search_url, params=search_params, headers=headers)
|
| 36 |
time.sleep(0.3)
|
| 37 |
search_data = search_response.json()
|
| 38 |
|
| 39 |
search_hits = search_data.get("query", {}).get("search", [])
|
| 40 |
if not search_hits:
|
| 41 |
-
return f"
|
| 42 |
|
| 43 |
best_title = search_hits[0]["title"]
|
| 44 |
|
|
|
|
| 45 |
extract_params = {
|
| 46 |
"action": "query",
|
| 47 |
"format": "json",
|
|
@@ -62,15 +64,21 @@ def get_wikipedia_facts(topic):
|
|
| 62 |
extract_text = page.get("extract", "")
|
| 63 |
|
| 64 |
if not extract_text:
|
| 65 |
-
return f"
|
| 66 |
|
|
|
|
| 67 |
sentences = [s.strip() for s in extract_text.replace("\n", " ").split(". ") if s.strip()]
|
|
|
|
|
|
|
|
|
|
| 68 |
facts = random.sample(sentences, min(3, len(sentences)))
|
| 69 |
facts = [fact if fact.endswith(".") else fact + "." for fact in facts]
|
| 70 |
facts_text = "\n\n".join(f"π‘ {fact}" for fact in facts)
|
| 71 |
|
|
|
|
| 72 |
image_url = page.get("thumbnail", {}).get("source", None)
|
| 73 |
|
|
|
|
| 74 |
labels = ["history", "science", "technology", "art", "geography", "biology", "music", "sports", "politics"]
|
| 75 |
classification = classifier(topic, labels)
|
| 76 |
top_label = classification["labels"][0]
|
|
@@ -81,21 +89,24 @@ def get_wikipedia_facts(topic):
|
|
| 81 |
|
| 82 |
except Exception as e:
|
| 83 |
print("Error:", e)
|
| 84 |
-
return "Something went wrong
|
| 85 |
|
| 86 |
-
# Surprise function
|
| 87 |
def surprise_topic(_):
|
| 88 |
topic = random.choice(random_topics)
|
| 89 |
return get_wikipedia_facts(topic)
|
| 90 |
|
| 91 |
-
# Gradio UI
|
| 92 |
-
with gr.Blocks(
|
| 93 |
gr.Markdown("""
|
| 94 |
-
#
|
| 95 |
-
|
|
|
|
|
|
|
|
|
|
| 96 |
|
| 97 |
-
|
| 98 |
-
""")
|
| 99 |
|
| 100 |
with gr.Row():
|
| 101 |
with gr.Column(scale=3):
|
|
@@ -110,16 +121,16 @@ Type a topic (like `quantum physics`) or click **π² Surprise me!**
|
|
| 110 |
facts_output = gr.Textbox(label="π Wikipedia Facts", lines=6)
|
| 111 |
classification_output = gr.Textbox(label="π§ Topic Classification")
|
| 112 |
with gr.Column():
|
| 113 |
-
image_output = gr.Image(label="πΌοΈ
|
| 114 |
|
|
|
|
| 115 |
topic_input.submit(get_wikipedia_facts, inputs=topic_input, outputs=[facts_output, image_output, classification_output])
|
| 116 |
surprise_button.click(surprise_topic, inputs=None, outputs=[facts_output, image_output, classification_output])
|
| 117 |
|
|
|
|
| 118 |
if __name__ == "__main__":
|
| 119 |
demo.launch()
|
| 120 |
|
| 121 |
|
| 122 |
-
|
| 123 |
-
|
| 124 |
|
| 125 |
|
|
|
|
| 4 |
import time
|
| 5 |
from transformers import pipeline
|
| 6 |
|
| 7 |
+
# Load the zero-shot classification pipeline
|
| 8 |
classifier = pipeline("zero-shot-classification", model="valhalla/distilbart-mnli-12-3")
|
| 9 |
|
| 10 |
+
# List of random topics for the "Surprise me" button
|
| 11 |
random_topics = [
|
| 12 |
"cats", "space", "chocolate", "Egypt", "Leonardo da Vinci",
|
| 13 |
"volcanoes", "Tokyo", "honeybees", "quantum physics", "orcas"
|
| 14 |
]
|
| 15 |
|
| 16 |
+
# Main function: get Wikipedia extract, image, and classify topic
|
| 17 |
def get_wikipedia_facts(topic):
|
| 18 |
if not topic.strip():
|
| 19 |
return "Please enter a topic or use 'Surprise me!'", None, None
|
| 20 |
|
| 21 |
headers = {
|
| 22 |
+
"User-Agent": "RandomFactApp/3.0 (https://huggingface.co/spaces/yourname) Python requests"
|
| 23 |
}
|
| 24 |
|
| 25 |
search_url = "https://en.wikipedia.org/w/api.php"
|
|
|
|
| 32 |
}
|
| 33 |
|
| 34 |
try:
|
| 35 |
+
# Step 1: Search Wikipedia
|
| 36 |
search_response = requests.get(search_url, params=search_params, headers=headers)
|
| 37 |
time.sleep(0.3)
|
| 38 |
search_data = search_response.json()
|
| 39 |
|
| 40 |
search_hits = search_data.get("query", {}).get("search", [])
|
| 41 |
if not search_hits:
|
| 42 |
+
return f"Sorry, no information found for '{topic}'.", None, None
|
| 43 |
|
| 44 |
best_title = search_hits[0]["title"]
|
| 45 |
|
| 46 |
+
# Step 2: Get page extract and image
|
| 47 |
extract_params = {
|
| 48 |
"action": "query",
|
| 49 |
"format": "json",
|
|
|
|
| 64 |
extract_text = page.get("extract", "")
|
| 65 |
|
| 66 |
if not extract_text:
|
| 67 |
+
return f"Sorry, no extract found for '{topic}'.", None, None
|
| 68 |
|
| 69 |
+
# Format extract into short facts
|
| 70 |
sentences = [s.strip() for s in extract_text.replace("\n", " ").split(". ") if s.strip()]
|
| 71 |
+
if not sentences:
|
| 72 |
+
return f"Sorry, no facts available for '{topic}'.", None, None
|
| 73 |
+
|
| 74 |
facts = random.sample(sentences, min(3, len(sentences)))
|
| 75 |
facts = [fact if fact.endswith(".") else fact + "." for fact in facts]
|
| 76 |
facts_text = "\n\n".join(f"π‘ {fact}" for fact in facts)
|
| 77 |
|
| 78 |
+
# Get image URL
|
| 79 |
image_url = page.get("thumbnail", {}).get("source", None)
|
| 80 |
|
| 81 |
+
# Zero-shot classification
|
| 82 |
labels = ["history", "science", "technology", "art", "geography", "biology", "music", "sports", "politics"]
|
| 83 |
classification = classifier(topic, labels)
|
| 84 |
top_label = classification["labels"][0]
|
|
|
|
| 89 |
|
| 90 |
except Exception as e:
|
| 91 |
print("Error:", e)
|
| 92 |
+
return "Oops! Something went wrong while fetching your facts.", None, None
|
| 93 |
|
| 94 |
+
# Surprise topic function
|
| 95 |
def surprise_topic(_):
|
| 96 |
topic = random.choice(random_topics)
|
| 97 |
return get_wikipedia_facts(topic)
|
| 98 |
|
| 99 |
+
# Gradio UI layout
|
| 100 |
+
with gr.Blocks() as demo:
|
| 101 |
gr.Markdown("""
|
| 102 |
+
# π Smart Wikipedia Fact Finder
|
| 103 |
+
Search any topic and discover:
|
| 104 |
+
- π Three interesting facts
|
| 105 |
+
- πΌοΈ A related image
|
| 106 |
+
- π§ AI-predicted topic category
|
| 107 |
|
| 108 |
+
π Try something like **"koalas"**, **"quantum physics"**, or click **π² Surprise me!**
|
| 109 |
+
""")
|
| 110 |
|
| 111 |
with gr.Row():
|
| 112 |
with gr.Column(scale=3):
|
|
|
|
| 121 |
facts_output = gr.Textbox(label="π Wikipedia Facts", lines=6)
|
| 122 |
classification_output = gr.Textbox(label="π§ Topic Classification")
|
| 123 |
with gr.Column():
|
| 124 |
+
image_output = gr.Image(label="πΌοΈ Related Image")
|
| 125 |
|
| 126 |
+
# Link functions to inputs
|
| 127 |
topic_input.submit(get_wikipedia_facts, inputs=topic_input, outputs=[facts_output, image_output, classification_output])
|
| 128 |
surprise_button.click(surprise_topic, inputs=None, outputs=[facts_output, image_output, classification_output])
|
| 129 |
|
| 130 |
+
# Run the app
|
| 131 |
if __name__ == "__main__":
|
| 132 |
demo.launch()
|
| 133 |
|
| 134 |
|
|
|
|
|
|
|
| 135 |
|
| 136 |
|