Spaces:
Sleeping
Sleeping
Update app.py
Browse files11 unmodified lines121314151617181920212223242526272829303132331516171819202122232413 unmodified lines383940535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697414243637 unmodified lines68168268374168474374468511 unmodified linesfrom gradio.themes import Softfrom gradio.themes.utils import colors, fonts, sizes
import threadingimport subprocess# ==========================================# --- 🌐 异步安装 Playwright 浏览器 ---# ==========================================def setup_playwright(): """在后台静默安装 Playwright,防止阻塞 Gradio 启动导致 HF 500 超时""" try: import playwright print("⏳ [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()# 自动安装 Playwright 浏览器(如果不存在)try: import playwright # 检查是否已经安装了浏览器,没有则下载 subprocess.run(["playwright", "install", "chromium"], check=True)except Exception as e: print(f"Playwright setup failed: {e}")
# ==========================================# --- 📁 全局目录配置 (修改为 Session 基础目录) ---13 unmodified lines os.makedirs(output_dir, exist_ok=True) return papers_dir, output_dir, zip_path
import time
# ==========================================# --- 🧹 垃圾回收 (后台清理过期 Session) ---# ==========================================# 设定 Session 过期时间(例如:2 小时 = 7200 秒)SESSION_MAX_AGE_SECONDS = 2 * 60 * 60# 设定清理器执行间隔(例如:每 30 分钟扫描一次 = 1800 秒)CLEANUP_INTERVAL_SECONDS = 30 * 60
def 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) demo.queue(default_concurrency_limit=5).launch()app.py+8-67