Spaces:
Sleeping
Sleeping
Update src/streamlit_app.py
Browse files- 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
|
25 |
# --- Camera input ---
|
26 |
img_file_buffer = st.camera_input("πΈ Take a picture (optional)")
|
27 |
|
28 |
-
# ---
|
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=
|
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
|
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
|
70 |
else:
|
71 |
st.subheader(f"π About {place_name}")
|
72 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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=
|
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
|
91 |
-
- π Multilingual Wikipedia
|
92 |
-
- πΌοΈ
|
93 |
-
- β
|
|
|
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 |
""")
|