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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +48 -17
app.py CHANGED
@@ -1,19 +1,3 @@
1
- import os
2
-
3
- # ==========================================
4
- # --- 🛡️ 核心修复:镇压 Gradio 底层网络冲突 ---
5
- # ==========================================
6
- # 1. 彻底干掉引起 500 错误的 SSR (兼容 Gradio 4.x 和 5.x)
7
- os.environ["GRADIO_SSR_MODE"] = "False"
8
- os.environ["GRADIO_NO_SSR"] = "1"
9
-
10
- # 2. 关闭可能在 HF 容器中导致网络阻塞的官方遥测
11
- os.environ["GRADIO_ANALYTICS_ENABLED"] = "False"
12
-
13
- # 3. 强行锁定容器的内网穿透端口,防止代理迷路
14
- os.environ["GRADIO_SERVER_NAME"] = "0.0.0.0"
15
- os.environ["GRADIO_SERVER_PORT"] = "7860"
16
-
17
  import gradio as gr
18
  import os
19
  import shutil
@@ -66,6 +50,51 @@ def get_user_dirs(session_id):
66
  os.makedirs(output_dir, exist_ok=True)
67
  return papers_dir, output_dir, zip_path
68
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
69
  # ==========================================
70
  # --- 🎨 Custom Purple Theme Definition ---
71
  # ==========================================
@@ -709,5 +738,7 @@ with gr.Blocks(theme=purple_theme, css=custom_css) as demo:
709
  )
710
 
711
  if __name__ == "__main__":
 
712
  # 并发放宽至 5
713
- demo.queue(default_concurrency_limit=5).launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
  import os
3
  import shutil
 
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
  )
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)