Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,91 +4,82 @@ import yaml
|
|
| 4 |
import shutil
|
| 5 |
import sys
|
| 6 |
|
| 7 |
-
#
|
| 8 |
-
|
| 9 |
-
PAPERS_DIR = os.path.join(BASE_DIR, "papers")
|
| 10 |
-
CONFIG_PATH = os.path.join(BASE_DIR, "config.yaml")
|
| 11 |
|
| 12 |
-
# 确保文件夹存在
|
| 13 |
-
os.makedirs(
|
| 14 |
|
| 15 |
-
def
|
| 16 |
-
"""
|
| 17 |
-
print(f"
|
| 18 |
|
| 19 |
def save_pdf(file):
|
| 20 |
if file is None:
|
| 21 |
-
return "
|
| 22 |
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
else:
|
| 37 |
-
return "❌ Failed to verify file on disk."
|
| 38 |
-
except Exception as e:
|
| 39 |
-
logger(f"Error saving PDF: {str(e)}")
|
| 40 |
-
return f"❌ Error: {str(e)}"
|
| 41 |
|
| 42 |
def save_api_key(api_key):
|
| 43 |
-
if not api_key
|
| 44 |
-
return "
|
|
|
|
|
|
|
|
|
|
| 45 |
|
| 46 |
try:
|
| 47 |
-
# 读取或初始化配置
|
| 48 |
config = {}
|
| 49 |
-
if os.path.exists(
|
| 50 |
-
with open(
|
| 51 |
config = yaml.safe_load(f) or {}
|
| 52 |
|
| 53 |
-
# 更新配置结构
|
| 54 |
if "api_keys" not in config:
|
| 55 |
config["api_keys"] = {}
|
|
|
|
| 56 |
config["api_keys"]["gemini_api_key"] = api_key
|
| 57 |
|
| 58 |
-
|
| 59 |
-
with open(CONFIG_PATH, "w", encoding="utf-8") as f:
|
| 60 |
yaml.dump(config, f, allow_unicode=True, default_flow_style=False)
|
| 61 |
|
| 62 |
-
#
|
| 63 |
-
with open(
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
logger(verify_content.replace(api_key, api_key[:5] + "******"))
|
| 68 |
|
| 69 |
-
return "
|
| 70 |
except Exception as e:
|
| 71 |
-
|
| 72 |
-
return f"
|
| 73 |
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
gr.Markdown("# 📄 AI Paper Assistant")
|
| 77 |
|
| 78 |
-
with gr.Tab("
|
| 79 |
-
gr.
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
api_btn.click(save_api_key, inputs=api_input, outputs=api_status)
|
| 84 |
|
| 85 |
-
with gr.Tab("
|
| 86 |
-
gr.
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
pdf_btn.click(save_pdf, inputs=pdf_input, outputs=pdf_status)
|
| 91 |
|
| 92 |
if __name__ == "__main__":
|
| 93 |
-
|
|
|
|
| 94 |
demo.launch()
|
|
|
|
| 4 |
import shutil
|
| 5 |
import sys
|
| 6 |
|
| 7 |
+
# 强制在启动时打印 START,并直接输出到 stderr 确保可见
|
| 8 |
+
print("==== [SYSTEM] APP STARTING... ====", file=sys.stderr, flush=True)
|
|
|
|
|
|
|
| 9 |
|
| 10 |
+
# 确保 papers 文件夹存在
|
| 11 |
+
os.makedirs("papers", exist_ok=True)
|
| 12 |
|
| 13 |
+
def debug_log(message):
|
| 14 |
+
"""自定义调试函数,确保内容立即出现在日志中"""
|
| 15 |
+
print(f"[DEBUG] {message}", file=sys.stderr, flush=True)
|
| 16 |
|
| 17 |
def save_pdf(file):
|
| 18 |
if file is None:
|
| 19 |
+
return "Please upload a PDF file."
|
| 20 |
|
| 21 |
+
debug_log(f"Received file: {file.name}")
|
| 22 |
+
|
| 23 |
+
# 获取文件名并保存
|
| 24 |
+
file_name = os.path.basename(file.name)
|
| 25 |
+
file_path = os.path.join("papers", file_name)
|
| 26 |
+
shutil.copy(file.name, file_path)
|
| 27 |
+
|
| 28 |
+
# 打印文件夹下的所有文件
|
| 29 |
+
all_files = os.listdir("papers")
|
| 30 |
+
debug_log(f"File saved to {file_path}")
|
| 31 |
+
debug_log(f"Current files in 'papers/': {all_files}")
|
| 32 |
+
|
| 33 |
+
return f"File '{file_name}' saved. Total files in folder: {len(all_files)}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
def save_api_key(api_key):
|
| 36 |
+
if not api_key:
|
| 37 |
+
return "API Key cannot be empty."
|
| 38 |
+
|
| 39 |
+
config_path = "config.yaml"
|
| 40 |
+
debug_log("Attempting to save API Key...")
|
| 41 |
|
| 42 |
try:
|
|
|
|
| 43 |
config = {}
|
| 44 |
+
if os.path.exists(config_path):
|
| 45 |
+
with open(config_path, "r", encoding="utf-8") as f:
|
| 46 |
config = yaml.safe_load(f) or {}
|
| 47 |
|
|
|
|
| 48 |
if "api_keys" not in config:
|
| 49 |
config["api_keys"] = {}
|
| 50 |
+
|
| 51 |
config["api_keys"]["gemini_api_key"] = api_key
|
| 52 |
|
| 53 |
+
with open(config_path, "w", encoding="utf-8") as f:
|
|
|
|
| 54 |
yaml.dump(config, f, allow_unicode=True, default_flow_style=False)
|
| 55 |
|
| 56 |
+
# 验证并打印 config 内容 (脱敏处理)
|
| 57 |
+
with open(config_path, "r", encoding="utf-8") as f:
|
| 58 |
+
current_config = f.read()
|
| 59 |
+
# 打印前20个字符确认文件结构,不打印完整 key
|
| 60 |
+
debug_log(f"Config updated. Content preview: {current_config[:20]}...")
|
|
|
|
| 61 |
|
| 62 |
+
return "Gemini API Key updated successfully!"
|
| 63 |
except Exception as e:
|
| 64 |
+
debug_log(f"ERROR: {str(e)}")
|
| 65 |
+
return f"Error: {str(e)}"
|
| 66 |
|
| 67 |
+
with gr.Blocks(title="PDF Uploader & Config") as demo:
|
| 68 |
+
gr.Markdown("# PDF and API Configuration")
|
|
|
|
| 69 |
|
| 70 |
+
with gr.Tab("Upload PDF"):
|
| 71 |
+
file_input = gr.File(label="Upload your PDF", file_types=[".pdf"])
|
| 72 |
+
upload_btn = gr.Button("Save PDF")
|
| 73 |
+
upload_output = gr.Textbox(label="Status")
|
| 74 |
+
upload_btn.click(save_pdf, inputs=file_input, outputs=upload_output)
|
|
|
|
| 75 |
|
| 76 |
+
with gr.Tab("Gemini Configuration"):
|
| 77 |
+
key_input = gr.Textbox(label="Enter Gemini API Key", type="password")
|
| 78 |
+
key_btn = gr.Button("Save API Key")
|
| 79 |
+
key_output = gr.Textbox(label="Status")
|
| 80 |
+
key_btn.click(save_api_key, inputs=key_input, outputs=key_output)
|
|
|
|
| 81 |
|
| 82 |
if __name__ == "__main__":
|
| 83 |
+
debug_log("Gradio Interface launching...")
|
| 84 |
+
# HF Spaces 环境下 server_name 和 port 通常由平台处理,简单 launch 即可
|
| 85 |
demo.launch()
|