ammadkhan5544 commited on
Commit
9f998db
·
verified ·
1 Parent(s): a84de7f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -79
app.py CHANGED
@@ -1,91 +1,39 @@
1
  import os
2
- import streamlit as st
3
  import whisper
4
  from gtts import gTTS
5
- from dotenv import load_dotenv
6
  import tempfile
7
- from streamlit_webrtc import webrtc_streamer, WebRtcMode, AudioProcessorBase, ClientSettings
8
- import numpy as np
9
- import wave
10
 
11
- # Load environment variables
12
- load_dotenv()
13
-
14
- # Load Whisper model
15
- st.write("Loading Whisper model...")
16
  whisper_model = whisper.load_model("base")
17
 
18
- # API Key for LLM
19
- api_key = os.getenv("GROQ_API_KEY")
20
- if not api_key:
21
- raise ValueError("API key is missing. Set GROQ_API_KEY in your .env file.")
22
 
23
  # Function to transcribe audio to text
24
  def transcribe_audio(audio_file):
25
- result = whisper_model.transcribe(audio_file)
26
- return result['text']
 
 
 
27
 
28
- # Simulated response from LLM (Replace with actual API call if available)
29
  def get_llm_response(user_input):
30
- # Replace with your actual LLM call logic here
31
- return f"Response to: {user_input}"
32
-
33
- # Convert text to speech
34
- def text_to_speech(text):
35
- tts = gTTS(text)
36
- temp_file = tempfile.NamedTemporaryFile(suffix=".mp3", delete=False)
37
- tts.save(temp_file.name)
38
- return temp_file.name
39
-
40
- # AudioProcessor class to capture audio from the microphone
41
- class AudioProcessor(AudioProcessorBase):
42
- def __init__(self):
43
- self.frames = []
44
-
45
- def recv(self, frame):
46
- self.frames.append(frame.to_ndarray().flatten())
47
- return frame
48
-
49
- def save_audio(self, path):
50
- with wave.open(path, "wb") as wf:
51
- wf.setnchannels(1) # Mono
52
- wf.setsampwidth(2) # 16-bit samples
53
- wf.setframerate(16000) # 16kHz
54
- wf.writeframes(np.concatenate(self.frames).tobytes())
55
-
56
- # Streamlit UI
57
- st.title("Real-Time Voice Chatbot")
58
- st.write("Interact with the chatbot using your voice.")
59
-
60
- webrtc_ctx = webrtc_streamer(
61
- key="example",
62
- mode=WebRtcMode.SENDONLY,
63
- audio_processor_factory=AudioProcessor,
64
- client_settings=ClientSettings(
65
- rtc_configuration={"iceServers": [{"urls": ["stun:stun.l.google.com:19302"]}]},
66
- media_stream_constraints={"audio": True, "video": False},
67
- ),
68
- )
69
-
70
- if webrtc_ctx.audio_processor:
71
- audio_processor = webrtc_ctx.audio_processor
72
-
73
- if st.button("Process Microphone Input"):
74
- if audio_processor:
75
- audio_path = tempfile.NamedTemporaryFile(suffix=".wav", delete=False).name
76
- audio_processor.save_audio(audio_path)
77
-
78
- # Step 1: Transcribe
79
- with st.spinner("Transcribing audio..."):
80
- user_input = transcribe_audio(audio_path)
81
- st.write(f"**You said:** {user_input}")
82
-
83
- # Step 2: Get LLM response
84
- with st.spinner("Generating response..."):
85
- response = get_llm_response(user_input)
86
- st.write(f"**Chatbot Response:** {response}")
87
-
88
- # Step 3: Text to speech
89
- with st.spinner("Converting response to audio..."):
90
- response_audio = text_to_speech(response)
91
- st.audio(response_audio, format="audio/mp3")
 
1
  import os
2
+ import gradio as gr
3
  import whisper
4
  from gtts import gTTS
5
+ from groq import Groq
6
  import tempfile
 
 
 
7
 
8
+ # Load Whisper model for speech-to-text
9
+ print("Loading Whisper model...")
 
 
 
10
  whisper_model = whisper.load_model("base")
11
 
12
+ # Initialize Groq API
13
+ print("Initializing Groq API...")
14
+ client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
 
15
 
16
  # Function to transcribe audio to text
17
  def transcribe_audio(audio_file):
18
+ try:
19
+ result = whisper_model.transcribe(audio_file)
20
+ return result["text"]
21
+ except Exception as e:
22
+ return f"Error in transcription: {e}"
23
 
24
+ # Function to get response from LLM using Groq API
25
  def get_llm_response(user_input):
26
+ try:
27
+ chat_completion = client.chat.completions.create(
28
+ messages=[
29
+ {"role": "user", "content": user_input}
30
+ ],
31
+ model="llama3-8b-8192",
32
+ stream=False,
33
+ )
34
+ return chat_completion.choices[0].message.content
35
+ except Exception as e:
36
+ return f"Error in LLM interaction: {e}"
37
+
38
+ # Function to convert text to speech
39
+ def text_to_sp