Spaces:
Running
Running
Adaptive API Base Url
Browse files
app.py
CHANGED
|
@@ -41,17 +41,31 @@ def save_pdf(file):
|
|
| 41 |
except Exception as e:
|
| 42 |
return f"❌ 出错: {str(e)}", get_debug_info()
|
| 43 |
|
| 44 |
-
def
|
| 45 |
if not api_key: return "❌ Key 不能为空", get_debug_info()
|
| 46 |
try:
|
| 47 |
config = {}
|
| 48 |
if os.path.exists(CONFIG_PATH):
|
| 49 |
with open(CONFIG_PATH, "r", encoding="utf-8") as f:
|
| 50 |
config = yaml.safe_load(f) or {}
|
|
|
|
|
|
|
| 51 |
config.setdefault("api_keys", {})["gemini_api_key"] = api_key
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
with open(CONFIG_PATH, "w", encoding="utf-8") as f:
|
| 53 |
yaml.dump(config, f, allow_unicode=True)
|
| 54 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
except Exception as e:
|
| 56 |
return f"❌ 出错: {str(e)}", get_debug_info()
|
| 57 |
|
|
@@ -195,14 +209,16 @@ def run_final_generation(task_type="all"):
|
|
| 195 |
|
| 196 |
# --- UI ---
|
| 197 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 198 |
-
gr.Markdown("# 📑 Mineru PDF 解析调试器
|
| 199 |
|
| 200 |
with gr.Row():
|
| 201 |
with gr.Column(scale=1):
|
| 202 |
with gr.Group():
|
| 203 |
gr.Markdown("### 1. 配置 & 上传")
|
| 204 |
-
key_input = gr.Textbox(label="API Key", type="password")
|
| 205 |
-
|
|
|
|
|
|
|
| 206 |
gr.Markdown("---")
|
| 207 |
pdf_input = gr.File(label="选择 PDF", file_types=[".pdf"])
|
| 208 |
pdf_btn = gr.Button("保存 PDF")
|
|
@@ -238,7 +254,8 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
| 238 |
refresh_btn = gr.Button("🔄 刷新状态")
|
| 239 |
|
| 240 |
# ================= 逻辑绑定 =================
|
| 241 |
-
|
|
|
|
| 242 |
pdf_btn.click(save_pdf, inputs=pdf_input, outputs=[parse_status, debug_view])
|
| 243 |
|
| 244 |
parse_btn.click(
|
|
@@ -246,7 +263,7 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
| 246 |
outputs=[parse_status, debug_view, cmd_logs]
|
| 247 |
)
|
| 248 |
|
| 249 |
-
# 最终生成逻辑绑定
|
| 250 |
gen_ppt_btn.click(fn=lambda: run_final_generation("ppt"), outputs=[gen_status, debug_view, cmd_logs, download_file])
|
| 251 |
gen_poster_btn.click(fn=lambda: run_final_generation("poster"), outputs=[gen_status, debug_view, cmd_logs, download_file])
|
| 252 |
gen_pr_btn.click(fn=lambda: run_final_generation("pr"), outputs=[gen_status, debug_view, cmd_logs, download_file])
|
|
|
|
| 41 |
except Exception as e:
|
| 42 |
return f"❌ 出错: {str(e)}", get_debug_info()
|
| 43 |
|
| 44 |
+
def save_api_settings(api_key, api_base_url=None):
|
| 45 |
if not api_key: return "❌ Key 不能为空", get_debug_info()
|
| 46 |
try:
|
| 47 |
config = {}
|
| 48 |
if os.path.exists(CONFIG_PATH):
|
| 49 |
with open(CONFIG_PATH, "r", encoding="utf-8") as f:
|
| 50 |
config = yaml.safe_load(f) or {}
|
| 51 |
+
|
| 52 |
+
# 保存 API Key
|
| 53 |
config.setdefault("api_keys", {})["gemini_api_key"] = api_key
|
| 54 |
+
|
| 55 |
+
# 如果 api_base_url 不为空(不是 None 且不是空字符串),则保存该值
|
| 56 |
+
if api_base_url:
|
| 57 |
+
config["api_base_url"] = api_base_url
|
| 58 |
+
|
| 59 |
+
# 写入 YAML 文件
|
| 60 |
with open(CONFIG_PATH, "w", encoding="utf-8") as f:
|
| 61 |
yaml.dump(config, f, allow_unicode=True)
|
| 62 |
+
|
| 63 |
+
# 动态生成成功提示语
|
| 64 |
+
success_msg = "✅ Key 已保存"
|
| 65 |
+
if api_base_url:
|
| 66 |
+
success_msg += ",Base URL 已更新"
|
| 67 |
+
|
| 68 |
+
return success_msg, get_debug_info()
|
| 69 |
except Exception as e:
|
| 70 |
return f"❌ 出错: {str(e)}", get_debug_info()
|
| 71 |
|
|
|
|
| 209 |
|
| 210 |
# --- UI ---
|
| 211 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 212 |
+
gr.Markdown("# 📑 Mineru PDF 解析调试器")
|
| 213 |
|
| 214 |
with gr.Row():
|
| 215 |
with gr.Column(scale=1):
|
| 216 |
with gr.Group():
|
| 217 |
gr.Markdown("### 1. 配置 & 上传")
|
| 218 |
+
key_input = gr.Textbox(label="API Key", type="password", placeholder="请输入您的 API Key")
|
| 219 |
+
# 👇 这里新增了 Base URL 的输入框
|
| 220 |
+
api_base_url_input = gr.Textbox(label="API Base URL (可选)", placeholder="例如: https://api.example.com/v1")
|
| 221 |
+
key_btn = gr.Button("保存配置") # 稍微改了下名字更贴切
|
| 222 |
gr.Markdown("---")
|
| 223 |
pdf_input = gr.File(label="选择 PDF", file_types=[".pdf"])
|
| 224 |
pdf_btn = gr.Button("保存 PDF")
|
|
|
|
| 254 |
refresh_btn = gr.Button("🔄 刷新状态")
|
| 255 |
|
| 256 |
# ================= 逻辑绑定 =================
|
| 257 |
+
# 👇 这里绑定了两个输入框
|
| 258 |
+
key_btn.click(save_api_settings, inputs=[key_input, api_base_url_input], outputs=[parse_status, debug_view])
|
| 259 |
pdf_btn.click(save_pdf, inputs=pdf_input, outputs=[parse_status, debug_view])
|
| 260 |
|
| 261 |
parse_btn.click(
|
|
|
|
| 263 |
outputs=[parse_status, debug_view, cmd_logs]
|
| 264 |
)
|
| 265 |
|
| 266 |
+
# 最终生成逻辑绑定
|
| 267 |
gen_ppt_btn.click(fn=lambda: run_final_generation("ppt"), outputs=[gen_status, debug_view, cmd_logs, download_file])
|
| 268 |
gen_poster_btn.click(fn=lambda: run_final_generation("poster"), outputs=[gen_status, debug_view, cmd_logs, download_file])
|
| 269 |
gen_pr_btn.click(fn=lambda: run_final_generation("pr"), outputs=[gen_status, debug_view, cmd_logs, download_file])
|