ML_HF_OpenAPI / app.py
LymanML's picture
25
95bd976
raw
history blame contribute delete
No virus
906 Bytes
import gradio as gr
from gradio_client import Client
from typing import List, Optional, Tuple, Dict
# 创建Client实例,指定API的URL
client = Client("Qwen/Qwen2-72B-Instruct")
# 默认系统提示
default_system = "You are a helpful assistant."
api_name = "/model_chat_1"
# 清空聊天历史的函数
def clear_session():
return [], default_system
History = List[Tuple[str, str]]
# 模型对话函数
def model_chat(query: Optional[str], chat_history: Optional[History], system: str) -> Tuple[str, str, History]:
# 调用API接口
if not chat_history:
chat_history = []
if not query:
query = ''
result = client.predict(
query=query,
history=chat_history,
system=default_system,
api_name=api_name
)
# 更新聊天历史
new_round = [query, result]
chat_history.append(new_round)
return result, chat_history