Spaces:
Build error
Build error
liuyiyang01
commited on
Commit
·
acf15aa
1
Parent(s):
ef0b384
create user session folder
Browse files- app.py +35 -17
- app_utils.py +6 -0
app.py
CHANGED
|
@@ -10,9 +10,14 @@ from typing import Optional, List
|
|
| 10 |
import numpy as np
|
| 11 |
from datetime import datetime, timedelta
|
| 12 |
from collections import defaultdict
|
|
|
|
| 13 |
|
| 14 |
# os.environ["SPACES_QUEUE_ENABLED"] = "true"
|
| 15 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
# 后端API配置(可配置化)
|
| 17 |
BACKEND_URL = os.getenv("BACKEND_URL", "http://47.95.6.204:51001/")
|
| 18 |
API_ENDPOINTS = {
|
|
@@ -92,6 +97,13 @@ def log_submission(scene: str, prompt: str, model: str, max_step: int, user: str
|
|
| 92 |
with open(SUBMISSION_LOG, "a") as f:
|
| 93 |
f.write(json.dumps(log_entry) + "\n")
|
| 94 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 95 |
def read_logs(log_type: str = "all", max_entries: int = 50) -> list:
|
| 96 |
"""读取日志文件"""
|
| 97 |
logs = []
|
|
@@ -227,10 +239,13 @@ def stream_simulation_results(result_folder: str, task_id: str, fps: int = 30):
|
|
| 227 |
if max_time <= 0:
|
| 228 |
raise gr.Error("timeout 240s")
|
| 229 |
|
| 230 |
-
def create_video_segment(frames: List[np.ndarray], fps: int, width: int, height: int) -> str:
|
| 231 |
"""创建视频片段"""
|
| 232 |
-
os.
|
| 233 |
-
|
|
|
|
|
|
|
|
|
|
| 234 |
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
|
| 235 |
out = cv2.VideoWriter(segment_name, fourcc, fps, (width, height))
|
| 236 |
|
|
@@ -371,7 +386,7 @@ def run_simulation(
|
|
| 371 |
max_step: int,
|
| 372 |
history: list,
|
| 373 |
request: gr.Request
|
| 374 |
-
)
|
| 375 |
"""运行仿真并更新历史记录"""
|
| 376 |
# 获取当前时间
|
| 377 |
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
|
@@ -619,6 +634,16 @@ custom_css = """
|
|
| 619 |
}
|
| 620 |
"""
|
| 621 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 622 |
# 创建Gradio界面
|
| 623 |
with gr.Blocks(title="InternManip Model Inference Demo", css=custom_css) as demo:
|
| 624 |
gr.HTML(header_html)
|
|
@@ -753,30 +778,23 @@ with gr.Blocks(title="InternManip Model Inference Demo", css=custom_css) as demo
|
|
| 753 |
|
| 754 |
# 初始化场景描述和日志
|
| 755 |
demo.load(
|
|
|
|
|
|
|
| 756 |
fn=lambda: update_scene_display("scene_1"),
|
| 757 |
outputs=[scene_description, scene_preview]
|
| 758 |
).then(
|
| 759 |
-
fn=update_log_display,
|
| 760 |
-
outputs=logs_display
|
| 761 |
-
)
|
| 762 |
-
|
| 763 |
-
# 记录访问
|
| 764 |
-
def record_access(request: gr.Request):
|
| 765 |
-
user_ip = request.client.host if request else "unknown"
|
| 766 |
-
user_agent = request.headers.get("user-agent", "unknown")
|
| 767 |
-
log_access(user_ip, user_agent)
|
| 768 |
-
return update_log_display()
|
| 769 |
-
|
| 770 |
-
demo.load(
|
| 771 |
fn=record_access,
|
| 772 |
inputs=None,
|
| 773 |
outputs=logs_display,
|
| 774 |
queue=False
|
|
|
|
|
|
|
|
|
|
| 775 |
)
|
| 776 |
|
| 777 |
demo.queue(default_concurrency_limit=8)
|
| 778 |
|
| 779 |
-
demo.unload(fn=cleanup_session)
|
| 780 |
|
| 781 |
|
| 782 |
# 启动应用
|
|
|
|
| 10 |
import numpy as np
|
| 11 |
from datetime import datetime, timedelta
|
| 12 |
from collections import defaultdict
|
| 13 |
+
import shutil
|
| 14 |
|
| 15 |
# os.environ["SPACES_QUEUE_ENABLED"] = "true"
|
| 16 |
|
| 17 |
+
from app_utils import (
|
| 18 |
+
TMP_ROOT,
|
| 19 |
+
)
|
| 20 |
+
|
| 21 |
# 后端API配置(可配置化)
|
| 22 |
BACKEND_URL = os.getenv("BACKEND_URL", "http://47.95.6.204:51001/")
|
| 23 |
API_ENDPOINTS = {
|
|
|
|
| 97 |
with open(SUBMISSION_LOG, "a") as f:
|
| 98 |
f.write(json.dumps(log_entry) + "\n")
|
| 99 |
|
| 100 |
+
# 记录访问
|
| 101 |
+
def record_access(request: gr.Request):
|
| 102 |
+
user_ip = request.client.host if request else "unknown"
|
| 103 |
+
user_agent = request.headers.get("user-agent", "unknown")
|
| 104 |
+
log_access(user_ip, user_agent)
|
| 105 |
+
return update_log_display()
|
| 106 |
+
|
| 107 |
def read_logs(log_type: str = "all", max_entries: int = 50) -> list:
|
| 108 |
"""读取日志文件"""
|
| 109 |
logs = []
|
|
|
|
| 239 |
if max_time <= 0:
|
| 240 |
raise gr.Error("timeout 240s")
|
| 241 |
|
| 242 |
+
def create_video_segment(frames: List[np.ndarray], fps: int, width: int, height: int, req: gr.Request) -> str:
|
| 243 |
"""创建视频片段"""
|
| 244 |
+
user_dir = os.path.join(TMP_ROOT, str(req.session_hash))
|
| 245 |
+
os.makedirs(user_dir, exist_ok=True)
|
| 246 |
+
video_chunk_path = os.path.join(user_dir, "tasks/video_chunk")
|
| 247 |
+
os.makedirs(video_chunk_path, exist_ok=True)
|
| 248 |
+
segment_name = os.path.join(video_chunk_path, f"output_{uuid.uuid4()}.mp4")
|
| 249 |
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
|
| 250 |
out = cv2.VideoWriter(segment_name, fourcc, fps, (width, height))
|
| 251 |
|
|
|
|
| 386 |
max_step: int,
|
| 387 |
history: list,
|
| 388 |
request: gr.Request
|
| 389 |
+
):
|
| 390 |
"""运行仿真并更新历史记录"""
|
| 391 |
# 获取当前时间
|
| 392 |
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
|
|
|
| 634 |
}
|
| 635 |
"""
|
| 636 |
|
| 637 |
+
def start_session(req: gr.Request):
|
| 638 |
+
user_dir = os.path.join(TMP_ROOT, str(req.session_hash))
|
| 639 |
+
os.makedirs(user_dir, exist_ok=True)
|
| 640 |
+
|
| 641 |
+
|
| 642 |
+
def end_session(req: gr.Request):
|
| 643 |
+
user_dir = os.path.join(TMP_ROOT, str(req.session_hash))
|
| 644 |
+
shutil.rmtree(user_dir)
|
| 645 |
+
|
| 646 |
+
|
| 647 |
# 创建Gradio界面
|
| 648 |
with gr.Blocks(title="InternManip Model Inference Demo", css=custom_css) as demo:
|
| 649 |
gr.HTML(header_html)
|
|
|
|
| 778 |
|
| 779 |
# 初始化场景描述和日志
|
| 780 |
demo.load(
|
| 781 |
+
start_session
|
| 782 |
+
).then(
|
| 783 |
fn=lambda: update_scene_display("scene_1"),
|
| 784 |
outputs=[scene_description, scene_preview]
|
| 785 |
).then(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 786 |
fn=record_access,
|
| 787 |
inputs=None,
|
| 788 |
outputs=logs_display,
|
| 789 |
queue=False
|
| 790 |
+
).then(
|
| 791 |
+
fn=update_log_display,
|
| 792 |
+
outputs=logs_display
|
| 793 |
)
|
| 794 |
|
| 795 |
demo.queue(default_concurrency_limit=8)
|
| 796 |
|
| 797 |
+
demo.unload(fn=cleanup_session).then(end_session)
|
| 798 |
|
| 799 |
|
| 800 |
# 启动应用
|
app_utils.py
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import os
|
| 3 |
+
|
| 4 |
+
|
| 5 |
+
TMP_ROOT = os.path.join(os.path.dirname(os.path.abspath(__file__)), "tmp")
|
| 6 |
+
os.makedirs(TMP_ROOT, exist_ok=True)
|