snym04 commited on
Commit
fe717e4
·
verified ·
1 Parent(s): 926c4f2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +52 -13
app.py CHANGED
@@ -1,20 +1,59 @@
1
  import gradio as gr
2
- import sys
 
 
3
 
4
- print("BOOT: app.py imported", flush=True)
5
- print("BOOT: Python version:", sys.version, flush=True)
6
 
7
- def hello():
8
- print("Button clicked", flush=True)
9
- return "App is running correctly."
 
 
 
 
 
10
 
11
- with gr.Blocks() as demo:
12
- gr.Markdown("# Minimal Test App")
13
- gr.Markdown("If you can see this, the app started successfully.")
14
- btn = gr.Button("Click me")
15
- output = gr.Textbox()
16
- btn.click(hello, outputs=output)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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)