Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,20 +1,59 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import
|
|
|
|
|
|
|
| 3 |
|
| 4 |
-
|
| 5 |
-
|
| 6 |
|
| 7 |
-
def
|
| 8 |
-
|
| 9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
if __name__ == "__main__":
|
| 19 |
-
print("BOOT: Launching Gradio", flush=True)
|
| 20 |
demo.launch(server_name="0.0.0.0", server_port=7860)
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import os
|
| 3 |
+
import yaml
|
| 4 |
+
import shutil
|
| 5 |
|
| 6 |
+
# 确保 papers 文件夹存在
|
| 7 |
+
os.makedirs("papers", exist_ok=True)
|
| 8 |
|
| 9 |
+
def save_pdf(file):
|
| 10 |
+
if file is None:
|
| 11 |
+
return "Please upload a PDF file."
|
| 12 |
+
|
| 13 |
+
# 获取文件名并保存到 papers 文件夹
|
| 14 |
+
file_path = os.path.join("papers", os.path.basename(file.name))
|
| 15 |
+
shutil.copy(file.name, file_path)
|
| 16 |
+
return f"File saved successfully to {file_path}"
|
| 17 |
|
| 18 |
+
def save_api_key(api_key):
|
| 19 |
+
if not api_key:
|
| 20 |
+
return "API Key cannot be empty."
|
| 21 |
+
|
| 22 |
+
config_path = "config.yaml"
|
| 23 |
+
try:
|
| 24 |
+
# 读取现有的 yaml 文件
|
| 25 |
+
with open(config_path, "r", encoding="utf-8") as f:
|
| 26 |
+
config = yaml.safe_load(f) or {}
|
| 27 |
+
|
| 28 |
+
# 确保 api_keys 字典存在,如果原文件中没有这个键,则初始化它
|
| 29 |
+
if "api_keys" not in config:
|
| 30 |
+
config["api_keys"] = {}
|
| 31 |
+
|
| 32 |
+
# 更新嵌套的键
|
| 33 |
+
config["api_keys"]["gemini_api_key"] = api_key
|
| 34 |
+
|
| 35 |
+
# 将更新后的内容写回文件
|
| 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 |
+
return "Gemini API Key updated successfully in api_keys section!"
|
| 40 |
+
except Exception as e:
|
| 41 |
+
return f"Error saving API Key: {str(e)}"
|
| 42 |
+
|
| 43 |
+
with gr.Blocks(title="PDF Uploader & Config") as demo:
|
| 44 |
+
gr.Markdown("# PDF and API Configuration")
|
| 45 |
+
|
| 46 |
+
with gr.Tab("Upload PDF"):
|
| 47 |
+
file_input = gr.File(label="Upload your PDF", file_types=[".pdf"])
|
| 48 |
+
upload_btn = gr.Button("Save PDF")
|
| 49 |
+
upload_output = gr.Textbox(label="Status")
|
| 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 |
demo.launch(server_name="0.0.0.0", server_port=7860)
|