Muhammad-Faizan-Ahmed commited on
Commit
6df8ec2
·
verified ·
1 Parent(s): 86f1078

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +54 -0
app.py ADDED
@@ -0,0 +1,54 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Import libraries
2
+ import whisper
3
+ import os
4
+ from gtts import gTTS
5
+ import gradio as gr
6
+ from groq import Groq
7
+
8
+ # Load Whisper model for transcription
9
+ model = whisper.load_model("base")
10
+
11
+ GROQ_API_KEY = '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'
12
+ client = Groq(api_key=GROQ_API_KEY)
13
+
14
+ # Set up Groq API client (ensure GROQ_API_KEY is set in your environment)
15
+ client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
16
+
17
+ # Function to get the LLM response from Groq
18
+ def get_llm_response(user_input):
19
+ chat_completion = client.chat.completions.create(
20
+ messages=[{"role": "user", "content": user_input}],
21
+ model="llama3-8b-8192", # Replace with your desired model
22
+ )
23
+ return chat_completion.choices[0].message.content
24
+
25
+ # Function to convert text to speech using gTTS
26
+ def text_to_speech(text, output_audio="output_audio.mp3"):
27
+ tts = gTTS(text)
28
+ tts.save(output_audio)
29
+ return output_audio
30
+
31
+ # Main chatbot function to handle audio input and output
32
+ def chatbot(audio):
33
+ # Step 1: Transcribe the audio using Whisper
34
+ result = model.transcribe(audio)
35
+ user_text = result["text"]
36
+
37
+ # Step 2: Get LLM response from Groq
38
+ response_text = get_llm_response(user_text)
39
+
40
+ # Step 3: Convert the response text to speech
41
+ output_audio = text_to_speech(response_text)
42
+
43
+ return response_text, output_audio
44
+
45
+ # Gradio interface for real-time interaction
46
+ iface = gr.Interface(
47
+ fn=chatbot,
48
+ inputs=gr.Audio(type="filepath"), # Input from mic or file
49
+ outputs=[gr.Textbox(), gr.Audio(type="filepath")], # Output: response text and audio
50
+ live=True
51
+ )
52
+
53
+ # Launch the Gradio app
54
+ iface.launch()