Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,157 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Required Libraries
|
2 |
+
import whisper
|
3 |
+
from groq import Groq
|
4 |
+
from gtts import gTTS
|
5 |
+
import gradio as gr
|
6 |
+
import os
|
7 |
+
import tempfile
|
8 |
+
from pydub import AudioSegment
|
9 |
+
|
10 |
+
# ---------------------------
|
11 |
+
# π API Key Configuration
|
12 |
+
# ---------------------------
|
13 |
+
GROQ_API_KEY= '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'
|
14 |
+
|
15 |
+
client=Groq(api_key=GROQ_API_KEY)
|
16 |
+
|
17 |
+
# Initialize Whisper model
|
18 |
+
whisper_model = whisper.load_model("base")
|
19 |
+
|
20 |
+
# ---------------------------
|
21 |
+
# ποΈ Audio Processing
|
22 |
+
# ---------------------------
|
23 |
+
|
24 |
+
def validate_audio_file(audio_file):
|
25 |
+
"""Validate if the audio file exists and is not empty."""
|
26 |
+
if not audio_file or not os.path.exists(audio_file) or os.path.getsize(audio_file) == 0:
|
27 |
+
print(f"[ERROR] Invalid or empty audio file: {audio_file}")
|
28 |
+
return False
|
29 |
+
return True
|
30 |
+
|
31 |
+
|
32 |
+
def convert_to_wav(audio_file):
|
33 |
+
"""Convert audio file to WAV format if needed."""
|
34 |
+
try:
|
35 |
+
audio = AudioSegment.from_file(audio_file)
|
36 |
+
wav_path = tempfile.NamedTemporaryFile(suffix=".wav", delete=False).name
|
37 |
+
audio.export(wav_path, format="wav")
|
38 |
+
print(f"[INFO] Audio converted to WAV: {wav_path}")
|
39 |
+
return wav_path
|
40 |
+
except Exception as e:
|
41 |
+
print(f"[ERROR] Audio Conversion Error: {e}")
|
42 |
+
return None
|
43 |
+
|
44 |
+
|
45 |
+
def transcribe_audio(audio_file):
|
46 |
+
"""Transcribe audio using Whisper."""
|
47 |
+
try:
|
48 |
+
print(f"[INFO] Transcribing audio file: {audio_file}")
|
49 |
+
if not validate_audio_file(audio_file):
|
50 |
+
raise FileNotFoundError("Audio file not found or invalid path.")
|
51 |
+
|
52 |
+
audio_file = convert_to_wav(audio_file)
|
53 |
+
if not audio_file:
|
54 |
+
raise Exception("Failed to convert audio to WAV format.")
|
55 |
+
|
56 |
+
result = whisper_model.transcribe(audio_file)
|
57 |
+
print(f"[INFO] Transcription result: {result['text']}")
|
58 |
+
return result['text']
|
59 |
+
except Exception as e:
|
60 |
+
print(f"[ERROR] Transcription Error: {e}")
|
61 |
+
return f"Transcription Error: {e}"
|
62 |
+
|
63 |
+
|
64 |
+
# ---------------------------
|
65 |
+
# π€ LLM Interaction
|
66 |
+
# ---------------------------
|
67 |
+
|
68 |
+
def get_groq_response(user_input):
|
69 |
+
"""Get chatbot response from Groq's API."""
|
70 |
+
try:
|
71 |
+
print(f"[INFO] Sending input to Groq: {user_input}")
|
72 |
+
chat_completion = client.chat.completions.create(
|
73 |
+
messages=[
|
74 |
+
{"role": "user", "content": user_input}
|
75 |
+
],
|
76 |
+
model="llama-3.3-70b-versatile",
|
77 |
+
stream=False,
|
78 |
+
)
|
79 |
+
response = chat_completion.choices[0].message.content
|
80 |
+
print(f"[INFO] Groq response: {response}")
|
81 |
+
return response
|
82 |
+
except Exception as e:
|
83 |
+
print(f"[ERROR] Groq API Error: {e}")
|
84 |
+
return f"Groq API Error: {e}"
|
85 |
+
|
86 |
+
|
87 |
+
# ---------------------------
|
88 |
+
# π£οΈ Text-to-Speech
|
89 |
+
# ---------------------------
|
90 |
+
|
91 |
+
def text_to_speech(text):
|
92 |
+
"""Convert text to speech using gTTS."""
|
93 |
+
try:
|
94 |
+
print(f"[INFO] Converting text to speech: {text}")
|
95 |
+
tts = gTTS(text)
|
96 |
+
audio_path = tempfile.NamedTemporaryFile(suffix=".mp3", delete=False).name
|
97 |
+
tts.save(audio_path)
|
98 |
+
print(f"[INFO] Audio file saved: {audio_path}")
|
99 |
+
return audio_path
|
100 |
+
except Exception as e:
|
101 |
+
print(f"[ERROR] TTS Error: {e}")
|
102 |
+
return f"TTS Error: {e}"
|
103 |
+
|
104 |
+
|
105 |
+
# ---------------------------
|
106 |
+
# π οΈ Main Interaction Logic
|
107 |
+
# ---------------------------
|
108 |
+
|
109 |
+
def chatbot(audio_input):
|
110 |
+
"""Handle full chatbot interaction."""
|
111 |
+
try:
|
112 |
+
print(f"[INFO] Audio Input Path: {audio_input}")
|
113 |
+
|
114 |
+
# Validate Audio File
|
115 |
+
if not validate_audio_file(audio_input):
|
116 |
+
return "Error: Audio file not found or invalid path", None
|
117 |
+
|
118 |
+
# Step 1: Transcribe Audio
|
119 |
+
text_input = transcribe_audio(audio_input)
|
120 |
+
if "Error" in text_input:
|
121 |
+
return text_input, None
|
122 |
+
|
123 |
+
# Step 2: Get Response from Groq
|
124 |
+
llm_response = get_groq_response(text_input)
|
125 |
+
if "Error" in llm_response:
|
126 |
+
return llm_response, None
|
127 |
+
|
128 |
+
# Step 3: Convert Response to Audio
|
129 |
+
audio_output = text_to_speech(llm_response)
|
130 |
+
if "Error" in audio_output:
|
131 |
+
return audio_output, None
|
132 |
+
|
133 |
+
return llm_response, audio_output
|
134 |
+
except Exception as e:
|
135 |
+
print(f"[ERROR] General Error: {e}")
|
136 |
+
return f"General Error: {e}", None
|
137 |
+
|
138 |
+
|
139 |
+
# ---------------------------
|
140 |
+
# π Gradio Interface
|
141 |
+
# ---------------------------
|
142 |
+
|
143 |
+
interface = gr.Interface(
|
144 |
+
fn=chatbot,
|
145 |
+
inputs=gr.Audio(type="filepath"),
|
146 |
+
outputs=[
|
147 |
+
gr.Textbox(label="LLM Response"),
|
148 |
+
gr.Audio(label="Audio Response")
|
149 |
+
],
|
150 |
+
title="Real-Time Voice-to-Voice Chatbot",
|
151 |
+
description="Speak into the microphone, and the chatbot will respond with audio.",
|
152 |
+
live=True # Ensures real-time interaction
|
153 |
+
)
|
154 |
+
|
155 |
+
# Launch Gradio App
|
156 |
+
if __name__ == "__main__":
|
157 |
+
interface.launch(share=True)
|