Spaces:
Sleeping
Sleeping
Upload 2 files
Browse files- app.py +52 -0
- requirements.txt +5 -0
app.py
ADDED
@@ -0,0 +1,52 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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()
|
requirements.txt
ADDED
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
openai-whisper
|
2 |
+
groq
|
3 |
+
gtts
|
4 |
+
gradio
|
5 |
+
pydub
|