import gradio as gr from gradio_client import Client # 初始化 Hugging Face Space 的 Client client = Client("Qwen/Qwen2.5-72B-Instruct") history = [] def respond(prompt, fhistory): global history messages = [{"role": "system", "content": "You are a helpful assistant."}] for user_text, assistant_text in history: messages.append({"role": "user", "content": user_text}) messages.append({"role": "assistant", "content": assistant_text}) messages.append({"role": "user", "content": prompt}) result = client.predict( query=messages, history=history, system="You are a helpful assistant.", api_name="/model_chat" ) print(result) response = result[1][-1][1] # 獲取最新的 AI 回應 history.append((prompt, response)) # **回傳符合 ChatInterface 格式** #return [{"role": "user", "content": prompt}, {"role": "assistant", "content": response}] return [{"role": "assistant", "content": response}] # 設定 Gradio 的聊天界面 demo = gr.ChatInterface( fn=respond, title="Qwen2.5-72B-Instruct Demo", description="透過 Hugging Face Space API 與 Qwen 2.5 Max 模型互動。", type='messages' ) if __name__ == "__main__": demo.launch()