Pijush2023 commited on
Commit
a140def
·
verified ·
1 Parent(s): 2f85060

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +152 -39
app.py CHANGED
@@ -32,6 +32,9 @@ from pathlib import Path
32
  import torchaudio
33
  import numpy as np
34
 
 
 
 
35
  # Check if the token is already set in the environment variables
36
  hf_token = os.getenv("HF_TOKEN")
37
  if hf_token is None:
@@ -58,11 +61,16 @@ conversational_memory = ConversationBufferWindowMemory(
58
  return_messages=True
59
  )
60
 
61
- def get_current_time_and_date():
62
- now = datetime.now()
63
- return now.strftime("%Y-%m-%d %H:%M:%S")
 
 
 
 
 
64
 
65
- current_time_and_date = get_current_time_and_date()
66
 
67
  def fetch_local_events():
68
  api_key = os.environ['SERP_API']
@@ -212,19 +220,55 @@ def get_weather_icon(condition):
212
  }
213
  return condition_map.get(condition, "c04d")
214
 
215
- template1 = """You are an expert concierge who is helpful and a renowned guide for Birmingham,Alabama. Based on weather being a sunny bright day and the today's date is 1st july 2024, use the following pieces of context,
216
- memory, and message history, along with your knowledge of perennial events in Birmingham,Alabama, to answer the question at the end. If you don't know the answer, just say "Homie, I need to get more data for this," and don't try to make up an answer.
217
- Use fifteen sentences maximum. Keep the answer as detailed as possible. Always include the address, time, date, and
218
- event type and description.And also add this Birmingham,AL at the end of each address, Always say "It was my pleasure!" at the end of the answer.
219
- {context}
220
- Question: {question}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
221
  Helpful Answer:"""
222
 
223
- template2 = """You are an expert concierge who is helpful and a renowned guide for Birmingham,Alabama. Based on today's weather being a sunny bright day and today's date is 16th july 2024, take the location or address but don't show the location or address on the output prompts. Use the following pieces of context,
224
- memory, and message history, along with your knowledge of perennial events in Birmingham,Alabama, to answer the question at the end. If you don't know the answer, just say "Homie, I need to get more data for this," and don't try to make up an answer.
225
- Keep the answer short ,sweet and crisp and in one shot. Always say "It was my pleasure!" at the end of the answer.
226
- {context}
227
- Question: {question}
228
  Helpful Answer:"""
229
 
230
  QA_CHAIN_PROMPT_1 = PromptTemplate(input_variables=["context", "question"], template=template1)
@@ -274,12 +318,35 @@ def generate_answer(message, choice):
274
  addresses = extract_addresses(response['output'])
275
  return response['output'], addresses
276
 
277
- def bot(history, choice, tts_choice, state):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
278
  if not history:
279
  return history
 
280
  response, addresses = generate_answer(history[-1][0], choice)
281
  history[-1][1] = ""
282
-
283
  with concurrent.futures.ThreadPoolExecutor() as executor:
284
  if tts_choice == "Alpha":
285
  audio_future = executor.submit(generate_audio_elevenlabs, response)
@@ -287,15 +354,17 @@ def bot(history, choice, tts_choice, state):
287
  audio_future = executor.submit(generate_audio_parler_tts, response)
288
  elif tts_choice == "Gamma":
289
  audio_future = executor.submit(generate_audio_mars5, response)
290
-
291
  for character in response:
292
  history[-1][1] += character
293
  time.sleep(0.05)
294
  yield history, None
295
-
296
  audio_path = audio_future.result()
297
  yield history, audio_path
298
 
 
 
299
  def add_message(history, message):
300
  history.append((message, None))
301
  return history, gr.Textbox(value="", interactive=True, placeholder="Enter message or upload file...", show_label=False)
@@ -470,6 +539,36 @@ def show_map_if_details(history, choice):
470
  else:
471
  return gr.update(visible=False), ""
472
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
473
  def generate_audio_elevenlabs(text):
474
  XI_API_KEY = os.environ['ELEVENLABS_API']
475
  VOICE_ID = 'd9MIrwLnvDeH7aZb61E9'
@@ -490,16 +589,26 @@ def generate_audio_elevenlabs(text):
490
  }
491
  response = requests.post(tts_url, headers=headers, json=data, stream=True)
492
  if response.ok:
 
493
  with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as f:
494
  for chunk in response.iter_content(chunk_size=1024):
495
- f.write(chunk)
 
 
496
  temp_audio_path = f.name
497
- logging.debug(f"Audio saved to {temp_audio_path}")
498
- return temp_audio_path
 
 
 
 
 
 
499
  else:
500
  logging.error(f"Error generating audio: {response.text}")
501
  return None
502
 
 
503
  repo_id = "parler-tts/parler-tts-mini-expresso"
504
 
505
  parler_model = ParlerTTSForConditionalGeneration.from_pretrained(repo_id).to(device)
@@ -630,27 +739,30 @@ def update_images():
630
  image_3 = generate_image(hardcoded_prompt_3)
631
  return image_1, image_2, image_3
632
 
633
- def clear_state_and_textbox():
634
- conversational_memory.clear()
635
- return ""
 
 
 
 
 
636
 
637
- def transcribe_and_update_textbox(audio, chat_input):
638
- transcribed_text = transcribe(audio)
639
- # return "",transcribed_text
640
- return transcribed_text
 
 
 
641
 
642
- def transcribe_function_whisper(audio):
643
- sr, y = audio
644
- y = y.astype(np.float32)
645
- y /= np.max(np.abs(y))
646
- result = pipe_asr({"array": y, "sampling_rate": sr}, return_timestamps=False)
647
- full_text = result.get("text", "")
648
- return full_text
649
 
650
 
 
 
651
 
652
- def clear_stream_transcription():
653
- return None, ""
654
 
655
  with gr.Blocks(theme='Pijush2023/scikit-learn-pijush') as demo:
656
  with gr.Row():
@@ -696,7 +808,8 @@ with gr.Blocks(theme='Pijush2023/scikit-learn-pijush') as demo:
696
 
697
  # Add the clear button for ASR history
698
  clear_btn = gr.Button("Clear ASR ")
699
- clear_btn.click(lambda: [None, ""], outputs=[stream_transcription, chat_input])
 
700
 
701
  with gr.Column():
702
  weather_output = gr.HTML(value=fetch_local_weather())
 
32
  import torchaudio
33
  import numpy as np
34
 
35
+ PYTORCH_USE_CUDA_DSA = 1
36
+ CUDA_LAUNCH_BLOCKING = 1
37
+
38
  # Check if the token is already set in the environment variables
39
  hf_token = os.getenv("HF_TOKEN")
40
  if hf_token is None:
 
61
  return_messages=True
62
  )
63
 
64
+ # def get_current_time_and_date():
65
+ # now = datetime.now()
66
+ # return now.strftime("%Y-%m-%d %H:%M:%S")
67
+
68
+ # current_time_and_date = get_current_time_and_date()
69
+
70
+ def get_current_date():
71
+ return datetime.now().strftime("%B %d, %Y")
72
 
73
+ current_date = get_current_date()
74
 
75
  def fetch_local_events():
76
  api_key = os.environ['SERP_API']
 
220
  }
221
  return condition_map.get(condition, "c04d")
222
 
223
+ # template1 = """You are an expert concierge who is helpful and a renowned guide for Birmingham,Alabama. Based on weather being a sunny bright day and the today's date is 1st july 2024, use the following pieces of context,
224
+ # memory, and message history, along with your knowledge of perennial events in Birmingham,Alabama, to answer the question at the end. If you don't know the answer, just say "Homie, I need to get more data for this," and don't try to make up an answer.
225
+ # Use fifteen sentences maximum. Keep the answer as detailed as possible. Always include the address, time, date, and
226
+ # event type and description.And also add this Birmingham,AL at the end of each address, Always say "It was my pleasure!" at the end of the answer.
227
+ # {context}
228
+ # Question: {question}
229
+ # Helpful Answer:"""
230
+
231
+ # template2 = """You are an expert concierge who is helpful and a renowned guide for Birmingham,Alabama. Based on today's weather being a sunny bright day and today's date is 16th july 2024, take the location or address but don't show the location or address on the output prompts. Use the following pieces of context,
232
+ # memory, and message history, along with your knowledge of perennial events in Birmingham,Alabama, to answer the question at the end. If you don't know the answer, just say "Homie, I need to get more data for this," and don't try to make up an answer.
233
+ # Keep the answer short ,sweet and crisp and in one shot. Always say "It was my pleasure!" at the end of the answer.
234
+ # {context}
235
+ # Question: {question}
236
+ # Helpful Answer:"""
237
+
238
+ template1 = f"""As an expert concierge in Birmingham, Alabama, known for being a helpful and renowned guide, I am here to assist you on this sunny bright day of {current_date}. Given the current weather conditions and date, I have access to a plethora of information regarding events, places, and activities in Birmingham that can enhance your experience.
239
+ If you have any questions or need recommendations, feel free to ask. I have a wealth of knowledge of perennial events in Birmingham and can provide detailed information to ensure you make the most of your time here. Remember, I am here to assist you in any way possible.
240
+ Now, let me guide you through some of the exciting events happening today in Birmingham, Alabama:
241
+ Address: >>, Birmingham, AL
242
+ Time: >>__
243
+ Date: >>__
244
+ Description: >>__
245
+ Address: >>, Birmingham, AL
246
+ Time: >>__
247
+ Date: >>__
248
+ Description: >>__
249
+ Address: >>, Birmingham, AL
250
+ Time: >>__
251
+ Date: >>__
252
+ Description: >>__
253
+ Address: >>, Birmingham, AL
254
+ Time: >>__
255
+ Date: >>__
256
+ Description: >>__
257
+ Address: >>, Birmingham, AL
258
+ Time: >>__
259
+ Date: >>__
260
+ Description: >>__
261
+ If you have any specific preferences or questions about these events or any other inquiries, please feel free to ask. Remember, I am here to ensure you have a memorable and enjoyable experience in Birmingham, AL.
262
+ It was my pleasure!
263
+ {{context}}
264
+ Question: {{question}}
265
  Helpful Answer:"""
266
 
267
+ template2 = f"""As an expert concierge known for being helpful and a renowned guide for Birmingham, Alabama, I assist visitors in discovering the best that the city has to offer. Given today's sunny and bright weather on {current_date}, I am well-equipped to provide valuable insights and recommendations without revealing specific locations. I draw upon my extensive knowledge of the area, including perennial events and historical context.
268
+ In light of this, how can I assist you today? Feel free to ask any questions or seek recommendations for your day in Birmingham. If there's anything specific you'd like to know or experience, please share, and I'll be glad to help. Remember, keep the question concise for a quick and accurate response.
269
+ "It was my pleasure!"
270
+ {{context}}
271
+ Question: {{question}}
272
  Helpful Answer:"""
273
 
274
  QA_CHAIN_PROMPT_1 = PromptTemplate(input_variables=["context", "question"], template=template1)
 
318
  addresses = extract_addresses(response['output'])
319
  return response['output'], addresses
320
 
321
+ # def bot(history, choice, tts_choice, state):
322
+ # if not history:
323
+ # return history
324
+ # response, addresses = generate_answer(history[-1][0], choice)
325
+ # history[-1][1] = ""
326
+
327
+ # with concurrent.futures.ThreadPoolExecutor() as executor:
328
+ # if tts_choice == "Alpha":
329
+ # audio_future = executor.submit(generate_audio_elevenlabs, response)
330
+ # elif tts_choice == "Beta":
331
+ # audio_future = executor.submit(generate_audio_parler_tts, response)
332
+ # elif tts_choice == "Gamma":
333
+ # audio_future = executor.submit(generate_audio_mars5, response)
334
+
335
+ # for character in response:
336
+ # history[-1][1] += character
337
+ # time.sleep(0.05)
338
+ # yield history, None
339
+
340
+ # audio_path = audio_future.result()
341
+ # yield history, audio_path
342
+
343
+ def bot(history, choice, tts_choice):
344
  if not history:
345
  return history
346
+
347
  response, addresses = generate_answer(history[-1][0], choice)
348
  history[-1][1] = ""
349
+
350
  with concurrent.futures.ThreadPoolExecutor() as executor:
351
  if tts_choice == "Alpha":
352
  audio_future = executor.submit(generate_audio_elevenlabs, response)
 
354
  audio_future = executor.submit(generate_audio_parler_tts, response)
355
  elif tts_choice == "Gamma":
356
  audio_future = executor.submit(generate_audio_mars5, response)
357
+
358
  for character in response:
359
  history[-1][1] += character
360
  time.sleep(0.05)
361
  yield history, None
362
+
363
  audio_path = audio_future.result()
364
  yield history, audio_path
365
 
366
+ history.append([response, None]) # Ensure the response is added in the correct format
367
+
368
  def add_message(history, message):
369
  history.append((message, None))
370
  return history, gr.Textbox(value="", interactive=True, placeholder="Enter message or upload file...", show_label=False)
 
539
  else:
540
  return gr.update(visible=False), ""
541
 
542
+ # def generate_audio_elevenlabs(text):
543
+ # XI_API_KEY = os.environ['ELEVENLABS_API']
544
+ # VOICE_ID = 'd9MIrwLnvDeH7aZb61E9'
545
+ # tts_url = f"https://api.elevenlabs.io/v1/text-to-speech/{VOICE_ID}/stream"
546
+ # headers = {
547
+ # "Accept": "application/json",
548
+ # "xi-api-key": XI_API_KEY
549
+ # }
550
+ # data = {
551
+ # "text": str(text),
552
+ # "model_id": "eleven_multilingual_v2",
553
+ # "voice_settings": {
554
+ # "stability": 1.0,
555
+ # "similarity_boost": 0.0,
556
+ # "style": 0.60,
557
+ # "use_speaker_boost": False
558
+ # }
559
+ # }
560
+ # response = requests.post(tts_url, headers=headers, json=data, stream=True)
561
+ # if response.ok:
562
+ # with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as f:
563
+ # for chunk in response.iter_content(chunk_size=1024):
564
+ # f.write(chunk)
565
+ # temp_audio_path = f.name
566
+ # logging.debug(f"Audio saved to {temp_audio_path}")
567
+ # return temp_audio_path
568
+ # else:
569
+ # logging.error(f"Error generating audio: {response.text}")
570
+ # return None
571
+
572
  def generate_audio_elevenlabs(text):
573
  XI_API_KEY = os.environ['ELEVENLABS_API']
574
  VOICE_ID = 'd9MIrwLnvDeH7aZb61E9'
 
589
  }
590
  response = requests.post(tts_url, headers=headers, json=data, stream=True)
591
  if response.ok:
592
+ audio_segments = []
593
  with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as f:
594
  for chunk in response.iter_content(chunk_size=1024):
595
+ if chunk:
596
+ f.write(chunk)
597
+ audio_segments.append(chunk)
598
  temp_audio_path = f.name
599
+
600
+ # Combine all audio chunks into a single file
601
+ combined_audio = AudioSegment.from_file(temp_audio_path, format="mp3")
602
+ combined_audio_path = os.path.join(tempfile.gettempdir(), "elevenlabs_combined_audio.mp3")
603
+ combined_audio.export(combined_audio_path, format="mp3")
604
+
605
+ logging.debug(f"Audio saved to {combined_audio_path}")
606
+ return combined_audio_path
607
  else:
608
  logging.error(f"Error generating audio: {response.text}")
609
  return None
610
 
611
+
612
  repo_id = "parler-tts/parler-tts-mini-expresso"
613
 
614
  parler_model = ParlerTTSForConditionalGeneration.from_pretrained(repo_id).to(device)
 
739
  image_3 = generate_image(hardcoded_prompt_3)
740
  return image_1, image_2, image_3
741
 
742
+ # def clear_state_and_textbox():
743
+ # conversational_memory.clear()
744
+ # return ""
745
+
746
+ # def transcribe_and_update_textbox(audio, chat_input):
747
+ # transcribed_text = transcribe(audio)
748
+ # # return "",transcribed_text
749
+ # return transcribed_text
750
 
751
+ # def transcribe_function_whisper(audio):
752
+ # sr, y = audio
753
+ # y = y.astype(np.float32)
754
+ # y /= np.max(np.abs(y))
755
+ # result = pipe_asr({"array": y, "sampling_rate": sr}, return_timestamps=False)
756
+ # full_text = result.get("text", "")
757
+ # return full_text
758
 
 
 
 
 
 
 
 
759
 
760
 
761
+ # def clear_stream_transcription():
762
+ # return None, ""
763
 
764
+ def clear_textbox():
765
+ return ""
766
 
767
  with gr.Blocks(theme='Pijush2023/scikit-learn-pijush') as demo:
768
  with gr.Row():
 
808
 
809
  # Add the clear button for ASR history
810
  clear_btn = gr.Button("Clear ASR ")
811
+ # clear_btn.click(lambda: [None, ""], outputs=[stream_transcription, chat_input])
812
+ clear_button.click(lambda:[None,None] ,outputs=[chat_input, state])
813
 
814
  with gr.Column():
815
  weather_output = gr.HTML(value=fetch_local_weather())