Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,16 +1,26 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from huggingface_hub import InferenceClient
|
|
|
|
| 3 |
|
| 4 |
"""
|
| 5 |
-
For more information on `huggingface_hub` Inference API support, please check the docs:
|
|
|
|
| 6 |
"""
|
| 7 |
|
| 8 |
# 定义多个模型及其对应的InferenceClient
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
MODEL_CLIENTS = {
|
| 10 |
-
"Zephyr 7B Beta": InferenceClient("HuggingFaceH4/zephyr-7b-beta"),
|
| 11 |
-
"Mistral 7B Instruct v0.2": InferenceClient("mistralai/Mistral-7B-Instruct-v0.2"),
|
| 12 |
-
#
|
| 13 |
-
# "
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
}
|
| 15 |
|
| 16 |
def respond(
|
|
@@ -26,60 +36,105 @@ def respond(
|
|
| 26 |
# 根据选择的模型名称获取对应的client
|
| 27 |
client = MODEL_CLIENTS.get(selected_model_name)
|
| 28 |
if not client:
|
| 29 |
-
|
|
|
|
|
|
|
| 30 |
|
| 31 |
messages = [{"role": "system", "content": system_message}]
|
| 32 |
|
|
|
|
| 33 |
for val in history:
|
| 34 |
-
if val[0]:
|
| 35 |
messages.append({"role": "user", "content": val[0]})
|
| 36 |
-
if val[1]:
|
| 37 |
messages.append({"role": "assistant", "content": val[1]})
|
| 38 |
|
| 39 |
-
messages.append({"role": "user", "content": message})
|
| 40 |
|
| 41 |
response = ""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
-
#
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
response
|
| 53 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
|
| 55 |
|
| 56 |
"""
|
| 57 |
-
For information on how to customize the ChatInterface, peruse the gradio docs:
|
|
|
|
| 58 |
"""
|
| 59 |
demo = gr.ChatInterface(
|
| 60 |
respond,
|
| 61 |
additional_inputs=[
|
| 62 |
-
gr.Textbox(value="
|
| 63 |
-
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
| 64 |
-
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 65 |
gr.Slider(
|
| 66 |
minimum=0.1,
|
| 67 |
maximum=1.0,
|
| 68 |
value=0.95,
|
| 69 |
step=0.05,
|
| 70 |
-
label="Top-p (
|
| 71 |
),
|
| 72 |
# 新增一个Dropdown用于选择模型
|
| 73 |
gr.Dropdown(
|
| 74 |
list(MODEL_CLIENTS.keys()), # 选项为MODEL_CLIENTS的键(模型名称)
|
| 75 |
value=list(MODEL_CLIENTS.keys())[0], # 默认选中第一个模型
|
| 76 |
-
label="Select Model",
|
| 77 |
interactive=True, # 允许用户更改
|
| 78 |
),
|
| 79 |
],
|
| 80 |
-
#
|
| 81 |
-
#
|
| 82 |
-
|
|
|
|
|
|
|
| 83 |
)
|
| 84 |
|
| 85 |
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from huggingface_hub import InferenceClient, HfHubHTTPError, InferenceTimeoutError
|
| 3 |
+
import httpx # 确保这个库在requirements.txt中
|
| 4 |
|
| 5 |
"""
|
| 6 |
+
For more information on `huggingface_hub` Inference API support, please check the docs:
|
| 7 |
+
https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
|
| 8 |
"""
|
| 9 |
|
| 10 |
# 定义多个模型及其对应的InferenceClient
|
| 11 |
+
# !!重要!! 如果需要,请替换 'hf_YOUR_TOKEN_HERE' 为您的Hugging Face API Token。
|
| 12 |
+
# API Token可以在 https://huggingface.co/settings/tokens 生成。
|
| 13 |
+
# 使用API Token可以帮助解决一些访问限制问题,特别是对于热门模型。
|
| 14 |
+
# 同时,请确保您已经在Hugging Face网站上同意了Mistral 7B Instruct v0.2等模型的条款。
|
| 15 |
MODEL_CLIENTS = {
|
| 16 |
+
"Zephyr 7B Beta": InferenceClient("HuggingFaceH4/zephyr-7b-beta"), # 默认使用API Token
|
| 17 |
+
"Mistral 7B Instruct v0.2": InferenceClient("mistralai/Mistral-7B-Instruct-v0.2"), # 默认使用API Token
|
| 18 |
+
# 如果您需要使用API Token,可以这样写:
|
| 19 |
+
# "Zephyr 7B Beta": InferenceClient("HuggingFaceH4/zephyr-7b-beta", token="hf_YOUR_TOKEN_HERE"),
|
| 20 |
+
# "Mistral 7B Instruct v0.2": InferenceClient("mistralai/Mistral-7B-Instruct-v0.2", token="hf_YOUR_TOKEN_HERE"),
|
| 21 |
+
# 更多模型示例(请根据需要添加或删除):
|
| 22 |
+
# "Llama 2 7B Chat": InferenceClient("meta-llama/Llama-2-7b-chat-hf", token="hf_YOUR_TOKEN_HERE"), # Llama 2通常需要访问权限和API Token
|
| 23 |
+
# "OpenHermes 2.5 Mistral 7B": InferenceClient("teknium/OpenHermes-2.5-Mistral-7B"),
|
| 24 |
}
|
| 25 |
|
| 26 |
def respond(
|
|
|
|
| 36 |
# 根据选择的模型名称获取对应的client
|
| 37 |
client = MODEL_CLIENTS.get(selected_model_name)
|
| 38 |
if not client:
|
| 39 |
+
# 如果模型名称无效,直接返回错误信息
|
| 40 |
+
yield "错误:未找到选定的模型客户端。请检查模型名称是否正确或已添加到列表中。"
|
| 41 |
+
return
|
| 42 |
|
| 43 |
messages = [{"role": "system", "content": system_message}]
|
| 44 |
|
| 45 |
+
# 构建完整的对话历史
|
| 46 |
for val in history:
|
| 47 |
+
if val[0]: # 用户消息
|
| 48 |
messages.append({"role": "user", "content": val[0]})
|
| 49 |
+
if val[1]: # 助手消息
|
| 50 |
messages.append({"role": "assistant", "content": val[1]})
|
| 51 |
|
| 52 |
+
messages.append({"role": "user", "content": message}) # 添加当前用户消息
|
| 53 |
|
| 54 |
response = ""
|
| 55 |
+
try:
|
| 56 |
+
# 使用选定的client进行推理
|
| 57 |
+
# client.chat_completion() 默认是一个生成器,用于流式传输
|
| 58 |
+
for message_chunk in client.chat_completion(
|
| 59 |
+
messages,
|
| 60 |
+
max_tokens=max_tokens,
|
| 61 |
+
stream=True, # 启用流式传输
|
| 62 |
+
temperature=temperature,
|
| 63 |
+
top_p=top_p,
|
| 64 |
+
):
|
| 65 |
+
# 确保 chunk 和 content 存在,以防API响应格式异常
|
| 66 |
+
if message_chunk.choices and message_chunk.choices[0].delta and message_chunk.choices[0].delta.content is not None:
|
| 67 |
+
token = message_chunk.choices[0].delta.content
|
| 68 |
+
response += token
|
| 69 |
+
yield response # 逐步返回生成的文本
|
| 70 |
+
else:
|
| 71 |
+
# 可能是流的末尾,或者是一个空的内容块
|
| 72 |
+
pass
|
| 73 |
|
| 74 |
+
# 错误处理:捕获可能出现的各种异常
|
| 75 |
+
except HfHubHTTPError as e:
|
| 76 |
+
error_message = ""
|
| 77 |
+
if e.response.status_code == 402:
|
| 78 |
+
error_message = "抱歉,此模型服务需要付费访问或您的Hugging Face账户额度已用尽。请检查您的Hugging Face账户设置或Space日志。"
|
| 79 |
+
elif e.response.status_code == 429:
|
| 80 |
+
error_message = "抱歉,请求过于频繁,触发了速率限制。请稍后再试,或考虑使用API Token提升额度。"
|
| 81 |
+
elif e.response.status_code == 401 or e.response.status_code == 403:
|
| 82 |
+
error_message = "抱歉,模型访问权限不足或API Token无效/缺失。请确保您已在Hugging Face上登录,接受模型条款,并正确配置API Token。"
|
| 83 |
+
elif e.response.status_code == 503:
|
| 84 |
+
error_message = "模型服务当前不可用,可能正在加载或维护中。请稍后再试。"
|
| 85 |
+
else:
|
| 86 |
+
error_message = f"模型服务出现HTTP错误 ({e.response.status_code}):{e.response.text}。请检查Hugging Face Space日志。"
|
| 87 |
+
print(f"HfHubHTTPError: {e}") # 打印到控制台以供调试
|
| 88 |
+
yield error_message # 将错误信息显示给用户
|
| 89 |
+
|
| 90 |
+
except InferenceTimeoutError as e:
|
| 91 |
+
error_message = "模型响应超时,可能是请求过于复杂或服务器繁忙。请尝试减少'Max new tokens'或稍后再试。"
|
| 92 |
+
print(f"InferenceTimeoutError: {e}")
|
| 93 |
+
yield error_message
|
| 94 |
+
|
| 95 |
+
except httpx.HTTPStatusError as e:
|
| 96 |
+
# 这是httpx库抛出的HTTP错误,可能发生在HfHubHTTPError之外
|
| 97 |
+
error_message = f"与Hugging Face服务通信时发生HTTP错误 ({e.response.status_code}):{e.response.text}。"
|
| 98 |
+
print(f"HTTPStatusError: {e}")
|
| 99 |
+
yield error_message
|
| 100 |
+
|
| 101 |
+
except Exception as e:
|
| 102 |
+
# 捕获所有其他未预期的错误
|
| 103 |
+
error_message = f"发生未知错误:{type(e).__name__} - {e}。请查看Hugging Face Space日志了解更多详情。"
|
| 104 |
+
print(f"General Error: {e}")
|
| 105 |
+
yield error_message
|
| 106 |
|
| 107 |
|
| 108 |
"""
|
| 109 |
+
For information on how to customize the ChatInterface, peruse the gradio docs:
|
| 110 |
+
https://www.gradio.app/docs/chatinterface
|
| 111 |
"""
|
| 112 |
demo = gr.ChatInterface(
|
| 113 |
respond,
|
| 114 |
additional_inputs=[
|
| 115 |
+
gr.Textbox(value="你是一个友好的AI助手,尽力提供帮助。", label="系统消息 (System message)"),
|
| 116 |
+
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="最大生成token数 (Max new tokens)"),
|
| 117 |
+
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="随机性 (Temperature)"),
|
| 118 |
gr.Slider(
|
| 119 |
minimum=0.1,
|
| 120 |
maximum=1.0,
|
| 121 |
value=0.95,
|
| 122 |
step=0.05,
|
| 123 |
+
label="Top-p (核心采样)",
|
| 124 |
),
|
| 125 |
# 新增一个Dropdown用于选择模型
|
| 126 |
gr.Dropdown(
|
| 127 |
list(MODEL_CLIENTS.keys()), # 选项为MODEL_CLIENTS的键(模型名称)
|
| 128 |
value=list(MODEL_CLIENTS.keys())[0], # 默认选中第一个模型
|
| 129 |
+
label="选择语言模型 (Select Model)",
|
| 130 |
interactive=True, # 允许用户更改
|
| 131 |
),
|
| 132 |
],
|
| 133 |
+
title="多模型AI聊天助手", # 给界面添加一个标题
|
| 134 |
+
description="选择一个语言模型,开始与AI对话。您可以调整参数或切换模型进行比较。", # 添加描述
|
| 135 |
+
submit_btn="发送",
|
| 136 |
+
stop_btn="停止",
|
| 137 |
+
clear_btn="清空对话",
|
| 138 |
)
|
| 139 |
|
| 140 |
|