Pijush2023 commited on
Commit
2a86723
·
verified ·
1 Parent(s): 8dde2a4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +88 -6
app.py CHANGED
@@ -126,18 +126,43 @@ def generate_answer(message, choice):
126
  addresses = extract_addresses(response['output'])
127
  return response['output'], addresses
128
 
 
 
 
 
 
 
 
 
 
 
 
129
  def bot(history, choice):
130
  if not history:
131
  return history
132
  response, addresses = generate_answer(history[-1][0], choice)
133
  history[-1][1] = ""
134
- for character in response:
135
- history[-1][1] += character
136
- time.sleep(0.05)
137
- yield history
138
- if addresses:
139
- return history, addresses
 
140
 
 
 
 
 
 
 
 
 
 
 
 
 
 
141
  def add_message(history, message):
142
  history.append((message, None))
143
  return history, gr.Textbox(value="", interactive=True, placeholder="Enter message or upload file...", show_label=False)
@@ -520,6 +545,63 @@ def generate_image(prompt):
520
  # Hardcoded prompt for image generation
521
  hardcoded_prompt = "Useing The top events like 'Summer Art Festival' and current time - 4:07 PM ,Date - 06/17/2024 ,Weather-Sunny Bright Day.Create Highly Visually Compelling High Resolution and High Quality Photographics Advatizement for 'Toyota'"
522
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
523
  with gr.Blocks(theme='rawrsor1/Everforest') as demo:
524
  with gr.Row():
525
  chatbot = gr.Chatbot([], elem_id="chatbot", bubble_full_width=False)
 
126
  addresses = extract_addresses(response['output'])
127
  return response['output'], addresses
128
 
129
+ # def bot(history, choice):
130
+ # if not history:
131
+ # return history
132
+ # response, addresses = generate_answer(history[-1][0], choice)
133
+ # history[-1][1] = ""
134
+ # for character in response:
135
+ # history[-1][1] += character
136
+ # time.sleep(0.05)
137
+ # yield history
138
+ # if addresses:
139
+ # return history, addresses
140
  def bot(history, choice):
141
  if not history:
142
  return history
143
  response, addresses = generate_answer(history[-1][0], choice)
144
  history[-1][1] = ""
145
+ audio_segments = []
146
+ tts = gTTS(response, lang='en')
147
+ with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as f:
148
+ tts.save(f.name)
149
+ temp_audio_path = f.name
150
+
151
+ audio_chunks = split_audio(temp_audio_path, len(response))
152
 
153
+ for idx, character in enumerate(response):
154
+ history[-1][1] += character
155
+ if idx < len(audio_chunks):
156
+ audio_segments.append(audio_chunks[idx])
157
+ yield history, audio_segments
158
+
159
+ def split_audio(audio_path, num_chunks):
160
+ from pydub import AudioSegment
161
+ audio = AudioSegment.from_file(audio_path)
162
+ chunk_length = len(audio) / num_chunks
163
+ chunks = [audio[i * chunk_length:(i + 1) * chunk_length] for i in range(num_chunks)]
164
+ return chunks
165
+
166
  def add_message(history, message):
167
  history.append((message, None))
168
  return history, gr.Textbox(value="", interactive=True, placeholder="Enter message or upload file...", show_label=False)
 
545
  # Hardcoded prompt for image generation
546
  hardcoded_prompt = "Useing The top events like 'Summer Art Festival' and current time - 4:07 PM ,Date - 06/17/2024 ,Weather-Sunny Bright Day.Create Highly Visually Compelling High Resolution and High Quality Photographics Advatizement for 'Toyota'"
547
 
548
+ # with gr.Blocks(theme='rawrsor1/Everforest') as demo:
549
+ # with gr.Row():
550
+ # chatbot = gr.Chatbot([], elem_id="chatbot", bubble_full_width=False)
551
+ # with gr.Column():
552
+ # weather_output = gr.HTML(value=fetch_local_weather())
553
+
554
+ # with gr.Column():
555
+ # news_output = gr.HTML(value=fetch_local_news())
556
+
557
+ # def setup_ui():
558
+ # state = gr.State()
559
+ # with gr.Row():
560
+ # with gr.Column():
561
+ # gr.Markdown("<h1>Choose the prompt</h1>", elem_id="prompt-markdown")
562
+ # choice = gr.Radio(label="Choose a prompt", choices=["Details", "Conversational"], value="Details")
563
+ # with gr.Column(): # Larger scale for the right column
564
+ # gr.Markdown("<h1>Enter the query / Voice Output</h1>", elem_id="query-markdown")
565
+
566
+ # chat_input = gr.Textbox(show_copy_button=True, interactive=True, show_label=False, label="Transcription")
567
+ # chat_msg = chat_input.submit(add_message, [chatbot, chat_input], [chatbot, chat_input])
568
+ # bot_msg = chat_msg.then(bot, [chatbot, choice], chatbot, api_name="bot_response")
569
+ # bot_msg.then(lambda: gr.Textbox(value="", interactive=True, placeholder="Enter message or upload file...", show_label=False), None, [chat_input])
570
+ # chatbot.like(print_like_dislike, None, None)
571
+ # clear_button = gr.Button("Clear")
572
+ # clear_button.click(fn=clear_textbox, inputs=None, outputs=chat_input)
573
+
574
+ # with gr.Column(): # Smaller scale for the left column
575
+ # gr.Markdown("<h1>Stream your Voice</h1>", elem_id="voice-markdown")
576
+
577
+ # audio_input = gr.Audio(sources=["microphone"], streaming=True, type='numpy')
578
+ # audio_input.stream(transcribe_function, inputs=[state, audio_input], outputs=[state, chat_input], api_name="SAMLOne_real_time")
579
+
580
+ # with gr.Row():
581
+ # with gr.Column():
582
+ # gr.Markdown("<h1>Locate the Events</h1>", elem_id="location-markdown")
583
+ # location_output = gr.HTML()
584
+ # bot_msg.then(show_map_if_details, [chatbot, choice], [location_output, location_output])
585
+ # with gr.Column():
586
+ # gr.Markdown("<h1>Listen the audio</h1>", elem_id="audio-markdown")
587
+ # audio_output = gr.Audio()
588
+ # #bot_msg.then(generate_audio_elevenlabs, chatbot, audio_output)
589
+ # bot_msg_audio = bot_msg.then(lambda history: generate_audio_elevenlabs(history[-1][1]), chatbot, audio_output)
590
+ # with gr.Row():
591
+
592
+ # with gr.Column():
593
+ # gr.Markdown("<h1>Local Events</h1>", elem_id="events-markdown")
594
+ # news_output = gr.HTML(value=fetch_local_events())
595
+
596
+ # with gr.Column():
597
+ # gr.Markdown("<h1>Generated Image</h1>", elem_id="image-markdown")
598
+ # image_output = gr.Image(value=generate_image(hardcoded_prompt))
599
+
600
+ # setup_ui()
601
+
602
+ # demo.queue()
603
+ # demo.launch(share=True)
604
+
605
  with gr.Blocks(theme='rawrsor1/Everforest') as demo:
606
  with gr.Row():
607
  chatbot = gr.Chatbot([], elem_id="chatbot", bubble_full_width=False)