Rohanharsh163 commited on
Commit
9381767
·
verified ·
1 Parent(s): b5d9a9e

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. 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
- # Welcome to Streamlit!
8
-
9
- Edit `/streamlit_app.py` to customize this app to your heart's desire :heart:.
10
- If you have any questions, checkout our [documentation](https://docs.streamlit.io) and [community
11
- forums](https://discuss.streamlit.io).
12
-
13
- In the meantime, below is an example of what you can do with just a few lines of code:
14
- """
15
-
16
- num_points = st.slider("Number of points in spiral", 1, 10000, 1100)
17
- num_turns = st.slider("Number of turns in spiral", 1, 300, 31)
18
-
19
- indices = np.linspace(0, 1, num_points)
20
- theta = 2 * np.pi * num_turns * indices
21
- radius = indices
22
-
23
- x = radius * np.cos(theta)
24
- y = radius * np.sin(theta)
25
-
26
- df = pd.DataFrame({
27
- "x": x,
28
- "y": y,
29
- "idx": indices,
30
- "rand": np.random.randn(num_points),
31
- })
32
-
33
- st.altair_chart(alt.Chart(df, height=700, width=700)
34
- .mark_point(filled=True)
35
- .encode(
36
- x=alt.X("x", axis=None),
37
- y=alt.Y("y", axis=None),
38
- color=alt.Color("idx", legend=None, scale=alt.Scale()),
39
- size=alt.Size("rand", legend=None, scale=alt.Scale(range=[1, 150])),
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
+ """)