Spaces:
Sleeping
Sleeping
File size: 9,903 Bytes
7259cb7 2419b2c 7259cb7 2419b2c 7259cb7 1d11345 7259cb7 1d11345 7259cb7 f90c777 d903b86 f90c777 d903b86 7259cb7 d903b86 20e2a26 d903b86 20e2a26 d903b86 f90c777 7259cb7 f90c777 7259cb7 f90c777 1d11345 d903b86 1d11345 f90c777 d903b86 f90c777 d903b86 f90c777 d903b86 f90c777 d903b86 f90c777 d903b86 b9b85bf 7259cb7 b9b85bf 7259cb7 d903b86 293338f d903b86 b9b85bf 7259cb7 1d11345 7259cb7 1d11345 |
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 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 |
import streamlit as st
from PIL import Image
import os
import wikipedia
import csv
from datetime import datetime
# ====== Setup ======
USERS = {}
UPLOAD_FOLDER = "/tmp/uploads"
MEDIA_FOLDER = os.path.join(UPLOAD_FOLDER, "feedback_media")
FEEDBACK_FILE = os.path.join(UPLOAD_FOLDER, "feedback.csv")
os.makedirs(UPLOAD_FOLDER, exist_ok=True)
os.makedirs(MEDIA_FOLDER, exist_ok=True)
STATES = {
"Telangana": ["Hyderabad", "Warangal", "Nizamabad", "Karimnagar", "Khammam"],
"Maharashtra": ["Mumbai", "Pune", "Nagpur", "Nashik", "Aurangabad"],
"Tamil Nadu": ["Chennai", "Coimbatore", "Madurai", "Tiruchirappalli", "Salem"],
"Karnataka": ["Bangalore", "Mysore", "Mangalore", "Hubli", "Belgaum"],
"Uttar Pradesh": ["Lucknow", "Varanasi", "Agra", "Kanpur", "Allahabad"],
"West Bengal": ["Kolkata", "Darjeeling", "Durgapur", "Siliguri", "Asansol"],
"Rajasthan": ["Jaipur", "Udaipur", "Jodhpur", "Bikaner", "Ajmer"],
"Gujarat": ["Ahmedabad", "Surat", "Vadodara", "Rajkot", "Bhavnagar"],
"Andhra Pradesh": ["Visakhapatnam", "Vijayawada", "Guntur", "Tirupati", "Kurnool"],
}
for key, default in {
"logged_in": False,
"email": "",
"otp_sent_to": None,
"otp_verified": False,
"login_email": None,
"place_image": None,
}.items():
if key not in st.session_state:
st.session_state[key] = default
# ====== Helper Functions ======
def send_otp(email):
st.session_state.otp_sent_to = email
st.session_state.otp_verified = False
return "1234"
def verify_otp(entered_otp):
if st.session_state.otp_sent_to and entered_otp == "1234":
st.session_state.logged_in = True
st.session_state.email = st.session_state.otp_sent_to
st.session_state.otp_verified = True
st.session_state.login_email = None
return True
return False
def logout():
for key in ["logged_in", "email", "otp_sent_to", "otp_verified", "login_email", "place_image"]:
st.session_state[key] = False if isinstance(st.session_state.get(key), bool) else None
def fetch_wikipedia_summary(title, lang="en"):
try:
wikipedia.set_lang(lang)
return wikipedia.summary(title, sentences=4)
except wikipedia.exceptions.DisambiguationError as e:
return f"Multiple results found for '{title}': {e.options}"
except wikipedia.exceptions.PageError:
return f"No Wikipedia page found for '{title}' in selected language."
except Exception as e:
return f"Error fetching summary: {str(e)}"
def save_temp_file(uploaded_file, filetype):
if uploaded_file:
filename = f"{filetype}_{datetime.now().strftime('%Y%m%d%H%M%S')}_{uploaded_file.name}"
filepath = os.path.join(MEDIA_FOLDER, filename)
with open(filepath, "wb") as f:
f.write(uploaded_file.read())
return filepath
return ""
def save_feedback(email, place, state, district, text, img_path="", audio_path="", video_path=""):
if not os.path.exists(FEEDBACK_FILE):
with open(FEEDBACK_FILE, 'w', newline='', encoding='utf-8') as f:
writer = csv.writer(f)
writer.writerow([
"user_email", "place_name", "state", "district", "feedback_text",
"image_file", "audio_file", "video_file", "timestamp"
])
with open(FEEDBACK_FILE, 'a', newline='', encoding='utf-8') as f:
writer = csv.writer(f)
writer.writerow([
email, place, state, district, text,
img_path, audio_path, video_path, datetime.now().isoformat()
])
# ====== Pages ======
def signup_page():
st.header("π Signup")
name = st.text_input("Name")
email = st.text_input("Email (Gmail)")
contact = st.text_input("Contact Number")
if st.button("Sign Up"):
if email in USERS:
st.error("User already exists, please login.")
elif not email or not name:
st.error("Please enter name and email.")
else:
USERS[email] = {"name": name, "email": email, "contact": contact}
send_otp(email)
st.success(f"User {name} registered! OTP sent to {email} (simulated). Please verify OTP on Login page.")
st.session_state.login_email = email
def login_page():
st.header("π Login")
email = st.text_input("Email", key="login_email_input")
if st.button("Send OTP"):
if email in USERS:
send_otp(email)
st.success(f"OTP sent to {email} (simulated). Please enter OTP below.")
st.session_state.login_email = email
else:
st.error("Email not found. Please sign up first.")
if st.session_state.login_email == email:
otp = st.text_input("Enter OTP (use 1234 for demo)", type="password")
if st.button("Verify OTP"):
if verify_otp(otp):
st.success("OTP verified! Logged in successfully.")
else:
st.error("Incorrect OTP or no OTP sent.")
def main_app():
st.sidebar.success(f"Logged in as {st.session_state.email}")
if st.sidebar.button("Logout"):
logout()
st.experimental_rerun()
st.sidebar.markdown("---")
st.sidebar.subheader("π Give Feedback")
feedback_text = st.sidebar.text_area("Your thoughts about this place", height=100)
feedback_image = st.sidebar.file_uploader("Upload an image", type=["jpg", "jpeg", "png"])
feedback_audio = st.sidebar.file_uploader("Upload audio", type=["mp3", "wav", "m4a"])
feedback_video = st.sidebar.file_uploader("Upload video", type=["mp4", "mov", "avi"])
if feedback_image:
st.sidebar.image(feedback_image, caption="Your uploaded image", use_container_width=True)
if feedback_audio:
st.sidebar.audio(feedback_audio, format=feedback_audio.type)
if feedback_video:
st.sidebar.video(feedback_video)
st.title("π Gyana Vedika - Cultural Explorer")
state = st.selectbox("Select State", list(STATES.keys()))
district = st.selectbox("Select District", STATES[state])
st.subheader(f"Explore {district}, {state}")
uploaded_image = st.file_uploader("Upload a cultural/historical place image", type=["png", "jpg", "jpeg"])
if uploaded_image:
st.session_state["place_image"] = uploaded_image
place_to_search = None
if st.session_state["place_image"]:
st.image(st.session_state["place_image"], caption="Uploaded Image", use_container_width=True)
filename = st.session_state["place_image"].name.lower()
keywords_map = {
"charminar": "Charminar", "golconda": "Golconda Fort", "qutubshahi": "Qutb Shahi Tombs",
"birla": "Birla Mandir", "salarjung": "Salar Jung Museum", "warangal": "Warangal Fort",
"ramappa": "Ramappa Temple", "bhadrakali": "Bhadra Kali Temple", "kakatiya": "Kakatiya Kala Thoranam",
"pakhal": "Pakhal Lake", "medak": "Medak Cathedral", "nagarjuna": "Nagarjuna Sagar Dam",
"taj": "Taj Mahal", "gateway": "Gateway of India", "qutub": "Qutub Minar", "mysore": "Mysore Palace",
"hampi": "Hampi", "konark": "Konark Sun Temple", "varanasi": "Varanasi", "madurai": "Meenakshi Temple",
"ajanta": "Ajanta Caves", "ellora": "Ellora Caves",
}
for kw, place in keywords_map.items():
if kw in filename:
place_to_search = place
st.success(f"Recognized Place: {place_to_search}")
break
if not place_to_search:
st.warning("Could not recognize place. Try renaming the image file with landmark name.")
manual_place = st.text_input("Or manually enter place name to search info")
if manual_place.strip():
place_to_search = manual_place.strip()
if place_to_search:
language = st.selectbox("Select Wikipedia summary language", ["English", "Hindi", "Telugu", "Tamil", "Marathi", "Bengali"])
lang_codes = {"English": "en", "Hindi": "hi", "Telugu": "te", "Tamil": "ta", "Marathi": "mr", "Bengali": "bn"}
lang_code = lang_codes.get(language, "en")
if st.button(f"Get info about {place_to_search}"):
with st.spinner(f"Fetching Wikipedia summary for {place_to_search}..."):
summary = fetch_wikipedia_summary(place_to_search, lang=lang_code)
st.markdown(f"### Summary of {place_to_search}")
st.write(summary)
if st.sidebar.button("Submit Feedback"):
if feedback_text.strip():
img_path = save_temp_file(feedback_image, "img")
audio_path = save_temp_file(feedback_audio, "audio")
video_path = save_temp_file(feedback_video, "video")
save_feedback(
email=st.session_state.email,
place=place_to_search or "Unknown",
state=state,
district=district,
text=feedback_text.strip(),
img_path=img_path,
audio_path=audio_path,
video_path=video_path
)
st.sidebar.success("Thank you! Feedback submitted.")
else:
st.sidebar.warning("Please enter your feedback text.")
# ====== UI Styling ======
st.set_page_config(page_title="Gyana Vedika", layout="wide")
st.markdown("""
<style>
.stButton>button {
background-color: #008080; color: white; font-weight: bold; border-radius: 10px;
}
.stTextInput>div>input, .stSelectbox>div>div>select, .stTextArea>div>textarea {
border: 2px solid #008080 !important; border-radius: 8px !important;
}
h1, h2, h3, h4, h5, h6 {
color: #004d4d;
}
</style>
""", unsafe_allow_html=True)
# ====== Main Routing ======
if not st.session_state.logged_in:
page = st.sidebar.radio("Choose Option", ["Signup", "Login"])
if page == "Signup":
signup_page()
elif page == "Login":
login_page()
else:
main_app()
|