import gradio as gr import json import asyncio from pyodide.http import pyfetch # ------------------ 全局变量 ------------------ templates = { "default_v0.0.4_1K335": "# 糖度计AI Agent系统提示词\n\n## 核心功能\n你是一个专业的“糖度计”AI Agent,能够为输入的内容计算和评估“糖度”值。糖度有两种含义:\n1. **食物糖度**:使用Brix值衡量食物的实际含糖量\n2. **网络俚语糖度**:衡量事件、人物、行为等对周围环境产生的负面影响程度或令人感到羞愧/恶心/低能/离谱的程度\n\n## 输出格式要求\n**只输出一个浮点数表示糖度**", "default_v0.0.3_extra_223": "你是“糖度计”AI,根据输入:\n- 若为食物名称,输出其Brix糖度,浮点数,精确到6位小数\n- 若为事件/任务/行为,根据知识评估其“糖度”,输出浮点数,范围【0.000000, 100.000000】,极少数超100表示极度离谱\n仅输出浮点数,无其他内容。" } api_url = "https://api.openai.com/v1/chat/completions" api_key = "" model_name = "gpt-4o-mini" # ------------------ 异步调用模型 ------------------ async def query_model(food_name, template_name): prompt = templates.get(template_name, templates["default_v0.0.4_1K335"]) headers = {"Authorization": f"Bearer {api_key}", "Content-Type": "application/json"} payload = {"model": model_name, "messages": [ {"role": "system", "content": prompt}, {"role": "user", "content": food_name} ]} try: resp = await pyfetch(api_url, method="POST", headers=headers, body=json.dumps(payload)) if not resp.ok: return "0.0", f"HTTP {resp.status}", f"HTTP {resp.status}" data = await resp.json() raw_output = data["choices"][0]["message"]["content"].strip() val = float(raw_output) except Exception as e: return "0.0", f"错误: {e}", f"错误: {e}" if val < 20.0: remark = "🟦 低糖" elif val < 60.0: remark = "🟩 中糖" elif val < 80.0: remark = "🟧 高糖" elif val <= 100.0: remark = "🟥 糖王" else: remark = "🌌 糖到没边" return str(val), remark, raw_output # ------------------ 模版管理 ------------------ def add_template(new_name, new_content): if new_name and new_content: templates[new_name] = new_content updated_keys = list(templates.keys()) return updated_keys, gr.Dropdown(choices=updated_keys, value=new_name), f"✅ 模版 **{new_name}** 已添加" current_keys = list(templates.keys()) return current_keys, gr.Dropdown(choices=current_keys, value=current_keys[0]), "❌ 添加失败:名称或内容不能为空" # ------------------ API 设置 ------------------ def update_settings(url, model, key): global api_url, model_name, api_key if url: api_url = url if model: model_name = model if key: api_key = key return f"✅ 已更新设置\nAPI URL: {api_url}\n模型: {model_name}" # ------------------ 自定义 CSS ------------------ custom_css = """ .gradio-container { border-radius: 16px; padding: 25px; } button { background: linear-gradient(90deg, #3bb2b8, #6ed0cf, #3bb2b8) !important; background-size: 200% 200% !important; color: #fff !important; border: none !important; border-radius: 10px !important; font-weight: bold; transition: all 0.3s ease; } button:hover { background-position: 100% 0; transform: translateY(-2px); box-shadow: 0 6px 15px rgba(59,178,184,0.4); } textarea, input { border-radius: 8px !important; border: 1px solid #d0d7de !important; } .output-box { background: #ffffff; border-radius: 12px; padding: 12px; box-shadow: 0 2px 6px rgba(0,0,0,0.05); } """ # ------------------ Gradio UI ------------------ with gr.Blocks(title="糖度计", theme="soft", css=custom_css) as demo: gr.Markdown("# 🍬 糖度计") gr.Markdown("###### forked by [rain_x3](https://www.luogu.com.cn/user/964072)") with gr.Tabs(): with gr.Tab("检测"): with gr.Row(): with gr.Column(scale=2): food_input = gr.Textbox(label="输入食物/事件") template_dropdown = gr.Dropdown( choices=list(templates.keys()), value="default_v0.0.4_1K335", label="模版选择" ) btn = gr.Button("开始检测") with gr.Column(scale=1, elem_classes="output-box"): val_output = gr.Textbox(label="糖度值", lines=1, interactive=False) remark_output = gr.Textbox(label="鉴定", lines=1, interactive=False) btn.click(query_model, inputs=[food_input, template_dropdown], outputs=[val_output, remark_output]) with gr.Tab("模版管理"): new_name = gr.Textbox(label="新模版名称") new_content = gr.Textbox(label="新模版提示词", lines=5) add_btn = gr.Button("添加模版") template_list = gr.Dropdown(choices=list(templates.keys()), label="现有模版") add_status = gr.Textbox(label="状态", interactive=False) add_btn.click(add_template, inputs=[new_name, new_content], outputs=[template_list, template_dropdown, add_status]) with gr.Tab("高级设置"): api_url_in = gr.Textbox(label="API URL", value=api_url) model_in = gr.Textbox(label="模型名称", value=model_name) api_key_in = gr.Textbox(label="API Key", type="password", value=api_key) update_btn = gr.Button("更新设置") update_status = gr.Textbox(label="状态", interactive=False) update_btn.click(update_settings, inputs=[api_url_in, model_in, api_key_in], outputs=update_status) demo.launch()