Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,83 +3,88 @@ import os
|
|
| 3 |
import yaml
|
| 4 |
import shutil
|
| 5 |
import sys
|
|
|
|
| 6 |
|
| 7 |
-
# 强制
|
| 8 |
-
print("
|
| 9 |
|
| 10 |
-
#
|
| 11 |
-
os.
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
-
def
|
| 14 |
-
"""
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
def save_pdf(file):
|
| 18 |
if file is None:
|
| 19 |
-
return "Please upload a PDF
|
| 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
|
| 38 |
-
|
| 39 |
-
config_path = "config.yaml"
|
| 40 |
-
debug_log("Attempting to save API Key...")
|
| 41 |
|
| 42 |
try:
|
| 43 |
config = {}
|
| 44 |
-
if os.path.exists(
|
| 45 |
-
with open(
|
| 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(
|
| 54 |
-
yaml.dump(config, f, allow_unicode=True
|
| 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 "
|
| 63 |
except Exception as e:
|
| 64 |
-
|
| 65 |
-
return f"Error: {str(e)}"
|
| 66 |
|
| 67 |
-
|
| 68 |
-
|
|
|
|
| 69 |
|
| 70 |
-
with gr.
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
|
| 82 |
if __name__ == "__main__":
|
| 83 |
-
debug_log("Gradio Interface launching...")
|
| 84 |
-
# HF Spaces 环境下 server_name 和 port 通常由平台处理,简单 launch 即可
|
| 85 |
demo.launch()
|
|
|
|
| 3 |
import yaml
|
| 4 |
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 |
+
with open(CONFIG_PATH, "r", encoding="utf-8") as f:
|
| 25 |
+
config_content = f.read()
|
| 26 |
+
|
| 27 |
+
return f"[{now}] 📁 Files in papers/: {files}\n\n[{now}] 📄 config.yaml content:\n{config_content}"
|
| 28 |
|
| 29 |
def save_pdf(file):
|
| 30 |
if file is None:
|
| 31 |
+
return "Please upload a PDF.", get_debug_info()
|
|
|
|
|
|
|
| 32 |
|
| 33 |
+
file_path = os.path.join(PAPERS_DIR, os.path.basename(file.name))
|
|
|
|
|
|
|
| 34 |
shutil.copy(file.name, file_path)
|
| 35 |
+
return f"✅ Saved {os.path.basename(file.name)}", get_debug_info()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
def save_api_key(api_key):
|
| 38 |
if not api_key:
|
| 39 |
+
return "API Key is empty.", get_debug_info()
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
try:
|
| 42 |
config = {}
|
| 43 |
+
if os.path.exists(CONFIG_PATH):
|
| 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: config["api_keys"] = {}
|
|
|
|
|
|
|
| 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 saved!", get_debug_info()
|
| 54 |
except Exception as e:
|
| 55 |
+
return f"❌ Error: {str(e)}", get_debug_info()
|
|
|
|
| 56 |
|
| 57 |
+
# --- UI 界面 ---
|
| 58 |
+
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 59 |
+
gr.Markdown("# 🛠️ Space Files Debugger")
|
| 60 |
|
| 61 |
+
with gr.Row():
|
| 62 |
+
with gr.Column(scale=2):
|
| 63 |
+
with gr.Tab("1. Upload PDF"):
|
| 64 |
+
pdf_input = gr.File(label="Upload PDF", file_types=[".pdf"])
|
| 65 |
+
pdf_btn = gr.Button("Save PDF")
|
| 66 |
+
pdf_msg = gr.Textbox(label="Status")
|
| 67 |
+
|
| 68 |
+
with gr.Tab("2. Config"):
|
| 69 |
+
key_input = gr.Textbox(label="Gemini API Key", type="password")
|
| 70 |
+
key_btn = gr.Button("Save Key")
|
| 71 |
+
key_msg = gr.Textbox(label="Status")
|
| 72 |
|
| 73 |
+
# 右侧调试面板��实时显示服务器文件系统状态
|
| 74 |
+
with gr.Column(scale=1):
|
| 75 |
+
gr.Markdown("### 🔍 System Real-time Monitor")
|
| 76 |
+
debug_view = gr.Textbox(
|
| 77 |
+
label="Server File Explorer",
|
| 78 |
+
value=get_debug_info(),
|
| 79 |
+
lines=15,
|
| 80 |
+
interactive=False
|
| 81 |
+
)
|
| 82 |
+
refresh_btn = gr.Button("🔄 Force Refresh View")
|
| 83 |
+
|
| 84 |
+
# 绑定逻辑:每次操作都更新右侧的调试面板
|
| 85 |
+
pdf_btn.click(save_pdf, inputs=pdf_input, outputs=[pdf_msg, debug_view])
|
| 86 |
+
key_btn.click(save_api_key, inputs=key_input, outputs=[key_msg, debug_view])
|
| 87 |
+
refresh_btn.click(get_debug_info, outputs=debug_view)
|
| 88 |
|
| 89 |
if __name__ == "__main__":
|
|
|
|
|
|
|
| 90 |
demo.launch()
|