Update app.py
Browse files
app.py
CHANGED
@@ -3,10 +3,6 @@ import time
|
|
3 |
import requests
|
4 |
from streamlit.components.v1 import html
|
5 |
import os
|
6 |
-
import base64
|
7 |
-
import io
|
8 |
-
from pydub import AudioSegment
|
9 |
-
import speech_recognition as sr
|
10 |
|
11 |
# Import transformers and cache the help agent for performance
|
12 |
@st.cache_resource
|
@@ -95,18 +91,6 @@ def inject_custom_css():
|
|
95 |
border-radius: 5px;
|
96 |
margin: 10px 0;
|
97 |
}
|
98 |
-
|
99 |
-
.mic-btn {
|
100 |
-
background: #6C63FF !important;
|
101 |
-
color: white !important;
|
102 |
-
border-radius: 50% !important;
|
103 |
-
width: 40px !important;
|
104 |
-
height: 40px !important;
|
105 |
-
padding: 0 !important;
|
106 |
-
display: flex !important;
|
107 |
-
align-items: center !important;
|
108 |
-
justify-content: center !important;
|
109 |
-
}
|
110 |
</style>
|
111 |
""", unsafe_allow_html=True)
|
112 |
|
@@ -209,41 +193,6 @@ def ask_help_agent(query):
|
|
209 |
except Exception as e:
|
210 |
return f"Error in help agent: {str(e)}"
|
211 |
|
212 |
-
# Audio processing functions
|
213 |
-
def transcribe_audio(audio_bytes):
|
214 |
-
"""Convert audio bytes to text using SpeechRecognition"""
|
215 |
-
recognizer = sr.Recognizer()
|
216 |
-
try:
|
217 |
-
# Convert bytes to audio file
|
218 |
-
audio = AudioSegment.from_file(io.BytesIO(audio_bytes))
|
219 |
-
# Export as WAV
|
220 |
-
wav_io = io.BytesIO()
|
221 |
-
audio.export(wav_io, format="wav")
|
222 |
-
wav_io.seek(0)
|
223 |
-
|
224 |
-
with sr.AudioFile(wav_io) as source:
|
225 |
-
audio_data = recognizer.record(source)
|
226 |
-
text = recognizer.recognize_google(audio_data)
|
227 |
-
return text.lower()
|
228 |
-
except sr.UnknownValueError:
|
229 |
-
st.error("Could not understand audio")
|
230 |
-
except sr.RequestError as e:
|
231 |
-
st.error(f"Speech recognition error: {e}")
|
232 |
-
except Exception as e:
|
233 |
-
st.error(f"Error processing audio: {e}")
|
234 |
-
return ""
|
235 |
-
|
236 |
-
def microphone_input(key):
|
237 |
-
"""Create microphone widget and return transcribed text"""
|
238 |
-
audio_bytes = st.audio_recorder("Speak your answer", key=key)
|
239 |
-
|
240 |
-
if audio_bytes:
|
241 |
-
with st.spinner("Processing audio..."):
|
242 |
-
text = transcribe_audio(audio_bytes)
|
243 |
-
if text:
|
244 |
-
return text
|
245 |
-
return None
|
246 |
-
|
247 |
# Main game logic
|
248 |
def main():
|
249 |
inject_custom_css()
|
@@ -265,7 +214,7 @@ def main():
|
|
265 |
if st.session_state.game_state == "start":
|
266 |
st.markdown("""
|
267 |
<div class="question-box">
|
268 |
-
<h3>Welcome to <span style='color:#6C63FF;'>KASOTI
|
269 |
<p>Think of something and I'll try to guess it in 20 questions or less!</p>
|
270 |
<p>Choose a category:</p>
|
271 |
<ul>
|
@@ -278,20 +227,8 @@ def main():
|
|
278 |
""", unsafe_allow_html=True)
|
279 |
|
280 |
with st.form("start_form"):
|
281 |
-
|
282 |
-
with col1:
|
283 |
-
category_input = st.text_input("Enter category (person/place/object):", key="category_input").strip().lower()
|
284 |
-
with col2:
|
285 |
-
st.write("")
|
286 |
-
st.write("")
|
287 |
-
if st.form_submit_button("🎤", key="start_mic"):
|
288 |
-
audio_text = microphone_input("start_mic")
|
289 |
-
if audio_text:
|
290 |
-
st.session_state.category_input = audio_text
|
291 |
-
st.experimental_rerun()
|
292 |
-
|
293 |
if st.form_submit_button("Start Game"):
|
294 |
-
category_input = st.session_state.get("category_input", category_input)
|
295 |
if not category_input:
|
296 |
st.error("Please enter a category!")
|
297 |
elif category_input not in ["person", "place", "object"]:
|
@@ -323,21 +260,9 @@ def main():
|
|
323 |
unsafe_allow_html=True)
|
324 |
|
325 |
with st.form("answer_form"):
|
326 |
-
|
327 |
-
|
328 |
-
answer_input = st.text_input("Your answer (yes/no/both):",
|
329 |
-
key=f"answer_{st.session_state.current_q}").strip().lower()
|
330 |
-
with col2:
|
331 |
-
st.write("")
|
332 |
-
st.write("")
|
333 |
-
if st.form_submit_button("🎤", key=f"mic_{st.session_state.current_q}"):
|
334 |
-
audio_text = microphone_input(f"mic_{st.session_state.current_q}")
|
335 |
-
if audio_text:
|
336 |
-
st.session_state[f"answer_{st.session_state.current_q}"] = audio_text
|
337 |
-
st.experimental_rerun()
|
338 |
-
|
339 |
if st.form_submit_button("Submit"):
|
340 |
-
answer_input = st.session_state.get(f"answer_{st.session_state.current_q}", answer_input)
|
341 |
if answer_input not in ["yes", "no", "both"]:
|
342 |
st.error("Please answer with 'yes', 'no', or 'both'!")
|
343 |
else:
|
@@ -371,28 +296,13 @@ def main():
|
|
371 |
|
372 |
# Side Help Option: independent chat with an AI help assistant using Hugging Face model
|
373 |
with st.expander("Need Help? Chat with AI Assistant"):
|
374 |
-
|
375 |
-
with col1:
|
376 |
-
help_query = st.text_input("Enter your help query:", key="help_query")
|
377 |
-
with col2:
|
378 |
-
st.write("")
|
379 |
-
st.write("")
|
380 |
-
if st.button("🎤", key="help_mic"):
|
381 |
-
audio_text = microphone_input("help_mic")
|
382 |
-
if audio_text:
|
383 |
-
st.session_state.help_query = audio_text
|
384 |
-
st.experimental_rerun()
|
385 |
-
|
386 |
if st.button("Send", key="send_help"):
|
387 |
-
help_query = st.session_state.get("help_query", help_query)
|
388 |
if help_query:
|
389 |
help_response = ask_help_agent(help_query)
|
390 |
st.session_state.help_conversation.append({"query": help_query, "response": help_response})
|
391 |
-
st.session_state.help_query = "" # Clear the input after sending
|
392 |
-
st.experimental_rerun()
|
393 |
else:
|
394 |
st.error("Please enter a query!")
|
395 |
-
|
396 |
if st.session_state.help_conversation:
|
397 |
for msg in st.session_state.help_conversation:
|
398 |
st.markdown(f"**You:** {msg['query']}")
|
@@ -405,26 +315,15 @@ def main():
|
|
405 |
unsafe_allow_html=True)
|
406 |
|
407 |
with st.form("confirm_form"):
|
408 |
-
|
409 |
-
with col1:
|
410 |
-
confirm_input = st.text_input("Type your answer (yes/no/both):", key="confirm_input").strip().lower()
|
411 |
-
with col2:
|
412 |
-
st.write("")
|
413 |
-
st.write("")
|
414 |
-
if st.form_submit_button("🎤", key="confirm_mic"):
|
415 |
-
audio_text = microphone_input("confirm_mic")
|
416 |
-
if audio_text:
|
417 |
-
st.session_state.confirm_input = audio_text
|
418 |
-
st.experimental_rerun()
|
419 |
-
|
420 |
if st.form_submit_button("Submit"):
|
421 |
-
confirm_input = st.session_state.get("confirm_input", confirm_input)
|
422 |
if confirm_input not in ["yes", "no", "both"]:
|
423 |
st.error("Please answer with 'yes', 'no', or 'both'!")
|
424 |
else:
|
425 |
if confirm_input == "yes":
|
426 |
st.session_state.game_state = "result"
|
427 |
st.experimental_rerun()
|
|
|
428 |
else:
|
429 |
# Add negative response to history and continue gameplay
|
430 |
st.session_state.conversation_history.append(
|
@@ -471,4 +370,4 @@ def main():
|
|
471 |
st.experimental_rerun()
|
472 |
|
473 |
if __name__ == "__main__":
|
474 |
-
main()
|
|
|
3 |
import requests
|
4 |
from streamlit.components.v1 import html
|
5 |
import os
|
|
|
|
|
|
|
|
|
6 |
|
7 |
# Import transformers and cache the help agent for performance
|
8 |
@st.cache_resource
|
|
|
91 |
border-radius: 5px;
|
92 |
margin: 10px 0;
|
93 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
94 |
</style>
|
95 |
""", unsafe_allow_html=True)
|
96 |
|
|
|
193 |
except Exception as e:
|
194 |
return f"Error in help agent: {str(e)}"
|
195 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
196 |
# Main game logic
|
197 |
def main():
|
198 |
inject_custom_css()
|
|
|
214 |
if st.session_state.game_state == "start":
|
215 |
st.markdown("""
|
216 |
<div class="question-box">
|
217 |
+
<h3>Welcome to <span style='color:#6C63FF;'>KASOTI 🎯</span></h3>
|
218 |
<p>Think of something and I'll try to guess it in 20 questions or less!</p>
|
219 |
<p>Choose a category:</p>
|
220 |
<ul>
|
|
|
227 |
""", unsafe_allow_html=True)
|
228 |
|
229 |
with st.form("start_form"):
|
230 |
+
category_input = st.text_input("Enter category (person/place/object):").strip().lower()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
231 |
if st.form_submit_button("Start Game"):
|
|
|
232 |
if not category_input:
|
233 |
st.error("Please enter a category!")
|
234 |
elif category_input not in ["person", "place", "object"]:
|
|
|
260 |
unsafe_allow_html=True)
|
261 |
|
262 |
with st.form("answer_form"):
|
263 |
+
answer_input = st.text_input("Your answer (yes/no/both):",
|
264 |
+
key=f"answer_{st.session_state.current_q}").strip().lower()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
265 |
if st.form_submit_button("Submit"):
|
|
|
266 |
if answer_input not in ["yes", "no", "both"]:
|
267 |
st.error("Please answer with 'yes', 'no', or 'both'!")
|
268 |
else:
|
|
|
296 |
|
297 |
# Side Help Option: independent chat with an AI help assistant using Hugging Face model
|
298 |
with st.expander("Need Help? Chat with AI Assistant"):
|
299 |
+
help_query = st.text_input("Enter your help query:", key="help_query")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
300 |
if st.button("Send", key="send_help"):
|
|
|
301 |
if help_query:
|
302 |
help_response = ask_help_agent(help_query)
|
303 |
st.session_state.help_conversation.append({"query": help_query, "response": help_response})
|
|
|
|
|
304 |
else:
|
305 |
st.error("Please enter a query!")
|
|
|
306 |
if st.session_state.help_conversation:
|
307 |
for msg in st.session_state.help_conversation:
|
308 |
st.markdown(f"**You:** {msg['query']}")
|
|
|
315 |
unsafe_allow_html=True)
|
316 |
|
317 |
with st.form("confirm_form"):
|
318 |
+
confirm_input = st.text_input("Type your answer (yes/no/both):", key="confirm_input").strip().lower()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
319 |
if st.form_submit_button("Submit"):
|
|
|
320 |
if confirm_input not in ["yes", "no", "both"]:
|
321 |
st.error("Please answer with 'yes', 'no', or 'both'!")
|
322 |
else:
|
323 |
if confirm_input == "yes":
|
324 |
st.session_state.game_state = "result"
|
325 |
st.experimental_rerun()
|
326 |
+
st.stop() # Immediately halt further execution
|
327 |
else:
|
328 |
# Add negative response to history and continue gameplay
|
329 |
st.session_state.conversation_history.append(
|
|
|
370 |
st.experimental_rerun()
|
371 |
|
372 |
if __name__ == "__main__":
|
373 |
+
main()
|