|
|
import gradio as gr |
|
|
import requests |
|
|
import os |
|
|
import json |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
HF_TOKEN = os.getenv("HF_TOKEN") |
|
|
API_URL = "https://api-inference.huggingface.co/models/badanwang/teacher_basic_qwen3-0.6b" |
|
|
|
|
|
|
|
|
def predict(message, history): |
|
|
""" |
|
|
主函数,用于与Hugging Face Inference API进行流式对话。 |
|
|
:param message: 用户当前发送的消息 (str) |
|
|
:param history: 对话历史 (list of lists),格式为 [[user_msg, assistant_msg], ...] |
|
|
:return: 一个生成器 (generator),逐字(token)返回模型的响应 |
|
|
""" |
|
|
if not HF_TOKEN: |
|
|
raise gr.Error("Hugging Face API Token 未配置!请在Space的Secrets中添加 HF_TOKEN。") |
|
|
|
|
|
headers = { |
|
|
"Authorization": f"Bearer {HF_TOKEN}", |
|
|
"Content-Type": "application/json" |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
messages = [] |
|
|
for turn in history: |
|
|
user_msg, assistant_msg = turn |
|
|
messages.append({"role": "user", "content": user_msg}) |
|
|
messages.append({"role": "assistant", "content": assistant_msg}) |
|
|
|
|
|
|
|
|
messages.append({"role": "user", "content": message}) |
|
|
|
|
|
|
|
|
|
|
|
payload = { |
|
|
"inputs": messages, |
|
|
"parameters": { |
|
|
"max_new_tokens": 2048, |
|
|
"temperature": 0.7, |
|
|
"top_p": 0.95, |
|
|
"repetition_penalty": 1.1, |
|
|
"return_full_text": False, |
|
|
}, |
|
|
"stream": True |
|
|
} |
|
|
|
|
|
|
|
|
full_response = "" |
|
|
try: |
|
|
|
|
|
with requests.post(API_URL, headers=headers, json=payload, stream=True, timeout=120) as response: |
|
|
|
|
|
response.raise_for_status() |
|
|
|
|
|
|
|
|
for line in response.iter_lines(): |
|
|
if line: |
|
|
|
|
|
decoded_line = line.decode('utf-8') |
|
|
if decoded_line.startswith("data:"): |
|
|
try: |
|
|
|
|
|
json_data = json.loads(decoded_line[5:]) |
|
|
|
|
|
token = json_data.get("token", {}).get("text", "") |
|
|
if token: |
|
|
full_response += token |
|
|
yield full_response |
|
|
except json.JSONDecodeError: |
|
|
|
|
|
continue |
|
|
|
|
|
except requests.exceptions.RequestException as e: |
|
|
print(f"API请求错误: {e}") |
|
|
yield f"抱歉,与模型API通信时发生错误: {e}" |
|
|
except Exception as e: |
|
|
print(f"发生未知错误: {e}") |
|
|
yield f"抱歉,发生了一个未知错误: {e}" |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
demo = gr.ChatInterface( |
|
|
fn=predict, |
|
|
title="小Q老师 - 基础问答", |
|
|
description="与 badanwang/teacher_basic_qwen3-0.6b 模型进行流式对话。直接输入问题开始。", |
|
|
examples=[["你好"], ["请用python写一个快速排序算法"], ["给我讲个笑话吧"]], |
|
|
cache_examples=False, |
|
|
) |
|
|
|
|
|
if __name__ == "__main__": |
|
|
|
|
|
demo.launch() |