abdullahzunorain commited on
Commit
27f9093
·
verified ·
1 Parent(s): b2859de

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +59 -163
app.py CHANGED
@@ -95,11 +95,11 @@ iface = gr.Interface(
95
  fn=chatbot,
96
  inputs=gr.Audio(type="filepath"),
97
  outputs=[
98
- gr.Textbox(label="Chat History"), # Display chat history
99
  gr.Audio(type="filepath", label="Response Audio"),
100
  ],
101
  live=True,
102
- title="Stylish Audio Chatbot with Groq API",
103
  description="Upload your audio, and the chatbot will transcribe and respond to it with a synthesized response.",
104
  theme="default",
105
  css='''
@@ -109,27 +109,37 @@ iface = gr.Interface(
109
  background-position: center;
110
  background-repeat: no-repeat;
111
  color: white;
112
- font-family: Arial, sans-serif;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
113
  }
114
- # .gradio-container {
115
- # background-color: rgba(0, 0, 0, 0.6);
116
- # padding: 20px;
117
- # border-radius: 8px;
118
- # box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
119
- # }
120
- # h1, h2, p, .gradio-label {
121
- # color: #FFD700; /* Gold color for labels and titles */
122
- # }
123
- # .gradio-button {
124
- # background-color: #FFD700;
125
- # color: black;
126
- # border-radius: 4px;
127
- # font-weight: bold;
128
- # }
129
- # .gradio-input {
130
- # background-color: rgba(255, 255, 255, 0.9);
131
- # border-radius: 4px;
132
- # }
133
  '''
134
  )
135
 
@@ -152,6 +162,7 @@ if __name__ == "__main__":
152
  # import gradio as gr
153
  # from groq import Groq
154
  # from datetime import datetime
 
155
 
156
  # # Load a smaller Whisper model for faster processing
157
  # try:
@@ -186,12 +197,13 @@ if __name__ == "__main__":
186
  # print(f"Error during LLM response retrieval: {e}")
187
  # return "Sorry, there was an error retrieving the response. Please try again."
188
 
189
- # # Function to convert text to speech using gTTS
190
  # def text_to_speech(text):
191
  # try:
192
  # tts = gTTS(text)
193
- # output_audio = "output_audio.mp3"
194
- # tts.save(output_audio)
 
195
  # return output_audio
196
  # except Exception as e:
197
  # print(f"Error generating TTS: {e}")
@@ -245,37 +257,37 @@ if __name__ == "__main__":
245
  # gr.Audio(type="filepath", label="Response Audio"),
246
  # ],
247
  # live=True,
248
- # title="Stylish Audio Chatbot with Groq API",
249
  # description="Upload your audio, and the chatbot will transcribe and respond to it with a synthesized response.",
250
  # theme="default",
251
- # css='''
252
  # body {
253
- # background-image: url("https://huggingface.co/spaces/abdullahzunorain/voice-to-voice-Chatbot/blob/main/BG_1.jpg");
254
  # background-size: cover;
255
  # background-position: center;
256
  # background-repeat: no-repeat;
257
  # color: white;
258
  # font-family: Arial, sans-serif;
259
  # }
260
- # .gradio-container {
261
- # background-color: rgba(0, 0, 0, 0.6);
262
- # padding: 20px;
263
- # border-radius: 8px;
264
- # box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
265
- # }
266
- # h1, h2, p, .gradio-label {
267
- # color: #FFD700; /* Gold color for labels and titles */
268
- # }
269
- # .gradio-button {
270
- # background-color: #FFD700;
271
- # color: black;
272
- # border-radius: 4px;
273
- # font-weight: bold;
274
- # }
275
- # .gradio-input {
276
- # background-color: rgba(255, 255, 255, 0.9);
277
- # border-radius: 4px;
278
- # }
279
  # '''
280
  # )
281
 
@@ -284,119 +296,3 @@ if __name__ == "__main__":
284
  # iface.launch()
285
 
286
 
287
-
288
-
289
-
290
- # ---------------------------------------------------------------------------------------------------------------------------------------------------------------
291
-
292
-
293
-
294
-
295
- # import whisper
296
- # import os
297
- # from gtts import gTTS
298
- # import gradio as gr
299
- # from groq import Groq
300
- # from datetime import datetime # Import datetime to handle timestamps
301
-
302
- # # Load a smaller Whisper model for faster processing
303
- # try:
304
- # model = whisper.load_model("tiny")
305
- # except Exception as e:
306
- # print(f"Error loading Whisper model: {e}")
307
- # model = None
308
-
309
- # # Set up Groq API client using environment variable
310
- # GROQ_API_TOKEN = os.getenv("GROQ_API")
311
- # if not GROQ_API_TOKEN:
312
- # raise ValueError("Groq API token is missing. Set 'GROQ_API' in your environment variables.")
313
- # client = Groq(api_key=GROQ_API_TOKEN)
314
-
315
- # # Initialize the chat history
316
- # chat_history = []
317
-
318
- # # Function to get the LLM response from Groq with timeout handling
319
- # def get_llm_response(user_input, role="detailed responder"):
320
- # prompt = f"As an expert, provide a detailed and knowledgeable response: {user_input}" if role == "expert" else \
321
- # f"As a good assistant, provide a clear, concise, and helpful response: {user_input}" if role == "good assistant" else \
322
- # f"Provide a thorough and detailed response: {user_input}"
323
-
324
- # try:
325
- # chat_completion = client.chat.completions.create(
326
- # messages=[{"role": "user", "content": user_input}],
327
- # model="llama3-8b-8192", # Replace with your desired model
328
- # timeout=20 # Increased timeout to 20 seconds
329
- # )
330
- # return chat_completion.choices[0].message.content
331
- # except Exception as e:
332
- # print(f"Error during LLM response retrieval: {e}")
333
- # return "Sorry, there was an error retrieving the response. Please try again."
334
-
335
- # # Function to convert text to speech using gTTS
336
- # def text_to_speech(text):
337
- # try:
338
- # tts = gTTS(text)
339
- # output_audio = "output_audio.mp3"
340
- # tts.save(output_audio)
341
- # return output_audio
342
- # except Exception as e:
343
- # print(f"Error generating TTS: {e}")
344
- # return None
345
-
346
- # # Main chatbot function to handle audio input and output with chat history
347
- # def chatbot(audio):
348
- # if not model:
349
- # return "Error: Whisper model is not available.", None, chat_history
350
-
351
- # if not audio:
352
- # return "No audio provided. Please upload a valid audio file.", None, chat_history
353
-
354
- # try:
355
- # # Step 1: Transcribe the audio using Whisper
356
- # result = model.transcribe(audio)
357
- # user_text = result.get("text", "")
358
- # if not user_text.strip():
359
- # return "Could not understand the audio. Please try speaking more clearly.", None, chat_history
360
-
361
- # # Get current timestamp
362
- # timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
363
-
364
- # # Display transcription in chat history
365
- # chat_history.append((timestamp, "User", user_text))
366
-
367
- # # Step 2: Get LLM response from Groq
368
- # response_text = get_llm_response(user_text)
369
-
370
- # # Step 3: Convert the response text to speech
371
- # output_audio = text_to_speech(response_text)
372
-
373
- # # Append the latest interaction to the chat history
374
- # chat_history.append((timestamp, "Chatbot", response_text))
375
-
376
- # # Format the chat history for display with timestamps and clear labels
377
- # formatted_history = "\n".join([f"[{time}] {speaker}: {text}" for time, speaker, text in chat_history])
378
-
379
- # return formatted_history, output_audio, chat_history
380
-
381
- # except Exception as e:
382
- # print(f"Error in chatbot function: {e}")
383
- # return "Sorry, there was an error processing your request.", None, chat_history
384
-
385
- # # Gradio interface for real-time interaction with chat history display
386
- # iface = gr.Interface(
387
- # fn=chatbot,
388
- # inputs=gr.Audio(type="filepath"),
389
- # outputs=[
390
- # gr.Textbox(label="Chat History"), # Display chat history
391
- # gr.Audio(type="filepath", label="Response Audio"),
392
- # ],
393
- # live=True,
394
- # title="Audio Chatbot with Groq API",
395
- # description="Upload your audio, and the chatbot will transcribe and respond to it with a synthesized response.",
396
- # theme="default"
397
- # )
398
-
399
- # # Launch the Gradio app
400
- # if __name__ == "__main__":
401
- # iface.launch()
402
-
 
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='''
 
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
 
 
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:
 
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}")
 
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
 
 
296
  # iface.launch()
297
 
298