Spaces:
Sleeping
Sleeping
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +47 -100
src/streamlit_app.py
CHANGED
@@ -4,15 +4,16 @@ import os
|
|
4 |
import wikipedia
|
5 |
import csv
|
6 |
from datetime import datetime
|
|
|
7 |
|
8 |
# ====== Setup ======
|
9 |
USERS = {}
|
10 |
-
|
|
|
|
|
11 |
MEDIA_FOLDER = os.path.join(UPLOAD_FOLDER, "feedback_media")
|
12 |
FEEDBACK_FILE = os.path.join(UPLOAD_FOLDER, "feedback.csv")
|
13 |
|
14 |
-
# Create folders if they don't exist
|
15 |
-
os.makedirs(UPLOAD_FOLDER, exist_ok=True)
|
16 |
os.makedirs(MEDIA_FOLDER, exist_ok=True)
|
17 |
|
18 |
STATES = {
|
@@ -43,7 +44,7 @@ for key, default in {
|
|
43 |
def send_otp(email):
|
44 |
st.session_state.otp_sent_to = email
|
45 |
st.session_state.otp_verified = False
|
46 |
-
return "1234"
|
47 |
|
48 |
def verify_otp(entered_otp):
|
49 |
if st.session_state.otp_sent_to and entered_otp == "1234":
|
@@ -75,7 +76,7 @@ def save_temp_file(uploaded_file, filetype):
|
|
75 |
filepath = os.path.join(MEDIA_FOLDER, filename)
|
76 |
try:
|
77 |
with open(filepath, "wb") as f:
|
78 |
-
f.write(uploaded_file.getbuffer())
|
79 |
return filepath
|
80 |
except Exception as e:
|
81 |
st.error(f"Failed to save {filetype} file: {e}")
|
@@ -89,7 +90,6 @@ def save_feedback(email, place, state, district, text, img_path="", audio_path="
|
|
89 |
"user_email", "place_name", "state", "district", "feedback_text",
|
90 |
"image_file", "audio_file", "video_file", "timestamp"
|
91 |
])
|
92 |
-
|
93 |
with open(FEEDBACK_FILE, 'a', newline='', encoding='utf-8') as f:
|
94 |
writer = csv.writer(f)
|
95 |
writer.writerow([
|
@@ -112,7 +112,7 @@ def signup_page():
|
|
112 |
else:
|
113 |
USERS[email] = {"name": name, "email": email, "contact": contact}
|
114 |
send_otp(email)
|
115 |
-
st.success(f"User {name} registered! OTP sent to {email} (simulated).
|
116 |
st.session_state.login_email = email
|
117 |
|
118 |
def login_page():
|
@@ -121,15 +121,15 @@ def login_page():
|
|
121 |
if st.button("Send OTP"):
|
122 |
if email in USERS:
|
123 |
send_otp(email)
|
124 |
-
st.success(f"OTP sent to {email} (simulated).
|
125 |
st.session_state.login_email = email
|
126 |
else:
|
127 |
st.error("Email not found. Please sign up first.")
|
128 |
if st.session_state.login_email == email:
|
129 |
-
otp = st.text_input("Enter OTP (use 1234
|
130 |
if st.button("Verify OTP"):
|
131 |
if verify_otp(otp):
|
132 |
-
st.success("
|
133 |
else:
|
134 |
st.error("Incorrect OTP or no OTP sent.")
|
135 |
|
@@ -138,81 +138,52 @@ def main_app():
|
|
138 |
if st.sidebar.button("Logout"):
|
139 |
logout()
|
140 |
st.experimental_rerun()
|
141 |
-
|
142 |
-
# Feedback inputs on left sidebar
|
143 |
st.sidebar.markdown("---")
|
144 |
st.sidebar.subheader("📝 Give Feedback")
|
145 |
-
feedback_text = st.sidebar.text_area("Your thoughts
|
146 |
-
feedback_image = st.sidebar.file_uploader("Upload
|
147 |
-
feedback_audio = st.sidebar.file_uploader("Upload audio", type=["mp3",
|
148 |
-
feedback_video = st.sidebar.file_uploader("Upload video", type=["mp4",
|
149 |
|
150 |
-
|
151 |
-
if
|
152 |
-
|
153 |
-
if feedback_audio:
|
154 |
-
st.sidebar.audio(feedback_audio, format=feedback_audio.type)
|
155 |
-
if feedback_video:
|
156 |
-
st.sidebar.video(feedback_video)
|
157 |
|
158 |
-
# Main content
|
159 |
st.title("📚 Gyana Vedika - Cultural Explorer")
|
160 |
-
|
161 |
state = st.selectbox("Select State", list(STATES.keys()))
|
162 |
district = st.selectbox("Select District", STATES[state])
|
163 |
st.subheader(f"Explore {district}, {state}")
|
164 |
|
165 |
-
uploaded_image = st.file_uploader("Upload
|
166 |
place_to_search = None
|
167 |
if uploaded_image:
|
168 |
-
st.image(uploaded_image,
|
169 |
-
|
170 |
-
|
171 |
-
|
172 |
-
"
|
173 |
-
"
|
174 |
-
"
|
175 |
-
"birla": "Birla Mandir",
|
176 |
-
"salarjung": "Salar Jung Museum",
|
177 |
-
"warangal": "Warangal Fort",
|
178 |
-
"ramappa": "Ramappa Temple",
|
179 |
-
"bhadrakali": "Bhadra Kali Temple",
|
180 |
-
"kakatiya": "Kakatiya Kala Thoranam",
|
181 |
-
"pakhal": "Pakhal Lake",
|
182 |
-
"medak": "Medak Cathedral",
|
183 |
-
"nagarjuna": "Nagarjuna Sagar Dam",
|
184 |
-
# Other famous places (existing)
|
185 |
-
"taj": "Taj Mahal",
|
186 |
-
"gateway": "Gateway of India",
|
187 |
-
"qutub": "Qutub Minar",
|
188 |
-
"mysore": "Mysore Palace",
|
189 |
-
"hampi": "Hampi",
|
190 |
-
"konark": "Konark Sun Temple",
|
191 |
-
"varanasi": "Varanasi",
|
192 |
-
"madurai": "Meenakshi Temple",
|
193 |
-
"ajanta": "Ajanta Caves",
|
194 |
-
"ellora": "Ellora Caves",
|
195 |
}
|
196 |
-
for kw, place in
|
197 |
-
if kw in
|
198 |
place_to_search = place
|
199 |
-
st.success(f"Recognized
|
200 |
break
|
201 |
if not place_to_search:
|
202 |
-
st.warning("Could not recognize place.
|
203 |
|
204 |
-
manual_place = st.text_input("Or
|
205 |
if manual_place.strip():
|
206 |
place_to_search = manual_place.strip()
|
207 |
|
208 |
if place_to_search:
|
209 |
-
|
210 |
-
|
211 |
-
|
212 |
-
|
213 |
-
|
214 |
-
|
215 |
-
st.markdown(f"### Summary of {place_to_search}")
|
216 |
st.write(summary)
|
217 |
|
218 |
if st.sidebar.button("Submit Feedback"):
|
@@ -220,48 +191,24 @@ def main_app():
|
|
220 |
img_path = save_temp_file(feedback_image, "img")
|
221 |
audio_path = save_temp_file(feedback_audio, "audio")
|
222 |
video_path = save_temp_file(feedback_video, "video")
|
223 |
-
|
224 |
-
st.
|
225 |
-
st.sidebar.
|
226 |
-
st.sidebar.write(f"Video saved at: {video_path}")
|
227 |
-
|
228 |
-
save_feedback(
|
229 |
-
email=st.session_state.email,
|
230 |
-
place=place_to_search or "Unknown",
|
231 |
-
state=state,
|
232 |
-
district=district,
|
233 |
-
text=feedback_text.strip(),
|
234 |
-
img_path=img_path,
|
235 |
-
audio_path=audio_path,
|
236 |
-
video_path=video_path
|
237 |
-
)
|
238 |
-
st.sidebar.success("Thank you! Feedback submitted.")
|
239 |
else:
|
240 |
-
st.sidebar.warning("Please enter
|
241 |
|
242 |
# ====== UI Styling ======
|
243 |
st.set_page_config(page_title="Gyana Vedika", layout="wide")
|
244 |
-
|
245 |
st.markdown("""
|
246 |
<style>
|
247 |
-
.stButton>button {
|
248 |
-
|
249 |
-
}
|
250 |
-
|
251 |
-
border: 2px solid #008080 !important; border-radius: 8px !important;
|
252 |
-
}
|
253 |
-
h1, h2, h3, h4, h5, h6 {
|
254 |
-
color: #004d4d;
|
255 |
-
}
|
256 |
-
</style>
|
257 |
-
""", unsafe_allow_html=True)
|
258 |
|
259 |
-
# ====== Run
|
260 |
if not st.session_state.logged_in:
|
261 |
-
page = st.sidebar.radio("Choose Option", ["Signup",
|
262 |
-
if page
|
263 |
-
signup_page()
|
264 |
-
elif page == "Login":
|
265 |
-
login_page()
|
266 |
else:
|
267 |
main_app()
|
|
|
4 |
import wikipedia
|
5 |
import csv
|
6 |
from datetime import datetime
|
7 |
+
import tempfile
|
8 |
|
9 |
# ====== Setup ======
|
10 |
USERS = {}
|
11 |
+
|
12 |
+
# Use a writable temporary directory to avoid permission issues
|
13 |
+
UPLOAD_FOLDER = tempfile.mkdtemp()
|
14 |
MEDIA_FOLDER = os.path.join(UPLOAD_FOLDER, "feedback_media")
|
15 |
FEEDBACK_FILE = os.path.join(UPLOAD_FOLDER, "feedback.csv")
|
16 |
|
|
|
|
|
17 |
os.makedirs(MEDIA_FOLDER, exist_ok=True)
|
18 |
|
19 |
STATES = {
|
|
|
44 |
def send_otp(email):
|
45 |
st.session_state.otp_sent_to = email
|
46 |
st.session_state.otp_verified = False
|
47 |
+
return "1234"
|
48 |
|
49 |
def verify_otp(entered_otp):
|
50 |
if st.session_state.otp_sent_to and entered_otp == "1234":
|
|
|
76 |
filepath = os.path.join(MEDIA_FOLDER, filename)
|
77 |
try:
|
78 |
with open(filepath, "wb") as f:
|
79 |
+
f.write(uploaded_file.getbuffer())
|
80 |
return filepath
|
81 |
except Exception as e:
|
82 |
st.error(f"Failed to save {filetype} file: {e}")
|
|
|
90 |
"user_email", "place_name", "state", "district", "feedback_text",
|
91 |
"image_file", "audio_file", "video_file", "timestamp"
|
92 |
])
|
|
|
93 |
with open(FEEDBACK_FILE, 'a', newline='', encoding='utf-8') as f:
|
94 |
writer = csv.writer(f)
|
95 |
writer.writerow([
|
|
|
112 |
else:
|
113 |
USERS[email] = {"name": name, "email": email, "contact": contact}
|
114 |
send_otp(email)
|
115 |
+
st.success(f"User {name} registered! OTP sent to {email} (simulated).")
|
116 |
st.session_state.login_email = email
|
117 |
|
118 |
def login_page():
|
|
|
121 |
if st.button("Send OTP"):
|
122 |
if email in USERS:
|
123 |
send_otp(email)
|
124 |
+
st.success(f"OTP sent to {email} (simulated).")
|
125 |
st.session_state.login_email = email
|
126 |
else:
|
127 |
st.error("Email not found. Please sign up first.")
|
128 |
if st.session_state.login_email == email:
|
129 |
+
otp = st.text_input("Enter OTP (use 1234)", type="password")
|
130 |
if st.button("Verify OTP"):
|
131 |
if verify_otp(otp):
|
132 |
+
st.success("Logged in successfully.")
|
133 |
else:
|
134 |
st.error("Incorrect OTP or no OTP sent.")
|
135 |
|
|
|
138 |
if st.sidebar.button("Logout"):
|
139 |
logout()
|
140 |
st.experimental_rerun()
|
|
|
|
|
141 |
st.sidebar.markdown("---")
|
142 |
st.sidebar.subheader("📝 Give Feedback")
|
143 |
+
feedback_text = st.sidebar.text_area("Your thoughts", height=100)
|
144 |
+
feedback_image = st.sidebar.file_uploader("Upload image", type=["jpg","jpeg","png"])
|
145 |
+
feedback_audio = st.sidebar.file_uploader("Upload audio", type=["mp3","wav","m4a"])
|
146 |
+
feedback_video = st.sidebar.file_uploader("Upload video", type=["mp4","mov","avi"])
|
147 |
|
148 |
+
if feedback_image: st.sidebar.image(feedback_image, caption="Preview", use_container_width=True)
|
149 |
+
if feedback_audio: st.sidebar.audio(feedback_audio)
|
150 |
+
if feedback_video: st.sidebar.video(feedback_video)
|
|
|
|
|
|
|
|
|
151 |
|
|
|
152 |
st.title("📚 Gyana Vedika - Cultural Explorer")
|
|
|
153 |
state = st.selectbox("Select State", list(STATES.keys()))
|
154 |
district = st.selectbox("Select District", STATES[state])
|
155 |
st.subheader(f"Explore {district}, {state}")
|
156 |
|
157 |
+
uploaded_image = st.file_uploader("Upload cultural place image", type=["png","jpg","jpeg"])
|
158 |
place_to_search = None
|
159 |
if uploaded_image:
|
160 |
+
st.image(uploaded_image, use_container_width=True)
|
161 |
+
fn = uploaded_image.name.lower()
|
162 |
+
keywords = {
|
163 |
+
"charminar":"Charminar","golconda":"Golconda Fort","taj":"Taj Mahal",
|
164 |
+
"gateway":"Gateway of India","qutub":"Qutub Minar","mysore":"Mysore Palace",
|
165 |
+
"hampi":"Hampi","konark":"Konark Sun Temple","varanasi":"Varanasi",
|
166 |
+
"madurai":"Meenakshi Temple","ajanta":"Ajanta Caves","ellora":"Ellora Caves"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
167 |
}
|
168 |
+
for kw, place in keywords.items():
|
169 |
+
if kw in fn:
|
170 |
place_to_search = place
|
171 |
+
st.success(f"Recognized: {place}")
|
172 |
break
|
173 |
if not place_to_search:
|
174 |
+
st.warning("Could not recognize place. Rename your file.")
|
175 |
|
176 |
+
manual_place = st.text_input("Or enter place name manually")
|
177 |
if manual_place.strip():
|
178 |
place_to_search = manual_place.strip()
|
179 |
|
180 |
if place_to_search:
|
181 |
+
lang = st.selectbox("Wikipedia language", ["English","Hindi","Telugu","Tamil","Marathi","Bengali"])
|
182 |
+
codes = {"English":"en","Hindi":"hi","Telugu":"te","Tamil":"ta","Marathi":"mr","Bengali":"bn"}
|
183 |
+
if st.button(f"Get info: {place_to_search}"):
|
184 |
+
with st.spinner("Fetching..."):
|
185 |
+
summary = fetch_wikipedia_summary(place_to_search, lang=codes[lang])
|
186 |
+
st.markdown(f"### {place_to_search} ({lang})")
|
|
|
187 |
st.write(summary)
|
188 |
|
189 |
if st.sidebar.button("Submit Feedback"):
|
|
|
191 |
img_path = save_temp_file(feedback_image, "img")
|
192 |
audio_path = save_temp_file(feedback_audio, "audio")
|
193 |
video_path = save_temp_file(feedback_video, "video")
|
194 |
+
st.sidebar.write(f"Saved files:\n• Image: {img_path}\n• Audio: {audio_path}\n• Video: {video_path}")
|
195 |
+
save_feedback(st.session_state.email, place_to_search or "Unknown", state, district, feedback_text.strip(), img_path, audio_path, video_path)
|
196 |
+
st.sidebar.success("Thanks for your feedback!")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
197 |
else:
|
198 |
+
st.sidebar.warning("Please enter feedback text.")
|
199 |
|
200 |
# ====== UI Styling ======
|
201 |
st.set_page_config(page_title="Gyana Vedika", layout="wide")
|
|
|
202 |
st.markdown("""
|
203 |
<style>
|
204 |
+
.stButton>button { background-color: #008080; color: white; font-weight: bold; border-radius: 8px; }
|
205 |
+
.stTextInput>div>input, .stSelectbox>div>div>select, .stTextArea>div>textarea { border:2px solid #008080!important; border-radius:6px!important; }
|
206 |
+
h1, h2, h3, h4 { color:#004d4d; }
|
207 |
+
</style>""", unsafe_allow_html=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
208 |
|
209 |
+
# ====== Run App ======
|
210 |
if not st.session_state.logged_in:
|
211 |
+
page = st.sidebar.radio("Choose Option", ["Signup","Login"])
|
212 |
+
signup_page() if page=="Signup" else login_page()
|
|
|
|
|
|
|
213 |
else:
|
214 |
main_app()
|