Update app.py
Browse files
app.py
CHANGED
@@ -10,6 +10,7 @@ from langchain_groq import ChatGroq
|
|
10 |
from langchain.memory import ConversationBufferMemory
|
11 |
from langchain.chains import ConversationalRetrievalChain
|
12 |
from PyPDF2 import PdfReader
|
|
|
13 |
from groq import Groq
|
14 |
|
15 |
# Clear ChromaDB cache to fix tenant issue
|
@@ -18,7 +19,7 @@ chromadb.api.client.SharedSystemClient.clear_system_cache()
|
|
18 |
# Ensure required environment variables are set
|
19 |
GROQ_API_KEY = os.getenv("GROQ_API_KEY")
|
20 |
if not GROQ_API_KEY:
|
21 |
-
st.error("GROQ_API_KEY is not set. Please configure it in
|
22 |
st.stop()
|
23 |
|
24 |
# Initialize Groq Client for transcription and LLM
|
@@ -52,43 +53,21 @@ def chat_chain(vectorstore):
|
|
52 |
)
|
53 |
return chain
|
54 |
|
55 |
-
#
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
recorder.start();
|
69 |
-
await sleep(time);
|
70 |
-
recorder.onstop = async () => {
|
71 |
-
blob = new Blob(chunks);
|
72 |
-
text = await b2text(blob);
|
73 |
-
resolve(text);
|
74 |
-
};
|
75 |
-
recorder.stop();
|
76 |
-
});
|
77 |
-
"""
|
78 |
-
|
79 |
-
def record_audio(seconds=5):
|
80 |
-
"""Record audio via JavaScript and save it as a .wav file."""
|
81 |
-
st.write("Recording audio...")
|
82 |
-
from streamlit.components.v1 import html
|
83 |
-
audio_b64 = st.experimental_js("record", seconds * 1000)
|
84 |
-
audio_bytes = b64decode(audio_b64.split(",")[1])
|
85 |
-
audio_file_path = "recorded_audio.wav"
|
86 |
-
with open(audio_file_path, "wb") as f:
|
87 |
-
f.write(audio_bytes)
|
88 |
-
return audio_file_path
|
89 |
-
|
90 |
def transcribe_audio(file_path):
|
91 |
-
"""Transcribe audio using Groq Whisper."""
|
92 |
with open(file_path, "rb") as file:
|
93 |
transcription = groq_client.audio.transcriptions.create(
|
94 |
file=(file_path, file.read()),
|
@@ -120,16 +99,13 @@ if uploaded_files:
|
|
120 |
|
121 |
# Audio Input Mode
|
122 |
elif input_method == "Audio Input":
|
123 |
-
|
124 |
-
|
125 |
-
st.audio(audio_file)
|
126 |
-
|
127 |
-
# Transcription
|
128 |
st.write("Transcribing audio...")
|
129 |
transcription = transcribe_audio(audio_file)
|
130 |
st.write(f"**You said:** {transcription}")
|
131 |
|
132 |
-
# Generate Response
|
133 |
with st.spinner("Generating response..."):
|
134 |
response = chain({"question": transcription})["answer"]
|
135 |
st.write(f"**Response:** {response}")
|
|
|
10 |
from langchain.memory import ConversationBufferMemory
|
11 |
from langchain.chains import ConversationalRetrievalChain
|
12 |
from PyPDF2 import PdfReader
|
13 |
+
from streamlit_audio_recorder import st_audio_recorder
|
14 |
from groq import Groq
|
15 |
|
16 |
# Clear ChromaDB cache to fix tenant issue
|
|
|
19 |
# Ensure required environment variables are set
|
20 |
GROQ_API_KEY = os.getenv("GROQ_API_KEY")
|
21 |
if not GROQ_API_KEY:
|
22 |
+
st.error("GROQ_API_KEY is not set. Please configure it in your environment variables.")
|
23 |
st.stop()
|
24 |
|
25 |
# Initialize Groq Client for transcription and LLM
|
|
|
53 |
)
|
54 |
return chain
|
55 |
|
56 |
+
# Function to record audio using streamlit_audio_recorder
|
57 |
+
def record_audio():
|
58 |
+
st.write("Click below to record your audio:")
|
59 |
+
audio_bytes = st_audio_recorder()
|
60 |
+
if audio_bytes:
|
61 |
+
audio_file_path = "recorded_audio.wav"
|
62 |
+
with open(audio_file_path, "wb") as f:
|
63 |
+
f.write(audio_bytes)
|
64 |
+
st.success("Audio recorded successfully!")
|
65 |
+
return audio_file_path
|
66 |
+
return None
|
67 |
+
|
68 |
+
# Transcribe audio using Groq Whisper
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
69 |
def transcribe_audio(file_path):
|
70 |
+
"""Transcribe audio using Groq's Whisper model."""
|
71 |
with open(file_path, "rb") as file:
|
72 |
transcription = groq_client.audio.transcriptions.create(
|
73 |
file=(file_path, file.read()),
|
|
|
99 |
|
100 |
# Audio Input Mode
|
101 |
elif input_method == "Audio Input":
|
102 |
+
audio_file = record_audio()
|
103 |
+
if audio_file:
|
104 |
+
st.audio(audio_file, format="audio/wav")
|
|
|
|
|
105 |
st.write("Transcribing audio...")
|
106 |
transcription = transcribe_audio(audio_file)
|
107 |
st.write(f"**You said:** {transcription}")
|
108 |
|
|
|
109 |
with st.spinner("Generating response..."):
|
110 |
response = chain({"question": transcription})["answer"]
|
111 |
st.write(f"**Response:** {response}")
|