Spaces:
Sleeping
Sleeping
Update app.py
Browse files11 unmodified lines1213141516171819202122232425262728293031323334353613 unmodified lines5051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100637 unmodified lines73873974074174274374411 unmodified linesfrom gradio.themes import Softfrom gradio.themes.utils import colors, fonts, sizesimport threadingimport subprocess# ==========================================# --- 🌐 异步安装 Playwright 浏览器 ---# ==========================================def setup_playwright():"""在后台静默安装 Playwright,防止阻塞 Gradio 启动导致 HF 500 超时"""try:import playwrightprint("⏳ [System] Downloading Playwright Chromium in background...")# 增加 --with-deps 尝试安装系统级依赖 (虽然在非 root 容器可能失效,但有备无患)subprocess.run(["playwright", "install", "chromium"], check=True)print("✅ [System] Playwright browsers ready.")except Exception as e:print(f"❌ [System] Playwright setup failed: {e}")# 这一步非常关键:启动一个后台守护线程去下载,主进程直接往下走!threading.Thread(target=setup_playwright, daemon=True).start()# ==========================================# --- 📁 全局目录配置 (修改为 Session 基础目录) ---13 unmodified linesos.makedirs(output_dir, exist_ok=True)return papers_dir, output_dir, zip_pathimport time# ==========================================# --- 🧹 垃圾回收 (后台清理过期 Session) ---# ==========================================# 设定 Session 过期时间(例如:2 小时 = 7200 秒)SESSION_MAX_AGE_SECONDS = 2 * 60 * 60# 设定清理器执行间隔(例如:每 30 分钟扫描一次 = 1800 秒)CLEANUP_INTERVAL_SECONDS = 30 * 60def cleanup_expired_sessions():"""后台运行的垃圾回收任务"""while True:try:if os.path.exists(SESSIONS_BASE_DIR):current_time = time.time()for session_folder in os.listdir(SESSIONS_BASE_DIR):folder_path = os.path.join(SESSIONS_BASE_DIR, session_folder)# 确保只处理目录if os.path.isdir(folder_path):# 获取文件夹的最后修改时间folder_mtime = os.path.getmtime(folder_path)# 判断是否超过了最大存活时间if (current_time - folder_mtime) > SESSION_MAX_AGE_SECONDS:try:shutil.rmtree(folder_path)print(f"🧹 [Garbage Collector] Deleted expired session: {session_folder}")except Exception as e:print(f"⚠️ [Garbage Collector] Failed to delete {session_folder}: {e}")except Exception as e:print(f"⚠️ [Garbage Collector] Error during cleanup scan: {e}")# 休眠到下一次扫描时间time.sleep(CLEANUP_INTERVAL_SECONDS)def start_garbage_collector():"""启动后台守护线程"""gc_thread = threading.Thread(target=cleanup_expired_sessions, daemon=True)gc_thread.start()print("🚀 [Garbage Collector] Background cleanup service started.")# ==========================================# --- 🎨 Custom Purple Theme Definition ---# ==========================================637 unmodified lines)if __name__ == "__main__":start_garbage_collector()# 并发放宽至 5# demo.queue(default_concurrency_limit=5).launch()demo.launch(server_name="0.0.0.0", server_port=7860)11 unmodified lines1213141516171819202122232413 unmodified lines383940414243637 unmodified lines68168268368468511 unmodified linesfrom gradio.themes import Softfrom gradio.themes.utils import colors, fonts, sizes# 自动安装 Playwright 浏览器(如果不存在)try:import playwright# 检查是否已经安装了浏览器,没有则下载subprocess.run(["playwright", "install", "chromium"], check=True)except Exception as e:print(f"Playwright setup failed: {e}")# ==========================================# --- 📁 全局目录配置 (修改为 Session 基础目录) ---13 unmodified linesos.makedirs(output_dir, exist_ok=True)return papers_dir, output_dir, zip_path# ==========================================# --- 🎨 Custom Purple Theme Definition ---# ==========================================637 unmodified lines)if __name__ == "__main__":# 并发放宽至 5demo.queue(default_concurrency_limit=5).launch()
app.py+8-67