Spaces:
Running
Running
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +29 -19
src/streamlit_app.py
CHANGED
@@ -3,11 +3,11 @@ from PIL import Image
|
|
3 |
import requests
|
4 |
|
5 |
st.set_page_config(page_title="WikiExplorer AR", layout="centered")
|
6 |
-
st.title("WikiExplorer AR (Streamlit Edition)")
|
7 |
|
8 |
# --- Multilingual language selector ---
|
9 |
lang = st.selectbox(
|
10 |
-
"Select Language",
|
11 |
options=[
|
12 |
("English", "en"),
|
13 |
("ΰ€Ήΰ€Ώΰ€¨ΰ₯ΰ€¦ΰ₯", "hi"),
|
@@ -20,12 +20,12 @@ 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 API ---
|
29 |
def get_place_info(place, lang):
|
30 |
if not place:
|
31 |
return None
|
@@ -36,11 +36,11 @@ 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 = []
|
@@ -48,37 +48,47 @@ def get_place_info(place, lang):
|
|
48 |
result = commons_resp.json().get('query', {}).get('pages', {})
|
49 |
for page in result.values():
|
50 |
imginfo = page.get('imageinfo', [{}])[0]
|
51 |
-
|
|
|
|
|
52 |
|
53 |
return {
|
54 |
"wikipedia": wiki_data,
|
55 |
"commons": commons_data,
|
56 |
}
|
57 |
except Exception as e:
|
58 |
-
st.error(f"API request failed: {e}")
|
59 |
return None
|
60 |
|
61 |
-
# --- Display info ---
|
62 |
if place_name.strip():
|
63 |
-
st.info(f"Fetching info for **{place_name}** in
|
64 |
data = get_place_info(place_name, lang_code)
|
|
|
65 |
if not data:
|
66 |
-
st.error("Could not
|
67 |
else:
|
68 |
-
st.subheader(f"
|
69 |
-
st.write(data['wikipedia'].get('extract', 'No
|
70 |
|
71 |
if data['commons']:
|
|
|
72 |
for img in data['commons']:
|
73 |
-
|
|
|
|
|
|
|
74 |
|
75 |
# --- Show captured image ---
|
76 |
if img_file_buffer is not None:
|
77 |
-
st.
|
|
|
78 |
|
|
|
79 |
st.markdown("""
|
80 |
-
|
|
|
81 |
- π Multilingual Wikipedia summaries.
|
82 |
-
- πΌοΈ Wikimedia Commons
|
83 |
-
- β
Runs entirely in Streamlit β
|
84 |
""")
|
|
|
3 |
import requests
|
4 |
|
5 |
st.set_page_config(page_title="WikiExplorer AR", layout="centered")
|
6 |
+
st.title("π· WikiExplorer AR (Streamlit Edition)")
|
7 |
|
8 |
# --- Multilingual language selector ---
|
9 |
lang = st.selectbox(
|
10 |
+
"π Select Language",
|
11 |
options=[
|
12 |
("English", "en"),
|
13 |
("ΰ€Ήΰ€Ώΰ€¨ΰ₯ΰ€¦ΰ₯", "hi"),
|
|
|
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 |
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 = []
|
|
|
48 |
result = commons_resp.json().get('query', {}).get('pages', {})
|
49 |
for page in result.values():
|
50 |
imginfo = page.get('imageinfo', [{}])[0]
|
51 |
+
img_url = imginfo.get('url')
|
52 |
+
if img_url:
|
53 |
+
commons_data.append({"url": img_url})
|
54 |
|
55 |
return {
|
56 |
"wikipedia": wiki_data,
|
57 |
"commons": commons_data,
|
58 |
}
|
59 |
except Exception as e:
|
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 |
|
82 |
# --- Show captured image ---
|
83 |
if img_file_buffer is not None:
|
84 |
+
st.markdown("### π· Captured Image")
|
85 |
+
st.image(img_file_buffer, caption="Uploaded via camera", use_column_width=True)
|
86 |
|
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 |
""")
|