Qudrat0708 commited on
Commit
3169cc7
·
verified ·
1 Parent(s): 0912de7

Create app.py

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