File size: 3,918 Bytes
9a95e8a
9381767
 
9a95e8a
9381767
90afc7c
9381767
 
 
90afc7c
9381767
 
 
 
 
 
 
 
 
 
 
 
f4ed80e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9381767
 
90afc7c
9381767
f4ed80e
9381767
 
 
 
 
 
 
 
 
 
f4ed80e
 
 
90afc7c
9381767
 
 
f4ed80e
9381767
 
 
 
 
 
 
90afc7c
 
 
9381767
 
 
 
 
 
90afc7c
9381767
 
f4ed80e
9381767
90afc7c
9381767
90afc7c
9381767
f4ed80e
9381767
90afc7c
f4ed80e
 
 
 
 
 
 
 
9381767
 
90afc7c
9381767
90afc7c
f4ed80e
90afc7c
 
9381767
 
 
90afc7c
 
9381767
90afc7c
9381767
90afc7c
f4ed80e
 
 
 
 
9381767
1
2
3
4
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import streamlit as st
from PIL import Image
import requests

st.set_page_config(page_title="WikiExplorer AR", layout="centered")
st.title("๐Ÿ“ท WikiExplorer AR (Streamlit Edition)")

# --- Multilingual language selector ---
lang = st.selectbox(
    "๐ŸŒ Select Language",
    options=[
        ("English", "en"),
        ("เคนเคฟเคจเฅเคฆเฅ€", "hi"),
        ("เฐคเฑ†เฐฒเฑเฐ—เฑ", "te"),
        ("เฎคเฎฎเฎฟเฎดเฏ", "ta"),
    ],
    format_func=lambda x: x[0]
)

lang_code = lang[1]

# --- Place name input ---
st.markdown("**๐Ÿ“ Enter a place or person name to learn more:**")
place_name = st.text_input("๐Ÿ›๏ธ For example: Charminar, Taj Mahal, Shah Jahan")

# --- Quick place buttons ---
st.markdown("Popular:")
col1, col2, col3 = st.columns(3)
with col1:
    if st.button("๐Ÿ•Œ Charminar"):
        place_name = "Charminar"
with col2:
    if st.button("๐Ÿ• Taj Mahal"):
        place_name = "Taj Mahal"
with col3:
    if st.button("๐Ÿ‘‘ Shah Jahan"):
        place_name = "Shah Jahan"

# --- Camera input ---
img_file_buffer = st.camera_input("๐Ÿ“ธ Take a picture (optional)")

# --- Wikipedia + Commons API ---
def get_place_info(place, lang):
    if not place:
        return None

    try:
        # Wikipedia API
        wiki_url = f"https://{lang}.wikipedia.org/api/rest_v1/page/summary/{place}"
        wiki_resp = requests.get(wiki_url)
        wiki_data = wiki_resp.json() if wiki_resp.status_code == 200 else {}

        # Additional data using Wikidata
        wikidata_id = wiki_data.get("wikidata")

        # Wikimedia Commons
        commons_url = (
            f"https://commons.wikimedia.org/w/api.php"
            f"?action=query&format=json&prop=imageinfo&generator=search"
            f"&gsrsearch={place}&gsrlimit=5&iiprop=url"
        )
        commons_resp = requests.get(commons_url)
        commons_data = []
        if commons_resp.status_code == 200:
            result = commons_resp.json().get('query', {}).get('pages', {})
            for page in result.values():
                imginfo = page.get('imageinfo', [{}])[0]
                img_url = imginfo.get('url')
                if img_url:
                    commons_data.append({"url": img_url})

        return {
            "wikipedia": wiki_data,
            "commons": commons_data,
        }
    except Exception as e:
        st.error(f"โŒ API request failed: {e}")
        return None

# --- Display content ---
if place_name.strip():
    st.info(f"๐Ÿ” Fetching info for **{place_name}** in **{lang_code.upper()}**...")
    data = get_place_info(place_name, lang_code)

    if not data:
        st.error("โš ๏ธ Could not retrieve data. Check the name or try again.")
    else:
        st.subheader(f"๐Ÿ“– About {place_name}")
        summary = data['wikipedia'].get('extract', 'No information found.')
        st.write(summary)

        if 'description' in data['wikipedia']:
            st.markdown(f"**๐Ÿ“Œ Type:** _{data['wikipedia']['description']}_")

        if 'content_urls' in data['wikipedia']:
            st.markdown("[๐Ÿ”— Full Wikipedia Page](%s)" % data['wikipedia']['content_urls']['desktop']['page'])

        if data['commons']:
            st.markdown("### ๐Ÿ–ผ๏ธ Related Images")
            for img in data['commons']:
                if img and img.get('url'):
                    st.image(img['url'], width=300)
        else:
            st.warning("No images found on Wikimedia Commons.")

# --- Show captured image ---
if img_file_buffer is not None:
    st.markdown("### ๐Ÿ“ท Captured Image")
    st.image(img_file_buffer, caption="Uploaded via camera", use_column_width=True)

# --- Footer ---
st.markdown("""
---
- ๐Ÿ“Œ Supports text search, button shortcuts, and camera input.
- ๐ŸŒ Multilingual summaries using Wikipedia REST API.
- ๐Ÿ–ผ๏ธ Relevant Commons image gallery.
- โœ… Ready for Hugging Face deployment.
- ๐Ÿ› ๏ธ Built entirely with Streamlit, no backend needed.
""")