-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchat.py
More file actions
160 lines (137 loc) · 5.15 KB
/
Copy pathchat.py
File metadata and controls
160 lines (137 loc) · 5.15 KB
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
from gtts import gTTS
import io
import streamlit as st
import google.generativeai as genai
# ----------------- CONFIG -----------------
# Supported languages
LANGUAGES = {
"English": "en",
"Hindi (हिंदी)": "hi",
"Marathi (मराठी)": "mr",
"Gujarati (ગુજરાતી)": "gu",
"Bengali (বাংলা)": "bn",
"Tamil (தமிழ்)": "ta",
"Telugu (తెలుగు)": "te",
"Kannada (ಕನ್ನಡ)": "kn",
"Malayalam (മലയാളം)": "ml",
"Punjabi (ਪੰਜਾਬੀ)": "pa"
}
# Page settings
st.set_page_config(page_title="Drishti Access Chatbot", layout="wide", page_icon="👁️")
# Custom CSS
st.markdown("""
<style>
.stApp {
background-color: #F0F8FF;
font-family: 'Segoe UI', sans-serif;
}
section[data-testid="stSidebar"] {
background-color: #F0F8FF;
border-right: 1px solid #d0d7e2;
}
.stButton > button {
background-color: #6f4fcf;
color: white;
border-radius: 8px;
padding: 0.5em 1.2em;
font-weight: 600;
border: none;
}
.stButton > button:hover {
background-color: #5c3bb6;
}
.chat-message {
padding: 1em;
background-color: #ffffff;
border-radius: 10px;
margin-bottom: 10px;
box-shadow: 0 2px 6px rgba(0,0,0,0.05);
}
.user-message { border-left: 4px solid #6f4fcf; }
.bot-message { border-left: 4px solid #4caf50; }
</style>
""", unsafe_allow_html=True)
# ----------------- HEADER -----------------
st.markdown("<h2 style='color:#7B61FF;'>👁️ Drishti Access: Event Situations Simulation Chatbot</h2>", unsafe_allow_html=True)
# ----------------- SESSION -----------------
if "event_description" not in st.session_state:
st.session_state.event_description = ""
if "chat_history" not in st.session_state:
st.session_state.chat_history = []
if "selected_language" not in st.session_state:
st.session_state.selected_language = "English"
# ----------------- SIDEBAR -----------------
with st.sidebar:
st.header("📋 Event Setup")
venue = st.text_input("Venue Name", "Nehru Stadium")
capacity = st.number_input("Expected Crowd", min_value=0, value=10000)
gates = st.text_area("Gate Info", "Gate A: 8:00 AM - 11:00 AM\nGate B: 8:00 AM - 11:00 AM")
structure = st.text_area("Venue Structure", "Stage center, Gate A East, Gate B West, Toilets near Gate A, Food Stalls near Gate B...")
timeline = st.text_area("Event Timeline", "Opening: 8 AM\nMain: 12 PM\nClose: 2 PM")
if st.button("✅ Save Event Details"):
st.session_state.event_description = f"""
Event Name: {venue}
Expected Crowd: {capacity}
Gates:
{gates}
Structure:
{structure}
Timeline:
{timeline}
"""
st.success("✅ Event details saved.")
st.divider()
st.markdown("### 🌐 Language & 🔑 API Settings")
st.session_state.selected_language = st.selectbox("Response Language", list(LANGUAGES.keys()))
use_custom_key = st.checkbox("Use my own Gemini API key", value=False)
if use_custom_key:
user_api_key = st.text_input("Enter Gemini API Key", type="password")
if user_api_key:
genai.configure(api_key=user_api_key)
else:
st.warning("Please enter a valid key.")
else:
genai.configure(api_key=st.secrets["GEMINI_API_KEY"])
# ----------------- CHAT -----------------
st.subheader("💬 Ask What-If Scenarios")
if st.session_state.event_description:
for i, message in enumerate(st.session_state.chat_history):
with st.chat_message("user"):
st.markdown(f"<div class='chat-message user-message'>{message['user']}</div>", unsafe_allow_html=True)
with st.chat_message("assistant"):
st.markdown(f"<div class='chat-message bot-message'>{message['bot']}</div>", unsafe_allow_html=True)
if "audio" in message:
st.audio(message["audio"], format="audio/mp3")
user_input = st.chat_input("What do you want to simulate?")
if user_input:
lang_name = st.session_state.selected_language
lang_code = LANGUAGES[lang_name]
prompt = f"""
You are a crowd simulation assistant for large public events.
Based on the following event details:
{st.session_state.event_description}
Respond in {lang_name}.
Question: {user_input}
"""
with st.spinner("🔄 Simulating..."):
model = genai.GenerativeModel('gemini-2.5-flash')
response = model.generate_content(prompt)
answer = response.text
# TTS in memory using BytesIO
audio_bytes = None
try:
tts = gTTS(answer, lang=lang_code)
audio_buffer = io.BytesIO()
tts.write_to_fp(audio_buffer)
audio_buffer.seek(0)
audio_bytes = audio_buffer.read()
except Exception as e:
st.warning(f"TTS failed: {e}")
st.session_state.chat_history.append({
"user": user_input,
"bot": answer,
"audio": audio_bytes
})
st.rerun()
else:
st.info("👈 Fill out event details to begin simulation.")