Spaces:
Sleeping
Sleeping
Adaptive API Base Url
Browse files40 unmodified lines4142434445464748495051525354555657137 unmodified lines19519619719819920020120220320420520620720829 unmodified lines2382392402412422432441 unmodified line24624724824925025125240 unmodified linesexcept Exception as e:return f"❌ 出错: {str(e)}", get_debug_info()def save_api_key(api_key):if not api_key: return "❌ Key 不能为空", get_debug_info()try:config = {}if os.path.exists(CONFIG_PATH):with open(CONFIG_PATH, "r", encoding="utf-8") as f:config = yaml.safe_load(f) or {}config.setdefault("api_keys", {})["gemini_api_key"] = api_keywith open(CONFIG_PATH, "w", encoding="utf-8") as f:yaml.dump(config, f, allow_unicode=True)return "✅ Key 已保存", get_debug_info()except Exception as e:return f"❌ 出错: {str(e)}", get_debug_info()137 unmodified lines# --- UI ---with gr.Blocks(theme=gr.themes.Soft()) as demo:gr.Markdown("# 📑 Mineru PDF 解析调试器--testv2")with gr.Row():with gr.Column(scale=1):with gr.Group():gr.Markdown("### 1. 配置 & 上传")key_input = gr.Textbox(label="API Key", type="password")key_btn = gr.Button("保存 Key")gr.Markdown("---")pdf_input = gr.File(label="选择 PDF", file_types=[".pdf"])pdf_btn = gr.Button("保存 PDF")29 unmodified linesrefresh_btn = gr.Button("🔄 刷新状态")# ================= 逻辑绑定 =================key_btn.click(save_api_key, inputs=key_input, outputs=[parse_status, debug_view])pdf_btn.click(save_pdf, inputs=pdf_input, outputs=[parse_status, debug_view])parse_btn.click(1 unmodified lineoutputs=[parse_status, debug_view, cmd_logs])# 最终生成逻辑绑定 (使用 lambda 传递固定的 task_type 参数)gen_ppt_btn.click(fn=lambda: run_final_generation("ppt"), outputs=[gen_status, debug_view, cmd_logs, download_file])gen_poster_btn.click(fn=lambda: run_final_generation("poster"), outputs=[gen_status, debug_view, cmd_logs, download_file])gen_pr_btn.click(fn=lambda: run_final_generation("pr"), outputs=[gen_status, debug_view, cmd_logs, download_file])40 unmodified lines41424344454647484950515253545556575859606162636465666768697071137 unmodified lines20921021121221321421521621721821922022122222322429 unmodified lines2542552562572582592602611 unmodified line26326426526626726826940 unmodified linesexcept Exception as e:return f"❌ 出错: {str(e)}", get_debug_info()def save_api_settings(api_key, api_base_url=None):if not api_key: return "❌ Key 不能为空", get_debug_info()try:config = {}if os.path.exists(CONFIG_PATH):with open(CONFIG_PATH, "r", encoding="utf-8") as f:config = yaml.safe_load(f) or {}# 保存 API Keyconfig.setdefault("api_keys", {})["gemini_api_key"] = api_key# 如果 api_base_url 不为空(不是 None 且不是空字符串),则保存该值if api_base_url:config["api_base_url"] = api_base_url# 写入 YAML 文件with open(CONFIG_PATH, "w", encoding="utf-8") as f:yaml.dump(config, f, allow_unicode=True)# 动态生成成功提示语success_msg = "✅ Key 已保存"if api_base_url:success_msg += ",Base URL 已更新"return success_msg, get_debug_info()except Exception as e:return f"❌ 出错: {str(e)}", get_debug_info()137 unmodified lines# --- UI ---with gr.Blocks(theme=gr.themes.Soft()) as demo:gr.Markdown("# 📑 Mineru PDF 解析调试器")with gr.Row():with gr.Column(scale=1):with gr.Group():gr.Markdown("### 1. 配置 & 上传")key_input = gr.Textbox(label="API Key", type="password", placeholder="请输入您的 API Key")# 👇 这里新增了 Base URL 的输入框api_base_url_input = gr.Textbox(label="API Base URL (可选)", placeholder="例如: https://api.example.com/v1")key_btn = gr.Button("保存配置") # 稍微改了下名字更贴切gr.Markdown("---")pdf_input = gr.File(label="选择 PDF", file_types=[".pdf"])pdf_btn = gr.Button("保存 PDF")29 unmodified linesrefresh_btn = gr.Button("🔄 刷新状态")# ================= 逻辑绑定 =================# 👇 这里绑定了两个输入框key_btn.click(save_api_settings, inputs=[key_input, api_base_url_input], outputs=[parse_status, debug_view])pdf_btn.click(save_pdf, inputs=pdf_input, outputs=[parse_status, debug_view])parse_btn.click(1 unmodified lineoutputs=[parse_status, debug_view, cmd_logs])# 最终生成逻辑绑定gen_ppt_btn.click(fn=lambda: run_final_generation("ppt"), outputs=[gen_status, debug_view, cmd_logs, download_file])gen_poster_btn.click(fn=lambda: run_final_generation("poster"), outputs=[gen_status, debug_view, cmd_logs, download_file])gen_pr_btn.click(fn=lambda: run_final_generation("pr"), outputs=[gen_status, debug_view, cmd_logs, download_file])
app.py+24-7