Spaces:
Sleeping
Sleeping
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +188 -19
src/streamlit_app.py
CHANGED
@@ -1,19 +1,188 @@
|
|
1 |
-
|
2 |
-
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from PIL import Image
|
3 |
+
import os
|
4 |
+
import wikipedia
|
5 |
+
import csv
|
6 |
+
from datetime import datetime
|
7 |
+
|
8 |
+
# ====== Setup ======
|
9 |
+
if "_temp_dir" not in st.session_state:
|
10 |
+
st.session_state["_temp_dir"] = "/tmp"
|
11 |
+
|
12 |
+
UPLOAD_FOLDER = os.path.join(st.session_state["_temp_dir"], "uploads")
|
13 |
+
MEDIA_FOLDER = os.path.join(UPLOAD_FOLDER, "feedback_media")
|
14 |
+
FEEDBACK_FILE = os.path.join(UPLOAD_FOLDER, "feedback.csv")
|
15 |
+
|
16 |
+
# Ensure folders exist
|
17 |
+
os.makedirs(MEDIA_FOLDER, exist_ok=True)
|
18 |
+
|
19 |
+
USERS = {}
|
20 |
+
|
21 |
+
STATES = {
|
22 |
+
"Telangana": ["Hyderabad", "Warangal", "Nizamabad", "Karimnagar", "Khammam"],
|
23 |
+
"Maharashtra": ["Mumbai", "Pune", "Nagpur", "Nashik", "Aurangabad"],
|
24 |
+
"Tamil Nadu": ["Chennai", "Coimbatore", "Madurai", "Tiruchirappalli", "Salem"],
|
25 |
+
"Karnataka": ["Bangalore", "Mysore", "Mangalore", "Hubli", "Belgaum"],
|
26 |
+
"Uttar Pradesh": ["Lucknow", "Varanasi", "Agra", "Kanpur", "Allahabad"],
|
27 |
+
"West Bengal": ["Kolkata", "Darjeeling", "Durgapur", "Siliguri", "Asansol"],
|
28 |
+
"Rajasthan": ["Jaipur", "Udaipur", "Jodhpur", "Bikaner", "Ajmer"],
|
29 |
+
"Gujarat": ["Ahmedabad", "Surat", "Vadodara", "Rajkot", "Bhavnagar"],
|
30 |
+
"Andhra Pradesh": ["Visakhapatnam", "Vijayawada", "Guntur", "Tirupati", "Kurnool"],
|
31 |
+
}
|
32 |
+
|
33 |
+
# ====== Session State Initialization ======
|
34 |
+
for key, default in {
|
35 |
+
"logged_in": False,
|
36 |
+
"email": "",
|
37 |
+
"otp_sent_to": None,
|
38 |
+
"otp_verified": False,
|
39 |
+
"login_email": None,
|
40 |
+
}.items():
|
41 |
+
if key not in st.session_state:
|
42 |
+
st.session_state[key] = default
|
43 |
+
|
44 |
+
# ====== Helper Functions ======
|
45 |
+
def send_otp(email):
|
46 |
+
st.session_state.otp_sent_to = email
|
47 |
+
st.session_state.otp_verified = False
|
48 |
+
return "1234"
|
49 |
+
|
50 |
+
def verify_otp(entered_otp):
|
51 |
+
if st.session_state.otp_sent_to and entered_otp == "1234":
|
52 |
+
st.session_state.logged_in = True
|
53 |
+
st.session_state.email = st.session_state.otp_sent_to
|
54 |
+
st.session_state.otp_verified = True
|
55 |
+
st.session_state.login_email = None
|
56 |
+
return True
|
57 |
+
return False
|
58 |
+
|
59 |
+
def logout():
|
60 |
+
for key in ["logged_in", "email", "otp_sent_to", "otp_verified", "login_email"]:
|
61 |
+
st.session_state[key] = False if isinstance(st.session_state.get(key), bool) else None
|
62 |
+
|
63 |
+
def fetch_wikipedia_summary(title, lang="en"):
|
64 |
+
try:
|
65 |
+
wikipedia.set_lang(lang)
|
66 |
+
return wikipedia.summary(title, sentences=4)
|
67 |
+
except wikipedia.exceptions.DisambiguationError as e:
|
68 |
+
return f"Multiple results found for '{title}': {e.options}"
|
69 |
+
except wikipedia.exceptions.PageError:
|
70 |
+
return f"No Wikipedia page found for '{title}' in selected language."
|
71 |
+
except Exception as e:
|
72 |
+
return f"Error fetching summary: {str(e)}"
|
73 |
+
|
74 |
+
def save_temp_file(uploaded_file, filetype):
|
75 |
+
if uploaded_file:
|
76 |
+
filename = f"{filetype}_{datetime.now().strftime('%Y%m%d%H%M%S')}_{uploaded_file.name}"
|
77 |
+
filepath = os.path.join(MEDIA_FOLDER, filename)
|
78 |
+
with open(filepath, "wb") as f:
|
79 |
+
f.write(uploaded_file.read())
|
80 |
+
return filepath
|
81 |
+
return ""
|
82 |
+
|
83 |
+
def save_feedback(email, place, state, district, text, img_path="", audio_path="", video_path=""):
|
84 |
+
if not os.path.exists(FEEDBACK_FILE):
|
85 |
+
with open(FEEDBACK_FILE, 'w', newline='', encoding='utf-8') as f:
|
86 |
+
writer = csv.writer(f)
|
87 |
+
writer.writerow([
|
88 |
+
"user_email", "place_name", "state", "district", "feedback_text",
|
89 |
+
"image_file", "audio_file", "video_file", "timestamp"
|
90 |
+
])
|
91 |
+
with open(FEEDBACK_FILE, 'a', newline='', encoding='utf-8') as f:
|
92 |
+
writer = csv.writer(f)
|
93 |
+
writer.writerow([
|
94 |
+
email, place, state, district, text,
|
95 |
+
img_path, audio_path, video_path, datetime.now().isoformat()
|
96 |
+
])
|
97 |
+
|
98 |
+
# ====== Pages ======
|
99 |
+
def signup_page():
|
100 |
+
st.header("π Signup")
|
101 |
+
name = st.text_input("Name")
|
102 |
+
email = st.text_input("Email (Gmail)")
|
103 |
+
contact = st.text_input("Contact Number")
|
104 |
+
if st.button("Sign Up"):
|
105 |
+
if email in USERS:
|
106 |
+
st.error("User already exists, please login.")
|
107 |
+
elif not email or not name:
|
108 |
+
st.error("Please enter name and email.")
|
109 |
+
else:
|
110 |
+
USERS[email] = {"name": name, "email": email, "contact": contact}
|
111 |
+
send_otp(email)
|
112 |
+
st.success(f"User {name} registered! OTP sent to {email} (simulated). Please verify OTP on Login page.")
|
113 |
+
st.session_state.login_email = email
|
114 |
+
|
115 |
+
def login_page():
|
116 |
+
st.header("π Login")
|
117 |
+
email = st.text_input("Email", key="login_email_input")
|
118 |
+
if st.button("Send OTP"):
|
119 |
+
if email in USERS:
|
120 |
+
send_otp(email)
|
121 |
+
st.success(f"OTP sent to {email} (simulated). Please enter OTP below.")
|
122 |
+
st.session_state.login_email = email
|
123 |
+
else:
|
124 |
+
st.error("Email not found. Please sign up first.")
|
125 |
+
if st.session_state.login_email == email:
|
126 |
+
otp = st.text_input("Enter OTP (use 1234 for demo)", type="password")
|
127 |
+
if st.button("Verify OTP"):
|
128 |
+
if verify_otp(otp):
|
129 |
+
st.success("OTP verified! Logged in successfully.")
|
130 |
+
else:
|
131 |
+
st.error("Incorrect OTP or no OTP sent.")
|
132 |
+
|
133 |
+
def main_app():
|
134 |
+
st.sidebar.success(f"Logged in as {st.session_state.email}")
|
135 |
+
if st.sidebar.button("Logout"):
|
136 |
+
logout()
|
137 |
+
st.experimental_rerun()
|
138 |
+
|
139 |
+
# Feedback Form
|
140 |
+
st.sidebar.markdown("---")
|
141 |
+
st.sidebar.subheader("π Give Feedback")
|
142 |
+
feedback_text = st.sidebar.text_area("Your thoughts about this place", height=100)
|
143 |
+
feedback_image = st.sidebar.file_uploader("Upload an image", type=["jpg", "jpeg", "png"])
|
144 |
+
feedback_audio = st.sidebar.file_uploader("Upload audio", type=["mp3", "wav", "m4a"])
|
145 |
+
feedback_video = st.sidebar.file_uploader("Upload video", type=["mp4", "mov", "avi"])
|
146 |
+
|
147 |
+
if feedback_image:
|
148 |
+
st.sidebar.image(feedback_image, caption="Your uploaded image", use_container_width=True)
|
149 |
+
if feedback_audio:
|
150 |
+
st.sidebar.audio(feedback_audio, format=feedback_audio.type)
|
151 |
+
if feedback_video:
|
152 |
+
st.sidebar.video(feedback_video)
|
153 |
+
|
154 |
+
st.title("π Gyana Vedika - Cultural Explorer")
|
155 |
+
|
156 |
+
state = st.selectbox("Select State", list(STATES.keys()))
|
157 |
+
district = st.selectbox("Select District", STATES[state])
|
158 |
+
st.subheader(f"Explore {district}, {state}")
|
159 |
+
|
160 |
+
uploaded_image = st.file_uploader("Upload a cultural/historical place image", type=["png", "jpg", "jpeg"])
|
161 |
+
place_to_search = None
|
162 |
+
|
163 |
+
if uploaded_image:
|
164 |
+
st.image(uploaded_image, caption="Uploaded Image", use_container_width=True)
|
165 |
+
filename = uploaded_image.name.lower()
|
166 |
+
keywords_map = {
|
167 |
+
"charminar": "Charminar", "golconda": "Golconda Fort", "qutubshahi": "Qutb Shahi Tombs",
|
168 |
+
"birla": "Birla Mandir", "salarjung": "Salar Jung Museum", "warangal": "Warangal Fort",
|
169 |
+
"ramappa": "Ramappa Temple", "bhadrakali": "Bhadra Kali Temple", "kakatiya": "Kakatiya Kala Thoranam",
|
170 |
+
"pakhal": "Pakhal Lake", "medak": "Medak Cathedral", "nagarjuna": "Nagarjuna Sagar Dam",
|
171 |
+
"taj": "Taj Mahal", "gateway": "Gateway of India", "qutub": "Qutub Minar",
|
172 |
+
"mysore": "Mysore Palace", "hampi": "Hampi", "konark": "Konark Sun Temple",
|
173 |
+
"varanasi": "Varanasi", "madurai": "Meenakshi Temple", "ajanta": "Ajanta Caves", "ellora": "Ellora Caves",
|
174 |
+
}
|
175 |
+
for kw, place in keywords_map.items():
|
176 |
+
if kw in filename:
|
177 |
+
place_to_search = place
|
178 |
+
st.success(f"Recognized Place: {place_to_search}")
|
179 |
+
break
|
180 |
+
if not place_to_search:
|
181 |
+
st.warning("Could not recognize place. Try renaming the image file with landmark name.")
|
182 |
+
|
183 |
+
manual_place = st.text_input("Or manually enter place name to search info")
|
184 |
+
if manual_place.strip():
|
185 |
+
place_to_search = manual_place.strip()
|
186 |
+
|
187 |
+
if place_to_search:
|
188 |
+
language = st.selectbox("Select Wikipedia summary language", ["English", "Hindi", "Telugu", "Tamil
|