Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,7 +6,7 @@ from transformers import pipeline
|
|
| 6 |
# Initialize classifier once
|
| 7 |
classifier = pipeline("zero-shot-classification", model="valhalla/distilbart-mnli-12-3")
|
| 8 |
|
| 9 |
-
# Expanded
|
| 10 |
labels = [
|
| 11 |
# Living things
|
| 12 |
"animals", "pets", "dogs", "cats", "birds", "insects", "plants", "trees", "flowers", "marine life", "fish", "reptiles",
|
|
@@ -29,6 +29,8 @@ labels = [
|
|
| 29 |
# Business and economy
|
| 30 |
"business", "finance", "marketing", "economics", "startups", "career", "work", "law", "politics", "government", "military",
|
| 31 |
"crime", "education", "schools", "universities", "news", "media", "social media", "apps", "brands", "products",
|
|
|
|
|
|
|
| 32 |
# Abstract and other
|
| 33 |
"philosophy", "emotions", "relationships", "travel", "transportation", "vehicles", "cars", "bikes", "trains", "airplanes",
|
| 34 |
"spacecraft", "nature", "oceans", "forests", "mountains"
|
|
@@ -39,7 +41,19 @@ random_topics = [
|
|
| 39 |
"volcanoes", "Tokyo", "honeybees", "quantum physics", "orcas"
|
| 40 |
]
|
| 41 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
def get_wikipedia_facts(topic):
|
|
|
|
|
|
|
|
|
|
| 43 |
if not topic.strip():
|
| 44 |
return "Please enter a topic or use 'Surprise me!'", None, None
|
| 45 |
|
|
@@ -66,7 +80,9 @@ def get_wikipedia_facts(topic):
|
|
| 66 |
|
| 67 |
pages = data.get("query", {}).get("pages", {})
|
| 68 |
if not pages:
|
| 69 |
-
|
|
|
|
|
|
|
| 70 |
|
| 71 |
page = next(iter(pages.values()))
|
| 72 |
extract_text = page.get("extract", "")
|
|
@@ -82,7 +98,7 @@ def get_wikipedia_facts(topic):
|
|
| 82 |
facts = [fact if fact.endswith(".") else fact + "." for fact in facts]
|
| 83 |
facts_text = "\n\n".join(f"💡 {fact}" for fact in facts)
|
| 84 |
|
| 85 |
-
classification = classifier(topic, candidate_labels=labels)
|
| 86 |
top_labels = classification["labels"][:3]
|
| 87 |
top_scores = classification["scores"][:3]
|
| 88 |
|
|
@@ -93,11 +109,15 @@ def get_wikipedia_facts(topic):
|
|
| 93 |
for label, score in zip(top_labels, top_scores):
|
| 94 |
classification_text += f"- {label} ({score:.2%})\n"
|
| 95 |
|
| 96 |
-
|
|
|
|
|
|
|
| 97 |
|
| 98 |
except Exception as e:
|
| 99 |
print("Error:", e)
|
| 100 |
-
|
|
|
|
|
|
|
| 101 |
|
| 102 |
def surprise_topic(_):
|
| 103 |
topic = random.choice(random_topics)
|
|
@@ -149,3 +169,4 @@ if __name__ == "__main__":
|
|
| 149 |
demo.launch()
|
| 150 |
|
| 151 |
|
|
|
|
|
|
| 6 |
# Initialize classifier once
|
| 7 |
classifier = pipeline("zero-shot-classification", model="valhalla/distilbart-mnli-12-3")
|
| 8 |
|
| 9 |
+
# Expanded label list with clothing/fashion added
|
| 10 |
labels = [
|
| 11 |
# Living things
|
| 12 |
"animals", "pets", "dogs", "cats", "birds", "insects", "plants", "trees", "flowers", "marine life", "fish", "reptiles",
|
|
|
|
| 29 |
# Business and economy
|
| 30 |
"business", "finance", "marketing", "economics", "startups", "career", "work", "law", "politics", "government", "military",
|
| 31 |
"crime", "education", "schools", "universities", "news", "media", "social media", "apps", "brands", "products",
|
| 32 |
+
# Clothing / fashion additions
|
| 33 |
+
"clothing", "shirt", "apparel", "fashion", "garment", "textile", "t-shirt", "jeans", "pants", "dress", "shoes", "accessories",
|
| 34 |
# Abstract and other
|
| 35 |
"philosophy", "emotions", "relationships", "travel", "transportation", "vehicles", "cars", "bikes", "trains", "airplanes",
|
| 36 |
"spacecraft", "nature", "oceans", "forests", "mountains"
|
|
|
|
| 41 |
"volcanoes", "Tokyo", "honeybees", "quantum physics", "orcas"
|
| 42 |
]
|
| 43 |
|
| 44 |
+
# Simple cache dictionary
|
| 45 |
+
cache = {}
|
| 46 |
+
|
| 47 |
+
def preprocess_topic(topic):
|
| 48 |
+
if len(topic.split()) == 1:
|
| 49 |
+
# Add a hint for single-word topics to improve classification
|
| 50 |
+
return "clothing item " + topic if topic.lower() in ["shirt", "dress", "jeans", "pants", "shoes", "t-shirt"] else topic
|
| 51 |
+
return topic
|
| 52 |
+
|
| 53 |
def get_wikipedia_facts(topic):
|
| 54 |
+
if topic in cache:
|
| 55 |
+
return cache[topic]
|
| 56 |
+
|
| 57 |
if not topic.strip():
|
| 58 |
return "Please enter a topic or use 'Surprise me!'", None, None
|
| 59 |
|
|
|
|
| 80 |
|
| 81 |
pages = data.get("query", {}).get("pages", {})
|
| 82 |
if not pages:
|
| 83 |
+
result = (f"Sorry, no information found for '{topic}'.", None, None)
|
| 84 |
+
cache[topic] = result
|
| 85 |
+
return result
|
| 86 |
|
| 87 |
page = next(iter(pages.values()))
|
| 88 |
extract_text = page.get("extract", "")
|
|
|
|
| 98 |
facts = [fact if fact.endswith(".") else fact + "." for fact in facts]
|
| 99 |
facts_text = "\n\n".join(f"💡 {fact}" for fact in facts)
|
| 100 |
|
| 101 |
+
classification = classifier(preprocess_topic(topic), candidate_labels=labels)
|
| 102 |
top_labels = classification["labels"][:3]
|
| 103 |
top_scores = classification["scores"][:3]
|
| 104 |
|
|
|
|
| 109 |
for label, score in zip(top_labels, top_scores):
|
| 110 |
classification_text += f"- {label} ({score:.2%})\n"
|
| 111 |
|
| 112 |
+
result = (facts_text, image_url, classification_text)
|
| 113 |
+
cache[topic] = result
|
| 114 |
+
return result
|
| 115 |
|
| 116 |
except Exception as e:
|
| 117 |
print("Error:", e)
|
| 118 |
+
result = ("Oops! Something went wrong while fetching your facts.", None, None)
|
| 119 |
+
cache[topic] = result
|
| 120 |
+
return result
|
| 121 |
|
| 122 |
def surprise_topic(_):
|
| 123 |
topic = random.choice(random_topics)
|
|
|
|
| 169 |
demo.launch()
|
| 170 |
|
| 171 |
|
| 172 |
+
|