Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -11,9 +11,17 @@ def save_pdf(file):
|
|
| 11 |
return "Please upload a PDF file."
|
| 12 |
|
| 13 |
# 获取文件名并保存到 papers 文件夹
|
| 14 |
-
|
|
|
|
| 15 |
shutil.copy(file.name, file_path)
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
def save_api_key(api_key):
|
| 19 |
if not api_key:
|
|
@@ -21,11 +29,15 @@ def save_api_key(api_key):
|
|
| 21 |
|
| 22 |
config_path = "config.yaml"
|
| 23 |
try:
|
| 24 |
-
#
|
| 25 |
-
|
| 26 |
-
config =
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
-
# 确保 api_keys 字典存在
|
| 29 |
if "api_keys" not in config:
|
| 30 |
config["api_keys"] = {}
|
| 31 |
|
|
@@ -36,7 +48,14 @@ def save_api_key(api_key):
|
|
| 36 |
with open(config_path, "w", encoding="utf-8") as f:
|
| 37 |
yaml.dump(config, f, allow_unicode=True, default_flow_style=False)
|
| 38 |
|
| 39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
except Exception as e:
|
| 41 |
return f"Error saving API Key: {str(e)}"
|
| 42 |
|
|
@@ -50,10 +69,11 @@ with gr.Blocks(title="PDF Uploader & Config") as demo:
|
|
| 50 |
upload_btn.click(save_pdf, inputs=file_input, outputs=upload_output)
|
| 51 |
|
| 52 |
with gr.Tab("Gemini Configuration"):
|
| 53 |
-
key_input = gr.Textbox(label="Enter Gemini API Key", type="password")
|
| 54 |
key_btn = gr.Button("Save API Key")
|
| 55 |
key_output = gr.Textbox(label="Status")
|
| 56 |
key_btn.click(save_api_key, inputs=key_input, outputs=key_output)
|
| 57 |
|
| 58 |
if __name__ == "__main__":
|
| 59 |
-
|
|
|
|
|
|
| 11 |
return "Please upload a PDF file."
|
| 12 |
|
| 13 |
# 获取文件名并保存到 papers 文件夹
|
| 14 |
+
file_name = os.path.basename(file.name)
|
| 15 |
+
file_path = os.path.join("papers", file_name)
|
| 16 |
shutil.copy(file.name, file_path)
|
| 17 |
+
|
| 18 |
+
# --- 调试打印逻辑 ---
|
| 19 |
+
print(f"\n[DEBUG] 正在检查 papers 文件夹内容...")
|
| 20 |
+
files_in_dir = os.listdir("papers")
|
| 21 |
+
print(f"[DEBUG] 当前 papers 目录下的文件列表: {files_in_dir}")
|
| 22 |
+
# ------------------
|
| 23 |
+
|
| 24 |
+
return f"File '{file_name}' saved successfully. Total files: {len(files_in_dir)}"
|
| 25 |
|
| 26 |
def save_api_key(api_key):
|
| 27 |
if not api_key:
|
|
|
|
| 29 |
|
| 30 |
config_path = "config.yaml"
|
| 31 |
try:
|
| 32 |
+
# 如果文件不存在,初始化一个空字典
|
| 33 |
+
if not os.path.exists(config_path):
|
| 34 |
+
config = {}
|
| 35 |
+
else:
|
| 36 |
+
# 读取现有的 yaml 文件
|
| 37 |
+
with open(config_path, "r", encoding="utf-8") as f:
|
| 38 |
+
config = yaml.safe_load(f) or {}
|
| 39 |
|
| 40 |
+
# 确保 api_keys 字典存在
|
| 41 |
if "api_keys" not in config:
|
| 42 |
config["api_keys"] = {}
|
| 43 |
|
|
|
|
| 48 |
with open(config_path, "w", encoding="utf-8") as f:
|
| 49 |
yaml.dump(config, f, allow_unicode=True, default_flow_style=False)
|
| 50 |
|
| 51 |
+
# --- 调试打印逻辑 ---
|
| 52 |
+
print(f"\n[DEBUG] 正在验证 {config_path} 内容...")
|
| 53 |
+
with open(config_path, "r", encoding="utf-8") as f:
|
| 54 |
+
content = f.read()
|
| 55 |
+
print(f"[DEBUG] 当前 config.yaml 内容:\n{content}")
|
| 56 |
+
# ------------------
|
| 57 |
+
|
| 58 |
+
return "Gemini API Key updated successfully in config.yaml!"
|
| 59 |
except Exception as e:
|
| 60 |
return f"Error saving API Key: {str(e)}"
|
| 61 |
|
|
|
|
| 69 |
upload_btn.click(save_pdf, inputs=file_input, outputs=upload_output)
|
| 70 |
|
| 71 |
with gr.Tab("Gemini Configuration"):
|
| 72 |
+
key_input = gr.Textbox(label="Enter Gemini API Key", type="password", placeholder="Paste your API key here...")
|
| 73 |
key_btn = gr.Button("Save API Key")
|
| 74 |
key_output = gr.Textbox(label="Status")
|
| 75 |
key_btn.click(save_api_key, inputs=key_input, outputs=key_output)
|
| 76 |
|
| 77 |
if __name__ == "__main__":
|
| 78 |
+
# 在 HF Spaces 运行通常只需要 launch(),不需要手动指定端口
|
| 79 |
+
demo.launch()
|