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