Spaces:
Sleeping
Sleeping
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +82 -38
src/streamlit_app.py
CHANGED
@@ -1,40 +1,84 @@
|
|
1 |
-
import altair as alt
|
2 |
-
import numpy as np
|
3 |
-
import pandas as pd
|
4 |
import streamlit as st
|
|
|
|
|
5 |
|
6 |
-
"""
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
"""
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import streamlit as st
|
2 |
+
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"),
|
14 |
+
("తెలుగు", "te"),
|
15 |
+
("தமிழ்", "ta"),
|
16 |
+
],
|
17 |
+
format_func=lambda x: x[0]
|
18 |
+
)
|
19 |
+
|
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
|
32 |
+
|
33 |
+
try:
|
34 |
+
# Wikipedia API
|
35 |
+
wiki_url = f"https://{lang}.wikipedia.org/api/rest_v1/page/summary/{place}"
|
36 |
+
wiki_resp = requests.get(wiki_url)
|
37 |
+
wiki_data = wiki_resp.json() if wiki_resp.status_code == 200 else {}
|
38 |
+
|
39 |
+
# Wikimedia Commons images (example)
|
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=5&iiprop=url"
|
44 |
+
)
|
45 |
+
commons_resp = requests.get(commons_url)
|
46 |
+
commons_data = []
|
47 |
+
if commons_resp.status_code == 200:
|
48 |
+
result = commons_resp.json().get('query', {}).get('pages', {})
|
49 |
+
for page in result.values():
|
50 |
+
imginfo = page.get('imageinfo', [{}])[0]
|
51 |
+
commons_data.append({"url": imginfo.get('url')})
|
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 language **{lang_code}**...")
|
64 |
+
data = get_place_info(place_name, lang_code)
|
65 |
+
if not data:
|
66 |
+
st.error("Could not get data. Please check the name or try again.")
|
67 |
+
else:
|
68 |
+
st.subheader(f"Information about {place_name}")
|
69 |
+
st.write(data['wikipedia'].get('extract', 'No info found.'))
|
70 |
+
|
71 |
+
if data['commons']:
|
72 |
+
for img in data['commons']:
|
73 |
+
st.image(img['url'], width=250)
|
74 |
+
|
75 |
+
# --- Show captured image ---
|
76 |
+
if img_file_buffer is not None:
|
77 |
+
st.image(img_file_buffer, caption="Captured Image")
|
78 |
+
|
79 |
+
st.markdown("""
|
80 |
+
- 📌 Supports text search + camera input.
|
81 |
+
- 🌐 Multilingual Wikipedia summaries.
|
82 |
+
- 🖼️ Wikimedia Commons images.
|
83 |
+
- ✅ Runs entirely in Streamlit — no separate backend needed!
|
84 |
+
""")
|