Spaces:
Runtime error
Runtime error
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +187 -38
src/streamlit_app.py
CHANGED
|
@@ -1,40 +1,189 @@
|
|
| 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 os
|
| 4 |
+
import wikipedia
|
| 5 |
+
import requests
|
| 6 |
|
| 7 |
+
# ===== Session State Initialization =====
|
| 8 |
+
if "logged_in" not in st.session_state:
|
| 9 |
+
st.session_state.logged_in = False
|
| 10 |
+
if "email" not in st.session_state:
|
| 11 |
+
st.session_state.email = ""
|
| 12 |
+
|
| 13 |
+
# ===== Static Data =====
|
| 14 |
+
USERS = {}
|
| 15 |
+
UPLOAD_FOLDER = "uploads"
|
| 16 |
+
if not os.path.exists(UPLOAD_FOLDER):
|
| 17 |
+
os.makedirs(UPLOAD_FOLDER)
|
| 18 |
+
|
| 19 |
+
STATES = {
|
| 20 |
+
"Telangana": ["Hyderabad", "Warangal", "Nizamabad", "Karimnagar", "Khammam"],
|
| 21 |
+
"Maharashtra": ["Mumbai", "Pune", "Nagpur", "Nashik", "Aurangabad"],
|
| 22 |
+
"Tamil Nadu": ["Chennai", "Coimbatore", "Madurai", "Tiruchirappalli", "Salem"],
|
| 23 |
+
"Karnataka": ["Bangalore", "Mysore", "Mangalore", "Hubli", "Belgaum"],
|
| 24 |
+
"Uttar Pradesh": ["Lucknow", "Varanasi", "Agra", "Kanpur", "Allahabad"],
|
| 25 |
+
"West Bengal": ["Kolkata", "Darjeeling", "Durgapur", "Siliguri", "Asansol"],
|
| 26 |
+
"Rajasthan": ["Jaipur", "Udaipur", "Jodhpur", "Bikaner", "Ajmer"],
|
| 27 |
+
"Gujarat": ["Ahmedabad", "Surat", "Vadodara", "Rajkot", "Bhavnagar"],
|
| 28 |
+
"Andhra Pradesh": ["Visakhapatnam", "Vijayawada", "Guntur", "Tirupati", "Kurnool"],
|
| 29 |
+
}
|
| 30 |
+
|
| 31 |
+
# ===== Helper Functions =====
|
| 32 |
+
def save_uploaded_file(uploaded_file):
|
| 33 |
+
path = os.path.join(UPLOAD_FOLDER, uploaded_file.name)
|
| 34 |
+
with open(path, "wb") as f:
|
| 35 |
+
f.write(uploaded_file.getbuffer())
|
| 36 |
+
return path
|
| 37 |
+
|
| 38 |
+
def recognize_place(image_file):
|
| 39 |
+
name = image_file.name.lower()
|
| 40 |
+
keywords_map = {
|
| 41 |
+
"charminar": "Charminar",
|
| 42 |
+
"taj": "Taj Mahal",
|
| 43 |
+
"gateway": "Gateway of India",
|
| 44 |
+
"qutub": "Qutub Minar",
|
| 45 |
+
"mysore": "Mysore Palace",
|
| 46 |
+
"hampi": "Hampi",
|
| 47 |
+
"konark": "Konark Sun Temple",
|
| 48 |
+
"varanasi": "Varanasi",
|
| 49 |
+
"madurai": "Meenakshi Temple",
|
| 50 |
+
"ajanta": "Ajanta Caves",
|
| 51 |
+
"ellora": "Ellora Caves",
|
| 52 |
+
}
|
| 53 |
+
for kw, place in keywords_map.items():
|
| 54 |
+
if kw in name:
|
| 55 |
+
return place
|
| 56 |
+
return None
|
| 57 |
+
|
| 58 |
+
def fetch_wikipedia_summary(title, lang="en"):
|
| 59 |
+
try:
|
| 60 |
+
wikipedia.set_lang(lang)
|
| 61 |
+
summary = wikipedia.summary(title, sentences=4)
|
| 62 |
+
return summary
|
| 63 |
+
except wikipedia.exceptions.DisambiguationError as e:
|
| 64 |
+
return f"Multiple results found for '{title}': {e.options}"
|
| 65 |
+
except wikipedia.exceptions.PageError:
|
| 66 |
+
return f"No Wikipedia page found for '{title}' in selected language."
|
| 67 |
+
except Exception as e:
|
| 68 |
+
return f"Error fetching summary: {str(e)}"
|
| 69 |
+
|
| 70 |
+
# ===== Auth Pages =====
|
| 71 |
+
def signup():
|
| 72 |
+
st.header("π Signup")
|
| 73 |
+
name = st.text_input("Name")
|
| 74 |
+
email = st.text_input("Email (Gmail)")
|
| 75 |
+
contact = st.text_input("Contact Number")
|
| 76 |
+
if st.button("Sign Up"):
|
| 77 |
+
if email in USERS:
|
| 78 |
+
st.error("User already exists, please login.")
|
| 79 |
+
return
|
| 80 |
+
USERS[email] = {"name": name, "email": email, "contact": contact}
|
| 81 |
+
st.success(f"User {name} registered! Please verify OTP sent to your email (simulated).")
|
| 82 |
+
st.session_state["otp_sent_to"] = email
|
| 83 |
+
st.session_state["otp_verified"] = False
|
| 84 |
+
|
| 85 |
+
def login():
|
| 86 |
+
st.header("π Login")
|
| 87 |
+
email = st.text_input("Email")
|
| 88 |
+
if st.button("Send OTP"):
|
| 89 |
+
if email in USERS:
|
| 90 |
+
st.session_state["otp_sent_to"] = email
|
| 91 |
+
st.session_state["otp_verified"] = False
|
| 92 |
+
st.info("OTP sent (simulated). Please enter OTP below.")
|
| 93 |
+
else:
|
| 94 |
+
st.error("Email not found. Please sign up first.")
|
| 95 |
+
|
| 96 |
+
def verify_otp():
|
| 97 |
+
st.header("π OTP Verification")
|
| 98 |
+
otp = st.text_input("Enter OTP (use 1234 for demo)", type="password")
|
| 99 |
+
if st.button("Verify OTP"):
|
| 100 |
+
if otp == "1234" and st.session_state.get("otp_sent_to"):
|
| 101 |
+
st.session_state.logged_in = True
|
| 102 |
+
st.session_state.email = st.session_state["otp_sent_to"]
|
| 103 |
+
st.success("OTP verified! Logged in successfully.")
|
| 104 |
+
else:
|
| 105 |
+
st.error("Incorrect OTP or no OTP sent.")
|
| 106 |
+
|
| 107 |
+
def logout():
|
| 108 |
+
st.session_state.logged_in = False
|
| 109 |
+
st.session_state.email = ""
|
| 110 |
+
st.success("Logged out successfully.")
|
| 111 |
+
|
| 112 |
+
# ===== UI Styling =====
|
| 113 |
+
st.set_page_config(page_title="Gyana Vedika v2", layout="wide")
|
| 114 |
+
st.markdown("""
|
| 115 |
+
<style>
|
| 116 |
+
.stButton>button {background-color: #008080; color: white; font-weight: bold; border-radius:10px;}
|
| 117 |
+
.stTextInput>div>input, .stSelectbox>div>div>select, .stTextArea>div>textarea {
|
| 118 |
+
border: 2px solid #008080 !important; border-radius: 8px !important;
|
| 119 |
+
}
|
| 120 |
+
h1, h2, h3, h4, h5, h6 {
|
| 121 |
+
color: #004d4d;
|
| 122 |
+
}
|
| 123 |
+
</style>
|
| 124 |
+
""", unsafe_allow_html=True)
|
| 125 |
+
|
| 126 |
+
# ===== Main App Logic =====
|
| 127 |
+
if not st.session_state.logged_in:
|
| 128 |
+
page = st.sidebar.selectbox("Choose", ["Login", "Signup", "Verify OTP"])
|
| 129 |
+
if page == "Login":
|
| 130 |
+
login()
|
| 131 |
+
elif page == "Signup":
|
| 132 |
+
signup()
|
| 133 |
+
else:
|
| 134 |
+
verify_otp()
|
| 135 |
+
else:
|
| 136 |
+
st.sidebar.success(f"Logged in as {st.session_state.email}")
|
| 137 |
+
if st.sidebar.button("Logout"):
|
| 138 |
+
logout()
|
| 139 |
+
|
| 140 |
+
st.title("π Gyana Vedika - Cultural Explorer v2")
|
| 141 |
+
|
| 142 |
+
state = st.selectbox("Select State", list(STATES.keys()))
|
| 143 |
+
district = st.selectbox("Select District", STATES[state])
|
| 144 |
+
|
| 145 |
+
st.subheader(f"Explore {district}, {state}")
|
| 146 |
+
|
| 147 |
+
uploaded_image = st.file_uploader("Upload an image of a cultural/historical place", type=["png", "jpg", "jpeg"])
|
| 148 |
+
|
| 149 |
+
recognized_place = None
|
| 150 |
+
if uploaded_image:
|
| 151 |
+
st.image(uploaded_image, caption="Uploaded Image", use_column_width=True)
|
| 152 |
+
recognized_place = recognize_place(uploaded_image)
|
| 153 |
+
if recognized_place:
|
| 154 |
+
st.success(f"Recognized Place: {recognized_place}")
|
| 155 |
+
else:
|
| 156 |
+
st.warning("Could not recognize place. Try renaming the image file with landmark name, e.g., charminar.jpg")
|
| 157 |
+
|
| 158 |
+
st.markdown("### Or manually enter place name to search info")
|
| 159 |
+
manual_place = st.text_input("Enter place name")
|
| 160 |
+
|
| 161 |
+
place_to_search = recognized_place or manual_place.strip()
|
| 162 |
+
|
| 163 |
+
if place_to_search:
|
| 164 |
+
language = st.selectbox("Select Wikipedia summary language", ["English", "Hindi", "Telugu", "Tamil", "Marathi", "Bengali"], key="lang2")
|
| 165 |
+
lang_codes = {"English": "en", "Hindi": "hi", "Telugu": "te", "Tamil": "ta", "Marathi": "mr", "Bengali": "bn"}
|
| 166 |
+
lang_code = lang_codes.get(language, "en")
|
| 167 |
+
with st.spinner(f"Fetching Wikipedia summary for {place_to_search} in {language}..."):
|
| 168 |
+
summary = fetch_wikipedia_summary(place_to_search, lang=lang_code)
|
| 169 |
+
st.markdown(f"### Summary of {place_to_search}")
|
| 170 |
+
st.write(summary)
|
| 171 |
+
|
| 172 |
+
st.markdown("---")
|
| 173 |
+
st.subheader("Feedback about this place")
|
| 174 |
+
feedback_text = st.text_area("Share your experience or info about this place")
|
| 175 |
+
uploaded_images = st.file_uploader("Upload Images", type=["png", "jpg", "jpeg"], accept_multiple_files=True, key="fb_images")
|
| 176 |
+
uploaded_audio = st.file_uploader("Upload Audio", type=["mp3", "wav"], accept_multiple_files=True, key="fb_audio")
|
| 177 |
+
uploaded_videos = st.file_uploader("Upload Videos", type=["mp4", "mov", "avi"], accept_multiple_files=True, key="fb_videos")
|
| 178 |
+
|
| 179 |
+
if st.button("Submit Feedback"):
|
| 180 |
+
feedback_folder = os.path.join(UPLOAD_FOLDER, "feedback")
|
| 181 |
+
if not os.path.exists(feedback_folder):
|
| 182 |
+
os.makedirs(feedback_folder)
|
| 183 |
+
saved_files = []
|
| 184 |
+
for file_list in [uploaded_images, uploaded_audio, uploaded_videos]:
|
| 185 |
+
if file_list:
|
| 186 |
+
for f in file_list:
|
| 187 |
+
path = save_uploaded_file(f)
|
| 188 |
+
saved_files.append(path)
|
| 189 |
+
st.success("Thank you for your feedback and uploads!")
|