onisj commited on
Commit
ce0846b
·
verified ·
1 Parent(s): c3449ff
Files changed (1) hide show
  1. Main2.py +51 -42
Main2.py CHANGED
@@ -237,50 +237,59 @@ def create_interface():
237
  refresh_characters_btn.click(fn=lambda: gr.update(value=get_existing_characters()), outputs=[character_list])
238
 
239
  with gr.Tab("Chat with Character"):
 
240
  with gr.Row():
241
- character_dropdown = gr.Dropdown(label="Choose Character", choices=[char[0] for char in get_existing_characters()], elem_id="character_dropdown")
242
- chat_id_display = gr.Textbox(label="Current Chat ID", interactive=False, elem_id="chat_id_display")
243
- user_input = gr.Textbox(label="Your Message", placeholder="Type your message or use audio/video input", elem_id="user_input", lines=2)
244
- audio_input = gr.Audio(label="Audio Input", type="filepath", elem_id="audio_input")
245
- video_input = gr.Video(label="Video Input", elem_id="video_input")
246
- chat_btn = gr.Button("Send", variant="primary")
247
- chat_response = gr.Chatbot(label="Chat Responses", elem_id="chat_response", height=500, type="messages") # Increased height here
248
-
249
- def handle_chat(character_name, user_input, audio_file, video_file, user_id, chat_messages, current_chat_id):
250
- if not user_id:
251
- return chat_messages, current_chat_id, "Please sign in with a numeric User ID first!"
252
- if not character_name:
253
- return chat_messages, current_chat_id, "Please select a character!"
254
- final_input = user_input or ""
255
-
256
- if audio_file:
257
- audio_text = speech_to_text(audio_file)
258
- if audio_text:
259
- final_input += f" {audio_text}"
260
- else:
261
- chat_messages.append({"role": "assistant", "content": "Could not understand audio."})
262
- return chat_messages, current_chat_id, None
263
-
264
- if video_file:
265
- audio_file_path = extract_audio_from_video(video_file)
266
- if audio_file_path:
267
- video_text = speech_to_text(audio_file_path)
268
- if video_text:
269
- final_input += f" {video_text}"
270
- chat_messages.append({"role": "user", "content": "Video uploaded"})
271
- else:
272
- chat_messages.append({"role": "assistant", "content": "Failed to extract audio from video."})
273
- return chat_messages, current_chat_id, None
274
-
275
- if not final_input.strip():
276
- return chat_messages, current_chat_id, "Please provide a message, audio, or video!"
277
-
278
- response, new_chat_id = chat_with_character(character_name, final_input, user_id, current_chat_id)
279
- chat_messages.append({"role": "user", "content": final_input})
280
- chat_messages.append({"role": "assistant", "content": response})
281
- return chat_messages, new_chat_id, new_chat_id
282
 
283
- chat_btn.click(fn=handle_chat, inputs=[character_dropdown, user_input, audio_input, video_input, user_id, chat_messages, current_chat_id], outputs=[chat_response, current_chat_id, chat_id_display])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
284
 
285
  with gr.Tab("Chat History"):
286
  with gr.Row():
 
237
  refresh_characters_btn.click(fn=lambda: gr.update(value=get_existing_characters()), outputs=[character_list])
238
 
239
  with gr.Tab("Chat with Character"):
240
+ # Top row for all input elements
241
  with gr.Row():
242
+ with gr.Column(scale=1):
243
+ character_dropdown = gr.Dropdown(label="Choose Character", choices=[char[0] for char in get_existing_characters()], elem_id="character_dropdown")
244
+ chat_id_display = gr.Textbox(label="Current Chat ID", interactive=False, elem_id="chat_id_display")
245
+ with gr.Column(scale=2):
246
+ user_input = gr.Textbox(label="Your Message", placeholder="Type your message or use audio/video input", elem_id="user_input", lines=2)
247
+ with gr.Column(scale=1):
248
+ audio_input = gr.Audio(label="Audio Input", type="filepath", elem_id="audio_input")
249
+ video_input = gr.Video(label="Video Input", elem_id="video_input")
250
+ with gr.Column(scale=1):
251
+ chat_btn = gr.Button("Send", variant="primary")
252
+
253
+ # Separate div below for the large chat response area
254
+ with gr.Row():
255
+ with gr.Column(scale=12): # Make this column take up most of the space for a large response area
256
+ chat_response = gr.Chatbot(label="Chat Responses", elem_id="chat_response", height=700, type="messages") # Increased height for a larger response area
257
+
258
+ def handle_chat(character_name, user_input, audio_file, video_file, user_id, chat_messages, current_chat_id):
259
+ if not user_id:
260
+ return chat_messages, current_chat_id, "Please sign in with a numeric User ID first!"
261
+ if not character_name:
262
+ return chat_messages, current_chat_id, "Please select a character!"
263
+ final_input = user_input or ""
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
264
 
265
+ if audio_file:
266
+ audio_text = speech_to_text(audio_file)
267
+ if audio_text:
268
+ final_input += f" {audio_text}"
269
+ else:
270
+ chat_messages.append({"role": "assistant", "content": "Could not understand audio."})
271
+ return chat_messages, current_chat_id, None
272
+
273
+ if video_file:
274
+ audio_file_path = extract_audio_from_video(video_file)
275
+ if audio_file_path:
276
+ video_text = speech_to_text(audio_file_path)
277
+ if video_text:
278
+ final_input += f" {video_text}"
279
+ chat_messages.append({"role": "user", "content": "Video uploaded"})
280
+ else:
281
+ chat_messages.append({"role": "assistant", "content": "Failed to extract audio from video."})
282
+ return chat_messages, current_chat_id, None
283
+
284
+ if not final_input.strip():
285
+ return chat_messages, current_chat_id, "Please provide a message, audio, or video!"
286
+
287
+ response, new_chat_id = chat_with_character(character_name, final_input, user_id, current_chat_id)
288
+ chat_messages.append({"role": "user", "content": final_input})
289
+ chat_messages.append({"role": "assistant", "content": response})
290
+ return chat_messages, new_chat_id, new_chat_id
291
+
292
+ chat_btn.click(fn=handle_chat, inputs=[character_dropdown, user_input, audio_input, video_input, user_id, chat_messages, current_chat_id], outputs=[chat_response, current_chat_id, chat_id_display])
293
 
294
  with gr.Tab("Chat History"):
295
  with gr.Row():