snym04 commited on
Commit
1485f56
·
verified ·
1 Parent(s): b59f6e2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -126
app.py CHANGED
@@ -1,133 +1,20 @@
1
- # app.py
2
- import os
3
- import re
4
- import shutil
5
- from pathlib import Path
6
- from datetime import datetime
7
-
8
  import gradio as gr
 
9
 
10
- ROOT_DIR = Path(__file__).resolve().parent
11
- PAPERS_DIR = ROOT_DIR / "papers"
12
- CONFIG_PATH = ROOT_DIR / "config.yaml"
13
-
14
-
15
- def ensure_dirs():
16
- PAPERS_DIR.mkdir(parents=True, exist_ok=True)
17
-
18
-
19
- def save_pdf_to_papers(uploaded_file):
20
- """
21
- Save uploaded PDF into papers/ with a timestamped filename to avoid overwriting.
22
- """
23
- ensure_dirs()
24
-
25
- if uploaded_file is None:
26
- return "❌ No file received. Please upload a PDF."
27
-
28
- # gr.File returns a tempfile path-like object; in recent gradio it's a NamedString / temp path
29
- src_path = Path(uploaded_file)
30
- if not src_path.exists():
31
- return "❌ Uploaded file path not found."
32
-
33
- if src_path.suffix.lower() != ".pdf":
34
- return "❌ Only PDF files are supported. Please upload a .pdf file."
35
-
36
- ts = datetime.now().strftime("%Y%m%d_%H%M%S")
37
- safe_name = re.sub(r"[^a-zA-Z0-9._-]+", "_", src_path.name)
38
- dst_name = f"{ts}_{safe_name}"
39
- dst_path = PAPERS_DIR / dst_name
40
-
41
- shutil.copy2(src_path, dst_path)
42
- return f"✅ PDF saved to: {dst_path.as_posix()}"
43
-
44
-
45
- def update_gemini_api_key(api_key: str):
46
- """
47
- Update config.yaml: api_keys -> gemini_api_key: "<api_key>"
48
- Keeps other content unchanged as much as possible.
49
- """
50
- api_key = (api_key or "").strip()
51
- if not api_key:
52
- return "❌ API key is empty. Please input your Google API Key."
53
-
54
- if not CONFIG_PATH.exists():
55
- return f"❌ config.yaml not found at: {CONFIG_PATH.as_posix()}"
56
-
57
- text = CONFIG_PATH.read_text(encoding="utf-8")
58
-
59
- # Strategy:
60
- # 1) Try to replace an existing line: gemini_api_key: "..."
61
- # 2) If not found, try to insert under api_keys:
62
- replaced = False
63
-
64
- # Replace gemini_api_key line with quoted value (handles single/double/no quotes)
65
- pattern = r'(^\s*gemini_api_key\s*:\s*)(?:"[^"]*"|\'[^\']*\'|.*)?\s*$'
66
- def repl(m):
67
- nonlocal replaced
68
- replaced = True
69
- return f'{m.group(1)}"{api_key}"'
70
-
71
- new_text, n = re.subn(pattern, repl, text, flags=re.MULTILINE)
72
- if n > 0 and replaced:
73
- CONFIG_PATH.write_text(new_text, encoding="utf-8")
74
- return "✅ Gemini API key saved into config.yaml."
75
-
76
- # If gemini_api_key line does not exist, try inserting it inside api_keys block
77
- # Find "api_keys:" line and insert next line with indentation (2 spaces)
78
- lines = text.splitlines()
79
- out = []
80
- inserted = False
81
- for i, line in enumerate(lines):
82
- out.append(line)
83
- if not inserted and re.match(r'^\s*api_keys\s*:\s*$', line):
84
- # Insert gemini_api_key right after api_keys:
85
- out.append(f' gemini_api_key: "{api_key}"')
86
- inserted = True
87
-
88
- if inserted:
89
- CONFIG_PATH.write_text("\n".join(out) + ("\n" if text.endswith("\n") else ""), encoding="utf-8")
90
- return "✅ Gemini API key inserted into config.yaml under api_keys."
91
- else:
92
- return '❌ Could not find "api_keys:" section in config.yaml. Please check your config.yaml format.'
93
-
94
-
95
- with gr.Blocks(title="Paper Upload & API Key Setup") as demo:
96
- gr.Markdown("# Demo Space")
97
- gr.Markdown(
98
- "This app lets you upload a paper PDF and save it into the `papers/` folder, "
99
- "and input your Google API Key (Gemini) to update `config.yaml`."
100
- )
101
-
102
- with gr.Tab("Upload PDF"):
103
- gr.Markdown("**Please upload your paper PDF here.**")
104
- pdf_input = gr.File(label="Upload paper PDF here", file_types=[".pdf"])
105
- pdf_save_btn = gr.Button("Save PDF to papers/")
106
- pdf_status = gr.Markdown()
107
-
108
- pdf_save_btn.click(
109
- fn=save_pdf_to_papers,
110
- inputs=[pdf_input],
111
- outputs=[pdf_status],
112
- )
113
-
114
- with gr.Tab("API Key"):
115
- gr.Markdown("**Please input your Google API Key here.**")
116
- api_key_input = gr.Textbox(
117
- label="Google API Key (Gemini)",
118
- placeholder="Paste your Google API Key here...",
119
- type="password",
120
- )
121
- api_save_btn = gr.Button("Save API Key to config.yaml")
122
- api_status = gr.Markdown()
123
 
124
- api_save_btn.click(
125
- fn=update_gemini_api_key,
126
- inputs=[api_key_input],
127
- outputs=[api_status],
128
- )
129
 
 
 
 
 
 
 
130
 
131
  if __name__ == "__main__":
132
- print("BOOT: about to launch gradio", flush=True)
133
  demo.launch(server_name="0.0.0.0", server_port=7860)
 
 
 
 
 
 
 
 
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)