sivan26 commited on
Commit
f5edebd
Β·
verified Β·
1 Parent(s): 60c69c6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -48
app.py CHANGED
@@ -6,35 +6,37 @@ from transformers import pipeline
6
  classifier = pipeline("zero-shot-classification", model="valhalla/distilbart-mnli-12-3")
7
 
8
  labels = [
9
-
10
  "animals", "pets", "dogs", "cats", "birds", "insects", "plants", "trees", "flowers", "marine life", "fish", "reptiles",
11
- # People and culture (expanded)
12
  "people", "child", "children", "female", "male", "human", "humans", "person", "girl", "boy", "celebrities", "historical figures",
13
  "politicians", "artists", "musicians", "authors", "philosophers", "religion", "mythology", "traditions",
14
  "festivals", "languages", "culture", "society",
15
-
16
  "places", "countries", "cities", "towns", "villages", "continents", "mountains", "rivers", "lakes", "oceans", "forests",
17
  "deserts", "architecture", "buildings", "monuments", "landmarks", "interior", "floor", "room", "furniture", "construction",
18
-
 
 
19
  "science", "physics", "chemistry", "biology", "mathematics", "astronomy", "space", "technology", "computers", "software",
20
  "hardware", "internet", "robots", "engineering", "energy", "environment", "climate", "weather",
21
-
22
  "art", "painting", "sculpture", "music", "movies", "television", "theater", "dance", "fashion", "design", "photography",
23
- "literature", "books", "poetry", "comics","house"
24
-
25
  "sports", "football", "basketball", "tennis", "games", "video games", "board games",
26
-
27
  "health", "medicine", "psychology", "nutrition", "fitness", "wellness", "food", "drink", "recipes", "cooking",
28
  "dessert", "baking", "snacks", "cookies", "pastries", "sweets",
29
-
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
-
33
  "clothing", "shirt", "apparel", "fashion", "garment", "textile", "t-shirt", "jeans", "pants", "dress", "shoes", "accessories",
34
  "bags", "handbags", "backpack", "hat", "scarf", "watch", "sandals",
35
-
36
  "philosophy", "emotions", "relationships", "travel", "transportation", "vehicles", "cars", "bikes", "trains", "airplanes",
37
- "spacecraft", "nature", "oceans", "forests", "mountains"
38
  ]
39
 
40
  random_topics = [
@@ -46,30 +48,23 @@ cache = {}
46
 
47
  def preprocess_topic(topic):
48
  single_word_hints = {
49
- "shirt": "clothing item",
50
- "dress": "clothing item",
51
- "jeans": "clothing item",
52
- "pants": "clothing item",
53
- "shoes": "clothing item",
54
- "t-shirt": "clothing item",
55
- "bag": "accessory item",
56
- "bags": "accessory item",
57
- "handbag": "accessory item",
58
- "handbags": "accessory item",
59
- "backpack": "accessory item",
60
- "hat": "accessory item",
61
- "scarf": "accessory item",
62
- "watch": "accessory item",
63
- "cookie": "food item",
64
- "sandals": "clothing item",
65
- "girl": "human person girl",
66
- "humans": "person people human beings",
67
- "kids": "children",
68
  }
69
  word = topic.lower()
70
- if word in single_word_hints:
71
- return f"{single_word_hints[word]} {topic}"
72
- return topic
73
 
74
  def get_wikipedia_facts(topic):
75
  if topic in cache:
@@ -79,7 +74,7 @@ def get_wikipedia_facts(topic):
79
  return "Please enter a topic or use 'Surprise me!'", None, None
80
 
81
  headers = {
82
- "User-Agent": "RandomFactApp/3.0 (https://huggingface.co/spaces/yourname) Python requests"
83
  }
84
 
85
  params = {
@@ -110,12 +105,7 @@ def get_wikipedia_facts(topic):
110
  image_url = page.get("thumbnail", {}).get("source", None)
111
 
112
  sentences = [s.strip() for s in extract_text.replace("\n", " ").split(". ") if s.strip() and len(s) > 10]
113
-
114
- if len(sentences) < 3:
115
- facts = sentences
116
- else:
117
- facts = random.sample(sentences, 3)
118
-
119
  facts = [fact if fact.endswith(".") else fact + "." for fact in facts]
120
  facts_text = "\n\n".join(f"πŸ’‘ {fact}" for fact in facts)
121
 
@@ -123,10 +113,9 @@ def get_wikipedia_facts(topic):
123
  top_labels = classification["labels"][:3]
124
  top_scores = classification["scores"][:3]
125
 
126
- classification_text = ""
127
  if top_scores[0] < 0.3:
128
- classification_text += "🧠 Category uncertain.\n"
129
- classification_text += "🧠 Top categories:\n"
130
  for label, score in zip(top_labels, top_scores):
131
  classification_text += f"- {label} ({score:.2%})\n"
132
 
@@ -145,7 +134,6 @@ def surprise_topic(_):
145
  return get_wikipedia_facts(topic)
146
 
147
  with gr.Blocks() as demo:
148
-
149
  gr.HTML("""
150
  <style>
151
  body {
@@ -160,16 +148,16 @@ with gr.Blocks() as demo:
160
  gr.Markdown("""
161
  # 🌍 Smart Wikipedia Fact Finder
162
  Search any topic and discover:
163
- - πŸ“š Interesting facts
164
  - πŸ–ΌοΈ A related image
165
  - 🧠 AI-predicted topic category
166
 
167
- πŸ‘‰ Try something like **"koalas"**, **"quantum physics"**, or click **🎲 Surprise me!**
168
  """)
169
 
170
  with gr.Row():
171
  with gr.Column(scale=3):
172
- topic_input = gr.Textbox(label="Enter a Topic", placeholder="e.g. Eiffel Tower, koalas, World War II")
173
  with gr.Column(scale=1):
174
  surprise_button = gr.Button("🎲 Surprise me!")
175
 
 
6
  classifier = pipeline("zero-shot-classification", model="valhalla/distilbart-mnli-12-3")
7
 
8
  labels = [
9
+ # Living things
10
  "animals", "pets", "dogs", "cats", "birds", "insects", "plants", "trees", "flowers", "marine life", "fish", "reptiles",
11
+ # People and culture
12
  "people", "child", "children", "female", "male", "human", "humans", "person", "girl", "boy", "celebrities", "historical figures",
13
  "politicians", "artists", "musicians", "authors", "philosophers", "religion", "mythology", "traditions",
14
  "festivals", "languages", "culture", "society",
15
+ # Places and geography
16
  "places", "countries", "cities", "towns", "villages", "continents", "mountains", "rivers", "lakes", "oceans", "forests",
17
  "deserts", "architecture", "buildings", "monuments", "landmarks", "interior", "floor", "room", "furniture", "construction",
18
+ # Household items
19
+ "household items", "bedding", "furniture", "decor", "interior", "home", "bedroom", "living room",
20
+ # Science and technology
21
  "science", "physics", "chemistry", "biology", "mathematics", "astronomy", "space", "technology", "computers", "software",
22
  "hardware", "internet", "robots", "engineering", "energy", "environment", "climate", "weather",
23
+ # Arts and entertainment
24
  "art", "painting", "sculpture", "music", "movies", "television", "theater", "dance", "fashion", "design", "photography",
25
+ "literature", "books", "poetry", "comics",
26
+ # Sports and games
27
  "sports", "football", "basketball", "tennis", "games", "video games", "board games",
28
+ # Health and lifestyle
29
  "health", "medicine", "psychology", "nutrition", "fitness", "wellness", "food", "drink", "recipes", "cooking",
30
  "dessert", "baking", "snacks", "cookies", "pastries", "sweets",
31
+ # Business and economy
32
  "business", "finance", "marketing", "economics", "startups", "career", "work", "law", "politics", "government", "military",
33
  "crime", "education", "schools", "universities", "news", "media", "social media", "apps", "brands", "products",
34
+ # Clothing
35
  "clothing", "shirt", "apparel", "fashion", "garment", "textile", "t-shirt", "jeans", "pants", "dress", "shoes", "accessories",
36
  "bags", "handbags", "backpack", "hat", "scarf", "watch", "sandals",
37
+ # Misc
38
  "philosophy", "emotions", "relationships", "travel", "transportation", "vehicles", "cars", "bikes", "trains", "airplanes",
39
+ "spacecraft", "nature", "oceans", "forests", "mountains"
40
  ]
41
 
42
  random_topics = [
 
48
 
49
  def preprocess_topic(topic):
50
  single_word_hints = {
51
+ # Clothing
52
+ "shirt": "clothing item", "dress": "clothing item", "jeans": "clothing item", "pants": "clothing item",
53
+ "shoes": "clothing item", "t-shirt": "clothing item", "sandals": "clothing item",
54
+ # Accessories
55
+ "bag": "accessory item", "bags": "accessory item", "handbag": "accessory item", "handbags": "accessory item",
56
+ "backpack": "accessory item", "hat": "accessory item", "scarf": "accessory item", "watch": "accessory item",
57
+ # Food
58
+ "cookie": "food item", "cookies": "food item",
59
+ # People
60
+ "girl": "human person girl", "kids": "children", "humans": "person people human beings",
61
+ # Household
62
+ "blanket": "household item", "blankets": "household item", "bed": "household item", "beds": "household item",
63
+ "sofa": "household item", "couch": "household item", "pillow": "household item", "mattress": "household item",
64
+ "curtain": "household item", "lamp": "household item", "rug": "household item",
 
 
 
 
 
65
  }
66
  word = topic.lower()
67
+ return f"{single_word_hints[word]} {topic}" if word in single_word_hints else topic
 
 
68
 
69
  def get_wikipedia_facts(topic):
70
  if topic in cache:
 
74
  return "Please enter a topic or use 'Surprise me!'", None, None
75
 
76
  headers = {
77
+ "User-Agent": "SmartFactApp/1.0"
78
  }
79
 
80
  params = {
 
105
  image_url = page.get("thumbnail", {}).get("source", None)
106
 
107
  sentences = [s.strip() for s in extract_text.replace("\n", " ").split(". ") if s.strip() and len(s) > 10]
108
+ facts = random.sample(sentences, min(3, len(sentences)))
 
 
 
 
 
109
  facts = [fact if fact.endswith(".") else fact + "." for fact in facts]
110
  facts_text = "\n\n".join(f"πŸ’‘ {fact}" for fact in facts)
111
 
 
113
  top_labels = classification["labels"][:3]
114
  top_scores = classification["scores"][:3]
115
 
116
+ classification_text = "🧠 Top categories:\n"
117
  if top_scores[0] < 0.3:
118
+ classification_text = "🧠 Category uncertain.\n" + classification_text
 
119
  for label, score in zip(top_labels, top_scores):
120
  classification_text += f"- {label} ({score:.2%})\n"
121
 
 
134
  return get_wikipedia_facts(topic)
135
 
136
  with gr.Blocks() as demo:
 
137
  gr.HTML("""
138
  <style>
139
  body {
 
148
  gr.Markdown("""
149
  # 🌍 Smart Wikipedia Fact Finder
150
  Search any topic and discover:
151
+ - πŸ“š Three interesting facts
152
  - πŸ–ΌοΈ A related image
153
  - 🧠 AI-predicted topic category
154
 
155
+ πŸ‘‰ Try something like **"bed"**, **"quantum physics"**, or click **🎲 Surprise me!**
156
  """)
157
 
158
  with gr.Row():
159
  with gr.Column(scale=3):
160
+ topic_input = gr.Textbox(label="Enter a Topic", placeholder="e.g. Eiffel Tower, blankets, World War II")
161
  with gr.Column(scale=1):
162
  surprise_button = gr.Button("🎲 Surprise me!")
163