aasherkamal216 commited on
Commit
1cedabf
·
unverified ·
1 Parent(s): ddfedb9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +48 -51
app.py CHANGED
@@ -7,7 +7,8 @@ import base64
7
  import docx
8
  from streamlit_lottie import st_lottie
9
  import json
10
- from utils import set_safety_settings, about, speech_to_text
 
11
  import google.generativeai as genai
12
  import os, random, validators
13
  import time
@@ -331,11 +332,7 @@ def is_valid_content(content):
331
  content["type"] in ["pdf_file", "docx_file"]
332
  )
333
 
334
- ###---SPEECH HANDLING---###
335
- def handle_speech_input(audio_bytes):
336
- with st.spinner("Transcribing your speech..."):
337
- return speech_to_text(audio_bytes)
338
-
339
  ###---CHAT HISTORY UPDATE---###
340
  def update_chat_history(role, content, history):
341
  history.append({"role": role, "content": content})
@@ -353,14 +350,14 @@ def handle_groq_response(model_params, api_key, question, chat_history, llm_type
353
  return response
354
 
355
  ###---- MAIN FUNCTION FOR ALL MODELS CONVERSATION HANDLING---###
356
- def process_user_input(message_container):
357
  prompt = st.chat_input("Type your question", key="question") or speech_file_added
358
 
359
  if not prompt:
360
  return
361
 
362
  if model_type == "groq":
363
- question = handle_speech_input(audio_bytes) if speech_file_added else prompt
364
 
365
  if question is None:
366
  message_container.error("Couldn't recognize your speech.", icon="❌")
@@ -491,25 +488,53 @@ else:
491
  options=["Wikipedia", "ArXiv", "DuckDuckGo Search"])
492
 
493
 
 
 
 
 
 
 
494
  ######----- Main Interface -----#######
495
  chat_col1, chat_col2 = st.columns([1,4])
496
 
497
  with chat_col1:
498
  ###--- Audio Recording ---###
499
- audio_bytes = audio_recorder("Speak",
500
- pause_threshold=3,
501
- neutral_color="#f5f8fc",
502
- recording_color="#f81f6f",
503
- icon_name="microphone-lines",
504
- icon_size="3x")
505
-
506
- ###--- Reset Conversation ---###
507
- st.button(
508
- "🗑 Reset",
509
- use_container_width=True,
510
- on_click=reset_conversation,
511
- help="If clicked, conversation will be reset.",
512
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
513
  ###--- Session state variables ---###
514
  if "pdf_docx_uploaded" not in st.session_state:
515
  st.session_state.pdf_docx_uploaded = None
@@ -520,34 +545,6 @@ else:
520
  st.info(f"Your file :green['{file_name}'] has been uploaded!")
521
 
522
 
523
- session_keys = ["messages", "uploaded_files", "groq_chat_history"]
524
- for key in session_keys:
525
- if key not in st.session_state:
526
- st.session_state[key] = []
527
-
528
- ###-- Handle speech input --###
529
- speech_file_added = False
530
- if "prev_speech_hash" not in st.session_state:
531
- st.session_state.prev_speech_hash = None
532
-
533
- if audio_bytes and st.session_state.prev_speech_hash != hash(audio_bytes):
534
- st.session_state.prev_speech_hash = hash(audio_bytes)
535
- if model_type == "google":
536
- speech_base64 = base64.b64encode(audio_bytes).decode()
537
- unique_id = random.randint(1000, 9999)
538
-
539
- st.session_state.messages.append(
540
- {
541
- "role": "user",
542
- "content": [{
543
- "type": "speech_input",
544
- "speech_input": f"data:audio/wav;base64,{speech_base64}",
545
- "unique_name": f"temp_{unique_id}"
546
- }]
547
- }
548
- )
549
- speech_file_added = True
550
-
551
  ####---DISPLAY CONVERSATION---###
552
  with chat_col2:
553
  message_container = st.container(height=400, border=False)
@@ -587,4 +584,4 @@ else:
587
 
588
  ###----- User Question -----###
589
  else:
590
- process_user_input(message_container)
 
7
  import docx
8
  from streamlit_lottie import st_lottie
9
  import json
10
+ from utils import set_safety_settings, about
11
+ from streamlit_mic_recorder import speech_to_text
12
  import google.generativeai as genai
13
  import os, random, validators
14
  import time
 
332
  content["type"] in ["pdf_file", "docx_file"]
333
  )
334
 
335
+
 
 
 
 
336
  ###---CHAT HISTORY UPDATE---###
337
  def update_chat_history(role, content, history):
338
  history.append({"role": role, "content": content})
 
350
  return response
351
 
352
  ###---- MAIN FUNCTION FOR ALL MODELS CONVERSATION HANDLING---###
353
+ def process_user_input(message_container, trasncribed_text):
354
  prompt = st.chat_input("Type your question", key="question") or speech_file_added
355
 
356
  if not prompt:
357
  return
358
 
359
  if model_type == "groq":
360
+ question = trasncribed_text if speech_file_added else prompt
361
 
362
  if question is None:
363
  message_container.error("Couldn't recognize your speech.", icon="❌")
 
488
  options=["Wikipedia", "ArXiv", "DuckDuckGo Search"])
489
 
490
 
491
+ ###--- Session state variables ---###
492
+ session_keys = ["messages", "uploaded_files", "groq_chat_history"]
493
+ for key in session_keys:
494
+ if key not in st.session_state:
495
+ st.session_state[key] = []
496
+
497
  ######----- Main Interface -----#######
498
  chat_col1, chat_col2 = st.columns([1,4])
499
 
500
  with chat_col1:
501
  ###--- Audio Recording ---###
502
+ speech_file_added = False
503
+ if model_type == "google":
504
+ audio_bytes = audio_recorder("Speak",
505
+ pause_threshold=3,
506
+ neutral_color="#f5f8fc",
507
+ recording_color="#f81f6f",
508
+ icon_name="microphone-lines",
509
+ icon_size="3x")
510
+
511
+ if "prev_speech_hash" not in st.session_state:
512
+ st.session_state.prev_speech_hash = None
513
+
514
+ if audio_bytes and st.session_state.prev_speech_hash != hash(audio_bytes):
515
+ st.session_state.prev_speech_hash = hash(audio_bytes)
516
+
517
+ speech_base64 = base64.b64encode(audio_bytes).decode()
518
+ unique_id = random.randint(1000, 9999)
519
+
520
+ st.session_state.messages.append(
521
+ {
522
+ "role": "user",
523
+ "content": [{
524
+ "type": "speech_input",
525
+ "speech_input": f"data:audio/wav;base64,{speech_base64}",
526
+ "unique_name": f"temp_{unique_id}"
527
+ }]
528
+ }
529
+ )
530
+ speech_file_added = True
531
+
532
+ else:
533
+
534
+ trasncribed_text = speech_to_text(language="en", just_once=True, key="STT", use_container_width=True)
535
+
536
+ if trasncribed_text: speech_file_added = True
537
+
538
  ###--- Session state variables ---###
539
  if "pdf_docx_uploaded" not in st.session_state:
540
  st.session_state.pdf_docx_uploaded = None
 
545
  st.info(f"Your file :green['{file_name}'] has been uploaded!")
546
 
547
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
548
  ####---DISPLAY CONVERSATION---###
549
  with chat_col2:
550
  message_container = st.container(height=400, border=False)
 
584
 
585
  ###----- User Question -----###
586
  else:
587
+ process_user_input(message_container, trasncribed_text)