prasanth345 commited on
Commit
9405eca
·
verified ·
1 Parent(s): 39ef4ac

Update app.py

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