from huggingface_hub import InferenceClient import gradio as gr import datetime import edge_tts import asyncio import os # Khởi tạo các mô hình từ Hugging Face MissAIVietnam = InferenceClient("mistralai/Mixtral-8x7B-Instruct-v0.1") MissAIChina = InferenceClient("mistralai/Mixtral-8x7B-Instruct-v0.1") # Biến toàn cục để lưu trữ chủ đề, vị trí và lịch sử tranh luận topic = None position_1 = None position_2 = None turn = None history = [] audio_files = [] # Danh sách lưu trữ các file audio # Hàm để tạo phản hồi tranh luận def generate_response(llm, position, who, topic, message): system_message = { "role": "system", "content": f"You are a debate participant tasked with defending the position '{position}' on the topic '{topic}'. Your goal is to articulate your arguments with clarity, logic, and professionalism while addressing counterpoints made by the opposing side. " f"Ensure that your responses are thoughtful, evidence-based, and persuasive. Strictly keep them concise—aim for responses that are 4 to 5 lines in a single paragraph. " f"During the debate, if the user presents arguments challenging your stance, analyze their points critically and provide respectful but firm counterarguments. " f"Stay consistent with your assigned position ('{position}') and maintain a respectful, formal tone throughout." } messages = [system_message] messages.append({"role": "user", "content": message}) response = f"{who}:\n" for message_chunk in llm.chat_completion( messages, max_tokens=128, stream=True, temperature=0.4, top_p=0.95): response += message_chunk.choices[0].delta.content return response # Hàm để chuyển văn bản thành âm thanh bằng edge_tts async def text_to_speech(text, voice, output_file="output.mp3"): communicate = edge_tts.Communicate(text, voice) # Chọn giọng nói await communicate.save(output_file) return output_file # Hàm để bắt đầu tranh luận giữa Miss AI Vietnam và Miss AI China def start_debate(topic, position_1, position_2): global turn, history, audio_files if not topic or not position_1 or not position_2: return "Please provide the debate topic and positions for both participants.", [], None # Đảm bảo các vị trí là đối lập if position_1 == position_2: return "The positions of both participants must be opposite. Please adjust them.", [], None turn = "Miss AI Vietnam" history = [] # Đặt lại lịch sử audio_files = [] # Đặt lại danh sách file audio initial_message = "Opening Statement" response = generate_response(MissAIVietnam, position_1, 'Miss AI Vietnam', topic, initial_message) history.append((initial_message, response)) # Chuyển văn bản thành âm thanh với giọng của Miss AI Vietnam output_file = asyncio.run(text_to_speech(response, "en-US-JennyNeural")) # Giọng nữ tiếng Anh Mỹ audio_files.append(output_file) # Thêm file audio vào danh sách return f"The debate has started! {turn} begins.", history, output_file # Hàm để chuyển lượt trong tranh luận def next_turn(topic, position_1, position_2, current_history): global turn, history, audio_files if not current_history: return "No ongoing debate. Please start a debate first.", [], None # Logic chuyển lượt if turn == "Miss AI Vietnam": turn = "Miss AI China" llm, position, who = MissAIChina, position_2, 'Miss AI China' voice = "en-GB-LibbyNeural" # Giọng nữ tiếng Anh Anh else: turn = "Miss AI Vietnam" llm, position, who = MissAIVietnam, position_1, "Miss AI Vietnam" voice = "en-US-JennyNeural" # Giọng nữ tiếng Anh Mỹ last_response = current_history[-1][1] # Lấy tin nhắn cuối cùng response = generate_response(llm, position, who, topic, last_response) history.append(("", response)) # Thêm phản hồi vào lịch sử # Chuyển văn bản thành âm thanh với giọng tương ứng output_file = asyncio.run(text_to_speech(response, voice)) audio_files.append(output_file) # Thêm file audio vào danh sách return f"It's now {turn}'s turn.", history, output_file # Giao diện Gradio with gr.Blocks(theme=gr.themes.Soft(font=[gr.themes.GoogleFont("Roboto Mono")])) as demo: gr.Markdown("# Welcome to The Debate Master 🗣️🤖") with gr.Row(): with gr.Column(scale=1): topic_input = gr.Textbox(label="STEP 1: Debate Topic", placeholder="Enter the debate topic") position_1_input = gr.Radio(["For", "Against"], label="STEP 2: Miss AI Vietnam's Position") position_2_input = gr.Radio(["For", "Against"], label="STEP 3: Miss AI China's Position") start_button = gr.Button("STEP 4: Start", variant='primary') next_button = gr.Button("Next Turn") status_output = gr.Textbox(label="Status", interactive=False) with gr.Column(scale=2): chatbot = gr.Chatbot(label="Debate Arena", height=500) audio_output = gr.Audio(label="Debate Audio", autoplay=True) # Thêm thành phần Audio start_button.click( fn=start_debate, inputs=[topic_input, position_1_input, position_2_input], outputs=[status_output, chatbot, audio_output], # Trả về audio ) next_button.click( fn=next_turn, inputs=[topic_input, position_1_input, position_2_input, chatbot], outputs=[status_output, chatbot, audio_output], # Trả về audio ) if __name__ == "__main__": demo.launch(share=True)