Update app.py
Browse files
app.py
CHANGED
@@ -47,11 +47,11 @@ async def text_to_speech(text, voice, output_file="output.mp3"):
|
|
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ử
|
@@ -64,40 +64,13 @@ def start_debate(topic, position_1, position_2):
|
|
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 |
-
|
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 để bắt đầu tranh luận giữa Miss AI Vietnam và Miss AI China
|
72 |
-
def start_debate(topic, position_1, position_2):
|
73 |
-
global turn, history, audio_files
|
74 |
-
if not topic or not position_1 or not position_2:
|
75 |
-
return "Please provide the debate topic and positions for both participants.", [], ""
|
76 |
-
|
77 |
-
# Đảm bảo các vị trí là đối lập
|
78 |
-
if position_1 == position_2:
|
79 |
-
return "The positions of both participants must be opposite. Please adjust them.", [], ""
|
80 |
-
|
81 |
-
turn = "Miss AI Vietnam"
|
82 |
-
history = [] # Đặt lại lịch sử
|
83 |
-
audio_files = [] # Đặt lại danh sách file audio
|
84 |
-
initial_message = "Opening Statement"
|
85 |
-
response = generate_response(MissAIVietnam, position_1, 'Miss AI Vietnam', topic, initial_message)
|
86 |
-
history.append((initial_message, response))
|
87 |
-
|
88 |
-
# Chuyển văn bản thành âm thanh với giọng của Miss AI Vietnam
|
89 |
-
output_file = asyncio.run(text_to_speech(response, "en-US-JennyNeural")) # Giọng nữ tiếng Anh Mỹ
|
90 |
-
audio_files.append(output_file) # Thêm file audio vào danh sách
|
91 |
-
|
92 |
-
# Tạo chuỗi liên kết audio
|
93 |
-
audio_links = "\n".join([f"[Turn {i+1}]({file})" for i, file in enumerate(audio_files)])
|
94 |
-
return f"The debate has started! {turn} begins.", history, audio_links
|
95 |
|
96 |
# Hàm để chuyển lượt trong tranh luận
|
97 |
def next_turn(topic, position_1, position_2, current_history):
|
98 |
global turn, history, audio_files
|
99 |
if not current_history:
|
100 |
-
return "No ongoing debate. Please start a debate first.", [],
|
101 |
|
102 |
# Logic chuyển lượt
|
103 |
if turn == "Miss AI Vietnam":
|
@@ -117,13 +90,11 @@ def next_turn(topic, position_1, position_2, current_history):
|
|
117 |
output_file = asyncio.run(text_to_speech(response, voice))
|
118 |
audio_files.append(output_file) # Thêm file audio vào danh sách
|
119 |
|
120 |
-
|
121 |
-
audio_links = "\n".join([f"[Turn {i+1}]({file})" for i, file in enumerate(audio_files)])
|
122 |
-
return f"It's now {turn}'s turn.", history, audio_links
|
123 |
|
124 |
# Giao diện Gradio
|
125 |
with gr.Blocks(theme=gr.themes.Soft(font=[gr.themes.GoogleFont("Roboto Mono")])) as demo:
|
126 |
-
gr.Markdown("# Welcome to
|
127 |
|
128 |
with gr.Row():
|
129 |
with gr.Column(scale=1):
|
@@ -135,17 +106,17 @@ with gr.Blocks(theme=gr.themes.Soft(font=[gr.themes.GoogleFont("Roboto Mono")]))
|
|
135 |
status_output = gr.Textbox(label="Status", interactive=False)
|
136 |
with gr.Column(scale=2):
|
137 |
chatbot = gr.Chatbot(label="Debate Arena", height=500)
|
138 |
-
|
139 |
|
140 |
start_button.click(
|
141 |
fn=start_debate,
|
142 |
inputs=[topic_input, position_1_input, position_2_input],
|
143 |
-
outputs=[status_output, chatbot,
|
144 |
)
|
145 |
next_button.click(
|
146 |
fn=next_turn,
|
147 |
inputs=[topic_input, position_1_input, position_2_input, chatbot],
|
148 |
-
outputs=[status_output, chatbot,
|
149 |
)
|
150 |
|
151 |
if __name__ == "__main__":
|
|
|
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.", [], None
|
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.", [], None
|
55 |
|
56 |
turn = "Miss AI Vietnam"
|
57 |
history = [] # Đặt lại lịch sử
|
|
|
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 |
+
return f"The debate has started! {turn} begins.", history, output_file
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
68 |
|
69 |
# Hàm để chuyển lượt trong tranh luận
|
70 |
def next_turn(topic, position_1, position_2, current_history):
|
71 |
global turn, history, audio_files
|
72 |
if not current_history:
|
73 |
+
return "No ongoing debate. Please start a debate first.", [], None
|
74 |
|
75 |
# Logic chuyển lượt
|
76 |
if turn == "Miss AI Vietnam":
|
|
|
90 |
output_file = asyncio.run(text_to_speech(response, voice))
|
91 |
audio_files.append(output_file) # Thêm file audio vào danh sách
|
92 |
|
93 |
+
return f"It's now {turn}'s turn.", history, output_file
|
|
|
|
|
94 |
|
95 |
# Giao diện Gradio
|
96 |
with gr.Blocks(theme=gr.themes.Soft(font=[gr.themes.GoogleFont("Roboto Mono")])) as demo:
|
97 |
+
gr.Markdown("# Welcome to The Debate Master 🗣️🤖")
|
98 |
|
99 |
with gr.Row():
|
100 |
with gr.Column(scale=1):
|
|
|
106 |
status_output = gr.Textbox(label="Status", interactive=False)
|
107 |
with gr.Column(scale=2):
|
108 |
chatbot = gr.Chatbot(label="Debate Arena", height=500)
|
109 |
+
audio_output = gr.Audio(label="Debate Audio", autoplay=True) # Thêm thành phần Audio
|
110 |
|
111 |
start_button.click(
|
112 |
fn=start_debate,
|
113 |
inputs=[topic_input, position_1_input, position_2_input],
|
114 |
+
outputs=[status_output, chatbot, audio_output], # Trả về audio
|
115 |
)
|
116 |
next_button.click(
|
117 |
fn=next_turn,
|
118 |
inputs=[topic_input, position_1_input, position_2_input, chatbot],
|
119 |
+
outputs=[status_output, chatbot, audio_output], # Trả về audio
|
120 |
)
|
121 |
|
122 |
if __name__ == "__main__":
|