siddhartharyaai commited on
Commit
cc24111
·
verified ·
1 Parent(s): b4f26dc

Update qa.py

Browse files
Files changed (1) hide show
  1. qa.py +4 -6
qa.py CHANGED
@@ -18,9 +18,9 @@ def transcribe_audio_deepgram(local_audio_path: str) -> str:
18
  raise ValueError("Deepgram API key not found in environment variables.")
19
 
20
  url = "https://api.deepgram.com/v1/listen?model=nova-2&smart_format=true"
 
21
  headers = {
22
  "Authorization": f"Token {DEEPGRAM_API_KEY}",
23
- # Adjust Content-Type if user upload might be MP3: "audio/mpeg"
24
  "Content-Type": "audio/wav"
25
  }
26
 
@@ -51,11 +51,10 @@ def call_llm_for_qa(conversation_so_far: str, user_question: str) -> dict:
51
  {{ "speaker": "John", "text": "Sure, here's my answer..." }}
52
  """
53
 
54
- # We'll rely on a helper in utils.py to do the Groq call:
55
  from utils import call_groq_api_for_qa
56
 
57
  raw_json_response = call_groq_api_for_qa(system_prompt)
58
- # Expect a JSON string like: {"speaker": "John", "text": "some short answer"}
59
  response_dict = json.loads(raw_json_response)
60
  return response_dict
61
 
@@ -69,13 +68,12 @@ def handle_qa_exchange(user_question: str) -> (bytes, str):
69
  """
70
  conversation_so_far = st.session_state.get("conversation_history", "")
71
 
72
- # Ask LLM
73
  response_dict = call_llm_for_qa(conversation_so_far, user_question)
74
  answer_text = response_dict.get("text", "")
75
  speaker = response_dict.get("speaker", "John")
76
 
77
  # Update conversation
78
- # e.g., "User: question" and "John: answer"
79
  new_history = conversation_so_far + f"\nUser: {user_question}\n{speaker}: {answer_text}\n"
80
  st.session_state["conversation_history"] = new_history
81
 
@@ -83,7 +81,7 @@ def handle_qa_exchange(user_question: str) -> (bytes, str):
83
  return (None, "")
84
 
85
  # TTS
86
- audio_file_path = generate_audio_mp3(answer_text, "John") # using John voice
87
  with open(audio_file_path, "rb") as f:
88
  audio_bytes = f.read()
89
 
 
18
  raise ValueError("Deepgram API key not found in environment variables.")
19
 
20
  url = "https://api.deepgram.com/v1/listen?model=nova-2&smart_format=true"
21
+ # For WAV -> "audio/wav". If user uploads MP3, you'd use "audio/mpeg".
22
  headers = {
23
  "Authorization": f"Token {DEEPGRAM_API_KEY}",
 
24
  "Content-Type": "audio/wav"
25
  }
26
 
 
51
  {{ "speaker": "John", "text": "Sure, here's my answer..." }}
52
  """
53
 
 
54
  from utils import call_groq_api_for_qa
55
 
56
  raw_json_response = call_groq_api_for_qa(system_prompt)
57
+ # Expect a JSON string: {"speaker": "John", "text": "some short answer"}
58
  response_dict = json.loads(raw_json_response)
59
  return response_dict
60
 
 
68
  """
69
  conversation_so_far = st.session_state.get("conversation_history", "")
70
 
71
+ # Ask the LLM
72
  response_dict = call_llm_for_qa(conversation_so_far, user_question)
73
  answer_text = response_dict.get("text", "")
74
  speaker = response_dict.get("speaker", "John")
75
 
76
  # Update conversation
 
77
  new_history = conversation_so_far + f"\nUser: {user_question}\n{speaker}: {answer_text}\n"
78
  st.session_state["conversation_history"] = new_history
79
 
 
81
  return (None, "")
82
 
83
  # TTS
84
+ audio_file_path = generate_audio_mp3(answer_text, "John") # always John
85
  with open(audio_file_path, "rb") as f:
86
  audio_bytes = f.read()
87