GHarshasri commited on
Commit
1569d36
·
verified ·
1 Parent(s): 739643c

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +6 -162
src/streamlit_app.py CHANGED
@@ -1,133 +1,3 @@
1
- import streamlit as st
2
- from PIL import Image
3
- import wikipedia
4
- import csv
5
- from datetime import datetime
6
- import os
7
-
8
- # ====== Setup ======
9
- USERS = {}
10
-
11
- # Use /tmp folder for uploads (common writable folder on Linux)
12
- UPLOAD_FOLDER = "/tmp/gyanavedika_upload"
13
- MEDIA_FOLDER = os.path.join(UPLOAD_FOLDER, "feedback_media")
14
- FEEDBACK_FILE = os.path.join(UPLOAD_FOLDER, "feedback.csv")
15
-
16
- os.makedirs(MEDIA_FOLDER, exist_ok=True)
17
-
18
- STATES = {
19
- "Telangana": ["Hyderabad", "Warangal", "Nizamabad", "Karimnagar", "Khammam"],
20
- "Maharashtra": ["Mumbai", "Pune", "Nagpur", "Nashik", "Aurangabad"],
21
- "Tamil Nadu": ["Chennai", "Coimbatore", "Madurai", "Tiruchirappalli", "Salem"],
22
- "Karnataka": ["Bangalore", "Mysore", "Mangalore", "Hubli", "Belgaum"],
23
- "Uttar Pradesh": ["Lucknow", "Varanasi", "Agra", "Kanpur", "Allahabad"],
24
- "West Bengal": ["Kolkata", "Darjeeling", "Durgapur", "Siliguri", "Asansol"],
25
- "Rajasthan": ["Jaipur", "Udaipur", "Jodhpur", "Bikaner", "Ajmer"],
26
- "Gujarat": ["Ahmedabad", "Surat", "Vadodara", "Rajkot", "Bhavnagar"],
27
- "Andhra Pradesh": ["Visakhapatnam", "Vijayawada", "Guntur", "Tirupati", "Kurnool"],
28
- }
29
-
30
- # ====== Session State Initialization ======
31
- for key, default in {
32
- "logged_in": False,
33
- "email": "",
34
- "otp_sent_to": None,
35
- "otp_verified": False,
36
- "login_email": None,
37
- }.items():
38
- if key not in st.session_state:
39
- st.session_state[key] = default
40
-
41
- # ====== Helper Functions ======
42
- def send_otp(email):
43
- st.session_state.otp_sent_to = email
44
- st.session_state.otp_verified = False
45
- return "1234"
46
-
47
- def verify_otp(entered_otp):
48
- if st.session_state.otp_sent_to and entered_otp == "1234":
49
- st.session_state.logged_in = True
50
- st.session_state.email = st.session_state.otp_sent_to
51
- st.session_state.otp_verified = True
52
- st.session_state.login_email = None
53
- return True
54
- return False
55
-
56
- def logout():
57
- for key in ["logged_in", "email", "otp_sent_to", "otp_verified", "login_email"]:
58
- st.session_state[key] = False if isinstance(st.session_state.get(key), bool) else None
59
-
60
- def fetch_wikipedia_summary(title, lang="en"):
61
- try:
62
- wikipedia.set_lang(lang)
63
- return wikipedia.summary(title, sentences=4)
64
- except wikipedia.exceptions.DisambiguationError as e:
65
- return f"Multiple results found for '{title}': {e.options}"
66
- except wikipedia.exceptions.PageError:
67
- return f"No Wikipedia page found for '{title}' in selected language."
68
- except Exception as e:
69
- return f"Error fetching summary: {str(e)}"
70
-
71
- def save_temp_file(uploaded_file, filetype):
72
- if uploaded_file:
73
- filename = f"{filetype}_{datetime.now().strftime('%Y%m%d%H%M%S')}_{uploaded_file.name}"
74
- filepath = os.path.join(MEDIA_FOLDER, filename)
75
- with open(filepath, "wb") as f:
76
- f.write(uploaded_file.getbuffer())
77
- return filepath
78
- return ""
79
-
80
- def save_feedback(email, place, state, district, text, img_path="", audio_path="", video_path=""):
81
- if not os.path.exists(FEEDBACK_FILE):
82
- with open(FEEDBACK_FILE, 'w', newline='', encoding='utf-8') as f:
83
- writer = csv.writer(f)
84
- writer.writerow([
85
- "user_email", "place_name", "state", "district", "feedback_text",
86
- "image_file", "audio_file", "video_file", "timestamp"
87
- ])
88
-
89
- with open(FEEDBACK_FILE, 'a', newline='', encoding='utf-8') as f:
90
- writer = csv.writer(f)
91
- writer.writerow([
92
- email, place, state, district, text,
93
- img_path, audio_path, video_path, datetime.now().isoformat()
94
- ])
95
-
96
- # ====== Pages ======
97
- def signup_page():
98
- st.header("🚀 Signup")
99
- name = st.text_input("Name")
100
- email = st.text_input("Email (Gmail)")
101
- contact = st.text_input("Contact Number")
102
- if st.button("Sign Up"):
103
- if email in USERS:
104
- st.error("User already exists, please login.")
105
- elif not email or not name:
106
- st.error("Please enter name and email.")
107
- else:
108
- USERS[email] = {"name": name, "email": email, "contact": contact}
109
- send_otp(email)
110
- st.success(f"User {name} registered! OTP sent to {email} (simulated). Please verify OTP on Login page.")
111
- st.session_state.login_email = email
112
-
113
- def login_page():
114
- st.header("🔐 Login")
115
- email = st.text_input("Email", key="login_email_input")
116
- if st.button("Send OTP"):
117
- if email in USERS:
118
- send_otp(email)
119
- st.success(f"OTP sent to {email} (simulated). Please enter OTP below.")
120
- st.session_state.login_email = email
121
- else:
122
- st.error("Email not found. Please sign up first.")
123
- if st.session_state.login_email == email:
124
- otp = st.text_input("Enter OTP (use 1234 for demo)", type="password")
125
- if st.button("Verify OTP"):
126
- if verify_otp(otp):
127
- st.success("OTP verified! Logged in successfully.")
128
- else:
129
- st.error("Incorrect OTP or no OTP sent.")
130
-
131
  def main_app():
132
  st.sidebar.success(f"Logged in as {st.session_state.email}")
133
  if st.sidebar.button("Logout"):
@@ -138,9 +8,9 @@ def main_app():
138
  st.sidebar.markdown("---")
139
  st.sidebar.subheader("📝 Give Feedback")
140
  feedback_text = st.sidebar.text_area("Your thoughts about this place", height=100)
141
- feedback_image = st.sidebar.file_uploader("Upload an image", type=["jpg", "jpeg", "png"])
142
- feedback_audio = st.sidebar.file_uploader("Upload audio", type=["mp3", "wav", "m4a"])
143
- feedback_video = st.sidebar.file_uploader("Upload video", type=["mp4", "mov", "avi"])
144
 
145
  if feedback_image:
146
  st.sidebar.image(feedback_image, caption="Your uploaded image", use_container_width=True)
@@ -154,7 +24,7 @@ def main_app():
154
  district = st.selectbox("Select District", STATES[state])
155
  st.subheader(f"Explore {district}, {state}")
156
 
157
- uploaded_image = st.file_uploader("Upload a cultural/historical place image", type=["png", "jpg", "jpeg"])
158
  place_to_search = None
159
  if uploaded_image:
160
  st.image(uploaded_image, caption="Uploaded Image", use_container_width=True)
@@ -191,7 +61,7 @@ def main_app():
191
  st.write(summary)
192
 
193
  if st.sidebar.button("Submit Feedback"):
194
- if feedback_text.strip():
195
  img_path = save_temp_file(feedback_image, "img")
196
  audio_path = save_temp_file(feedback_audio, "audio")
197
  video_path = save_temp_file(feedback_video, "video")
@@ -207,30 +77,4 @@ def main_app():
207
  )
208
  st.sidebar.success("Thank you! Feedback submitted.")
209
  else:
210
- st.sidebar.warning("Please enter your feedback text.")
211
-
212
- # ====== UI Styling ======
213
- st.set_page_config(page_title="Gyana Vedika", layout="wide")
214
-
215
- st.markdown("""
216
- <style>
217
- .stButton>button {
218
- background-color: #008080; color: white; font-weight: bold; border-radius: 10px;
219
- }
220
- .stTextInput>div>input, .stSelectbox>div>div>select, .stTextArea>div>textarea {
221
- border: 2px solid #008080 !important; border-radius: 8px !important;
222
- }
223
- h1, h2, h3, h4, h5, h6 {
224
- color: #004d4d;
225
- }
226
- </style>
227
- """, unsafe_allow_html=True)
228
-
229
- if not st.session_state.logged_in:
230
- page = st.sidebar.radio("Choose Option", ["Signup", "Login"])
231
- if page == "Signup":
232
- signup_page()
233
- elif page == "Login":
234
- login_page()
235
- else:
236
- main_app()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  def main_app():
2
  st.sidebar.success(f"Logged in as {st.session_state.email}")
3
  if st.sidebar.button("Logout"):
 
8
  st.sidebar.markdown("---")
9
  st.sidebar.subheader("📝 Give Feedback")
10
  feedback_text = st.sidebar.text_area("Your thoughts about this place", height=100)
11
+ feedback_image = st.sidebar.file_uploader("Upload an image", type=["jpg", "jpeg", "png"], key="image_upload")
12
+ feedback_audio = st.sidebar.file_uploader("Upload audio", type=["mp3", "wav", "m4a"], key="audio_upload")
13
+ feedback_video = st.sidebar.file_uploader("Upload video", type=["mp4", "mov", "avi"], key="video_upload")
14
 
15
  if feedback_image:
16
  st.sidebar.image(feedback_image, caption="Your uploaded image", use_container_width=True)
 
24
  district = st.selectbox("Select District", STATES[state])
25
  st.subheader(f"Explore {district}, {state}")
26
 
27
+ uploaded_image = st.file_uploader("Upload a cultural/historical place image", type=["png", "jpg", "jpeg"], key="place_image")
28
  place_to_search = None
29
  if uploaded_image:
30
  st.image(uploaded_image, caption="Uploaded Image", use_container_width=True)
 
61
  st.write(summary)
62
 
63
  if st.sidebar.button("Submit Feedback"):
64
+ if feedback_text.strip() or feedback_image or feedback_audio or feedback_video:
65
  img_path = save_temp_file(feedback_image, "img")
66
  audio_path = save_temp_file(feedback_audio, "audio")
67
  video_path = save_temp_file(feedback_video, "video")
 
77
  )
78
  st.sidebar.success("Thank you! Feedback submitted.")
79
  else:
80
+ st.sidebar.warning("Please provide feedback text or upload a file.")