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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -22
app.py CHANGED
@@ -3,14 +3,12 @@
3
  import os
4
  import chromadb
5
  import streamlit as st
6
- from base64 import b64decode
7
  from langchain_huggingface import HuggingFaceEmbeddings
8
  from langchain_chroma import Chroma
9
  from langchain_groq import ChatGroq
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,7 +17,7 @@ chromadb.api.client.SharedSystemClient.clear_system_cache()
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,18 +51,6 @@ def chat_chain(vectorstore):
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."""
@@ -87,7 +73,7 @@ if uploaded_files:
87
  chain = chat_chain(vectorstore)
88
  st.success("PDFs processed! Ready to chat.")
89
 
90
- input_method = st.radio("Choose Input Method", ["Text Input", "Audio Input"])
91
 
92
  # Text Input Mode
93
  if input_method == "Text Input":
@@ -97,13 +83,17 @@ if uploaded_files:
97
  response = chain({"question": query})["answer"]
98
  st.write(f"**Response:** {response}")
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..."):
 
3
  import os
4
  import chromadb
5
  import streamlit as st
 
6
  from langchain_huggingface import HuggingFaceEmbeddings
7
  from langchain_chroma import Chroma
8
  from langchain_groq import ChatGroq
9
  from langchain.memory import ConversationBufferMemory
10
  from langchain.chains import ConversationalRetrievalChain
11
  from PyPDF2 import PdfReader
 
12
  from groq import Groq
13
 
14
  # Clear ChromaDB cache to fix tenant issue
 
17
  # Ensure required environment variables are set
18
  GROQ_API_KEY = os.getenv("GROQ_API_KEY")
19
  if not GROQ_API_KEY:
20
+ st.error("GROQ_API_KEY is not set. Please configure it in environment variables.")
21
  st.stop()
22
 
23
  # Initialize Groq Client for transcription and LLM
 
51
  )
52
  return chain
53
 
 
 
 
 
 
 
 
 
 
 
 
 
54
  # Transcribe audio using Groq Whisper
55
  def transcribe_audio(file_path):
56
  """Transcribe audio using Groq's Whisper model."""
 
73
  chain = chat_chain(vectorstore)
74
  st.success("PDFs processed! Ready to chat.")
75
 
76
+ input_method = st.radio("Choose Input Method", ["Text Input", "Audio File Upload"])
77
 
78
  # Text Input Mode
79
  if input_method == "Text Input":
 
83
  response = chain({"question": query})["answer"]
84
  st.write(f"**Response:** {response}")
85
 
86
+ # Audio Input Mode (File Upload)
87
+ elif input_method == "Audio File Upload":
88
+ uploaded_audio = st.file_uploader("Upload an audio file (.wav, .mp3)", type=["wav", "mp3"])
89
+ if uploaded_audio:
90
+ audio_file_path = "uploaded_audio.wav"
91
+ with open(audio_file_path, "wb") as f:
92
+ f.write(uploaded_audio.read())
93
+
94
+ st.audio(audio_file_path, format="audio/wav")
95
  st.write("Transcribing audio...")
96
+ transcription = transcribe_audio(audio_file_path)
97
  st.write(f"**You said:** {transcription}")
98
 
99
  with st.spinner("Generating response..."):