Spaces:
Sleeping
Sleeping
Update app.py
Browse files122345456787899101112131415161711121314151618192021222324252627282930313233343536373839404142434445464748495051525354555657581959import gradio as grimport sysimport osimport yamlimport shutil
print("BOOT: app.py imported", flush=True)print("BOOT: Python version:", sys.version, flush=True)# 确保 papers 文件夹存在os.makedirs("papers", exist_ok=True)
def hello(): print("Button clicked", flush=True) return "App is running correctly."def save_pdf(file): if file is None: return "Please upload a PDF file." # 获取文件名并保存到 papers 文件夹 file_path = os.path.join("papers", os.path.basename(file.name)) shutil.copy(file.name, file_path) return f"File saved successfully to {file_path}"
with gr.Blocks() as demo: gr.Markdown("# Minimal Test App") gr.Markdown("If you can see this, the app started successfully.") btn = gr.Button("Click me") output = gr.Textbox() btn.click(hello, outputs=output)def save_api_key(api_key): if not api_key: return "API Key cannot be empty." config_path = "config.yaml" try: # 读取现有的 yaml 文件 with open(config_path, "r", encoding="utf-8") as f: config = yaml.safe_load(f) or {} # 确保 api_keys 字典存在,如果原文件中没有这个键,则初始化它 if "api_keys" not in config: config["api_keys"] = {} # 更新嵌套的键 config["api_keys"]["gemini_api_key"] = api_key # 将更新后的内容写回文件 with open(config_path, "w", encoding="utf-8") as f: yaml.dump(config, f, allow_unicode=True, default_flow_style=False) return "Gemini API Key updated successfully in api_keys section!" except Exception as e: return f"Error saving API Key: {str(e)}"
with gr.Blocks(title="PDF Uploader & Config") as demo: gr.Markdown("# PDF and API Configuration") with gr.Tab("Upload PDF"): file_input = gr.File(label="Upload your PDF", file_types=[".pdf"]) upload_btn = gr.Button("Save PDF") upload_output = gr.Textbox(label="Status") upload_btn.click(save_pdf, inputs=file_input, outputs=upload_output) with gr.Tab("Gemini Configuration"): key_input = gr.Textbox(label="Enter Gemini API Key", type="password") key_btn = gr.Button("Save API Key") key_output = gr.Textbox(label="Status") key_btn.click(save_api_key, inputs=key_input, outputs=key_output)
if __name__ == "__main__": print("BOOT: Launching Gradio", flush=True) demo.launch(server_name="0.0.0.0", server_port=7860)app.py+52-13