feat(frontend): 优化设置页面并添加数据刷新功能
- 为 get_providers 函数添加异常处理,提高数据获取的稳定性 - 在设置页面添加刷新按钮,用户可手动触发数据刷新 - 优化页面布局,调整组件间距和对齐方式
This commit is contained in:
parent
d40f5b1f24
commit
286db405ca
@ -1,21 +1,23 @@
|
||||
import gradio as gr
|
||||
from typing import List, Dict
|
||||
from typing import List
|
||||
from sqlmodel import Session, select
|
||||
from db import get_engine
|
||||
from schema import APIProvider
|
||||
import os
|
||||
|
||||
# 获取数据库引擎
|
||||
engine = get_engine(os.path.join(os.path.dirname(__file__), "..", "workdir"))
|
||||
|
||||
def setting_page():
|
||||
def get_providers() -> List[List[str]]:
|
||||
with Session(engine) as session:
|
||||
providers = session.exec(select(APIProvider)).all()
|
||||
return [
|
||||
[p.id, p.model_id, p.base_url, p.api_key or ""]
|
||||
for p in providers
|
||||
]
|
||||
try: # 添加异常处理
|
||||
with Session(engine) as session:
|
||||
providers = session.exec(select(APIProvider)).all()
|
||||
return [
|
||||
[p.id, p.model_id, p.base_url, p.api_key or ""]
|
||||
for p in providers
|
||||
]
|
||||
except Exception as e:
|
||||
raise gr.Error(f"获取数据失败: {str(e)}")
|
||||
|
||||
def add_provider(model_id, base_url, api_key):
|
||||
try:
|
||||
@ -30,34 +32,39 @@ def setting_page():
|
||||
session.refresh(new_provider)
|
||||
return get_providers()
|
||||
except Exception as e:
|
||||
# 抛出错误提示
|
||||
raise gr.Error(f"添加失败: {str(e)}")
|
||||
|
||||
with gr.Blocks() as demo:
|
||||
gr.Markdown("## API Provider 管理")
|
||||
|
||||
|
||||
with gr.Row():
|
||||
with gr.Column(scale=1):
|
||||
|
||||
model_id_input = gr.Textbox(label="Model ID")
|
||||
base_url_input = gr.Textbox(label="Base URL")
|
||||
api_key_input = gr.Textbox(label="API Key")
|
||||
add_button = gr.Button("添加新API")
|
||||
|
||||
# API Provider列表
|
||||
with gr.Column(scale=3):
|
||||
provider_table = gr.DataFrame(
|
||||
headers=["id" , "model id", "base URL", "API Key"],
|
||||
datatype=["number","str", "str", "str"],
|
||||
headers=["id", "model id", "base URL", "API Key"],
|
||||
datatype=["number", "str", "str", "str"],
|
||||
interactive=True,
|
||||
value=get_providers(),
|
||||
wrap=True,
|
||||
col_count=(4, "auto")
|
||||
)
|
||||
|
||||
with gr.Row():
|
||||
edit_button = gr.Button("编辑选中行")
|
||||
delete_button = gr.Button("删除选中行")
|
||||
refresh_button = gr.Button("刷新数据", variant="secondary")
|
||||
|
||||
# 绑定刷新按钮事件
|
||||
refresh_button.click(
|
||||
fn=get_providers,
|
||||
outputs=[provider_table],
|
||||
queue=False # 立即刷新不需要排队
|
||||
)
|
||||
|
||||
add_button.click(
|
||||
fn=add_provider,
|
||||
@ -65,4 +72,4 @@ def setting_page():
|
||||
outputs=[provider_table]
|
||||
)
|
||||
|
||||
return demo
|
||||
return demo
|
Loading…
x
Reference in New Issue
Block a user