Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,38 +5,42 @@ import shutil
|
|
| 5 |
import sys
|
| 6 |
from datetime import datetime
|
| 7 |
|
| 8 |
-
#
|
| 9 |
-
print("!!! CRITICAL STARTUP CHECK !!!", file=sys.stderr, flush=True)
|
| 10 |
-
|
| 11 |
-
# 初始化环境
|
| 12 |
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
|
| 13 |
PAPERS_DIR = os.path.join(BASE_DIR, "papers")
|
| 14 |
CONFIG_PATH = os.path.join(BASE_DIR, "config.yaml")
|
| 15 |
os.makedirs(PAPERS_DIR, exist_ok=True)
|
| 16 |
|
| 17 |
def get_debug_info():
|
| 18 |
-
"""
|
| 19 |
now = datetime.now().strftime("%H:%M:%S")
|
| 20 |
files = os.listdir(PAPERS_DIR) if os.path.exists(PAPERS_DIR) else "Directory missing"
|
| 21 |
|
| 22 |
config_content = "Not found"
|
| 23 |
if os.path.exists(CONFIG_PATH):
|
| 24 |
-
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
-
return f"[{now}] 📁
|
| 28 |
|
| 29 |
def save_pdf(file):
|
| 30 |
if file is None:
|
| 31 |
-
return "
|
| 32 |
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
def save_api_key(api_key):
|
| 38 |
if not api_key:
|
| 39 |
-
return "API Key
|
| 40 |
|
| 41 |
try:
|
| 42 |
config = {}
|
|
@@ -44,47 +48,72 @@ def save_api_key(api_key):
|
|
| 44 |
with open(CONFIG_PATH, "r", encoding="utf-8") as f:
|
| 45 |
config = yaml.safe_load(f) or {}
|
| 46 |
|
| 47 |
-
if "api_keys" not in config:
|
|
|
|
| 48 |
config["api_keys"]["gemini_api_key"] = api_key
|
| 49 |
|
| 50 |
with open(CONFIG_PATH, "w", encoding="utf-8") as f:
|
| 51 |
-
yaml.dump(config, f, allow_unicode=True)
|
| 52 |
|
| 53 |
-
return "✅ API Key
|
| 54 |
except Exception as e:
|
| 55 |
-
return f"❌
|
| 56 |
|
| 57 |
-
# --- UI
|
| 58 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 59 |
-
gr.Markdown("#
|
| 60 |
|
| 61 |
with gr.Row():
|
|
|
|
| 62 |
with gr.Column(scale=2):
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
with gr.Column(scale=1):
|
| 75 |
-
gr.Markdown("### 🔍
|
| 76 |
debug_view = gr.Textbox(
|
| 77 |
-
label="
|
| 78 |
value=get_debug_info(),
|
| 79 |
-
lines=
|
| 80 |
interactive=False
|
| 81 |
)
|
| 82 |
-
refresh_btn = gr.Button("🔄
|
| 83 |
|
| 84 |
-
# 绑定逻辑
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
|
| 89 |
if __name__ == "__main__":
|
| 90 |
demo.launch()
|
|
|
|
| 5 |
import sys
|
| 6 |
from datetime import datetime
|
| 7 |
|
| 8 |
+
# 初始化环境路径
|
|
|
|
|
|
|
|
|
|
| 9 |
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
|
| 10 |
PAPERS_DIR = os.path.join(BASE_DIR, "papers")
|
| 11 |
CONFIG_PATH = os.path.join(BASE_DIR, "config.yaml")
|
| 12 |
os.makedirs(PAPERS_DIR, exist_ok=True)
|
| 13 |
|
| 14 |
def get_debug_info():
|
| 15 |
+
"""读取服务器文件系统状态,用于实时监控"""
|
| 16 |
now = datetime.now().strftime("%H:%M:%S")
|
| 17 |
files = os.listdir(PAPERS_DIR) if os.path.exists(PAPERS_DIR) else "Directory missing"
|
| 18 |
|
| 19 |
config_content = "Not found"
|
| 20 |
if os.path.exists(CONFIG_PATH):
|
| 21 |
+
try:
|
| 22 |
+
with open(CONFIG_PATH, "r", encoding="utf-8") as f:
|
| 23 |
+
config_content = f.read()
|
| 24 |
+
except Exception:
|
| 25 |
+
config_content = "Error reading config"
|
| 26 |
|
| 27 |
+
return f"[{now}] 📁 papers/ 文件夹下的文件:\n{files}\n\n[{now}] 📄 config.yaml 完整内容:\n{config_content}"
|
| 28 |
|
| 29 |
def save_pdf(file):
|
| 30 |
if file is None:
|
| 31 |
+
return "❌ 请先选择一个 PDF 文件", get_debug_info()
|
| 32 |
|
| 33 |
+
try:
|
| 34 |
+
file_name = os.path.basename(file.name)
|
| 35 |
+
file_path = os.path.join(PAPERS_DIR, file_name)
|
| 36 |
+
shutil.copy(file.name, file_path)
|
| 37 |
+
return f"✅ 成功保存文件: {file_name}", get_debug_info()
|
| 38 |
+
except Exception as e:
|
| 39 |
+
return f"❌ 保存 PDF 出错: {str(e)}", get_debug_info()
|
| 40 |
|
| 41 |
def save_api_key(api_key):
|
| 42 |
if not api_key:
|
| 43 |
+
return "❌ API Key 不能为空", get_debug_info()
|
| 44 |
|
| 45 |
try:
|
| 46 |
config = {}
|
|
|
|
| 48 |
with open(CONFIG_PATH, "r", encoding="utf-8") as f:
|
| 49 |
config = yaml.safe_load(f) or {}
|
| 50 |
|
| 51 |
+
if "api_keys" not in config:
|
| 52 |
+
config["api_keys"] = {}
|
| 53 |
config["api_keys"]["gemini_api_key"] = api_key
|
| 54 |
|
| 55 |
with open(CONFIG_PATH, "w", encoding="utf-8") as f:
|
| 56 |
+
yaml.dump(config, f, allow_unicode=True, default_flow_style=False)
|
| 57 |
|
| 58 |
+
return "✅ API Key 已成功写入 config.yaml", get_debug_info()
|
| 59 |
except Exception as e:
|
| 60 |
+
return f"❌ 保存 Key 出错: {str(e)}", get_debug_info()
|
| 61 |
|
| 62 |
+
# --- 构建单页 UI ---
|
| 63 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 64 |
+
gr.Markdown("# 📑 PDF 助手管理后台")
|
| 65 |
|
| 66 |
with gr.Row():
|
| 67 |
+
# 左侧操作区
|
| 68 |
with gr.Column(scale=2):
|
| 69 |
+
# 第一部分:API 配置
|
| 70 |
+
with gr.Group():
|
| 71 |
+
gr.Markdown("### 1. 密钥配置")
|
| 72 |
+
key_input = gr.Textbox(
|
| 73 |
+
label="Gemini API Key",
|
| 74 |
+
type="password",
|
| 75 |
+
placeholder="在此输入您的 API Key..."
|
| 76 |
+
)
|
| 77 |
+
key_btn = gr.Button("保存配置", variant="primary")
|
| 78 |
+
key_status = gr.Textbox(label="配置状态", interactive=False)
|
| 79 |
+
|
| 80 |
+
gr.Markdown("---") # 分割线
|
| 81 |
+
|
| 82 |
+
# 第二部分:PDF 上传
|
| 83 |
+
with gr.Group():
|
| 84 |
+
gr.Markdown("### 2. 论文上传")
|
| 85 |
+
pdf_input = gr.File(label="选择 PDF 文件", file_types=[".pdf"])
|
| 86 |
+
pdf_btn = gr.Button("保存到 papers 文件夹", variant="primary")
|
| 87 |
+
pdf_status = gr.Textbox(label="上传状态", interactive=False)
|
| 88 |
+
|
| 89 |
+
# 右侧调试监控区
|
| 90 |
with gr.Column(scale=1):
|
| 91 |
+
gr.Markdown("### 🔍 实时系统监控")
|
| 92 |
debug_view = gr.Textbox(
|
| 93 |
+
label="服务器文件状态 (每操作一次即自动更新)",
|
| 94 |
value=get_debug_info(),
|
| 95 |
+
lines=20,
|
| 96 |
interactive=False
|
| 97 |
)
|
| 98 |
+
refresh_btn = gr.Button("🔄 手动刷新状态")
|
| 99 |
|
| 100 |
+
# 绑定事件逻辑
|
| 101 |
+
key_btn.click(
|
| 102 |
+
fn=save_api_key,
|
| 103 |
+
inputs=key_input,
|
| 104 |
+
outputs=[key_status, debug_view]
|
| 105 |
+
)
|
| 106 |
+
|
| 107 |
+
pdf_btn.click(
|
| 108 |
+
fn=save_pdf,
|
| 109 |
+
inputs=pdf_input,
|
| 110 |
+
outputs=[pdf_status, debug_view]
|
| 111 |
+
)
|
| 112 |
+
|
| 113 |
+
refresh_btn.click(
|
| 114 |
+
fn=get_debug_info,
|
| 115 |
+
outputs=debug_view
|
| 116 |
+
)
|
| 117 |
|
| 118 |
if __name__ == "__main__":
|
| 119 |
demo.launch()
|