This commit is contained in:
hailin 2025-07-25 16:48:48 +08:00
parent 174a6b2d76
commit 6aa0932210
1 changed files with 26 additions and 7 deletions

View File

@ -1,5 +1,6 @@
import gradio as gr
import requests
import json
API_URL = "http://localhost:30000/generate" # ✅ 使用原生 generate 接口
API_KEY = "token-abc123"
@ -19,15 +20,25 @@ def chat(user_message, history, max_tokens, temperature):
}
payload = {
"model": MODEL_NAME,
"text": prompt, # ✅ 注意是 text不是 prompt
"text": prompt, # ✅ generate 接口使用 text 字段
"max_tokens": max_tokens,
"temperature": temperature
}
print(f"\n🟡 [chat] 请求 payload:\n{json.dumps(payload, ensure_ascii=False, indent=2)}")
try:
response = requests.post(API_URL, headers=headers, json=payload, timeout=30)
print(f"🟢 [chat] HTTP 状态码: {response.status_code}")
print(f"🟢 [chat] 响应内容: {response.text}")
if response.status_code == 200 and response.content:
result = response.json()
reply = result["text"].strip() # ✅ generate 接口返回字段是 text
reply = result.get("text", "").strip()
if not reply:
reply = "[⚠️ 返回内容为空]"
else:
reply = f"[⚠️ 无效响应] 状态码: {response.status_code}, 内容: {response.text}"
except Exception as e:
reply = f"[请求失败] {e}"
@ -41,19 +52,27 @@ def test_api_connection(max_tokens, temperature):
}
payload = {
"model": MODEL_NAME,
"text": "Ping?", # ✅ 也改成 text 字段
"text": "Ping?",
"max_tokens": max_tokens,
"temperature": temperature
}
print(f"\n🔵 [test] 请求 payload:\n{json.dumps(payload, ensure_ascii=False, indent=2)}")
try:
resp = requests.post(API_URL, headers=headers, json=payload, timeout=10)
out = resp.json()["text"].strip()
return f"✅ API 可用,响应: {out}"
print(f"🟢 [test] HTTP 状态码: {resp.status_code}")
print(f"🟢 [test] 响应内容: {resp.text}")
if resp.status_code == 200 and resp.content:
out = resp.json().get("text", "").strip()
return f"✅ API 可用,响应: {out or '[空响应]'}"
else:
return f"⚠️ 非预期响应: 状态码: {resp.status_code}, 内容: {resp.text}"
except Exception as e:
return f"❌ API 请求失败: {e}"
# Gradio 界面
# Gradio UI
with gr.Blocks(title="Base 模型测试 UI") as demo:
gr.Markdown("# 💬 Base 模型对话界面")