Spaces:
Running
Running
Update qa.py
Browse files
qa.py
CHANGED
@@ -2,11 +2,11 @@
|
|
2 |
|
3 |
import os
|
4 |
import requests
|
5 |
-
import tempfile
|
6 |
import json
|
|
|
7 |
import streamlit as st
|
8 |
|
9 |
-
from utils import generate_audio_mp3 #
|
10 |
|
11 |
def transcribe_audio_deepgram(local_audio_path: str) -> str:
|
12 |
"""
|
@@ -18,11 +18,12 @@ 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 |
-
# For WAV, set Content-Type to audio/wav. For MP3, use audio/mpeg, etc.
|
22 |
headers = {
|
23 |
"Authorization": f"Token {DEEPGRAM_API_KEY}",
|
24 |
-
|
|
|
25 |
}
|
|
|
26 |
with open(local_audio_path, "rb") as f:
|
27 |
response = requests.post(url, headers=headers, data=f)
|
28 |
response.raise_for_status()
|
@@ -32,14 +33,14 @@ def transcribe_audio_deepgram(local_audio_path: str) -> str:
|
|
32 |
transcript = data["results"]["channels"][0]["alternatives"][0].get("transcript", "")
|
33 |
return transcript
|
34 |
|
|
|
35 |
def call_llm_for_qa(conversation_so_far: str, user_question: str) -> dict:
|
36 |
"""
|
37 |
Minimal function that calls your LLM (Groq) to answer a follow-up question.
|
38 |
Returns a Python dict, e.g.: {"speaker": "John", "text": "..."}
|
39 |
"""
|
40 |
-
# Example system prompt:
|
41 |
system_prompt = f"""
|
42 |
-
You are John, the guest
|
43 |
Conversation so far:
|
44 |
{conversation_so_far}
|
45 |
|
@@ -50,19 +51,15 @@ def call_llm_for_qa(conversation_so_far: str, user_question: str) -> dict:
|
|
50 |
{{ "speaker": "John", "text": "Sure, here's my answer..." }}
|
51 |
"""
|
52 |
|
53 |
-
#
|
54 |
-
|
55 |
-
# This should be replaced by your real LLM call, e.g., call_groq_api(system_prompt)
|
56 |
-
# and parse out the JSON response.
|
57 |
-
|
58 |
-
# Example pseudo-code:
|
59 |
-
from utils import call_groq_api_for_qa # Suppose you define this in utils
|
60 |
|
61 |
raw_json_response = call_groq_api_for_qa(system_prompt)
|
62 |
-
# Expect
|
63 |
response_dict = json.loads(raw_json_response)
|
64 |
return response_dict
|
65 |
|
|
|
66 |
def handle_qa_exchange(user_question: str) -> (bytes, str):
|
67 |
"""
|
68 |
1) Read conversation_so_far from session_state
|
@@ -78,7 +75,7 @@ def handle_qa_exchange(user_question: str) -> (bytes, str):
|
|
78 |
speaker = response_dict.get("speaker", "John")
|
79 |
|
80 |
# Update conversation
|
81 |
-
#
|
82 |
new_history = conversation_so_far + f"\nUser: {user_question}\n{speaker}: {answer_text}\n"
|
83 |
st.session_state["conversation_history"] = new_history
|
84 |
|
|
|
2 |
|
3 |
import os
|
4 |
import requests
|
|
|
5 |
import json
|
6 |
+
import tempfile
|
7 |
import streamlit as st
|
8 |
|
9 |
+
from utils import generate_audio_mp3 # Reuse your existing TTS function
|
10 |
|
11 |
def transcribe_audio_deepgram(local_audio_path: str) -> str:
|
12 |
"""
|
|
|
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 |
+
|
27 |
with open(local_audio_path, "rb") as f:
|
28 |
response = requests.post(url, headers=headers, data=f)
|
29 |
response.raise_for_status()
|
|
|
33 |
transcript = data["results"]["channels"][0]["alternatives"][0].get("transcript", "")
|
34 |
return transcript
|
35 |
|
36 |
+
|
37 |
def call_llm_for_qa(conversation_so_far: str, user_question: str) -> dict:
|
38 |
"""
|
39 |
Minimal function that calls your LLM (Groq) to answer a follow-up question.
|
40 |
Returns a Python dict, e.g.: {"speaker": "John", "text": "..."}
|
41 |
"""
|
|
|
42 |
system_prompt = f"""
|
43 |
+
You are John, the guest speaker. The user is asking a follow-up question.
|
44 |
Conversation so far:
|
45 |
{conversation_so_far}
|
46 |
|
|
|
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 |
|
62 |
+
|
63 |
def handle_qa_exchange(user_question: str) -> (bytes, str):
|
64 |
"""
|
65 |
1) Read conversation_so_far from session_state
|
|
|
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 |
|