Compare commits

..

2 Commits

Author SHA1 Message Date
carry
d40f5b1f24 fix(frontend): 优化 API Provider 添加功能并处理异常
- 为 model_id、base_url 和 api_key 添加空值检查,避免无效输入
- 添加异常处理,确保在出现错误时能够及时响应并提示用户
- 优化 add_provider 函数,提高代码可读性和健壮性
2025-04-07 13:02:45 +08:00
carry
7a77f61ee6 feat(frontend): 添加 API Provider 的增加功能 2025-04-07 00:28:52 +08:00

View File

@ -17,19 +17,52 @@ def setting_page():
for p in providers
]
def add_provider(model_id, base_url, api_key):
try:
with Session(engine) as session:
new_provider = APIProvider(
model_id=model_id if model_id else None,
base_url=base_url if base_url else None,
api_key=api_key if api_key else None
)
session.add(new_provider)
session.commit()
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=2):
with gr.Column(scale=3):
provider_table = gr.DataFrame(
headers=["id" , "model id", "URL", "API Key"],
headers=["id" , "model id", "base URL", "API Key"],
datatype=["number","str", "str", "str"],
interactive=True,
value=get_providers(),
wrap=True,
col_count=(4, "fixed")
col_count=(4, "auto")
)
with gr.Row():
edit_button = gr.Button("编辑选中行")
delete_button = gr.Button("删除选中行")
add_button.click(
fn=add_provider,
inputs=[model_id_input, base_url_input, api_key_input],
outputs=[provider_table]
)
return demo