abdullahzunorain
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -1,161 +1,3 @@
|
|
1 |
-
import whisper
|
2 |
-
import os
|
3 |
-
from gtts import gTTS
|
4 |
-
import gradio as gr
|
5 |
-
from groq import Groq
|
6 |
-
from datetime import datetime
|
7 |
-
import tempfile
|
8 |
-
|
9 |
-
# Load a smaller Whisper model for faster processing
|
10 |
-
try:
|
11 |
-
model = whisper.load_model("tiny")
|
12 |
-
except Exception as e:
|
13 |
-
print(f"Error loading Whisper model: {e}")
|
14 |
-
model = None
|
15 |
-
|
16 |
-
# Set up Groq API client using environment variable
|
17 |
-
GROQ_API_TOKEN = os.getenv("GROQ_API")
|
18 |
-
if not GROQ_API_TOKEN:
|
19 |
-
raise ValueError("Groq API token is missing. Set 'GROQ_API' in your environment variables.")
|
20 |
-
client = Groq(api_key=GROQ_API_TOKEN)
|
21 |
-
|
22 |
-
# Initialize the chat history
|
23 |
-
chat_history = []
|
24 |
-
|
25 |
-
# Function to get the LLM response from Groq with timeout handling
|
26 |
-
def get_llm_response(user_input, role="detailed responder"):
|
27 |
-
prompt = f"As an expert, provide a detailed and knowledgeable response: {user_input}" if role == "expert" else \
|
28 |
-
f"As a good assistant, provide a clear, concise, and helpful response: {user_input}" if role == "good assistant" else \
|
29 |
-
f"Provide a thorough and detailed response: {user_input}"
|
30 |
-
|
31 |
-
try:
|
32 |
-
chat_completion = client.chat.completions.create(
|
33 |
-
messages=[{"role": "user", "content": user_input}],
|
34 |
-
model="llama3-8b-8192", # Replace with your desired model
|
35 |
-
timeout=20 # Increased timeout to 20 seconds
|
36 |
-
)
|
37 |
-
return chat_completion.choices[0].message.content
|
38 |
-
except Exception as e:
|
39 |
-
print(f"Error during LLM response retrieval: {e}")
|
40 |
-
return "Sorry, there was an error retrieving the response. Please try again."
|
41 |
-
|
42 |
-
# Function to convert text to speech using gTTS and handle temporary files
|
43 |
-
def text_to_speech(text):
|
44 |
-
try:
|
45 |
-
tts = gTTS(text)
|
46 |
-
with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as temp_file:
|
47 |
-
output_audio = temp_file.name
|
48 |
-
tts.save(output_audio)
|
49 |
-
return output_audio
|
50 |
-
except Exception as e:
|
51 |
-
print(f"Error generating TTS: {e}")
|
52 |
-
return None
|
53 |
-
|
54 |
-
# Main chatbot function to handle audio input and output with chat history
|
55 |
-
def chatbot(audio):
|
56 |
-
if not model:
|
57 |
-
return "Error: Whisper model is not available.", None, chat_history
|
58 |
-
|
59 |
-
if not audio:
|
60 |
-
return "No audio provided. Please upload a valid audio file.", None, chat_history
|
61 |
-
|
62 |
-
try:
|
63 |
-
# Step 1: Transcribe the audio using Whisper
|
64 |
-
result = model.transcribe(audio)
|
65 |
-
user_text = result.get("text", "")
|
66 |
-
if not user_text.strip():
|
67 |
-
return "Could not understand the audio. Please try speaking more clearly.", None, chat_history
|
68 |
-
|
69 |
-
# Get current timestamp
|
70 |
-
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
71 |
-
|
72 |
-
# Display transcription in chat history
|
73 |
-
chat_history.append((timestamp, "User", user_text))
|
74 |
-
|
75 |
-
# Step 2: Get LLM response from Groq
|
76 |
-
response_text = get_llm_response(user_text)
|
77 |
-
|
78 |
-
# Step 3: Convert the response text to speech
|
79 |
-
output_audio = text_to_speech(response_text)
|
80 |
-
|
81 |
-
# Append the latest interaction to the chat history
|
82 |
-
chat_history.append((timestamp, "Chatbot", response_text))
|
83 |
-
|
84 |
-
# Format the chat history for display with timestamps and clear labels
|
85 |
-
formatted_history = "\n".join([f"[{time}] {speaker}: {text}" for time, speaker, text in chat_history])
|
86 |
-
|
87 |
-
return formatted_history, output_audio, chat_history
|
88 |
-
|
89 |
-
except Exception as e:
|
90 |
-
print(f"Error in chatbot function: {e}")
|
91 |
-
return "Sorry, there was an error processing your request.", None, chat_history
|
92 |
-
|
93 |
-
# Gradio interface for real-time interaction with chat history display
|
94 |
-
iface = gr.Interface(
|
95 |
-
fn=chatbot,
|
96 |
-
inputs=gr.Audio(type="filepath"),
|
97 |
-
outputs=[
|
98 |
-
gr.Textbox(label="Chat History", lines=10, interactive=False), # Display chat history
|
99 |
-
gr.Audio(type="filepath", label="Response Audio"),
|
100 |
-
],
|
101 |
-
live=True,
|
102 |
-
title="Voice to Voice Chatbot",
|
103 |
-
description="Upload your audio, and the chatbot will transcribe and respond to it with a synthesized response.",
|
104 |
-
theme="default",
|
105 |
-
css='''
|
106 |
-
body {
|
107 |
-
background-image: url("https://huggingface.co/spaces/abdullahzunorain/voice-to-voice-Chatbot/resolve/main/BG_1.jpg");
|
108 |
-
background-size: cover;
|
109 |
-
background-position: center;
|
110 |
-
background-repeat: no-repeat;
|
111 |
-
color: white;
|
112 |
-
font-family: 'Helvetica Neue', sans-serif;
|
113 |
-
}
|
114 |
-
.gradio-container {
|
115 |
-
background-color: rgba(0, 0, 0, 0.7);
|
116 |
-
padding: 20px;
|
117 |
-
border-radius: 10px;
|
118 |
-
box-shadow: 0 4px 20px rgba(0, 0, 0, 0.5);
|
119 |
-
}
|
120 |
-
h1, h2, p, .gradio-label {
|
121 |
-
color: #FFD700; /* Gold color for labels and titles */
|
122 |
-
text-align: center;
|
123 |
-
}
|
124 |
-
.gradio-button {
|
125 |
-
background-color: #FFD700;
|
126 |
-
color: black;
|
127 |
-
border-radius: 5px;
|
128 |
-
font-weight: bold;
|
129 |
-
transition: background-color 0.3s, transform 0.2s;
|
130 |
-
}
|
131 |
-
.gradio-button:hover {
|
132 |
-
background-color: #FFC107; /* Lighter gold on hover */
|
133 |
-
transform: scale(1.05);
|
134 |
-
}
|
135 |
-
.gradio-input {
|
136 |
-
background-color: rgba(255, 255, 255, 0.9);
|
137 |
-
border-radius: 4px;
|
138 |
-
border: 2px solid #FFD700; /* Gold border */
|
139 |
-
}
|
140 |
-
.gradio-audio {
|
141 |
-
border: 2px solid #FFD700; /* Gold border for audio */
|
142 |
-
}
|
143 |
-
'''
|
144 |
-
)
|
145 |
-
|
146 |
-
# Launch the Gradio app
|
147 |
-
if __name__ == "__main__":
|
148 |
-
iface.launch()
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
# import whisper
|
160 |
# import os
|
161 |
# from gtts import gTTS
|
@@ -253,7 +95,7 @@ if __name__ == "__main__":
|
|
253 |
# fn=chatbot,
|
254 |
# inputs=gr.Audio(type="filepath"),
|
255 |
# outputs=[
|
256 |
-
# gr.Textbox(label="Chat History"), # Display chat history
|
257 |
# gr.Audio(type="filepath", label="Response Audio"),
|
258 |
# ],
|
259 |
# live=True,
|
@@ -267,27 +109,37 @@ if __name__ == "__main__":
|
|
267 |
# background-position: center;
|
268 |
# background-repeat: no-repeat;
|
269 |
# color: white;
|
270 |
-
# font-family:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
271 |
# }
|
272 |
-
# # .gradio-container {
|
273 |
-
# # background-color: rgba(0, 0, 0, 0.6);
|
274 |
-
# # padding: 20px;
|
275 |
-
# # border-radius: 8px;
|
276 |
-
# # box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
|
277 |
-
# # }
|
278 |
-
# # h1, h2, p, .gradio-label {
|
279 |
-
# # color: #FFD700; /* Gold color for labels and titles */
|
280 |
-
# # }
|
281 |
-
# # .gradio-button {
|
282 |
-
# # background-color: #FFD700;
|
283 |
-
# # color: black;
|
284 |
-
# # border-radius: 4px;
|
285 |
-
# # font-weight: bold;
|
286 |
-
# # }
|
287 |
-
# # .gradio-input {
|
288 |
-
# # background-color: rgba(255, 255, 255, 0.9);
|
289 |
-
# # border-radius: 4px;
|
290 |
-
# # }
|
291 |
# '''
|
292 |
# )
|
293 |
|
@@ -296,3 +148,151 @@ if __name__ == "__main__":
|
|
296 |
# iface.launch()
|
297 |
|
298 |
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
# import whisper
|
2 |
# import os
|
3 |
# from gtts import gTTS
|
|
|
95 |
# fn=chatbot,
|
96 |
# inputs=gr.Audio(type="filepath"),
|
97 |
# outputs=[
|
98 |
+
# gr.Textbox(label="Chat History", lines=10, interactive=False), # Display chat history
|
99 |
# gr.Audio(type="filepath", label="Response Audio"),
|
100 |
# ],
|
101 |
# live=True,
|
|
|
109 |
# background-position: center;
|
110 |
# background-repeat: no-repeat;
|
111 |
# color: white;
|
112 |
+
# font-family: 'Helvetica Neue', sans-serif;
|
113 |
+
# }
|
114 |
+
# .gradio-container {
|
115 |
+
# background-color: rgba(0, 0, 0, 0.7);
|
116 |
+
# padding: 20px;
|
117 |
+
# border-radius: 10px;
|
118 |
+
# box-shadow: 0 4px 20px rgba(0, 0, 0, 0.5);
|
119 |
+
# }
|
120 |
+
# h1, h2, p, .gradio-label {
|
121 |
+
# color: #FFD700; /* Gold color for labels and titles */
|
122 |
+
# text-align: center;
|
123 |
+
# }
|
124 |
+
# .gradio-button {
|
125 |
+
# background-color: #FFD700;
|
126 |
+
# color: black;
|
127 |
+
# border-radius: 5px;
|
128 |
+
# font-weight: bold;
|
129 |
+
# transition: background-color 0.3s, transform 0.2s;
|
130 |
+
# }
|
131 |
+
# .gradio-button:hover {
|
132 |
+
# background-color: #FFC107; /* Lighter gold on hover */
|
133 |
+
# transform: scale(1.05);
|
134 |
+
# }
|
135 |
+
# .gradio-input {
|
136 |
+
# background-color: rgba(255, 255, 255, 0.9);
|
137 |
+
# border-radius: 4px;
|
138 |
+
# border: 2px solid #FFD700; /* Gold border */
|
139 |
+
# }
|
140 |
+
# .gradio-audio {
|
141 |
+
# border: 2px solid #FFD700; /* Gold border for audio */
|
142 |
# }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
143 |
# '''
|
144 |
# )
|
145 |
|
|
|
148 |
# iface.launch()
|
149 |
|
150 |
|
151 |
+
|
152 |
+
|
153 |
+
|
154 |
+
|
155 |
+
|
156 |
+
|
157 |
+
|
158 |
+
|
159 |
+
import whisper
|
160 |
+
import os
|
161 |
+
from gtts import gTTS
|
162 |
+
import gradio as gr
|
163 |
+
from groq import Groq
|
164 |
+
from datetime import datetime
|
165 |
+
import tempfile
|
166 |
+
|
167 |
+
# Load a smaller Whisper model for faster processing
|
168 |
+
try:
|
169 |
+
model = whisper.load_model("tiny")
|
170 |
+
except Exception as e:
|
171 |
+
print(f"Error loading Whisper model: {e}")
|
172 |
+
model = None
|
173 |
+
|
174 |
+
# Set up Groq API client using environment variable
|
175 |
+
GROQ_API_TOKEN = os.getenv("GROQ_API")
|
176 |
+
if not GROQ_API_TOKEN:
|
177 |
+
raise ValueError("Groq API token is missing. Set 'GROQ_API' in your environment variables.")
|
178 |
+
client = Groq(api_key=GROQ_API_TOKEN)
|
179 |
+
|
180 |
+
# Initialize the chat history
|
181 |
+
chat_history = []
|
182 |
+
|
183 |
+
# Function to get the LLM response from Groq with timeout handling
|
184 |
+
def get_llm_response(user_input, role="detailed responder"):
|
185 |
+
prompt = f"As an expert, provide a detailed and knowledgeable response: {user_input}" if role == "expert" else \
|
186 |
+
f"As a good assistant, provide a clear, concise, and helpful response: {user_input}" if role == "good assistant" else \
|
187 |
+
f"Provide a thorough and detailed response: {user_input}"
|
188 |
+
|
189 |
+
try:
|
190 |
+
chat_completion = client.chat.completions.create(
|
191 |
+
messages=[{"role": "user", "content": user_input}],
|
192 |
+
model="llama3-8b-8192", # Replace with your desired model
|
193 |
+
timeout=20 # Increased timeout to 20 seconds
|
194 |
+
)
|
195 |
+
return chat_completion.choices[0].message.content
|
196 |
+
except Exception as e:
|
197 |
+
print(f"Error during LLM response retrieval: {e}")
|
198 |
+
return "Sorry, there was an error retrieving the response. Please try again."
|
199 |
+
|
200 |
+
# Function to convert text to speech using gTTS and handle temporary files
|
201 |
+
def text_to_speech(text):
|
202 |
+
try:
|
203 |
+
tts = gTTS(text)
|
204 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as temp_file:
|
205 |
+
output_audio = temp_file.name
|
206 |
+
tts.save(output_audio)
|
207 |
+
return output_audio
|
208 |
+
except Exception as e:
|
209 |
+
print(f"Error generating TTS: {e}")
|
210 |
+
return None
|
211 |
+
|
212 |
+
# Main chatbot function to handle audio input and output with chat history
|
213 |
+
def chatbot(audio):
|
214 |
+
if not model:
|
215 |
+
return "Error: Whisper model is not available.", None, chat_history
|
216 |
+
|
217 |
+
if not audio:
|
218 |
+
return "No audio provided. Please upload a valid audio file.", None, chat_history
|
219 |
+
|
220 |
+
try:
|
221 |
+
# Step 1: Transcribe the audio using Whisper
|
222 |
+
result = model.transcribe(audio)
|
223 |
+
user_text = result.get("text", "")
|
224 |
+
if not user_text.strip():
|
225 |
+
return "Could not understand the audio. Please try speaking more clearly.", None, chat_history
|
226 |
+
|
227 |
+
# Get current timestamp
|
228 |
+
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
229 |
+
|
230 |
+
# Display transcription in chat history
|
231 |
+
chat_history.append((timestamp, "User", user_text))
|
232 |
+
|
233 |
+
# Step 2: Get LLM response from Groq
|
234 |
+
response_text = get_llm_response(user_text)
|
235 |
+
|
236 |
+
# Step 3: Convert the response text to speech
|
237 |
+
output_audio = text_to_speech(response_text)
|
238 |
+
|
239 |
+
# Append the latest interaction to the chat history
|
240 |
+
chat_history.append((timestamp, "Chatbot", response_text))
|
241 |
+
|
242 |
+
# Format the chat history for display with timestamps and clear labels
|
243 |
+
formatted_history = "\n".join([f"[{time}] {speaker}: {text}" for time, speaker, text in chat_history])
|
244 |
+
|
245 |
+
return formatted_history, output_audio, chat_history
|
246 |
+
|
247 |
+
except Exception as e:
|
248 |
+
print(f"Error in chatbot function: {e}")
|
249 |
+
return "Sorry, there was an error processing your request.", None, chat_history
|
250 |
+
|
251 |
+
# Gradio interface for real-time interaction with chat history display
|
252 |
+
iface = gr.Interface(
|
253 |
+
fn=chatbot,
|
254 |
+
inputs=gr.Audio(type="filepath"),
|
255 |
+
outputs=[
|
256 |
+
gr.Textbox(label="Chat History"), # Display chat history
|
257 |
+
gr.Audio(type="filepath", label="Response Audio"),
|
258 |
+
],
|
259 |
+
live=True,
|
260 |
+
title="Voice to Voice Chatbot",
|
261 |
+
description="Upload your audio, and the chatbot will transcribe and respond to it with a synthesized response.",
|
262 |
+
theme="default",
|
263 |
+
css='''
|
264 |
+
body {
|
265 |
+
background-image: url("https://huggingface.co/spaces/abdullahzunorain/voice-to-voice-Chatbot/resolve/main/BG_1.jpg");
|
266 |
+
background-size: cover;
|
267 |
+
background-position: center;
|
268 |
+
background-repeat: no-repeat;
|
269 |
+
color: white;
|
270 |
+
font-family: Arial, sans-serif;
|
271 |
+
}
|
272 |
+
# .gradio-container {
|
273 |
+
# background-color: rgba(0, 0, 0, 0.6);
|
274 |
+
# padding: 20px;
|
275 |
+
# border-radius: 8px;
|
276 |
+
# box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
|
277 |
+
# }
|
278 |
+
# h1, h2, p, .gradio-label {
|
279 |
+
# color: #FFD700; /* Gold color for labels and titles */
|
280 |
+
# }
|
281 |
+
# .gradio-button {
|
282 |
+
# background-color: #FFD700;
|
283 |
+
# color: black;
|
284 |
+
# border-radius: 4px;
|
285 |
+
# font-weight: bold;
|
286 |
+
# }
|
287 |
+
# .gradio-input {
|
288 |
+
# background-color: rgba(255, 255, 255, 0.9);
|
289 |
+
# border-radius: 4px;
|
290 |
+
# }
|
291 |
+
'''
|
292 |
+
)
|
293 |
+
|
294 |
+
# Launch the Gradio app
|
295 |
+
if __name__ == "__main__":
|
296 |
+
iface.launch()
|
297 |
+
|
298 |
+
|