Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,64 +1,63 @@
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
-
from
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
client
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
)
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
label="Top-p (nucleus sampling)",
|
58 |
-
),
|
59 |
-
],
|
60 |
)
|
61 |
|
62 |
-
|
63 |
-
|
64 |
-
demo.launch()
|
|
|
1 |
+
import os
|
2 |
+
import whisper
|
3 |
+
from gtts import gTTS
|
4 |
import gradio as gr
|
5 |
+
from groq import Groq
|
6 |
+
|
7 |
+
# Load the Whisper model for speech-to-text
|
8 |
+
whisper_model = whisper.load_model("large")
|
9 |
+
|
10 |
+
# Initialize Groq client for text generation
|
11 |
+
GROQ_API_KEY = "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"
|
12 |
+
client = Groq(api_key=GROQ_API_KEY)
|
13 |
+
|
14 |
+
# Function to convert text to speech using gTTS and return the audio path
|
15 |
+
def text_to_speech(text):
|
16 |
+
tts = gTTS(text)
|
17 |
+
response_audio_path = "response.mp3"
|
18 |
+
tts.save(response_audio_path)
|
19 |
+
return response_audio_path
|
20 |
+
|
21 |
+
# Function to transcribe audio to text using Whisper
|
22 |
+
def transcribe_audio(audio):
|
23 |
+
print("Transcribing audio...")
|
24 |
+
result = whisper_model.transcribe(audio)
|
25 |
+
return result["text"]
|
26 |
+
|
27 |
+
# Function to get a response from the Groq API using LLaMA model
|
28 |
+
def get_response_from_groq(input_text):
|
29 |
+
chat_completion = client.chat.completions.create(
|
30 |
+
messages=[{"role": "user", "content": input_text}],
|
31 |
+
model="llama3-8b-8192",
|
32 |
+
)
|
33 |
+
response = chat_completion.choices[0].message.content
|
34 |
+
return response
|
35 |
+
|
36 |
+
# Gradio function to handle chatbot interaction
|
37 |
+
def chatbot(audio):
|
38 |
+
# Step 1: Transcribe audio to text using Whisper
|
39 |
+
user_input = transcribe_audio(audio)
|
40 |
+
print(f"User said: {user_input}")
|
41 |
+
|
42 |
+
# Step 2: Get chatbot response from Groq (LLaMA model)
|
43 |
+
response = get_response_from_groq(user_input)
|
44 |
+
print(f"Chatbot response: {response}")
|
45 |
+
|
46 |
+
# Step 3: Convert response text to speech and return audio
|
47 |
+
response_audio_path = text_to_speech(response)
|
48 |
+
|
49 |
+
# Return text response and audio file for playback
|
50 |
+
return response, response_audio_path
|
51 |
+
|
52 |
+
# Gradio interface for the chatbot
|
53 |
+
interface = gr.Interface(
|
54 |
+
fn=chatbot,
|
55 |
+
inputs=gr.Audio(source="microphone", type="filepath"), # Using microphone as input
|
56 |
+
outputs=["text", gr.Audio(type="filepath")], # Text and audio output
|
57 |
+
live=True,
|
58 |
+
title="Voice-Enabled Chatbot",
|
59 |
+
description="Speak into your microphone, and the chatbot will respond with both text and audio."
|
|
|
|
|
|
|
60 |
)
|
61 |
|
62 |
+
# Launch Gradio interface
|
63 |
+
interface.launch(share=True)
|
|