factgenerator / app.py
sivan26's picture
Update app.py
65f4c3e verified
import gradio as gr
import requests
import random
from transformers import pipeline
classifier = pipeline("zero-shot-classification", model="valhalla/distilbart-mnli-12-3")
labels = [
"animals", "people", "places", "history", "science", "art", "technology",
"sports", "food", "clothing", "home", "entertainment", "education", "nature", "transportation"
]
def preprocess_topic(topic):
topic = topic.lower().strip()
mapping = {
"shirt": "clothing item shirt",
"jeans": "clothing item jeans",
"shoes": "clothing item shoes",
"dress": "clothing item dress",
"sandals": "clothing item sandals",
"cookie": "sweet snack cookie",
"orcas": "marine mammal orcas",
"penguin": "bird that swims penguin",
"whale": "large marine animal whale",
"floor": "interior surface floor",
"blanket": "household item blanket",
"bed": "furniture item bed",
"lamp": "household lighting lamp",
"girl": "young person female",
"bag": "fashion accessory bag",
"kids": "young people children",
}
return mapping.get(topic, topic)
random_topics = [
"cats", "space", "chocolate", "Egypt", "Leonardo da Vinci",
"volcanoes", "Tokyo", "honeybees", "quantum physics", "orcas"
]
def get_wikipedia_facts(topic):
if not topic.strip():
return "Please enter a topic or use 'Surprise me!'", None, None
headers = {
"User-Agent": "SmartFactApp/1.0"
}
params = {
"action": "query",
"format": "json",
"prop": "extracts|pageimages",
"exintro": True,
"explaintext": True,
"piprop": "thumbnail",
"pithumbsize": 400,
"generator": "search",
"gsrsearch": topic,
"gsrlimit": 1,
}
try:
response = requests.get("https://en.wikipedia.org/w/api.php", params=params, headers=headers, timeout=5)
data = response.json()
pages = data.get("query", {}).get("pages", {})
if not pages:
return f"Sorry, no information found for '{topic}'.", None, None
page = next(iter(pages.values()))
extract_text = page.get("extract", "")
image_url = page.get("thumbnail", {}).get("source", None)
sentences = [s.strip() for s in extract_text.replace("\n", " ").split(". ") if s.strip()]
facts = random.sample(sentences, min(3, len(sentences)))
facts = [fact if fact.endswith(".") else fact + "." for fact in facts]
facts_text = "\n\n".join(f"πŸ’‘ {fact}" for fact in facts)
processed_input = preprocess_topic(topic)
classification = classifier(processed_input, candidate_labels=labels)
top_labels = classification["labels"][:3]
top_scores = classification["scores"][:3]
classification_text = "🧠 Top categories:\n"
if top_scores[0] < 0.3:
classification_text = "🧠 Category uncertain.\n" + classification_text
for label, score in zip(top_labels, top_scores):
classification_text += f"- {label} ({score:.2%})\n"
return facts_text, image_url, classification_text
except Exception as e:
print("Error:", e)
return "Oops! Something went wrong while fetching your facts.", None, None
def surprise_topic(_):
topic = random.choice(random_topics)
return get_wikipedia_facts(topic)
with gr.Blocks() as demo:
gr.HTML("""
<style>
body {
background-color: #ADD8E6 !important;
}
.gradio-container {
background-color: transparent !important;
}
</style>
""")
gr.Markdown("""
# 🌍 Smart Wikipedia Fact Finder
Search any topic and discover:
- πŸ“š Three interesting facts
- πŸ–ΌοΈ A related image
- 🧠 AI-predicted topic category
πŸ‘‰ Try something like **"bed"**, **"quantum physics"**, or click **🎲 Surprise me!**
""")
with gr.Row():
with gr.Column(scale=3):
topic_input = gr.Textbox(label="Enter a Topic", placeholder="e.g. Eiffel Tower, cookies, World War II")
with gr.Column(scale=1):
surprise_button = gr.Button("🎲 Surprise me!")
gr.Markdown("---")
with gr.Row():
with gr.Column():
facts_output = gr.Textbox(label="πŸ“š Wikipedia Facts", lines=6)
classification_output = gr.Textbox(label="🧠 Topic Classification")
with gr.Column():
image_output = gr.Image(label="πŸ–ΌοΈ Related Image")
topic_input.submit(get_wikipedia_facts, inputs=topic_input, outputs=[facts_output, image_output, classification_output])
surprise_button.click(surprise_topic, inputs=None, outputs=[facts_output, image_output, classification_output])
if __name__ == "__main__":
demo.launch()