Laramie2 commited on
Commit
3760f2a
·
verified ·
1 Parent(s): c4f76be

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +8 -67
app.py CHANGED
@@ -12,25 +12,13 @@ from typing import Iterable
12
  from gradio.themes import Soft
13
  from gradio.themes.utils import colors, fonts, sizes
14
 
15
-
16
- import threading
17
- import subprocess
18
- # ==========================================
19
- # --- 🌐 异步安装 Playwright 浏览器 ---
20
- # ==========================================
21
- def setup_playwright():
22
- """在后台静默安装 Playwright,防止阻塞 Gradio 启动导致 HF 500 超时"""
23
- try:
24
- import playwright
25
- print("⏳ [System] Downloading Playwright Chromium in background...")
26
- # 增加 --with-deps 尝试安装系统级依赖 (虽然在非 root 容器可能失效,但有备无患)
27
- subprocess.run(["playwright", "install", "chromium"], check=True)
28
- print("✅ [System] Playwright browsers ready.")
29
- except Exception as e:
30
- print(f"❌ [System] Playwright setup failed: {e}")
31
-
32
- # 这一步非常关键:启动一个后台守护线程去下载,主进程直接往下走!
33
- threading.Thread(target=setup_playwright, daemon=True).start()
34
 
35
  # ==========================================
36
  # --- 📁 全局目录配置 (修改为 Session 基础目录) ---
@@ -50,51 +38,6 @@ def get_user_dirs(session_id):
50
  os.makedirs(output_dir, exist_ok=True)
51
  return papers_dir, output_dir, zip_path
52
 
53
-
54
- import time
55
-
56
- # ==========================================
57
- # --- 🧹 垃圾回收 (后台清理过期 Session) ---
58
- # ==========================================
59
- # 设定 Session 过期时间(例如:2 小时 = 7200 秒)
60
- SESSION_MAX_AGE_SECONDS = 2 * 60 * 60
61
- # 设定清理器执行间隔(例如:每 30 分钟扫描一次 = 1800 秒)
62
- CLEANUP_INTERVAL_SECONDS = 30 * 60
63
-
64
- def cleanup_expired_sessions():
65
- """后台运行的垃圾回收任务"""
66
- while True:
67
- try:
68
- if os.path.exists(SESSIONS_BASE_DIR):
69
- current_time = time.time()
70
- for session_folder in os.listdir(SESSIONS_BASE_DIR):
71
- folder_path = os.path.join(SESSIONS_BASE_DIR, session_folder)
72
-
73
- # 确保只处理目录
74
- if os.path.isdir(folder_path):
75
- # 获取文件夹的最后修改时间
76
- folder_mtime = os.path.getmtime(folder_path)
77
-
78
- # 判断是否超过了最大存活时间
79
- if (current_time - folder_mtime) > SESSION_MAX_AGE_SECONDS:
80
- try:
81
- shutil.rmtree(folder_path)
82
- print(f"🧹 [Garbage Collector] Deleted expired session: {session_folder}")
83
- except Exception as e:
84
- print(f"⚠️ [Garbage Collector] Failed to delete {session_folder}: {e}")
85
- except Exception as e:
86
- print(f"⚠️ [Garbage Collector] Error during cleanup scan: {e}")
87
-
88
- # 休眠到下一次扫描时间
89
- time.sleep(CLEANUP_INTERVAL_SECONDS)
90
-
91
- def start_garbage_collector():
92
- """启动后台守护线程"""
93
- gc_thread = threading.Thread(target=cleanup_expired_sessions, daemon=True)
94
- gc_thread.start()
95
- print("🚀 [Garbage Collector] Background cleanup service started.")
96
-
97
-
98
  # ==========================================
99
  # --- 🎨 Custom Purple Theme Definition ---
100
  # ==========================================
@@ -738,7 +681,5 @@ with gr.Blocks(theme=purple_theme, css=custom_css) as demo:
738
  )
739
 
740
  if __name__ == "__main__":
741
- start_garbage_collector()
742
  # 并发放宽至 5
743
- # demo.queue(default_concurrency_limit=5).launch()
744
- demo.launch(server_name="0.0.0.0", server_port=7860)
 
12
  from gradio.themes import Soft
13
  from gradio.themes.utils import colors, fonts, sizes
14
 
15
+ # 自动安装 Playwright 浏览器(如果不存在)
16
+ try:
17
+ import playwright
18
+ # 检查是否已经安装了浏览器,没有则下载
19
+ subprocess.run(["playwright", "install", "chromium"], check=True)
20
+ except Exception as e:
21
+ print(f"Playwright setup failed: {e}")
 
 
 
 
 
 
 
 
 
 
 
 
22
 
23
  # ==========================================
24
  # --- 📁 全局目录配置 (修改为 Session 基础目录) ---
 
38
  os.makedirs(output_dir, exist_ok=True)
39
  return papers_dir, output_dir, zip_path
40
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
41
  # ==========================================
42
  # --- 🎨 Custom Purple Theme Definition ---
43
  # ==========================================
 
681
  )
682
 
683
  if __name__ == "__main__":
 
684
  # 并发放宽至 5
685
+ demo.queue(default_concurrency_limit=5).launch()