Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,68 +2,181 @@ import gradio as gr
|
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
|
| 4 |
|
|
|
|
|
|
|
|
|
|
| 5 |
def respond(
|
| 6 |
-
message,
|
| 7 |
-
history: list[dict
|
| 8 |
-
system_message,
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
|
|
|
| 13 |
):
|
| 14 |
-
""
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
""
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
maximum=1.0,
|
| 55 |
value=0.95,
|
| 56 |
step=0.05,
|
| 57 |
-
label="Top-p
|
| 58 |
-
)
|
| 59 |
-
],
|
| 60 |
-
)
|
| 61 |
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
|
| 67 |
|
| 68 |
if __name__ == "__main__":
|
| 69 |
-
demo.
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
|
| 4 |
|
| 5 |
+
MODEL_ID = "openai/gpt-oss-20b"
|
| 6 |
+
|
| 7 |
+
|
| 8 |
def respond(
|
| 9 |
+
message: str,
|
| 10 |
+
history: list[dict],
|
| 11 |
+
system_message: str,
|
| 12 |
+
reasoning_effort: str,
|
| 13 |
+
max_tokens: int,
|
| 14 |
+
temperature: float,
|
| 15 |
+
top_p: float,
|
| 16 |
+
hf_token: gr.OAuthToken | None,
|
| 17 |
):
|
| 18 |
+
if hf_token is None or not getattr(hf_token, "token", None):
|
| 19 |
+
yield "请先点击左侧的“Sign in with Hugging Face”登录,然后再发送消息。"
|
| 20 |
+
return
|
| 21 |
+
|
| 22 |
+
client = InferenceClient(
|
| 23 |
+
model=MODEL_ID,
|
| 24 |
+
provider="auto",
|
| 25 |
+
token=hf_token.token,
|
| 26 |
+
)
|
| 27 |
+
|
| 28 |
+
messages = []
|
| 29 |
+
|
| 30 |
+
if system_message.strip():
|
| 31 |
+
messages.append(
|
| 32 |
+
{
|
| 33 |
+
"role": "system",
|
| 34 |
+
"content": system_message.strip(),
|
| 35 |
+
}
|
| 36 |
+
)
|
| 37 |
+
|
| 38 |
+
# Gradio messages 模式下,history 通常是:
|
| 39 |
+
# [{"role": "user", "content": "..."}, ...]
|
| 40 |
+
for item in history:
|
| 41 |
+
role = item.get("role")
|
| 42 |
+
content = item.get("content")
|
| 43 |
+
|
| 44 |
+
if role in {"user", "assistant", "system"} and isinstance(content, str):
|
| 45 |
+
messages.append(
|
| 46 |
+
{
|
| 47 |
+
"role": role,
|
| 48 |
+
"content": content,
|
| 49 |
+
}
|
| 50 |
+
)
|
| 51 |
+
|
| 52 |
+
messages.append(
|
| 53 |
+
{
|
| 54 |
+
"role": "user",
|
| 55 |
+
"content": message,
|
| 56 |
+
}
|
| 57 |
+
)
|
| 58 |
+
|
| 59 |
+
response_text = ""
|
| 60 |
+
|
| 61 |
+
try:
|
| 62 |
+
stream = client.chat_completion(
|
| 63 |
+
messages=messages,
|
| 64 |
+
max_tokens=int(max_tokens),
|
| 65 |
+
temperature=float(temperature),
|
| 66 |
+
top_p=float(top_p),
|
| 67 |
+
stream=True,
|
| 68 |
+
|
| 69 |
+
# 转交给兼容 OpenAI 风格的推理后端。
|
| 70 |
+
# 若当前 Provider 不接受该字段,删除 extra_body 即可。
|
| 71 |
+
extra_body={
|
| 72 |
+
"reasoning_effort": reasoning_effort,
|
| 73 |
+
},
|
| 74 |
+
)
|
| 75 |
+
|
| 76 |
+
for chunk in stream:
|
| 77 |
+
if not chunk.choices:
|
| 78 |
+
continue
|
| 79 |
+
|
| 80 |
+
delta = chunk.choices[0].delta
|
| 81 |
+
token = getattr(delta, "content", None)
|
| 82 |
+
|
| 83 |
+
if token:
|
| 84 |
+
response_text += token
|
| 85 |
+
yield response_text
|
| 86 |
+
|
| 87 |
+
if not response_text:
|
| 88 |
+
yield "模型没有返回可显示的文本。请稍后重试或更换推理参数。"
|
| 89 |
+
|
| 90 |
+
except Exception as error:
|
| 91 |
+
error_text = str(error)
|
| 92 |
+
|
| 93 |
+
if "401" in error_text or "unauthorized" in error_text.lower():
|
| 94 |
+
yield (
|
| 95 |
+
"身份验证失败。请退出后重新登录 Hugging Face,"
|
| 96 |
+
"并确认账户允许调用 Inference Providers。"
|
| 97 |
+
)
|
| 98 |
+
elif "402" in error_text or "payment" in error_text.lower():
|
| 99 |
+
yield (
|
| 100 |
+
"当前 Hugging Face 账户的推理额度不足,"
|
| 101 |
+
"请检查 Inference Providers 余额或计费设置。"
|
| 102 |
+
)
|
| 103 |
+
elif "429" in error_text or "rate limit" in error_text.lower():
|
| 104 |
+
yield "请求过于频繁或免费额度已达到限制,请稍后再试。"
|
| 105 |
+
else:
|
| 106 |
+
yield f"调用模型时发生错误:{error_text}"
|
| 107 |
+
|
| 108 |
+
|
| 109 |
+
with gr.Blocks(title="GPT-OSS 20B Chat") as demo:
|
| 110 |
+
gr.Markdown(
|
| 111 |
+
"""
|
| 112 |
+
# GPT-OSS 20B Chat
|
| 113 |
+
|
| 114 |
+
使用 Hugging Face Inference Providers 调用 `openai/gpt-oss-20b`。
|
| 115 |
+
|
| 116 |
+
请先登录 Hugging Face。推理请求将使用登录用户自己的 HF 账户和额度。
|
| 117 |
+
"""
|
| 118 |
+
)
|
| 119 |
+
|
| 120 |
+
with gr.Sidebar():
|
| 121 |
+
gr.Markdown("### Hugging Face 账户")
|
| 122 |
+
gr.LoginButton()
|
| 123 |
+
|
| 124 |
+
system_message = gr.Textbox(
|
| 125 |
+
value="You are a helpful and friendly assistant.",
|
| 126 |
+
label="System message",
|
| 127 |
+
lines=4,
|
| 128 |
+
)
|
| 129 |
+
|
| 130 |
+
reasoning_effort = gr.Radio(
|
| 131 |
+
choices=["low", "medium", "high"],
|
| 132 |
+
value="medium",
|
| 133 |
+
label="Reasoning effort",
|
| 134 |
+
)
|
| 135 |
+
|
| 136 |
+
max_tokens = gr.Slider(
|
| 137 |
+
minimum=64,
|
| 138 |
+
maximum=8192,
|
| 139 |
+
value=1024,
|
| 140 |
+
step=64,
|
| 141 |
+
label="Max output tokens",
|
| 142 |
+
)
|
| 143 |
+
|
| 144 |
+
temperature = gr.Slider(
|
| 145 |
+
minimum=0.0,
|
| 146 |
+
maximum=2.0,
|
| 147 |
+
value=0.7,
|
| 148 |
+
step=0.05,
|
| 149 |
+
label="Temperature",
|
| 150 |
+
)
|
| 151 |
+
|
| 152 |
+
top_p = gr.Slider(
|
| 153 |
+
minimum=0.05,
|
| 154 |
maximum=1.0,
|
| 155 |
value=0.95,
|
| 156 |
step=0.05,
|
| 157 |
+
label="Top-p",
|
| 158 |
+
)
|
|
|
|
|
|
|
| 159 |
|
| 160 |
+
gr.ChatInterface(
|
| 161 |
+
fn=respond,
|
| 162 |
+
type="messages",
|
| 163 |
+
additional_inputs=[
|
| 164 |
+
system_message,
|
| 165 |
+
reasoning_effort,
|
| 166 |
+
max_tokens,
|
| 167 |
+
temperature,
|
| 168 |
+
top_p,
|
| 169 |
+
],
|
| 170 |
+
examples=[
|
| 171 |
+
["请解释 MoE 模型中的总参数和激活参数有什么区别。"],
|
| 172 |
+
["用 Python 写一个并发批量请求 API 的示例。"],
|
| 173 |
+
],
|
| 174 |
+
cache_examples=False,
|
| 175 |
+
)
|
| 176 |
|
| 177 |
|
| 178 |
if __name__ == "__main__":
|
| 179 |
+
demo.queue(
|
| 180 |
+
default_concurrency_limit=8,
|
| 181 |
+
max_size=32,
|
| 182 |
+
).launch()
|