Rohanharsh163 commited on
Commit
90afc7c
Β·
verified Β·
1 Parent(s): 9381767

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. 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 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 = []
@@ -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
- 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
  """)
 
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
  """)