DrishtiSharma commited on
Commit
abce794
Β·
verified Β·
1 Parent(s): af77c7a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -43
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 Hugging Face Spaces secrets.")
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
- # JavaScript for recording audio
56
- RECORD_JS = """
57
- const sleep = time => new Promise(resolve => setTimeout(resolve, time));
58
- const b2text = blob => new Promise(resolve => {
59
- const reader = new FileReader();
60
- reader.onloadend = e => resolve(e.srcElement.result);
61
- reader.readAsDataURL(blob);
62
- });
63
- var record = time => new Promise(async resolve => {
64
- stream = await navigator.mediaDevices.getUserMedia({ audio: true });
65
- recorder = new MediaRecorder(stream);
66
- chunks = [];
67
- recorder.ondataavailable = e => chunks.push(e.data);
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
- if st.button("Record Audio"):
124
- audio_file = record_audio(5)
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}")