TDN-M commited on
Commit
371fde7
·
verified ·
1 Parent(s): 3c9cda6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -12
app.py CHANGED
@@ -15,6 +15,7 @@ position_1 = None
15
  position_2 = None
16
  turn = None
17
  history = []
 
18
 
19
  # Hàm để tạo phản hồi tranh luận
20
  def generate_response(llm, position, who, topic, message):
@@ -44,35 +45,40 @@ async def text_to_speech(text, voice, output_file="output.mp3"):
44
 
45
  # Hàm để bắt đầu tranh luận giữa Miss AI Vietnam và Miss AI China
46
  def start_debate(topic, position_1, position_2):
47
- global turn, history
48
  if not topic or not position_1 or not position_2:
49
- return "Please provide the debate topic and positions for both participants.", [], None
50
 
51
  # Đảm bảo các vị trí là đối lập
52
  if position_1 == position_2:
53
- return "The positions of both participants must be opposite. Please adjust them.", [], None
54
 
55
  turn = "Miss AI Vietnam"
56
  history = [] # Đặt lại lịch sử
 
57
  initial_message = "Opening Statement"
58
  response = generate_response(MissAIVietnam, position_1, 'Miss AI Vietnam', topic, initial_message)
59
  history.append((initial_message, response))
60
 
61
  # Chuyển văn bản thành âm thanh với giọng của Miss AI Vietnam
62
  output_file = asyncio.run(text_to_speech(response, "en-US-JennyNeural")) # Giọng nữ tiếng Anh Mỹ
63
- return f"The debate has started! {turn} begins.", history, output_file
 
 
 
 
64
 
65
  # Hàm để chuyển lượt trong tranh luận
66
  def next_turn(topic, position_1, position_2, current_history):
67
- global turn, history
68
  if not current_history:
69
- return "No ongoing debate. Please start a debate first.", [], None
70
 
71
  # Logic chuyển lượt
72
  if turn == "Miss AI Vietnam":
73
  turn = "Miss AI China"
74
  llm, position, who = MissAIChina, position_2, 'Miss AI China'
75
- voice = "en-US-MichelleNeural" # Giọng 2 tiếng Anh Anh
76
  else:
77
  turn = "Miss AI Vietnam"
78
  llm, position, who = MissAIVietnam, position_1, "Miss AI Vietnam"
@@ -84,11 +90,15 @@ def next_turn(topic, position_1, position_2, current_history):
84
 
85
  # Chuyển văn bản thành âm thanh với giọng tương ứng
86
  output_file = asyncio.run(text_to_speech(response, voice))
87
- return f"It's now {turn}'s turn.", history, output_file
 
 
 
 
88
 
89
  # Giao diện Gradio
90
  with gr.Blocks(theme=gr.themes.Soft(font=[gr.themes.GoogleFont("Roboto Mono")])) as demo:
91
- gr.Markdown("# Welcome to The Debate Master 🗣️🤖")
92
 
93
  with gr.Row():
94
  with gr.Column(scale=1):
@@ -100,17 +110,17 @@ with gr.Blocks(theme=gr.themes.Soft(font=[gr.themes.GoogleFont("Roboto Mono")]))
100
  status_output = gr.Textbox(label="Status", interactive=False)
101
  with gr.Column(scale=2):
102
  chatbot = gr.Chatbot(label="Debate Arena", height=500)
103
- audio_output = gr.Audio(label="Audio", autoplay=True)
104
 
105
  start_button.click(
106
  fn=start_debate,
107
  inputs=[topic_input, position_1_input, position_2_input],
108
- outputs=[status_output, chatbot, audio_output],
109
  )
110
  next_button.click(
111
  fn=next_turn,
112
  inputs=[topic_input, position_1_input, position_2_input, chatbot],
113
- outputs=[status_output, chatbot, audio_output],
114
  )
115
 
116
  if __name__ == "__main__":
 
15
  position_2 = None
16
  turn = None
17
  history = []
18
+ audio_files = [] # Danh sách lưu trữ các file audio
19
 
20
  # Hàm để tạo phản hồi tranh luận
21
  def generate_response(llm, position, who, topic, message):
 
45
 
46
  # Hàm để bắt đầu tranh luận giữa Miss AI Vietnam và Miss AI China
47
  def start_debate(topic, position_1, position_2):
48
+ global turn, history, audio_files
49
  if not topic or not position_1 or not position_2:
50
+ return "Please provide the debate topic and positions for both participants.", [], []
51
 
52
  # Đảm bảo các vị trí là đối lập
53
  if position_1 == position_2:
54
+ return "The positions of both participants must be opposite. Please adjust them.", [], []
55
 
56
  turn = "Miss AI Vietnam"
57
  history = [] # Đặt lại lịch sử
58
+ audio_files = [] # Đặt lại danh sách file audio
59
  initial_message = "Opening Statement"
60
  response = generate_response(MissAIVietnam, position_1, 'Miss AI Vietnam', topic, initial_message)
61
  history.append((initial_message, response))
62
 
63
  # Chuyển văn bản thành âm thanh với giọng của Miss AI Vietnam
64
  output_file = asyncio.run(text_to_speech(response, "en-US-JennyNeural")) # Giọng nữ tiếng Anh Mỹ
65
+ audio_files.append(output_file) # Thêm file audio vào danh sách
66
+
67
+ # Tạo danh sách link audio
68
+ audio_links = [f"[Turn {i+1}]({file})" for i, file in enumerate(audio_files)]
69
+ return f"The debate has started! {turn} begins.", history, audio_links
70
 
71
  # Hàm để chuyển lượt trong tranh luận
72
  def next_turn(topic, position_1, position_2, current_history):
73
+ global turn, history, audio_files
74
  if not current_history:
75
+ return "No ongoing debate. Please start a debate first.", [], []
76
 
77
  # Logic chuyển lượt
78
  if turn == "Miss AI Vietnam":
79
  turn = "Miss AI China"
80
  llm, position, who = MissAIChina, position_2, 'Miss AI China'
81
+ voice = "en-AU-NatashaNeural" # Giọng nữ tiếng Anh Anh
82
  else:
83
  turn = "Miss AI Vietnam"
84
  llm, position, who = MissAIVietnam, position_1, "Miss AI Vietnam"
 
90
 
91
  # Chuyển văn bản thành âm thanh với giọng tương ứng
92
  output_file = asyncio.run(text_to_speech(response, voice))
93
+ audio_files.append(output_file) # Thêm file audio vào danh sách
94
+
95
+ # Tạo danh sách link audio
96
+ audio_links = [f"[Turn {i+1}]({file})" for i, file in enumerate(audio_files)]
97
+ return f"It's now {turn}'s turn.", history, audio_links
98
 
99
  # Giao diện Gradio
100
  with gr.Blocks(theme=gr.themes.Soft(font=[gr.themes.GoogleFont("Roboto Mono")])) as demo:
101
+ gr.Markdown("# Welcome to MISS AI WORLD 🗣️🤖")
102
 
103
  with gr.Row():
104
  with gr.Column(scale=1):
 
110
  status_output = gr.Textbox(label="Status", interactive=False)
111
  with gr.Column(scale=2):
112
  chatbot = gr.Chatbot(label="Debate Arena", height=500)
113
+ audio_links_output = gr.Markdown(label="Audio Links") # Hiển thị link audio
114
 
115
  start_button.click(
116
  fn=start_debate,
117
  inputs=[topic_input, position_1_input, position_2_input],
118
+ outputs=[status_output, chatbot, audio_links_output],
119
  )
120
  next_button.click(
121
  fn=next_turn,
122
  inputs=[topic_input, position_1_input, position_2_input, chatbot],
123
+ outputs=[status_output, chatbot, audio_links_output],
124
  )
125
 
126
  if __name__ == "__main__":