Rohanharsh163 commited on
Commit
f4ed80e
Β·
verified Β·
1 Parent(s): 90afc7c

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +36 -11
src/streamlit_app.py CHANGED
@@ -20,12 +20,26 @@ lang = st.selectbox(
20
  lang_code = lang[1]
21
 
22
  # --- Place name input ---
23
- place_name = st.text_input("πŸ›οΈ Enter place name (e.g., Charminar)")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24
 
25
  # --- Camera input ---
26
  img_file_buffer = st.camera_input("πŸ“Έ Take a picture (optional)")
27
 
28
- # --- Function: Wikipedia + Commons API ---
29
  def get_place_info(place, lang):
30
  if not place:
31
  return None
@@ -36,11 +50,14 @@ def get_place_info(place, lang):
36
  wiki_resp = requests.get(wiki_url)
37
  wiki_data = wiki_resp.json() if wiki_resp.status_code == 200 else {}
38
 
 
 
 
39
  # Wikimedia Commons
40
  commons_url = (
41
  f"https://commons.wikimedia.org/w/api.php"
42
  f"?action=query&format=json&prop=imageinfo&generator=search"
43
- f"&gsrsearch={place}&gsrlimit=3&iiprop=url"
44
  )
45
  commons_resp = requests.get(commons_url)
46
  commons_data = []
@@ -60,22 +77,29 @@ def get_place_info(place, lang):
60
  st.error(f"❌ API request failed: {e}")
61
  return None
62
 
63
- # --- Display place info ---
64
  if place_name.strip():
65
  st.info(f"πŸ” Fetching info for **{place_name}** in **{lang_code.upper()}**...")
66
  data = get_place_info(place_name, lang_code)
67
 
68
  if not data:
69
- st.error("⚠️ Could not retrieve data. Check the place name or try again.")
70
  else:
71
  st.subheader(f"πŸ“– About {place_name}")
72
- st.write(data['wikipedia'].get('extract', 'No information found.'))
 
 
 
 
 
 
 
73
 
74
  if data['commons']:
75
  st.markdown("### πŸ–ΌοΈ Related Images")
76
  for img in data['commons']:
77
  if img and img.get('url'):
78
- st.image(img['url'], width=250)
79
  else:
80
  st.warning("No images found on Wikimedia Commons.")
81
 
@@ -87,8 +111,9 @@ if img_file_buffer is not None:
87
  # --- Footer ---
88
  st.markdown("""
89
  ---
90
- - πŸ“Œ Supports text-based search and camera input.
91
- - 🌐 Multilingual Wikipedia summaries.
92
- - πŸ–ΌοΈ Integrated with Wikimedia Commons.
93
- - βœ… Runs entirely in Streamlit β€” ready for Hugging Face Spaces deployment!
 
94
  """)
 
20
  lang_code = lang[1]
21
 
22
  # --- Place name input ---
23
+ st.markdown("**πŸ“ Enter a place or person name to learn more:**")
24
+ place_name = st.text_input("πŸ›οΈ For example: Charminar, Taj Mahal, Shah Jahan")
25
+
26
+ # --- Quick place buttons ---
27
+ st.markdown("Popular:")
28
+ col1, col2, col3 = st.columns(3)
29
+ with col1:
30
+ if st.button("πŸ•Œ Charminar"):
31
+ place_name = "Charminar"
32
+ with col2:
33
+ if st.button("πŸ• Taj Mahal"):
34
+ place_name = "Taj Mahal"
35
+ with col3:
36
+ if st.button("πŸ‘‘ Shah Jahan"):
37
+ place_name = "Shah Jahan"
38
 
39
  # --- Camera input ---
40
  img_file_buffer = st.camera_input("πŸ“Έ Take a picture (optional)")
41
 
42
+ # --- Wikipedia + Commons API ---
43
  def get_place_info(place, lang):
44
  if not place:
45
  return None
 
50
  wiki_resp = requests.get(wiki_url)
51
  wiki_data = wiki_resp.json() if wiki_resp.status_code == 200 else {}
52
 
53
+ # Additional data using Wikidata
54
+ wikidata_id = wiki_data.get("wikidata")
55
+
56
  # Wikimedia Commons
57
  commons_url = (
58
  f"https://commons.wikimedia.org/w/api.php"
59
  f"?action=query&format=json&prop=imageinfo&generator=search"
60
+ f"&gsrsearch={place}&gsrlimit=5&iiprop=url"
61
  )
62
  commons_resp = requests.get(commons_url)
63
  commons_data = []
 
77
  st.error(f"❌ API request failed: {e}")
78
  return None
79
 
80
+ # --- Display content ---
81
  if place_name.strip():
82
  st.info(f"πŸ” Fetching info for **{place_name}** in **{lang_code.upper()}**...")
83
  data = get_place_info(place_name, lang_code)
84
 
85
  if not data:
86
+ st.error("⚠️ Could not retrieve data. Check the name or try again.")
87
  else:
88
  st.subheader(f"πŸ“– About {place_name}")
89
+ summary = data['wikipedia'].get('extract', 'No information found.')
90
+ st.write(summary)
91
+
92
+ if 'description' in data['wikipedia']:
93
+ st.markdown(f"**πŸ“Œ Type:** _{data['wikipedia']['description']}_")
94
+
95
+ if 'content_urls' in data['wikipedia']:
96
+ st.markdown("[πŸ”— Full Wikipedia Page](%s)" % data['wikipedia']['content_urls']['desktop']['page'])
97
 
98
  if data['commons']:
99
  st.markdown("### πŸ–ΌοΈ Related Images")
100
  for img in data['commons']:
101
  if img and img.get('url'):
102
+ st.image(img['url'], width=300)
103
  else:
104
  st.warning("No images found on Wikimedia Commons.")
105
 
 
111
  # --- Footer ---
112
  st.markdown("""
113
  ---
114
+ - πŸ“Œ Supports text search, button shortcuts, and camera input.
115
+ - 🌐 Multilingual summaries using Wikipedia REST API.
116
+ - πŸ–ΌοΈ Relevant Commons image gallery.
117
+ - βœ… Ready for Hugging Face deployment.
118
+ - πŸ› οΈ Built entirely with Streamlit, no backend needed.
119
  """)