Spaces:
Sleeping
Sleeping
Update app.py
Browse files10 unmodified lines111213141415161617181920212223242526271 unmodified line293031323334352425263637383928404142434 unmodified lines4849505152535455565739585960617 unmodified lines6970715372737475767759787910 unmodified lines return "Please upload a PDF file." # 获取文件名并保存到 papers 文件夹 file_path = os.path.join("papers", os.path.basename(file.name)) file_name = os.path.basename(file.name) file_path = os.path.join("papers", file_name) shutil.copy(file.name, file_path) return f"File saved successfully to {file_path}" # --- 调试打印逻辑 --- print(f"\n[DEBUG] 正在检查 papers 文件夹内容...") files_in_dir = os.listdir("papers") print(f"[DEBUG] 当前 papers 目录下的文件列表: {files_in_dir}") # ------------------
return f"File '{file_name}' saved successfully. Total files: {len(files_in_dir)}"
def save_api_key(api_key): if not api_key:1 unmodified line config_path = "config.yaml" try: # 如果文件不存在,初始化一个空字典 if not os.path.exists(config_path): config = {} else: # 读取现有的 yaml 文件 with open(config_path, "r", encoding="utf-8") as f: config = yaml.safe_load(f) or {} # 读取现有的 yaml 文件 with open(config_path, "r", encoding="utf-8") as f: config = yaml.safe_load(f) or {} # 确保 api_keys 字典存在,如果原文件中没有这个键,则初始化它 # 确保 api_keys 字典存在 if "api_keys" not in config: config["api_keys"] = {} 4 unmodified lines with open(config_path, "w", encoding="utf-8") as f: yaml.dump(config, f, allow_unicode=True, default_flow_style=False) # --- 调试打印逻辑 --- print(f"\n[DEBUG] 正在验证 {config_path} 内容...") with open(config_path, "r", encoding="utf-8") as f: content = f.read() print(f"[DEBUG] 当前 config.yaml 内容:\n{content}") # ------------------
return "Gemini API Key updated successfully in api_keys section!" return "Gemini API Key updated successfully in config.yaml!" except Exception as e: return f"Error saving API Key: {str(e)}"
7 unmodified lines 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_input = gr.Textbox(label="Enter Gemini API Key", type="password", placeholder="Paste your API key here...") 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__": demo.launch(server_name="0.0.0.0", server_port=7860) # 在 HF Spaces 运行通常只需要 launch(),不需要手动指定端口 demo.launch()app.py+29-9