Spaces:
Sleeping
Sleeping
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +13 -4
src/streamlit_app.py
CHANGED
@@ -26,7 +26,7 @@ place_name = st.text_input("ποΈ For example: Charminar, Taj Mahal, Shah Jaha
|
|
26 |
# --- Camera input ---
|
27 |
img_file_buffer = st.camera_input("πΈ Take a picture (optional)")
|
28 |
|
29 |
-
# --- Translation
|
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)}"
|
@@ -37,6 +37,15 @@ def translate_text(text, target_lang):
|
|
37 |
return text
|
38 |
return text
|
39 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
40 |
|
41 |
# --- Wikipedia + Commons API ---
|
42 |
def get_place_info(place, lang):
|
@@ -54,7 +63,7 @@ def get_place_info(place, lang):
|
|
54 |
fallback_resp = requests.get(f"https://en.wikipedia.org/api/rest_v1/page/summary/{place}")
|
55 |
if fallback_resp.status_code == 200:
|
56 |
fallback_data = fallback_resp.json()
|
57 |
-
translated_summary =
|
58 |
wiki_data = fallback_data
|
59 |
wiki_data["extract"] = translated_summary
|
60 |
|
@@ -117,8 +126,8 @@ if img_file_buffer is not None:
|
|
117 |
st.markdown("""
|
118 |
---
|
119 |
- π Supports text search and camera input.
|
120 |
-
- π Multilingual summaries using Wikipedia REST API with fallback + translation.
|
121 |
- πΌοΈ Relevant Commons image gallery.
|
122 |
- β
Ready for Hugging Face deployment.
|
123 |
- π οΈ Built entirely with Streamlit, no backend needed.
|
124 |
-
""")
|
|
|
26 |
# --- Camera input ---
|
27 |
img_file_buffer = st.camera_input("πΈ Take a picture (optional)")
|
28 |
|
29 |
+
# --- Translation helpers ---
|
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)}"
|
|
|
37 |
return text
|
38 |
return text
|
39 |
|
40 |
+
def translate_paragraph(text, target_lang):
|
41 |
+
sentences = text.split('. ')
|
42 |
+
translated = []
|
43 |
+
for sentence in sentences:
|
44 |
+
sentence = sentence.strip()
|
45 |
+
if sentence:
|
46 |
+
translated_sentence = translate_text(sentence, target_lang)
|
47 |
+
translated.append(translated_sentence)
|
48 |
+
return '. '.join(translated)
|
49 |
|
50 |
# --- Wikipedia + Commons API ---
|
51 |
def get_place_info(place, lang):
|
|
|
63 |
fallback_resp = requests.get(f"https://en.wikipedia.org/api/rest_v1/page/summary/{place}")
|
64 |
if fallback_resp.status_code == 200:
|
65 |
fallback_data = fallback_resp.json()
|
66 |
+
translated_summary = translate_paragraph(fallback_data.get("extract", ""), lang)
|
67 |
wiki_data = fallback_data
|
68 |
wiki_data["extract"] = translated_summary
|
69 |
|
|
|
126 |
st.markdown("""
|
127 |
---
|
128 |
- π Supports text search and camera input.
|
129 |
+
- π Multilingual summaries using Wikipedia REST API with fallback + sentence-level translation.
|
130 |
- πΌοΈ Relevant Commons image gallery.
|
131 |
- β
Ready for Hugging Face deployment.
|
132 |
- π οΈ Built entirely with Streamlit, no backend needed.
|
133 |
+
""")
|