Spaces:
Sleeping
Sleeping
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +21 -28
src/streamlit_app.py
CHANGED
@@ -23,22 +23,20 @@ lang_code = lang[1]
|
|
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:
|
@@ -50,8 +48,14 @@ def get_place_info(place, lang):
|
|
50 |
wiki_resp = requests.get(wiki_url)
|
51 |
wiki_data = wiki_resp.json() if wiki_resp.status_code == 200 else {}
|
52 |
|
53 |
-
#
|
54 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
55 |
|
56 |
# Wikimedia Commons
|
57 |
commons_url = (
|
@@ -108,22 +112,11 @@ if img_file_buffer is not None:
|
|
108 |
st.markdown("### π· Captured Image")
|
109 |
st.image(img_file_buffer, caption="Uploaded via camera", use_column_width=True)
|
110 |
|
111 |
-
def translate_text(text, target_lang):
|
112 |
-
try:
|
113 |
-
url = f"https://translate.googleapis.com/translate_a/single?client=gtx&sl=en&tl={target_lang}&dt=t&q={requests.utils.quote(text)}"
|
114 |
-
response = requests.get(url)
|
115 |
-
if response.status_code == 200:
|
116 |
-
return response.json()[0][0][0]
|
117 |
-
except:
|
118 |
-
return text
|
119 |
-
return text
|
120 |
-
|
121 |
-
|
122 |
# --- Footer ---
|
123 |
st.markdown("""
|
124 |
---
|
125 |
-
- π Supports text search
|
126 |
-
- π Multilingual summaries using Wikipedia REST API.
|
127 |
- πΌοΈ Relevant Commons image gallery.
|
128 |
- β
Ready for Hugging Face deployment.
|
129 |
- π οΈ Built entirely with Streamlit, no backend needed.
|
|
|
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 |
# --- Camera input ---
|
27 |
img_file_buffer = st.camera_input("πΈ Take a picture (optional)")
|
28 |
|
29 |
+
# --- Translation helper ---
|
30 |
+
def translate_text(text, target_lang):
|
31 |
+
try:
|
32 |
+
url = f"https://translate.googleapis.com/translate_a/single?client=gtx&sl=en&tl={target_lang}&dt=t&q={requests.utils.quote(text)}"
|
33 |
+
response = requests.get(url)
|
34 |
+
if response.status_code == 200:
|
35 |
+
return response.json()[0][0][0]
|
36 |
+
except:
|
37 |
+
return text
|
38 |
+
return text
|
39 |
+
|
40 |
# --- Wikipedia + Commons API ---
|
41 |
def get_place_info(place, lang):
|
42 |
if not place:
|
|
|
48 |
wiki_resp = requests.get(wiki_url)
|
49 |
wiki_data = wiki_resp.json() if wiki_resp.status_code == 200 else {}
|
50 |
|
51 |
+
# If summary is missing and not English, try English and translate
|
52 |
+
if (not wiki_data.get("extract") or wiki_data.get("title") == "Not found.") and lang != "en":
|
53 |
+
fallback_resp = requests.get(f"https://en.wikipedia.org/api/rest_v1/page/summary/{place}")
|
54 |
+
if fallback_resp.status_code == 200:
|
55 |
+
fallback_data = fallback_resp.json()
|
56 |
+
translated_summary = translate_text(fallback_data.get("extract", ""), lang)
|
57 |
+
wiki_data = fallback_data
|
58 |
+
wiki_data["extract"] = translated_summary
|
59 |
|
60 |
# Wikimedia Commons
|
61 |
commons_url = (
|
|
|
112 |
st.markdown("### π· Captured Image")
|
113 |
st.image(img_file_buffer, caption="Uploaded via camera", use_column_width=True)
|
114 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
115 |
# --- Footer ---
|
116 |
st.markdown("""
|
117 |
---
|
118 |
+
- π Supports text search and camera input.
|
119 |
+
- π Multilingual summaries using Wikipedia REST API with fallback + translation.
|
120 |
- πΌοΈ Relevant Commons image gallery.
|
121 |
- β
Ready for Hugging Face deployment.
|
122 |
- π οΈ Built entirely with Streamlit, no backend needed.
|