TDN-M commited on
Commit
f072d45
·
verified ·
1 Parent(s): 2b48802

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -18
app.py CHANGED
@@ -5,6 +5,30 @@ import datetime
5
  import edge_tts
6
  import asyncio
7
  import os
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
 
9
  # Khởi tạo các mô hình từ Hugging Face
10
  MissAIVietnam = InferenceClient("mistralai/Mixtral-8x7B-Instruct-v0.1")
@@ -79,10 +103,13 @@ def start_debate(topic, position_1, position_2):
79
  history.append((initial_message, response))
80
 
81
  # Chuyển văn bản thành âm thanh với giọng của Miss AI Vietnam
82
- output_file = asyncio.run(text_to_speech(response, "en-US-JennyNeural")) # Giọng nữ tiếng Anh Mỹ
83
- audio_files.append(output_file) # Thêm file audio vào danh sách
84
 
85
- return f"The debate has started! {turn} begins.", history, output_file
 
 
 
86
 
87
  # Hàm để chuyển lượt trong tranh luận
88
  def next_turn(topic, position_1, position_2, current_history):
@@ -105,20 +132,13 @@ def next_turn(topic, position_1, position_2, current_history):
105
  history.append(("", response)) # Thêm phản hồi vào lịch sử
106
 
107
  # Chuyển văn bản thành âm thanh với giọng tương ứng
108
- output_file = asyncio.run(text_to_speech(response, voice))
109
- audio_files.append(output_file) # Thêm file audio vào danh sách
110
-
111
- return f"It's now {turn}'s turn.", history, output_file
112
 
113
- # Hàm để kết thúc tranh luận nối các file audio
114
- def end_debate():
115
- global audio_files
116
- if not audio_files:
117
- return "No debate audio found.", None
118
 
119
- # Nối các file audio thành một file duy nhất
120
- final_audio_file = concatenate_audio_files(audio_files)
121
- return "The debate has ended. Here is the full debate audio.", final_audio_file
122
 
123
  # Giao diện Gradio
124
  with gr.Blocks(theme=gr.themes.Soft(font=[gr.themes.GoogleFont("Roboto Mono")])) as demo:
@@ -135,18 +155,19 @@ 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
- audio_output = gr.Audio(label="Debate Audio", autoplay=True) # Hiển thị audio hiện tại
 
139
  final_audio_output = gr.Audio(label="Full Debate Audio", visible=False) # Hiển thị audio cuối cùng
140
 
141
  start_button.click(
142
  fn=start_debate,
143
  inputs=[topic_input, position_1_input, position_2_input],
144
- outputs=[status_output, chatbot, audio_output],
145
  )
146
  next_button.click(
147
  fn=next_turn,
148
  inputs=[topic_input, position_1_input, position_2_input, chatbot],
149
- outputs=[status_output, chatbot, audio_output],
150
  )
151
  end_button.click(
152
  fn=end_debate,
 
5
  import edge_tts
6
  import asyncio
7
  import os
8
+ import subprocess
9
+
10
+ def create_video(audio_file, turn, output_video="output.mp4"):
11
+ # Chọn file video nền dựa trên lượt
12
+ background_video = "missVN.mp4" if turn == "Miss AI Vietnam" else "missCN.mp4"
13
+
14
+ # Lệnh ffmpeg để tạo video
15
+ command = [
16
+ "ffmpeg",
17
+ "-stream_loop", "-1", # Lặp lại video nền vô hạn
18
+ "-i", background_video, # Video nền (tùy thuộc vào lượt)
19
+ "-i", audio_file, # File audio đầu vào
20
+ "-vf", "drawtext=text='MISS AI':fontcolor=white:fontsize=100:fontfile=/path/to/font.ttf:x=10:y=10", # Văn bản trên video
21
+ "-t", "45.7", # Thời lượng video
22
+ "-c:v", "libx264", # Codec video
23
+ "-c:a", "aac", # Codec audio
24
+ "-shortest", # Dừng khi audio kết thúc
25
+ "-y", # Ghi đè file nếu tồn tại
26
+ output_video # File video đầu ra
27
+ ]
28
+
29
+ # Chạy lệnh ffmpeg
30
+ subprocess.run(command, check=True)
31
+ return output_video
32
 
33
  # Khởi tạo các mô hình từ Hugging Face
34
  MissAIVietnam = InferenceClient("mistralai/Mixtral-8x7B-Instruct-v0.1")
 
103
  history.append((initial_message, response))
104
 
105
  # Chuyển văn bản thành âm thanh với giọng của Miss AI Vietnam
106
+ output_audio = asyncio.run(text_to_speech(response, "en-US-JennyNeural")) # Giọng nữ tiếng Anh Mỹ
107
+ audio_files.append(output_audio) # Thêm file audio vào danh sách
108
 
109
+ # Tạo video từ audio lượt hiện tại
110
+ output_video = create_video(output_audio, turn)
111
+
112
+ return f"The debate has started! {turn} begins.", history, output_video
113
 
114
  # Hàm để chuyển lượt trong tranh luận
115
  def next_turn(topic, position_1, position_2, current_history):
 
132
  history.append(("", response)) # Thêm phản hồi vào lịch sử
133
 
134
  # Chuyển văn bản thành âm thanh với giọng tương ứng
135
+ output_audio = asyncio.run(text_to_speech(response, voice))
136
+ audio_files.append(output_audio) # Thêm file audio vào danh sách
 
 
137
 
138
+ # Tạo video từ audiolượt hiện tại
139
+ output_video = create_video(output_audio, turn)
 
 
 
140
 
141
+ return f"It's now {turn}'s turn.", history, output_video
 
 
142
 
143
  # Giao diện Gradio
144
  with gr.Blocks(theme=gr.themes.Soft(font=[gr.themes.GoogleFont("Roboto Mono")])) as demo:
 
155
  status_output = gr.Textbox(label="Status", interactive=False)
156
  with gr.Column(scale=2):
157
  chatbot = gr.Chatbot(label="Debate Arena", height=500)
158
+ video_output = gr.Video(label="Debate Video", autoplay=True) # Hiển thị video
159
+ audio_output = gr.Audio(label="Debate Audio", autoplay=None) # Hiển thị audio hiện tại
160
  final_audio_output = gr.Audio(label="Full Debate Audio", visible=False) # Hiển thị audio cuối cùng
161
 
162
  start_button.click(
163
  fn=start_debate,
164
  inputs=[topic_input, position_1_input, position_2_input],
165
+ outputs=[status_output, chatbot, video_output, audio_output],
166
  )
167
  next_button.click(
168
  fn=next_turn,
169
  inputs=[topic_input, position_1_input, position_2_input, chatbot],
170
+ outputs=[status_output, chatbot, video_output, audio_output],
171
  )
172
  end_button.click(
173
  fn=end_debate,