Julian Bilcke
commited on
Commit
·
6abca92
1
Parent(s):
eb94d89
replace files
Browse files- Dockerfile +1 -2
- README.md +57 -107
- api_engine.py +363 -0
- api_server.py +649 -0
- api_utils.py +202 -0
- requirements.txt +2 -0
- run_api_on_hf.py +148 -0
Dockerfile
CHANGED
|
@@ -51,5 +51,4 @@ EXPOSE 8080
|
|
| 51 |
|
| 52 |
ENV PORT 8080
|
| 53 |
|
| 54 |
-
|
| 55 |
-
CMD ["python3", "run_hf_space.py"]
|
|
|
|
| 51 |
|
| 52 |
ENV PORT 8080
|
| 53 |
|
| 54 |
+
CMD ["python3", "run_api_on_hf.py"]
|
|
|
README.md
CHANGED
|
@@ -1,5 +1,5 @@
|
|
| 1 |
---
|
| 2 |
-
title: "Matrix"
|
| 3 |
emoji: 🐟
|
| 4 |
colorFrom: blue
|
| 5 |
colorTo: blue
|
|
@@ -11,113 +11,71 @@ app_port: 8080
|
|
| 11 |
disable_embedding: false
|
| 12 |
---
|
| 13 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
-
|
| 16 |
-
<!-- markdownlint-disable html -->
|
| 17 |
-
<!-- markdownlint-disable no-duplicate-header -->
|
| 18 |
|
| 19 |
-
|
| 20 |
-
<font size=7><div align='center' > [[🤗 Huggingface](https://huggingface.co/Skywork/Matrix-Game)] [[📖 Technical Report](https://github.com/SkyworkAI/Matrix-Game/blob/main/assets/report.pdf)] [[🚀 Project Website](https://matrix-game-homepage.github.io/)] </div></font>
|
| 21 |
-
|
| 22 |
-
<div align="center">
|
| 23 |
-
<img src="assets/videos/demo.gif" alt="teaser" />
|
| 24 |
-
</div>
|
| 25 |
|
| 26 |
## 📝 Overview
|
| 27 |
-
**Matrix-Game** is
|
| 28 |
-
|
| 29 |
-
## ✨ Key Features
|
| 30 |
-
|
| 31 |
-
- 🎯 **Feature 1**: **Interactive Generation.** A diffusion-based image-to-world model that generates high-quality videos conditioned on keyboard and mouse inputs, enabling fine-grained control and dynamic scene evolution.
|
| 32 |
-
- 🚀 **Feature 2**: **GameWorld Score.** A comprehensive benchmark for evaluating Minecraft world models across four key dimensions, including visual quality, temporal quality, action controllability, and physical rule understanding.
|
| 33 |
-
- 💡 **Feature 3**: **Matrix-Game Dataset** A large-scale Minecraft dataset with fine-grained action annotations, supporting scalable training for interactive and physically grounded world modeling.
|
| 34 |
-
|
| 35 |
-
## 🔥 Latest Updates
|
| 36 |
-
|
| 37 |
-
* [2025-05] 🎉 Initial release of Matrix-Game Model
|
| 38 |
-
|
| 39 |
-
## 🚀 Performance Comparison
|
| 40 |
-
### GameWorld Score Benchmark Comparison
|
| 41 |
-
|
| 42 |
-
| Model | Image Quality ↑ | Aesthetic Quality ↑ | Temporal Cons. ↑ | Motion Smooth. ↑ | Keyboard Acc. ↑ | Mouse Acc. ↑ | 3D Cons. ↑ |
|
| 43 |
-
|-----------|------------------|-------------|-------------------|-------------------|------------------|---------------|-------------|
|
| 44 |
-
| Oasis | 0.65 | 0.48 | 0.94 | **0.98** | 0.77 | 0.56 | 0.56 |
|
| 45 |
-
| MineWorld | 0.69 | 0.47 | 0.95 | **0.98** | 0.86 | 0.64 | 0.51 |
|
| 46 |
-
| **Ours** | **0.72** | **0.49** | **0.97** | **0.98** | **0.95** | **0.95** | **0.76** |
|
| 47 |
-
|
| 48 |
-
**Metric Descriptions**:
|
| 49 |
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
- **Keyboard Accuracy** / **Mouse Accuracy**: Accuracy in following user control signals
|
| 53 |
-
- **3D Consistency**: Geometric stability and physical plausibility over time
|
| 54 |
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
> Double-blind human evaluation by two independent groups across four key dimensions: **Overall Quality**, **Controllability**, **Visual Quality**, and **Temporal Consistency**.
|
| 62 |
-
> Scores represent the percentage of pairwise comparisons in which each method was preferred. Matrix-Game consistently outperforms prior models across all metrics and both groups.
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
## 🚀 Quick Start
|
| 66 |
|
|
|
|
|
|
|
| 67 |
```
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
cd Matrix-Game
|
| 71 |
-
|
| 72 |
-
# install dependencies:
|
| 73 |
pip install -r requirements.txt
|
| 74 |
-
|
| 75 |
-
#
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
bash run_inference.sh
|
| 80 |
-
|
| 81 |
-
# Run interactive websocket server
|
| 82 |
-
python server.py --model_root ./models/matrixgame
|
| 83 |
```
|
| 84 |
|
| 85 |
-
## Interactive WebSocket Server
|
| 86 |
-
|
| 87 |
-
We've implemented a real-time interactive WebSocket server that uses the Matrix-Game model to generate game frames based on keyboard and mouse inputs:
|
| 88 |
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
-
|
| 93 |
-
- **Fallback Mode**: Automatically falls back to demo mode when GPU resources are unavailable
|
| 94 |
-
|
| 95 |
-
### Usage:
|
| 96 |
-
```bash
|
| 97 |
-
# Basic startup
|
| 98 |
-
python server.py
|
| 99 |
-
|
| 100 |
-
# With custom model paths
|
| 101 |
-
python server.py --model_root ./models/matrixgame --port 8080
|
| 102 |
-
|
| 103 |
-
# With individual model component paths
|
| 104 |
-
python server.py --dit_path ./custom/dit --vae_path ./custom/vae --textenc_path ./custom/textenc
|
| 105 |
```
|
| 106 |
|
| 107 |
-
###
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
-
|
| 113 |
-
-
|
| 114 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 115 |
|
| 116 |
-
|
| 117 |
-
-
|
| 118 |
-
- NVIDIA A100/H100
|
| 119 |
-
- **VRAM**:
|
| 120 |
-
- Requires **≥80GB of GPU memory** for a single 65-frame video inference.
|
| 121 |
|
| 122 |
|
| 123 |
## ⭐ Acknowledgements
|
|
@@ -125,25 +83,17 @@ python server.py --dit_path ./custom/dit --vae_path ./custom/vae --textenc_path
|
|
| 125 |
We would like to express our gratitude to:
|
| 126 |
|
| 127 |
- [Diffusers](https://github.com/huggingface/diffusers) for their excellent diffusion model framework
|
| 128 |
-
- [
|
| 129 |
-
- [
|
|
|
|
| 130 |
- [MineRL](https://github.com/minerllabs/minerl) for their excellent gym framework
|
| 131 |
- [Video-Pre-Training](https://github.com/openai/Video-Pre-Training) for their accurate Inverse Dynamics Model
|
| 132 |
-
- [GameFactory](https://github.com/KwaiVGI/GameFactory) for their idea of action control module
|
| 133 |
-
|
| 134 |
-
We are grateful to the broader research community for their open exploration and contributions to the field of interactive world generation.
|
| 135 |
|
| 136 |
## 📄 License
|
| 137 |
|
| 138 |
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
|
| 139 |
|
| 140 |
-
##
|
| 141 |
-
If you find this
|
| 142 |
-
```
|
| 143 |
-
@article{zhang2025matrixgame,
|
| 144 |
-
title = {Matrix-Game: Interactive World Foundation Model},
|
| 145 |
-
author = {Yifan Zhang and Chunli Peng and Boyang Wang and Puyi Wang and Qingcheng Zhu and Zedong Gao and Eric Li and Yang Liu and Yahui Zhou},
|
| 146 |
-
journal = {arXiv},
|
| 147 |
-
year = {2025}
|
| 148 |
-
}
|
| 149 |
```
|
|
|
|
| 1 |
---
|
| 2 |
+
title: "Matrix Game 2"
|
| 3 |
emoji: 🐟
|
| 4 |
colorFrom: blue
|
| 5 |
colorTo: blue
|
|
|
|
| 11 |
disable_embedding: false
|
| 12 |
---
|
| 13 |
|
| 14 |
+
<p align="center">
|
| 15 |
+
<h1 align="center">Matrix-Game 2.0</h1>
|
| 16 |
+
<h3 align="center">An Open-Source, Real-Time, and Streaming Interactive World Model</h3>
|
| 17 |
+
</p>
|
| 18 |
|
| 19 |
+
<font size=7><div align='center' > [[🤗 HuggingFace](https://huggingface.co/Skywork/Matrix-Game-2.0)] [[📖 Technical Report](https://matrix-game-v2.github.io/static/pdf/report.pdf)] [[🚀 Project Website](https://matrix-game-v2.github.io/)] </div></font>
|
|
|
|
|
|
|
| 20 |
|
| 21 |
+
https://github.com/user-attachments/assets/336b0d4a-64f5-4e5c-9b60-6212ddb261c0
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
## 📝 Overview
|
| 24 |
+
**Matrix-Game-2.0** is an interactive world foundation model for real-time long video generation. Built upon an auto-regressive diffusion-based image-to-world framework, it can generate real-time[25fps] long videos conditioned on keyboard and mouse inputs, enabling fine-grained control and dynamic scene evolution.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
+
## 🤗 Matrix-Game-2.0 Model
|
| 27 |
+
we provide three pretrained model weights including universal scenes, GTA driving scene and TempleRun game scene. Please refer to our HuggingFace page to reach these resources.
|
|
|
|
|
|
|
| 28 |
|
| 29 |
+
## Requirements
|
| 30 |
+
We tested this repo on the following setup:
|
| 31 |
+
* Nvidia GPU with at least 24 GB memory (A100, and H100 are tested).
|
| 32 |
+
* Linux operating system.
|
| 33 |
+
* 64 GB RAM.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
+
## Installation
|
| 36 |
+
Create a conda environment and install dependencies:
|
| 37 |
```
|
| 38 |
+
conda create -n matrix-game-2.0 python=3.10 -y
|
| 39 |
+
conda activate matrix-game-2.0
|
|
|
|
|
|
|
|
|
|
| 40 |
pip install -r requirements.txt
|
| 41 |
+
# install apex and FlashAttention
|
| 42 |
+
# Our project also depends on [FlashAttention](https://github.com/Dao-AILab/flash-attention)
|
| 43 |
+
git clone https://github.com/SkyworkAI/Matrix-Game.git
|
| 44 |
+
cd Matrix-Game-2
|
| 45 |
+
python setup.py develop
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
```
|
| 47 |
|
|
|
|
|
|
|
|
|
|
| 48 |
|
| 49 |
+
## Quick Start
|
| 50 |
+
### Download checkpoints
|
| 51 |
+
```
|
| 52 |
+
huggingface-cli download Skywork/Matrix-Game-2.0 --local-dir Matrix-Game-2.0
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
```
|
| 54 |
|
| 55 |
+
### Inference
|
| 56 |
+
After downloading pretrained models, you can use the following command to generate an interactive video with random action trajectories:
|
| 57 |
+
```
|
| 58 |
+
python inference.py \
|
| 59 |
+
--config_path configs/inference_yaml/{your-config}.yaml \
|
| 60 |
+
--checkpoint_path {path-to-the-checkpoint} \
|
| 61 |
+
--img_path {path-to-the-input-image} \
|
| 62 |
+
--output_folder outputs \
|
| 63 |
+
--num_output_frames 150 \
|
| 64 |
+
--seed 42 \
|
| 65 |
+
--pretrained_model_path {path-to-the-vae-folder}
|
| 66 |
+
```
|
| 67 |
+
Or, you can use the script `inference_streaming.py` for generating the interactive videos with your own input actions and images:
|
| 68 |
+
```
|
| 69 |
+
python inference_streaming.py \
|
| 70 |
+
--config_path configs/inference_yaml/{your-config}.yaml \
|
| 71 |
+
--checkpoint_path {path-to-the-checkpoint} \
|
| 72 |
+
--output_folder outputs \
|
| 73 |
+
--seed 42 \
|
| 74 |
+
--pretrained_model_path {path-to-the-vae-folder}
|
| 75 |
+
```
|
| 76 |
|
| 77 |
+
### Tips
|
| 78 |
+
- In the current version, upward movement for camera may cause brief rendering glitches (e.g., black screens). A fix is planned for future updates. Adjust movement slightly or change direction to resolve it.
|
|
|
|
|
|
|
|
|
|
| 79 |
|
| 80 |
|
| 81 |
## ⭐ Acknowledgements
|
|
|
|
| 83 |
We would like to express our gratitude to:
|
| 84 |
|
| 85 |
- [Diffusers](https://github.com/huggingface/diffusers) for their excellent diffusion model framework
|
| 86 |
+
- [SkyReels-V2](https://github.com/SkyworkAI/SkyReels-V2) for their strong base model
|
| 87 |
+
- [Self-Forcing](https://github.com/guandeh17/Self-Forcing) for their excellent work
|
| 88 |
+
- [GameFactory](https://github.com/KwaiVGI/GameFactory) for their idea of action control module
|
| 89 |
- [MineRL](https://github.com/minerllabs/minerl) for their excellent gym framework
|
| 90 |
- [Video-Pre-Training](https://github.com/openai/Video-Pre-Training) for their accurate Inverse Dynamics Model
|
|
|
|
|
|
|
|
|
|
| 91 |
|
| 92 |
## 📄 License
|
| 93 |
|
| 94 |
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
|
| 95 |
|
| 96 |
+
## Citation
|
| 97 |
+
If you find this codebase useful for your research, please kindly cite our paper:
|
| 98 |
+
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 99 |
```
|
api_engine.py
ADDED
|
@@ -0,0 +1,363 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
# -*- coding: utf-8 -*-
|
| 3 |
+
|
| 4 |
+
"""
|
| 5 |
+
MatrixGame Engine
|
| 6 |
+
|
| 7 |
+
This module handles the core rendering and model inference for the MatrixGame project.
|
| 8 |
+
"""
|
| 9 |
+
|
| 10 |
+
import os
|
| 11 |
+
import logging
|
| 12 |
+
import argparse
|
| 13 |
+
import time
|
| 14 |
+
import torch
|
| 15 |
+
import numpy as np
|
| 16 |
+
from PIL import Image
|
| 17 |
+
import cv2
|
| 18 |
+
from einops import rearrange
|
| 19 |
+
from diffusers.utils import load_image
|
| 20 |
+
from diffusers.video_processor import VideoProcessor
|
| 21 |
+
from typing import Dict, List, Tuple, Any, Optional, Union
|
| 22 |
+
from huggingface_hub import snapshot_download
|
| 23 |
+
|
| 24 |
+
# MatrixGame specific imports
|
| 25 |
+
from matrixgame.sample.pipeline_matrixgame import MatrixGameVideoPipeline
|
| 26 |
+
from matrixgame.model_variants import get_dit
|
| 27 |
+
from matrixgame.vae_variants import get_vae
|
| 28 |
+
from matrixgame.encoder_variants import get_text_enc
|
| 29 |
+
from matrixgame.model_variants.matrixgame_dit_src import MGVideoDiffusionTransformerI2V
|
| 30 |
+
from matrixgame.sample.flow_matching_scheduler_matrixgame import FlowMatchDiscreteScheduler
|
| 31 |
+
from teacache_forward import teacache_forward
|
| 32 |
+
|
| 33 |
+
# Import utility functions
|
| 34 |
+
from api_utils import (
|
| 35 |
+
visualize_controls,
|
| 36 |
+
frame_to_jpeg,
|
| 37 |
+
load_scene_frames,
|
| 38 |
+
logger
|
| 39 |
+
)
|
| 40 |
+
|
| 41 |
+
class MatrixGameEngine:
|
| 42 |
+
"""
|
| 43 |
+
Core engine for MatrixGame model inference and frame generation.
|
| 44 |
+
"""
|
| 45 |
+
def __init__(self, args: Optional[argparse.Namespace] = None):
|
| 46 |
+
"""
|
| 47 |
+
Initialize the MatrixGame engine with configuration parameters.
|
| 48 |
+
|
| 49 |
+
Args:
|
| 50 |
+
args: Optional parsed command line arguments for model configuration
|
| 51 |
+
"""
|
| 52 |
+
# Set default parameters if args not provided
|
| 53 |
+
# Ensure frame dimensions are compatible with VAE downsampling (8x) and patch size [1,2,2]
|
| 54 |
+
# Dimensions must be divisible by vae_scale_factor * patch_size = 8 * 2 = 16
|
| 55 |
+
default_width = getattr(args, 'frame_width', 640)
|
| 56 |
+
default_height = getattr(args, 'frame_height', 368) # Changed from 360 to 368 (368/16=23)
|
| 57 |
+
|
| 58 |
+
# Ensure compatibility with VAE and patch size
|
| 59 |
+
vae_patch_factor = 16 # vae_scale_factor (8) * patch_size (2) for both H and W
|
| 60 |
+
self.frame_width = (default_width // vae_patch_factor) * vae_patch_factor
|
| 61 |
+
self.frame_height = (default_height // vae_patch_factor) * vae_patch_factor
|
| 62 |
+
self.fps = getattr(args, 'fps', 16)
|
| 63 |
+
self.inference_steps = getattr(args, 'inference_steps', 20)
|
| 64 |
+
self.guidance_scale = getattr(args, 'guidance_scale', 6.0)
|
| 65 |
+
self.num_pre_frames = getattr(args, 'num_pre_frames', 3)
|
| 66 |
+
|
| 67 |
+
# Initialize state
|
| 68 |
+
self.frame_count = 0
|
| 69 |
+
self.device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
| 70 |
+
self.weight_dtype = torch.bfloat16 if torch.cuda.is_available() else torch.float32
|
| 71 |
+
|
| 72 |
+
# Model paths from environment or args
|
| 73 |
+
self.vae_path = os.environ.get("VAE_PATH", "./models/matrixgame/vae/")
|
| 74 |
+
self.dit_path = os.environ.get("DIT_PATH", "./models/matrixgame/dit/")
|
| 75 |
+
self.textenc_path = os.environ.get("TEXTENC_PATH", "./models/matrixgame")
|
| 76 |
+
|
| 77 |
+
# Cache scene initial frames
|
| 78 |
+
self.scenes = {
|
| 79 |
+
'forest': load_scene_frames('forest', self.frame_width, self.frame_height),
|
| 80 |
+
'desert': load_scene_frames('desert', self.frame_width, self.frame_height),
|
| 81 |
+
'beach': load_scene_frames('beach', self.frame_width, self.frame_height),
|
| 82 |
+
'hills': load_scene_frames('hills', self.frame_width, self.frame_height),
|
| 83 |
+
'river': load_scene_frames('river', self.frame_width, self.frame_height),
|
| 84 |
+
'icy': load_scene_frames('icy', self.frame_width, self.frame_height),
|
| 85 |
+
'mushroom': load_scene_frames('mushroom', self.frame_width, self.frame_height),
|
| 86 |
+
'plain': load_scene_frames('plain', self.frame_width, self.frame_height)
|
| 87 |
+
}
|
| 88 |
+
|
| 89 |
+
# Cache initial images for model input
|
| 90 |
+
self.scene_initial_images = {}
|
| 91 |
+
|
| 92 |
+
# Initialize MatrixGame pipeline
|
| 93 |
+
self.model_loaded = False
|
| 94 |
+
if torch.cuda.is_available():
|
| 95 |
+
try:
|
| 96 |
+
self._init_models()
|
| 97 |
+
self.model_loaded = True
|
| 98 |
+
logger.info("MatrixGame models loaded successfully")
|
| 99 |
+
except Exception as e:
|
| 100 |
+
logger.error(f"Failed to initialize MatrixGame models: {str(e)}")
|
| 101 |
+
logger.info("Falling back to frame cycling mode")
|
| 102 |
+
else:
|
| 103 |
+
logger.warning("CUDA not available. Using frame cycling mode only.")
|
| 104 |
+
|
| 105 |
+
def _init_models(self):
|
| 106 |
+
"""Initialize MatrixGame models (VAE, text encoder, transformer)"""
|
| 107 |
+
# Initialize flow matching scheduler
|
| 108 |
+
self.scheduler = FlowMatchDiscreteScheduler(
|
| 109 |
+
shift=15.0,
|
| 110 |
+
reverse=True,
|
| 111 |
+
solver="euler"
|
| 112 |
+
)
|
| 113 |
+
|
| 114 |
+
# Initialize VAE
|
| 115 |
+
try:
|
| 116 |
+
self.vae = get_vae("matrixgame", self.vae_path, self.weight_dtype)
|
| 117 |
+
self.vae.requires_grad_(False)
|
| 118 |
+
self.vae.eval()
|
| 119 |
+
self.vae.enable_tiling()
|
| 120 |
+
logger.info("VAE model loaded successfully")
|
| 121 |
+
except Exception as e:
|
| 122 |
+
logger.error(f"Error loading VAE model: {str(e)}")
|
| 123 |
+
raise
|
| 124 |
+
|
| 125 |
+
# Initialize DIT (Transformer)
|
| 126 |
+
try:
|
| 127 |
+
# Check if DIT model exists locally, if not download from Hugging Face
|
| 128 |
+
if not os.path.exists(self.dit_path) or not os.path.isdir(self.dit_path):
|
| 129 |
+
logger.info(f"DIT model not found at {self.dit_path}, downloading from Hugging Face...")
|
| 130 |
+
try:
|
| 131 |
+
# Download the DIT subdirectory from Skywork/Matrix-Game-2.0
|
| 132 |
+
downloaded_path = snapshot_download(
|
| 133 |
+
repo_id="Skywork/Matrix-Game-2.0",
|
| 134 |
+
allow_patterns="dit/*",
|
| 135 |
+
local_dir=os.path.dirname(self.dit_path) if os.path.dirname(self.dit_path) else "./models/matrixgame"
|
| 136 |
+
)
|
| 137 |
+
# Point to the dit subdirectory
|
| 138 |
+
self.dit_path = os.path.join(downloaded_path, "dit")
|
| 139 |
+
logger.info(f"Successfully downloaded DIT model to {self.dit_path}")
|
| 140 |
+
except Exception as e:
|
| 141 |
+
logger.error(f"Failed to download DIT model from Hugging Face: {str(e)}")
|
| 142 |
+
raise
|
| 143 |
+
|
| 144 |
+
dit = MGVideoDiffusionTransformerI2V.from_pretrained(self.dit_path)
|
| 145 |
+
dit.requires_grad_(False)
|
| 146 |
+
dit.eval()
|
| 147 |
+
logger.info("DIT model loaded successfully")
|
| 148 |
+
except Exception as e:
|
| 149 |
+
logger.error(f"Error loading DIT model: {str(e)}")
|
| 150 |
+
raise
|
| 151 |
+
|
| 152 |
+
# Initialize text encoder
|
| 153 |
+
try:
|
| 154 |
+
self.text_enc = get_text_enc('matrixgame', self.textenc_path, weight_dtype=self.weight_dtype, i2v_type='refiner')
|
| 155 |
+
logger.info("Text encoder loaded successfully")
|
| 156 |
+
except Exception as e:
|
| 157 |
+
logger.error(f"Error loading text encoder: {str(e)}")
|
| 158 |
+
raise
|
| 159 |
+
|
| 160 |
+
# Initialize pipeline
|
| 161 |
+
try:
|
| 162 |
+
self.pipeline = MatrixGameVideoPipeline(
|
| 163 |
+
vae=self.vae.vae,
|
| 164 |
+
text_encoder=self.text_enc,
|
| 165 |
+
transformer=dit,
|
| 166 |
+
scheduler=self.scheduler,
|
| 167 |
+
).to(self.weight_dtype).to(self.device)
|
| 168 |
+
logger.info("Pipeline initialized successfully")
|
| 169 |
+
except Exception as e:
|
| 170 |
+
logger.error(f"Error initializing pipeline: {str(e)}")
|
| 171 |
+
raise
|
| 172 |
+
|
| 173 |
+
# Configure teacache for the transformer
|
| 174 |
+
self.pipeline.transformer.__class__.enable_teacache = True
|
| 175 |
+
self.pipeline.transformer.__class__.cnt = 0
|
| 176 |
+
self.pipeline.transformer.__class__.num_steps = self.inference_steps
|
| 177 |
+
self.pipeline.transformer.__class__.accumulated_rel_l1_distance = 0
|
| 178 |
+
self.pipeline.transformer.__class__.rel_l1_thresh = 0.075
|
| 179 |
+
self.pipeline.transformer.__class__.previous_modulated_input = None
|
| 180 |
+
self.pipeline.transformer.__class__.previous_residual = None
|
| 181 |
+
self.pipeline.transformer.__class__.forward = teacache_forward
|
| 182 |
+
|
| 183 |
+
# Preprocess initial images for all scenes
|
| 184 |
+
for scene_name, frames in self.scenes.items():
|
| 185 |
+
if frames:
|
| 186 |
+
# Use first frame as initial image
|
| 187 |
+
self.scene_initial_images[scene_name] = self._preprocess_image(frames[0])
|
| 188 |
+
|
| 189 |
+
def _preprocess_image(self, image_array: np.ndarray) -> torch.Tensor:
|
| 190 |
+
"""
|
| 191 |
+
Preprocess an image for the model.
|
| 192 |
+
|
| 193 |
+
Args:
|
| 194 |
+
image_array: Input image as numpy array
|
| 195 |
+
|
| 196 |
+
Returns:
|
| 197 |
+
torch.Tensor: Preprocessed image tensor
|
| 198 |
+
"""
|
| 199 |
+
# Convert numpy array to PIL Image if needed
|
| 200 |
+
if isinstance(image_array, np.ndarray):
|
| 201 |
+
image = Image.fromarray(image_array)
|
| 202 |
+
else:
|
| 203 |
+
image = image_array
|
| 204 |
+
|
| 205 |
+
# Preprocess for VAE
|
| 206 |
+
vae_scale_factor = 2 ** (len(self.vae.config.block_out_channels) - 1) if hasattr(self, 'vae') else 8
|
| 207 |
+
video_processor = VideoProcessor(vae_scale_factor=vae_scale_factor)
|
| 208 |
+
initial_image = video_processor.preprocess(image, height=self.frame_height, width=self.frame_width)
|
| 209 |
+
|
| 210 |
+
# Add past frames for stability (use same frame repeated)
|
| 211 |
+
past_frames = initial_image.repeat(self.num_pre_frames, 1, 1, 1)
|
| 212 |
+
initial_image = torch.cat([initial_image, past_frames], dim=0)
|
| 213 |
+
|
| 214 |
+
return initial_image
|
| 215 |
+
|
| 216 |
+
def generate_frame(self, scene_name: str, keyboard_condition: Optional[List] = None,
|
| 217 |
+
mouse_condition: Optional[List] = None) -> bytes:
|
| 218 |
+
"""
|
| 219 |
+
Generate the next frame based on current conditions using MatrixGame model.
|
| 220 |
+
|
| 221 |
+
Args:
|
| 222 |
+
scene_name: Name of the current scene
|
| 223 |
+
keyboard_condition: Keyboard input state
|
| 224 |
+
mouse_condition: Mouse input state
|
| 225 |
+
|
| 226 |
+
Returns:
|
| 227 |
+
bytes: JPEG bytes of the frame
|
| 228 |
+
"""
|
| 229 |
+
# Check if model is loaded
|
| 230 |
+
if not self.model_loaded or not torch.cuda.is_available():
|
| 231 |
+
# Fall back to frame cycling for demo mode or if models failed to load
|
| 232 |
+
return self._fallback_frame(scene_name, keyboard_condition, mouse_condition)
|
| 233 |
+
else:
|
| 234 |
+
# Use MatrixGame model for frame generation
|
| 235 |
+
try:
|
| 236 |
+
# Get initial image for this scene
|
| 237 |
+
initial_image = self.scene_initial_images.get(scene_name)
|
| 238 |
+
if initial_image is None:
|
| 239 |
+
# Use forest as default if we don't have an initial image for this scene
|
| 240 |
+
initial_image = self.scene_initial_images.get('forest')
|
| 241 |
+
if initial_image is None:
|
| 242 |
+
# If we still don't have an initial image, fall back to frame cycling
|
| 243 |
+
logger.error(f"No initial image available for scene {scene_name}")
|
| 244 |
+
return self._fallback_frame(scene_name, keyboard_condition, mouse_condition)
|
| 245 |
+
|
| 246 |
+
# Prepare input tensors (move to device and format correctly)
|
| 247 |
+
if keyboard_condition is None:
|
| 248 |
+
keyboard_condition = [[0, 0, 0, 0, 0, 0]]
|
| 249 |
+
if mouse_condition is None:
|
| 250 |
+
mouse_condition = [[0, 0]]
|
| 251 |
+
|
| 252 |
+
# Convert conditions to tensors
|
| 253 |
+
keyboard_tensor = torch.tensor(keyboard_condition, dtype=torch.float32)
|
| 254 |
+
mouse_tensor = torch.tensor(mouse_condition, dtype=torch.float32)
|
| 255 |
+
|
| 256 |
+
# Move to device and convert to correct dtype
|
| 257 |
+
keyboard_tensor = keyboard_tensor.to(self.weight_dtype).to(self.device)
|
| 258 |
+
mouse_tensor = mouse_tensor.to(self.weight_dtype).to(self.device)
|
| 259 |
+
|
| 260 |
+
# Get the first frame from the scene for semantic conditioning
|
| 261 |
+
scene_frames = self.scenes.get(scene_name, self.scenes['forest'])
|
| 262 |
+
if not scene_frames:
|
| 263 |
+
return self._fallback_frame(scene_name, keyboard_condition, mouse_condition)
|
| 264 |
+
|
| 265 |
+
semantic_image = Image.fromarray(scene_frames[0])
|
| 266 |
+
|
| 267 |
+
# Get PIL image version of the frame for visualization
|
| 268 |
+
for scene_frame in scene_frames:
|
| 269 |
+
if isinstance(scene_frame, np.ndarray):
|
| 270 |
+
semantic_image = Image.fromarray(scene_frame)
|
| 271 |
+
break
|
| 272 |
+
|
| 273 |
+
# Generate a single frame with the model
|
| 274 |
+
# Use fewer inference steps for interactive frame generation
|
| 275 |
+
with torch.no_grad():
|
| 276 |
+
# Create args object for pipeline
|
| 277 |
+
from types import SimpleNamespace
|
| 278 |
+
args = SimpleNamespace()
|
| 279 |
+
args.num_pre_frames = self.num_pre_frames
|
| 280 |
+
|
| 281 |
+
# Generate a short video (we'll just use the first frame)
|
| 282 |
+
# We're using a short length (4 frames) for real-time performance
|
| 283 |
+
video = self.pipeline(
|
| 284 |
+
height=self.frame_height,
|
| 285 |
+
width=self.frame_width,
|
| 286 |
+
video_length=1, # Generate a very short video for speed (must be 1 or multiple of 4)
|
| 287 |
+
mouse_condition=mouse_tensor,
|
| 288 |
+
keyboard_condition=keyboard_tensor,
|
| 289 |
+
initial_image=initial_image,
|
| 290 |
+
num_inference_steps=self.inference_steps,
|
| 291 |
+
guidance_scale=self.guidance_scale,
|
| 292 |
+
embedded_guidance_scale=None,
|
| 293 |
+
data_type="video",
|
| 294 |
+
vae_ver='884-16c-hy',
|
| 295 |
+
enable_tiling=True,
|
| 296 |
+
generator=torch.Generator(device=self.device).manual_seed(42),
|
| 297 |
+
i2v_type='refiner',
|
| 298 |
+
semantic_images=semantic_image,
|
| 299 |
+
args=args
|
| 300 |
+
).videos[0]
|
| 301 |
+
|
| 302 |
+
# Convert video tensor to numpy array (use first frame)
|
| 303 |
+
video_frame = video[0].permute(1, 2, 0).cpu().numpy()
|
| 304 |
+
video_frame = (video_frame * 255).astype(np.uint8)
|
| 305 |
+
frame = video_frame
|
| 306 |
+
|
| 307 |
+
# Increment frame counter
|
| 308 |
+
self.frame_count += 1
|
| 309 |
+
|
| 310 |
+
except Exception as e:
|
| 311 |
+
logger.error(f"Error generating frame with MatrixGame model: {str(e)}")
|
| 312 |
+
# Fall back to cycling demo frames if model generation fails
|
| 313 |
+
return self._fallback_frame(scene_name, keyboard_condition, mouse_condition)
|
| 314 |
+
|
| 315 |
+
# Add visualization of input controls
|
| 316 |
+
frame = visualize_controls(
|
| 317 |
+
frame, keyboard_condition, mouse_condition,
|
| 318 |
+
self.frame_width, self.frame_height
|
| 319 |
+
)
|
| 320 |
+
|
| 321 |
+
# Convert frame to JPEG
|
| 322 |
+
return frame_to_jpeg(frame, self.frame_height, self.frame_width)
|
| 323 |
+
|
| 324 |
+
def _fallback_frame(self, scene_name: str, keyboard_condition: Optional[List] = None,
|
| 325 |
+
mouse_condition: Optional[List] = None) -> bytes:
|
| 326 |
+
"""
|
| 327 |
+
Generate a fallback frame when model generation fails.
|
| 328 |
+
|
| 329 |
+
Args:
|
| 330 |
+
scene_name: Name of the current scene
|
| 331 |
+
keyboard_condition: Keyboard input state
|
| 332 |
+
mouse_condition: Mouse input state
|
| 333 |
+
|
| 334 |
+
Returns:
|
| 335 |
+
bytes: JPEG bytes of the frame
|
| 336 |
+
"""
|
| 337 |
+
scene_frames = self.scenes.get(scene_name, self.scenes['forest'])
|
| 338 |
+
frame_idx = self.frame_count % len(scene_frames)
|
| 339 |
+
frame = scene_frames[frame_idx].copy()
|
| 340 |
+
self.frame_count += 1
|
| 341 |
+
|
| 342 |
+
# Add fallback mode indicator
|
| 343 |
+
cv2.putText(frame, "Fallback mode",
|
| 344 |
+
(10, self.frame_height - 20),
|
| 345 |
+
cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 1)
|
| 346 |
+
|
| 347 |
+
# Add visualization of input controls
|
| 348 |
+
frame = visualize_controls(
|
| 349 |
+
frame, keyboard_condition, mouse_condition,
|
| 350 |
+
self.frame_width, self.frame_height
|
| 351 |
+
)
|
| 352 |
+
|
| 353 |
+
# Convert frame to JPEG
|
| 354 |
+
return frame_to_jpeg(frame, self.frame_height, self.frame_width)
|
| 355 |
+
|
| 356 |
+
def get_valid_scenes(self) -> List[str]:
|
| 357 |
+
"""
|
| 358 |
+
Get a list of valid scene names.
|
| 359 |
+
|
| 360 |
+
Returns:
|
| 361 |
+
List[str]: List of valid scene names
|
| 362 |
+
"""
|
| 363 |
+
return list(self.scenes.keys())
|
api_server.py
ADDED
|
@@ -0,0 +1,649 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
# -*- coding: utf-8 -*-
|
| 3 |
+
|
| 4 |
+
"""
|
| 5 |
+
MatrixGame Websocket Gaming Server
|
| 6 |
+
|
| 7 |
+
This script implements a websocket server for the MatrixGame project,
|
| 8 |
+
allowing real-time streaming of game frames based on player inputs.
|
| 9 |
+
"""
|
| 10 |
+
|
| 11 |
+
import asyncio
|
| 12 |
+
import json
|
| 13 |
+
import logging
|
| 14 |
+
import os
|
| 15 |
+
import pathlib
|
| 16 |
+
import time
|
| 17 |
+
import uuid
|
| 18 |
+
import base64
|
| 19 |
+
import argparse
|
| 20 |
+
from typing import Dict, List, Any, Optional
|
| 21 |
+
from aiohttp import web, WSMsgType
|
| 22 |
+
|
| 23 |
+
# Import the game engine
|
| 24 |
+
from api_engine import MatrixGameEngine
|
| 25 |
+
from api_utils import logger, parse_model_args, setup_gpu_environment
|
| 26 |
+
|
| 27 |
+
class GameSession:
|
| 28 |
+
"""
|
| 29 |
+
Represents a user's gaming session.
|
| 30 |
+
Each WebSocket connection gets its own session with separate queues.
|
| 31 |
+
"""
|
| 32 |
+
def __init__(self, user_id: str, ws: web.WebSocketResponse, game_manager):
|
| 33 |
+
self.user_id = user_id
|
| 34 |
+
self.ws = ws
|
| 35 |
+
self.game_manager = game_manager
|
| 36 |
+
|
| 37 |
+
# Create action queue for this user session
|
| 38 |
+
self.action_queue = asyncio.Queue()
|
| 39 |
+
|
| 40 |
+
# Session creation time
|
| 41 |
+
self.created_at = time.time()
|
| 42 |
+
self.last_activity = time.time()
|
| 43 |
+
|
| 44 |
+
# Game state
|
| 45 |
+
self.current_scene = "forest" # Default scene
|
| 46 |
+
self.is_streaming = False
|
| 47 |
+
self.stream_task = None
|
| 48 |
+
|
| 49 |
+
# Current input state
|
| 50 |
+
self.keyboard_state = [0, 0, 0, 0, 0, 0] # forward, back, left, right, jump, attack
|
| 51 |
+
self.mouse_state = [0, 0] # x, y
|
| 52 |
+
|
| 53 |
+
self.background_tasks = []
|
| 54 |
+
|
| 55 |
+
async def start(self):
|
| 56 |
+
"""Start all the queue processors for this session"""
|
| 57 |
+
self.background_tasks = [
|
| 58 |
+
asyncio.create_task(self._process_action_queue()),
|
| 59 |
+
]
|
| 60 |
+
logger.info(f"Started game session for user {self.user_id}")
|
| 61 |
+
|
| 62 |
+
async def stop(self):
|
| 63 |
+
"""Stop all background tasks for this session"""
|
| 64 |
+
# Stop streaming if active
|
| 65 |
+
if self.is_streaming and self.stream_task:
|
| 66 |
+
self.is_streaming = False
|
| 67 |
+
self.stream_task.cancel()
|
| 68 |
+
try:
|
| 69 |
+
await self.stream_task
|
| 70 |
+
except asyncio.CancelledError:
|
| 71 |
+
pass
|
| 72 |
+
|
| 73 |
+
# Cancel other background tasks
|
| 74 |
+
for task in self.background_tasks:
|
| 75 |
+
task.cancel()
|
| 76 |
+
|
| 77 |
+
try:
|
| 78 |
+
# Wait for tasks to complete cancellation
|
| 79 |
+
await asyncio.gather(*self.background_tasks, return_exceptions=True)
|
| 80 |
+
except asyncio.CancelledError:
|
| 81 |
+
pass
|
| 82 |
+
|
| 83 |
+
logger.info(f"Stopped game session for user {self.user_id}")
|
| 84 |
+
|
| 85 |
+
async def _process_action_queue(self):
|
| 86 |
+
"""Process game actions from the queue"""
|
| 87 |
+
while True:
|
| 88 |
+
data = await self.action_queue.get()
|
| 89 |
+
try:
|
| 90 |
+
action_type = data.get('action')
|
| 91 |
+
|
| 92 |
+
if action_type == 'start_stream':
|
| 93 |
+
result = await self._handle_start_stream(data)
|
| 94 |
+
elif action_type == 'stop_stream':
|
| 95 |
+
result = await self._handle_stop_stream(data)
|
| 96 |
+
elif action_type == 'keyboard_input':
|
| 97 |
+
result = await self._handle_keyboard_input(data)
|
| 98 |
+
elif action_type == 'mouse_input':
|
| 99 |
+
result = await self._handle_mouse_input(data)
|
| 100 |
+
elif action_type == 'change_scene':
|
| 101 |
+
result = await self._handle_scene_change(data)
|
| 102 |
+
else:
|
| 103 |
+
result = {
|
| 104 |
+
'action': action_type,
|
| 105 |
+
'requestId': data.get('requestId'),
|
| 106 |
+
'success': False,
|
| 107 |
+
'error': f'Unknown action: {action_type}'
|
| 108 |
+
}
|
| 109 |
+
|
| 110 |
+
# Send response back to the client
|
| 111 |
+
await self.ws.send_json(result)
|
| 112 |
+
|
| 113 |
+
# Update last activity time
|
| 114 |
+
self.last_activity = time.time()
|
| 115 |
+
|
| 116 |
+
except Exception as e:
|
| 117 |
+
logger.error(f"Error processing action for user {self.user_id}: {str(e)}")
|
| 118 |
+
try:
|
| 119 |
+
await self.ws.send_json({
|
| 120 |
+
'action': data.get('action'),
|
| 121 |
+
'requestId': data.get('requestId', 'unknown'),
|
| 122 |
+
'success': False,
|
| 123 |
+
'error': f'Error processing action: {str(e)}'
|
| 124 |
+
})
|
| 125 |
+
except Exception as send_error:
|
| 126 |
+
logger.error(f"Error sending error response: {send_error}")
|
| 127 |
+
finally:
|
| 128 |
+
self.action_queue.task_done()
|
| 129 |
+
|
| 130 |
+
async def _handle_start_stream(self, data: Dict) -> Dict:
|
| 131 |
+
"""Handle request to start streaming frames"""
|
| 132 |
+
if self.is_streaming:
|
| 133 |
+
return {
|
| 134 |
+
'action': 'start_stream',
|
| 135 |
+
'requestId': data.get('requestId'),
|
| 136 |
+
'success': False,
|
| 137 |
+
'error': 'Stream already active'
|
| 138 |
+
}
|
| 139 |
+
|
| 140 |
+
fps = data.get('fps', 16)
|
| 141 |
+
self.is_streaming = True
|
| 142 |
+
self.stream_task = asyncio.create_task(self._stream_frames(fps))
|
| 143 |
+
|
| 144 |
+
return {
|
| 145 |
+
'action': 'start_stream',
|
| 146 |
+
'requestId': data.get('requestId'),
|
| 147 |
+
'success': True,
|
| 148 |
+
'message': f'Streaming started at {fps} FPS'
|
| 149 |
+
}
|
| 150 |
+
|
| 151 |
+
async def _handle_stop_stream(self, data: Dict) -> Dict:
|
| 152 |
+
"""Handle request to stop streaming frames"""
|
| 153 |
+
if not self.is_streaming:
|
| 154 |
+
return {
|
| 155 |
+
'action': 'stop_stream',
|
| 156 |
+
'requestId': data.get('requestId'),
|
| 157 |
+
'success': False,
|
| 158 |
+
'error': 'No active stream to stop'
|
| 159 |
+
}
|
| 160 |
+
|
| 161 |
+
self.is_streaming = False
|
| 162 |
+
if self.stream_task:
|
| 163 |
+
self.stream_task.cancel()
|
| 164 |
+
try:
|
| 165 |
+
await self.stream_task
|
| 166 |
+
except asyncio.CancelledError:
|
| 167 |
+
pass
|
| 168 |
+
self.stream_task = None
|
| 169 |
+
|
| 170 |
+
return {
|
| 171 |
+
'action': 'stop_stream',
|
| 172 |
+
'requestId': data.get('requestId'),
|
| 173 |
+
'success': True,
|
| 174 |
+
'message': 'Streaming stopped'
|
| 175 |
+
}
|
| 176 |
+
|
| 177 |
+
async def _handle_keyboard_input(self, data: Dict) -> Dict:
|
| 178 |
+
"""Handle keyboard input from client"""
|
| 179 |
+
key = data.get('key', '')
|
| 180 |
+
pressed = data.get('pressed', False)
|
| 181 |
+
|
| 182 |
+
# Map key to keyboard state index
|
| 183 |
+
key_map = {
|
| 184 |
+
'w': 0, 'forward': 0,
|
| 185 |
+
's': 1, 'back': 1, 'backward': 1,
|
| 186 |
+
'a': 2, 'left': 2,
|
| 187 |
+
'd': 3, 'right': 3,
|
| 188 |
+
'space': 4, 'jump': 4,
|
| 189 |
+
'shift': 5, 'attack': 5, 'ctrl': 5
|
| 190 |
+
}
|
| 191 |
+
|
| 192 |
+
if key.lower() in key_map:
|
| 193 |
+
key_idx = key_map[key.lower()]
|
| 194 |
+
self.keyboard_state[key_idx] = 1 if pressed else 0
|
| 195 |
+
|
| 196 |
+
return {
|
| 197 |
+
'action': 'keyboard_input',
|
| 198 |
+
'requestId': data.get('requestId'),
|
| 199 |
+
'success': True,
|
| 200 |
+
'keyboardState': self.keyboard_state
|
| 201 |
+
}
|
| 202 |
+
|
| 203 |
+
async def _handle_mouse_input(self, data: Dict) -> Dict:
|
| 204 |
+
"""Handle mouse movement/input from client"""
|
| 205 |
+
mouse_x = data.get('x', 0)
|
| 206 |
+
mouse_y = data.get('y', 0)
|
| 207 |
+
|
| 208 |
+
# Update mouse state, normalize values between -1 and 1
|
| 209 |
+
self.mouse_state = [float(mouse_x), float(mouse_y)]
|
| 210 |
+
|
| 211 |
+
return {
|
| 212 |
+
'action': 'mouse_input',
|
| 213 |
+
'requestId': data.get('requestId'),
|
| 214 |
+
'success': True,
|
| 215 |
+
'mouseState': self.mouse_state
|
| 216 |
+
}
|
| 217 |
+
|
| 218 |
+
async def _handle_scene_change(self, data: Dict) -> Dict:
|
| 219 |
+
"""Handle scene change requests"""
|
| 220 |
+
scene_name = data.get('scene', 'forest')
|
| 221 |
+
valid_scenes = self.game_manager.valid_scenes
|
| 222 |
+
|
| 223 |
+
if scene_name not in valid_scenes:
|
| 224 |
+
return {
|
| 225 |
+
'action': 'change_scene',
|
| 226 |
+
'requestId': data.get('requestId'),
|
| 227 |
+
'success': False,
|
| 228 |
+
'error': f'Invalid scene: {scene_name}. Valid scenes are: {", ".join(valid_scenes)}'
|
| 229 |
+
}
|
| 230 |
+
|
| 231 |
+
self.current_scene = scene_name
|
| 232 |
+
|
| 233 |
+
return {
|
| 234 |
+
'action': 'change_scene',
|
| 235 |
+
'requestId': data.get('requestId'),
|
| 236 |
+
'success': True,
|
| 237 |
+
'scene': scene_name
|
| 238 |
+
}
|
| 239 |
+
|
| 240 |
+
async def _stream_frames(self, fps: int):
|
| 241 |
+
"""Stream frames to the client at the specified FPS"""
|
| 242 |
+
frame_interval = 1.0 / fps # Time between frames in seconds
|
| 243 |
+
|
| 244 |
+
try:
|
| 245 |
+
while self.is_streaming:
|
| 246 |
+
start_time = time.time()
|
| 247 |
+
|
| 248 |
+
# Generate frame based on current keyboard and mouse state
|
| 249 |
+
keyboard_condition = [self.keyboard_state]
|
| 250 |
+
mouse_condition = [self.mouse_state]
|
| 251 |
+
|
| 252 |
+
# Use the engine to generate the next frame
|
| 253 |
+
frame_bytes = self.game_manager.engine.generate_frame(
|
| 254 |
+
self.current_scene, keyboard_condition, mouse_condition
|
| 255 |
+
)
|
| 256 |
+
|
| 257 |
+
# Encode as base64 for sending in JSON
|
| 258 |
+
frame_base64 = base64.b64encode(frame_bytes).decode('utf-8')
|
| 259 |
+
|
| 260 |
+
# Send frame to client
|
| 261 |
+
await self.ws.send_json({
|
| 262 |
+
'action': 'frame',
|
| 263 |
+
'frameData': frame_base64,
|
| 264 |
+
'timestamp': time.time()
|
| 265 |
+
})
|
| 266 |
+
|
| 267 |
+
# Calculate sleep time to maintain FPS
|
| 268 |
+
elapsed = time.time() - start_time
|
| 269 |
+
sleep_time = max(0, frame_interval - elapsed)
|
| 270 |
+
await asyncio.sleep(sleep_time)
|
| 271 |
+
|
| 272 |
+
except asyncio.CancelledError:
|
| 273 |
+
logger.info(f"Frame streaming cancelled for user {self.user_id}")
|
| 274 |
+
except Exception as e:
|
| 275 |
+
logger.error(f"Error in frame streaming for user {self.user_id}: {str(e)}")
|
| 276 |
+
if self.ws.closed:
|
| 277 |
+
logger.info(f"WebSocket closed for user {self.user_id}")
|
| 278 |
+
return
|
| 279 |
+
|
| 280 |
+
# Notify client of error
|
| 281 |
+
try:
|
| 282 |
+
await self.ws.send_json({
|
| 283 |
+
'action': 'frame_error',
|
| 284 |
+
'error': f'Streaming error: {str(e)}'
|
| 285 |
+
})
|
| 286 |
+
except:
|
| 287 |
+
pass
|
| 288 |
+
|
| 289 |
+
# Stop streaming
|
| 290 |
+
self.is_streaming = False
|
| 291 |
+
|
| 292 |
+
class GameManager:
|
| 293 |
+
"""
|
| 294 |
+
Manages all active gaming sessions and shared resources.
|
| 295 |
+
"""
|
| 296 |
+
def __init__(self, args: argparse.Namespace):
|
| 297 |
+
self.sessions = {}
|
| 298 |
+
self.session_lock = asyncio.Lock()
|
| 299 |
+
|
| 300 |
+
# Initialize game engine
|
| 301 |
+
self.engine = MatrixGameEngine(args)
|
| 302 |
+
|
| 303 |
+
# Load valid scenes from engine
|
| 304 |
+
self.valid_scenes = self.engine.get_valid_scenes()
|
| 305 |
+
|
| 306 |
+
async def create_session(self, user_id: str, ws: web.WebSocketResponse) -> GameSession:
|
| 307 |
+
"""Create a new game session"""
|
| 308 |
+
async with self.session_lock:
|
| 309 |
+
# Create a new session for this user
|
| 310 |
+
session = GameSession(user_id, ws, self)
|
| 311 |
+
await session.start()
|
| 312 |
+
self.sessions[user_id] = session
|
| 313 |
+
return session
|
| 314 |
+
|
| 315 |
+
async def delete_session(self, user_id: str) -> None:
|
| 316 |
+
"""Delete a game session and clean up resources"""
|
| 317 |
+
async with self.session_lock:
|
| 318 |
+
if user_id in self.sessions:
|
| 319 |
+
session = self.sessions[user_id]
|
| 320 |
+
await session.stop()
|
| 321 |
+
del self.sessions[user_id]
|
| 322 |
+
logger.info(f"Deleted game session for user {user_id}")
|
| 323 |
+
|
| 324 |
+
def get_session(self, user_id: str) -> Optional[GameSession]:
|
| 325 |
+
"""Get a game session if it exists"""
|
| 326 |
+
return self.sessions.get(user_id)
|
| 327 |
+
|
| 328 |
+
async def close_all_sessions(self) -> None:
|
| 329 |
+
"""Close all active sessions (used during shutdown)"""
|
| 330 |
+
async with self.session_lock:
|
| 331 |
+
for user_id, session in list(self.sessions.items()):
|
| 332 |
+
await session.stop()
|
| 333 |
+
self.sessions.clear()
|
| 334 |
+
logger.info("Closed all active game sessions")
|
| 335 |
+
|
| 336 |
+
@property
|
| 337 |
+
def session_count(self) -> int:
|
| 338 |
+
"""Get the number of active sessions"""
|
| 339 |
+
return len(self.sessions)
|
| 340 |
+
|
| 341 |
+
def get_session_stats(self) -> Dict:
|
| 342 |
+
"""Get statistics about active sessions"""
|
| 343 |
+
stats = {
|
| 344 |
+
'total_sessions': len(self.sessions),
|
| 345 |
+
'active_scenes': {},
|
| 346 |
+
'streaming_sessions': 0
|
| 347 |
+
}
|
| 348 |
+
|
| 349 |
+
# Count sessions by scene and streaming status
|
| 350 |
+
for session in self.sessions.values():
|
| 351 |
+
scene = session.current_scene
|
| 352 |
+
stats['active_scenes'][scene] = stats['active_scenes'].get(scene, 0) + 1
|
| 353 |
+
if session.is_streaming:
|
| 354 |
+
stats['streaming_sessions'] += 1
|
| 355 |
+
|
| 356 |
+
return stats
|
| 357 |
+
|
| 358 |
+
# Create global game manager
|
| 359 |
+
game_manager = None
|
| 360 |
+
|
| 361 |
+
async def status_handler(request: web.Request) -> web.Response:
|
| 362 |
+
"""Handler for API status endpoint"""
|
| 363 |
+
# Get session statistics
|
| 364 |
+
session_stats = game_manager.get_session_stats()
|
| 365 |
+
|
| 366 |
+
return web.json_response({
|
| 367 |
+
'product': 'MatrixGame WebSocket Server',
|
| 368 |
+
'version': '1.0.0',
|
| 369 |
+
'active_sessions': session_stats,
|
| 370 |
+
'available_scenes': game_manager.valid_scenes
|
| 371 |
+
})
|
| 372 |
+
|
| 373 |
+
async def root_handler(request: web.Request) -> web.Response:
|
| 374 |
+
"""Handler for serving the client at the root path"""
|
| 375 |
+
client_path = pathlib.Path(__file__).parent / 'client' / 'index.html'
|
| 376 |
+
|
| 377 |
+
with open(client_path, 'r') as file:
|
| 378 |
+
html_content = file.read()
|
| 379 |
+
|
| 380 |
+
return web.Response(text=html_content, content_type='text/html')
|
| 381 |
+
|
| 382 |
+
async def websocket_handler(request: web.Request) -> web.WebSocketResponse:
|
| 383 |
+
"""Handle WebSocket connections with robust error handling"""
|
| 384 |
+
logger.info(f"WebSocket connection attempt - PATH: {request.path}, QUERY: {request.query_string}")
|
| 385 |
+
|
| 386 |
+
# Log request headers at debug level only (could contain sensitive information)
|
| 387 |
+
logger.debug(f"WebSocket request headers: {dict(request.headers)}")
|
| 388 |
+
|
| 389 |
+
# Prepare a WebSocket response with appropriate settings
|
| 390 |
+
ws = web.WebSocketResponse(
|
| 391 |
+
max_msg_size=1024*1024*10, # 10MB max message size
|
| 392 |
+
timeout=60.0,
|
| 393 |
+
heartbeat=30.0 # Add heartbeat to keep connection alive
|
| 394 |
+
)
|
| 395 |
+
|
| 396 |
+
# Check if WebSocket protocol is supported
|
| 397 |
+
if not ws.can_prepare(request):
|
| 398 |
+
logger.error("Cannot prepare WebSocket: WebSocket protocol not supported")
|
| 399 |
+
return web.Response(status=400, text="WebSocket protocol not supported")
|
| 400 |
+
|
| 401 |
+
try:
|
| 402 |
+
logger.info("Preparing WebSocket connection...")
|
| 403 |
+
await ws.prepare(request)
|
| 404 |
+
|
| 405 |
+
# Generate a unique user ID for this connection
|
| 406 |
+
user_id = str(uuid.uuid4())
|
| 407 |
+
|
| 408 |
+
# Get client IP address
|
| 409 |
+
peername = request.transport.get_extra_info('peername')
|
| 410 |
+
if peername is not None:
|
| 411 |
+
client_ip = peername[0]
|
| 412 |
+
else:
|
| 413 |
+
client_ip = request.headers.get('X-Forwarded-For', 'unknown').split(',')[0].strip()
|
| 414 |
+
|
| 415 |
+
# Log connection success
|
| 416 |
+
logger.info(f"Client {user_id} connecting from IP: {client_ip} - WebSocket connection established")
|
| 417 |
+
|
| 418 |
+
# Mark that the session is established
|
| 419 |
+
is_session_created = False
|
| 420 |
+
|
| 421 |
+
try:
|
| 422 |
+
# Store the user ID in the websocket for easy access
|
| 423 |
+
ws.user_id = user_id
|
| 424 |
+
|
| 425 |
+
# Create a new session for this user
|
| 426 |
+
logger.info(f"Creating game session for user {user_id}")
|
| 427 |
+
user_session = await game_manager.create_session(user_id, ws)
|
| 428 |
+
is_session_created = True
|
| 429 |
+
logger.info(f"Game session created for user {user_id}")
|
| 430 |
+
except Exception as session_error:
|
| 431 |
+
logger.error(f"Error creating game session: {str(session_error)}", exc_info=True)
|
| 432 |
+
if not ws.closed:
|
| 433 |
+
await ws.close(code=1011, message=f"Server error: {str(session_error)}".encode())
|
| 434 |
+
if is_session_created:
|
| 435 |
+
await game_manager.delete_session(user_id)
|
| 436 |
+
return ws
|
| 437 |
+
except Exception as e:
|
| 438 |
+
logger.error(f"Error establishing WebSocket connection: {str(e)}", exc_info=True)
|
| 439 |
+
if not ws.closed and ws.prepared:
|
| 440 |
+
await ws.close(code=1011, message=f"Server error: {str(e)}".encode())
|
| 441 |
+
return ws
|
| 442 |
+
|
| 443 |
+
# Send initial welcome message
|
| 444 |
+
try:
|
| 445 |
+
await ws.send_json({
|
| 446 |
+
'action': 'welcome',
|
| 447 |
+
'userId': user_id,
|
| 448 |
+
'message': 'Welcome to the MatrixGame WebSocket server!',
|
| 449 |
+
'scenes': game_manager.valid_scenes
|
| 450 |
+
})
|
| 451 |
+
logger.info(f"Sent welcome message to user {user_id}")
|
| 452 |
+
except Exception as welcome_error:
|
| 453 |
+
logger.error(f"Error sending welcome message: {str(welcome_error)}")
|
| 454 |
+
if not ws.closed:
|
| 455 |
+
await ws.close(code=1011, message=b"Failed to send welcome message")
|
| 456 |
+
await game_manager.delete_session(user_id)
|
| 457 |
+
return ws
|
| 458 |
+
|
| 459 |
+
try:
|
| 460 |
+
async for msg in ws:
|
| 461 |
+
if msg.type == WSMsgType.TEXT:
|
| 462 |
+
try:
|
| 463 |
+
data = json.loads(msg.data)
|
| 464 |
+
action = data.get('action')
|
| 465 |
+
|
| 466 |
+
logger.debug(f"Received {action} message from user {user_id}")
|
| 467 |
+
|
| 468 |
+
if action == 'ping':
|
| 469 |
+
# Respond to ping immediately
|
| 470 |
+
await ws.send_json({
|
| 471 |
+
'action': 'pong',
|
| 472 |
+
'requestId': data.get('requestId'),
|
| 473 |
+
'timestamp': time.time()
|
| 474 |
+
})
|
| 475 |
+
else:
|
| 476 |
+
# Route game actions to the session's action queue
|
| 477 |
+
await user_session.action_queue.put(data)
|
| 478 |
+
|
| 479 |
+
except json.JSONDecodeError:
|
| 480 |
+
logger.error(f"Invalid JSON from user {user_id}: {msg.data}")
|
| 481 |
+
if not ws.closed:
|
| 482 |
+
await ws.send_json({
|
| 483 |
+
'error': 'Invalid JSON message',
|
| 484 |
+
'success': False
|
| 485 |
+
})
|
| 486 |
+
except Exception as e:
|
| 487 |
+
logger.error(f"Error processing WebSocket message for user {user_id}: {str(e)}")
|
| 488 |
+
if not ws.closed:
|
| 489 |
+
await ws.send_json({
|
| 490 |
+
'action': data.get('action') if 'data' in locals() else 'unknown',
|
| 491 |
+
'success': False,
|
| 492 |
+
'error': f'Error processing message: {str(e)}'
|
| 493 |
+
})
|
| 494 |
+
|
| 495 |
+
elif msg.type == WSMsgType.ERROR:
|
| 496 |
+
logger.error(f"WebSocket error for user {user_id}: {ws.exception()}")
|
| 497 |
+
break
|
| 498 |
+
|
| 499 |
+
elif msg.type == WSMsgType.CLOSE:
|
| 500 |
+
logger.info(f"WebSocket close received for user {user_id} (code: {msg.data}, message: {msg.extra})")
|
| 501 |
+
break
|
| 502 |
+
|
| 503 |
+
elif msg.type == WSMsgType.CLOSING:
|
| 504 |
+
logger.info(f"WebSocket closing for user {user_id}")
|
| 505 |
+
break
|
| 506 |
+
|
| 507 |
+
elif msg.type == WSMsgType.CLOSED:
|
| 508 |
+
logger.info(f"WebSocket already closed for user {user_id}")
|
| 509 |
+
break
|
| 510 |
+
|
| 511 |
+
except Exception as ws_error:
|
| 512 |
+
logger.error(f"Unexpected WebSocket error for user {user_id}: {str(ws_error)}", exc_info=True)
|
| 513 |
+
finally:
|
| 514 |
+
# Cleanup session
|
| 515 |
+
try:
|
| 516 |
+
logger.info(f"Cleaning up session for user {user_id}")
|
| 517 |
+
await game_manager.delete_session(user_id)
|
| 518 |
+
logger.info(f"Connection closed for user {user_id}")
|
| 519 |
+
except Exception as cleanup_error:
|
| 520 |
+
logger.error(f"Error during session cleanup for user {user_id}: {str(cleanup_error)}")
|
| 521 |
+
|
| 522 |
+
return ws
|
| 523 |
+
|
| 524 |
+
async def init_app(args, base_path="") -> web.Application:
|
| 525 |
+
"""Initialize the web application"""
|
| 526 |
+
global game_manager
|
| 527 |
+
|
| 528 |
+
# Initialize game manager with command line args
|
| 529 |
+
game_manager = GameManager(args)
|
| 530 |
+
|
| 531 |
+
app = web.Application(
|
| 532 |
+
client_max_size=1024**2*10 # 10MB max size
|
| 533 |
+
)
|
| 534 |
+
|
| 535 |
+
# Add cleanup logic
|
| 536 |
+
async def cleanup(app):
|
| 537 |
+
logger.info("Shutting down server, closing all sessions...")
|
| 538 |
+
await game_manager.close_all_sessions()
|
| 539 |
+
|
| 540 |
+
app.on_shutdown.append(cleanup)
|
| 541 |
+
|
| 542 |
+
# Add routes with CORS headers for WebSockets
|
| 543 |
+
# Configure CORS for all routes
|
| 544 |
+
@web.middleware
|
| 545 |
+
async def cors_middleware(request, handler):
|
| 546 |
+
if request.method == 'OPTIONS':
|
| 547 |
+
# Handle preflight requests
|
| 548 |
+
resp = web.Response()
|
| 549 |
+
resp.headers['Access-Control-Allow-Origin'] = '*'
|
| 550 |
+
resp.headers['Access-Control-Allow-Methods'] = 'GET, POST, OPTIONS'
|
| 551 |
+
resp.headers['Access-Control-Allow-Headers'] = 'Content-Type, X-Requested-With'
|
| 552 |
+
return resp
|
| 553 |
+
|
| 554 |
+
# Normal request, call the handler
|
| 555 |
+
resp = await handler(request)
|
| 556 |
+
|
| 557 |
+
# Add CORS headers to the response
|
| 558 |
+
resp.headers['Access-Control-Allow-Origin'] = '*'
|
| 559 |
+
resp.headers['Access-Control-Allow-Methods'] = 'GET, POST, OPTIONS'
|
| 560 |
+
resp.headers['Access-Control-Allow-Headers'] = 'Content-Type, X-Requested-With'
|
| 561 |
+
return resp
|
| 562 |
+
|
| 563 |
+
app.middlewares.append(cors_middleware)
|
| 564 |
+
|
| 565 |
+
# Add a debug endpoint to help diagnose WebSocket issues
|
| 566 |
+
async def debug_handler(request):
|
| 567 |
+
client_ip = request.remote
|
| 568 |
+
headers = dict(request.headers)
|
| 569 |
+
server_host = request.host
|
| 570 |
+
|
| 571 |
+
debug_info = {
|
| 572 |
+
"client_ip": client_ip,
|
| 573 |
+
"server_host": server_host,
|
| 574 |
+
"headers": headers,
|
| 575 |
+
"request_path": request.path,
|
| 576 |
+
"server_time": time.time(),
|
| 577 |
+
"base_path": base_path,
|
| 578 |
+
"websocket_route": f"{base_path}/ws",
|
| 579 |
+
"all_routes": [route.name for route in app.router.routes() if route.name],
|
| 580 |
+
"server_info": {
|
| 581 |
+
"active_sessions": game_manager.session_count,
|
| 582 |
+
"available_scenes": game_manager.valid_scenes
|
| 583 |
+
}
|
| 584 |
+
}
|
| 585 |
+
|
| 586 |
+
return web.json_response(debug_info)
|
| 587 |
+
|
| 588 |
+
# Set up routes with the base_path
|
| 589 |
+
# Add multiple WebSocket routes to ensure compatibility
|
| 590 |
+
logger.info(f"Setting up WebSocket route at {base_path}/ws")
|
| 591 |
+
app.router.add_get(f'{base_path}/ws', websocket_handler, name='ws_handler')
|
| 592 |
+
|
| 593 |
+
# Also add WebSocket route at the root for Hugging Face compatibility
|
| 594 |
+
if base_path:
|
| 595 |
+
logger.info(f"Adding additional WebSocket route at /ws")
|
| 596 |
+
app.router.add_get('/ws', websocket_handler, name='ws_root_handler')
|
| 597 |
+
|
| 598 |
+
# Add routes for API and debug endpoints
|
| 599 |
+
app.router.add_get(f'{base_path}/api/status', status_handler, name='status_handler')
|
| 600 |
+
app.router.add_get(f'{base_path}/api/debug', debug_handler, name='debug_handler')
|
| 601 |
+
|
| 602 |
+
# Serve the client at both the base path and root path for compatibility
|
| 603 |
+
app.router.add_get(f'{base_path}/', root_handler, name='root_handler')
|
| 604 |
+
|
| 605 |
+
# Always serve at the root path for Hugging Face Spaces compatibility
|
| 606 |
+
if base_path:
|
| 607 |
+
app.router.add_get('/', root_handler, name='root_handler_no_base')
|
| 608 |
+
|
| 609 |
+
# Set up static file serving for the client assets
|
| 610 |
+
app.router.add_static(f'{base_path}/assets', pathlib.Path(__file__).parent / 'client', name='static_handler')
|
| 611 |
+
|
| 612 |
+
# Add static file serving at root for compatibility
|
| 613 |
+
if base_path:
|
| 614 |
+
app.router.add_static('/assets', pathlib.Path(__file__).parent / 'client', name='static_handler_no_base')
|
| 615 |
+
|
| 616 |
+
return app
|
| 617 |
+
|
| 618 |
+
def parse_args() -> argparse.Namespace:
|
| 619 |
+
"""Parse server-specific command line arguments"""
|
| 620 |
+
parser = argparse.ArgumentParser(description="MatrixGame WebSocket Server")
|
| 621 |
+
parser.add_argument("--host", type=str, default="0.0.0.0", help="Host IP to bind to")
|
| 622 |
+
parser.add_argument("--port", type=int, default=8080, help="Port to listen on")
|
| 623 |
+
parser.add_argument("--path", type=str, default="", help="Base path for the server (for proxy setups)")
|
| 624 |
+
|
| 625 |
+
# Parse server args first
|
| 626 |
+
server_args, remaining_args = parser.parse_known_args()
|
| 627 |
+
|
| 628 |
+
# Parse model args and combine
|
| 629 |
+
model_args = parse_model_args()
|
| 630 |
+
|
| 631 |
+
# Combine all args
|
| 632 |
+
combined_args = argparse.Namespace(**vars(server_args), **vars(model_args))
|
| 633 |
+
|
| 634 |
+
return combined_args
|
| 635 |
+
|
| 636 |
+
if __name__ == '__main__':
|
| 637 |
+
# Configure GPU environment
|
| 638 |
+
setup_gpu_environment()
|
| 639 |
+
|
| 640 |
+
# Parse command line arguments
|
| 641 |
+
args = parse_args()
|
| 642 |
+
|
| 643 |
+
# Initialize app
|
| 644 |
+
loop = asyncio.get_event_loop()
|
| 645 |
+
app = loop.run_until_complete(init_app(args, base_path=args.path))
|
| 646 |
+
|
| 647 |
+
# Start server
|
| 648 |
+
logger.info(f"Starting MatrixGame WebSocket Server at {args.host}:{args.port}")
|
| 649 |
+
web.run_app(app, host=args.host, port=args.port)
|
api_utils.py
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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- coding: utf-8 -*-
|
| 3 |
+
|
| 4 |
+
"""
|
| 5 |
+
MatrixGame Utility Functions
|
| 6 |
+
|
| 7 |
+
This module contains helper functions and utilities for the MatrixGame project.
|
| 8 |
+
"""
|
| 9 |
+
|
| 10 |
+
import os
|
| 11 |
+
import logging
|
| 12 |
+
import argparse
|
| 13 |
+
import torch
|
| 14 |
+
import numpy as np
|
| 15 |
+
import cv2
|
| 16 |
+
from PIL import Image
|
| 17 |
+
from typing import Dict, List, Tuple, Any, Optional, Union
|
| 18 |
+
|
| 19 |
+
# Configure logging
|
| 20 |
+
logging.basicConfig(
|
| 21 |
+
level=logging.INFO,
|
| 22 |
+
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
|
| 23 |
+
)
|
| 24 |
+
logger = logging.getLogger(__name__)
|
| 25 |
+
|
| 26 |
+
def setup_gpu_environment():
|
| 27 |
+
"""
|
| 28 |
+
Configure the GPU environment and log GPU information.
|
| 29 |
+
|
| 30 |
+
Returns:
|
| 31 |
+
bool: True if CUDA is available, False otherwise
|
| 32 |
+
"""
|
| 33 |
+
# Set CUDA memory allocation environment variable for better performance
|
| 34 |
+
os.environ.setdefault("PYTORCH_CUDA_ALLOC_CONF", "expandable_segments:True")
|
| 35 |
+
|
| 36 |
+
# Check if CUDA is available and log information
|
| 37 |
+
if torch.cuda.is_available():
|
| 38 |
+
gpu_count = torch.cuda.device_count()
|
| 39 |
+
gpu_info = []
|
| 40 |
+
|
| 41 |
+
for i in range(gpu_count):
|
| 42 |
+
gpu_name = torch.cuda.get_device_name(i)
|
| 43 |
+
gpu_memory = torch.cuda.get_device_properties(i).total_memory / (1024**3) # Convert to GB
|
| 44 |
+
gpu_info.append(f"GPU {i}: {gpu_name} ({gpu_memory:.2f} GB)")
|
| 45 |
+
|
| 46 |
+
logger.info(f"CUDA is available. Found {gpu_count} GPU(s):")
|
| 47 |
+
for info in gpu_info:
|
| 48 |
+
logger.info(f" {info}")
|
| 49 |
+
return True
|
| 50 |
+
else:
|
| 51 |
+
logger.warning("CUDA is not available. Running in CPU-only mode.")
|
| 52 |
+
return False
|
| 53 |
+
|
| 54 |
+
def parse_model_args() -> argparse.Namespace:
|
| 55 |
+
"""
|
| 56 |
+
Parse command line arguments for model paths and configuration.
|
| 57 |
+
|
| 58 |
+
Returns:
|
| 59 |
+
argparse.Namespace: Parsed arguments
|
| 60 |
+
"""
|
| 61 |
+
parser = argparse.ArgumentParser(description="MatrixGame Model Configuration")
|
| 62 |
+
|
| 63 |
+
# Model paths
|
| 64 |
+
parser.add_argument("--model_root", type=str, default="./models/matrixgame",
|
| 65 |
+
help="Root directory for model files")
|
| 66 |
+
parser.add_argument("--dit_path", type=str, default=None,
|
| 67 |
+
help="Path to DIT model. If not provided, will use MODEL_ROOT/dit/")
|
| 68 |
+
parser.add_argument("--vae_path", type=str, default=None,
|
| 69 |
+
help="Path to VAE model. If not provided, will use MODEL_ROOT/vae/")
|
| 70 |
+
parser.add_argument("--textenc_path", type=str, default=None,
|
| 71 |
+
help="Path to text encoder model. If not provided, will use MODEL_ROOT")
|
| 72 |
+
|
| 73 |
+
# Model settings
|
| 74 |
+
parser.add_argument("--inference_steps", type=int, default=20,
|
| 75 |
+
help="Number of inference steps for frame generation (lower is faster)")
|
| 76 |
+
parser.add_argument("--guidance_scale", type=float, default=6.0,
|
| 77 |
+
help="Guidance scale for generation")
|
| 78 |
+
parser.add_argument("--frame_width", type=int, default=640,
|
| 79 |
+
help="Width of the generated frames")
|
| 80 |
+
parser.add_argument("--frame_height", type=int, default=360,
|
| 81 |
+
help="Height of the generated frames")
|
| 82 |
+
parser.add_argument("--num_pre_frames", type=int, default=3,
|
| 83 |
+
help="Number of pre-frames for conditioning")
|
| 84 |
+
parser.add_argument("--fps", type=int, default=16,
|
| 85 |
+
help="Frames per second for video")
|
| 86 |
+
|
| 87 |
+
args = parser.parse_args()
|
| 88 |
+
|
| 89 |
+
# Set environment variables for model paths if provided
|
| 90 |
+
if args.model_root:
|
| 91 |
+
os.environ.setdefault("MODEL_ROOT", args.model_root)
|
| 92 |
+
if args.dit_path:
|
| 93 |
+
os.environ.setdefault("DIT_PATH", args.dit_path)
|
| 94 |
+
else:
|
| 95 |
+
os.environ.setdefault("DIT_PATH", os.path.join(os.environ.get("MODEL_ROOT", "./models/matrixgame"), "dit/"))
|
| 96 |
+
if args.vae_path:
|
| 97 |
+
os.environ.setdefault("VAE_PATH", args.vae_path)
|
| 98 |
+
else:
|
| 99 |
+
os.environ.setdefault("VAE_PATH", os.path.join(os.environ.get("MODEL_ROOT", "./models/matrixgame"), "vae/"))
|
| 100 |
+
if args.textenc_path:
|
| 101 |
+
os.environ.setdefault("TEXTENC_PATH", args.textenc_path)
|
| 102 |
+
else:
|
| 103 |
+
os.environ.setdefault("TEXTENC_PATH", os.environ.get("MODEL_ROOT", "./models/matrixgame"))
|
| 104 |
+
|
| 105 |
+
return args
|
| 106 |
+
|
| 107 |
+
def visualize_controls(frame: np.ndarray, keyboard_condition: List, mouse_condition: List,
|
| 108 |
+
frame_width: int, frame_height: int) -> np.ndarray:
|
| 109 |
+
"""
|
| 110 |
+
Visualize keyboard and mouse controls on the frame.
|
| 111 |
+
|
| 112 |
+
Args:
|
| 113 |
+
frame: The video frame to visualize on
|
| 114 |
+
keyboard_condition: Keyboard state as a list
|
| 115 |
+
mouse_condition: Mouse state as a list
|
| 116 |
+
frame_width: Width of the frame
|
| 117 |
+
frame_height: Height of the frame
|
| 118 |
+
|
| 119 |
+
Returns:
|
| 120 |
+
np.ndarray: Frame with visualized controls
|
| 121 |
+
"""
|
| 122 |
+
# Clone the frame to avoid modifying the original
|
| 123 |
+
frame = frame.copy()
|
| 124 |
+
|
| 125 |
+
# If we have keyboard/mouse conditions, visualize them on the frame
|
| 126 |
+
if keyboard_condition:
|
| 127 |
+
# Visualize keyboard inputs
|
| 128 |
+
keys = ["W", "S", "A", "D", "JUMP", "ATTACK"]
|
| 129 |
+
for i, key_pressed in enumerate(keyboard_condition[0]):
|
| 130 |
+
color = (0, 255, 0) if key_pressed else (100, 100, 100)
|
| 131 |
+
cv2.putText(frame, keys[i], (20 + i*100, 30),
|
| 132 |
+
cv2.FONT_HERSHEY_SIMPLEX, 0.7, color, 2)
|
| 133 |
+
|
| 134 |
+
if mouse_condition:
|
| 135 |
+
# Visualize mouse movement
|
| 136 |
+
mouse_x, mouse_y = mouse_condition[0]
|
| 137 |
+
# Scale mouse values for visualization
|
| 138 |
+
offset_x = int(mouse_x * 100)
|
| 139 |
+
offset_y = int(mouse_y * 100)
|
| 140 |
+
center_x, center_y = frame_width // 2, frame_height // 2
|
| 141 |
+
cv2.circle(frame, (center_x + offset_x, center_y - offset_y), 10, (255, 0, 0), -1)
|
| 142 |
+
cv2.putText(frame, f"Mouse: {mouse_x:.2f}, {mouse_y:.2f}",
|
| 143 |
+
(frame_width - 250, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (255, 0, 0), 2)
|
| 144 |
+
|
| 145 |
+
return frame
|
| 146 |
+
|
| 147 |
+
def frame_to_jpeg(frame: np.ndarray, frame_height: int, frame_width: int) -> bytes:
|
| 148 |
+
"""
|
| 149 |
+
Convert a frame to JPEG bytes.
|
| 150 |
+
|
| 151 |
+
Args:
|
| 152 |
+
frame: The video frame to convert
|
| 153 |
+
frame_height: Height of the frame for fallback
|
| 154 |
+
frame_width: Width of the frame for fallback
|
| 155 |
+
|
| 156 |
+
Returns:
|
| 157 |
+
bytes: JPEG bytes of the frame
|
| 158 |
+
"""
|
| 159 |
+
success, buffer = cv2.imencode('.jpg', frame)
|
| 160 |
+
if not success:
|
| 161 |
+
logger.error("Failed to encode frame as JPEG")
|
| 162 |
+
# Return a blank frame
|
| 163 |
+
blank = np.ones((frame_height, frame_width, 3), dtype=np.uint8) * 100
|
| 164 |
+
success, buffer = cv2.imencode('.jpg', blank)
|
| 165 |
+
|
| 166 |
+
return buffer.tobytes()
|
| 167 |
+
|
| 168 |
+
def load_scene_frames(scene_name: str, frame_width: int, frame_height: int) -> List[np.ndarray]:
|
| 169 |
+
"""
|
| 170 |
+
Load initial frames for a scene from asset directory.
|
| 171 |
+
|
| 172 |
+
Args:
|
| 173 |
+
scene_name: Name of the scene
|
| 174 |
+
frame_width: Width to resize frames to
|
| 175 |
+
frame_height: Height to resize frames to
|
| 176 |
+
|
| 177 |
+
Returns:
|
| 178 |
+
List[np.ndarray]: List of frames as numpy arrays
|
| 179 |
+
"""
|
| 180 |
+
frames = []
|
| 181 |
+
scene_dir = f"./GameWorldScore/asset/init_image/{scene_name}"
|
| 182 |
+
|
| 183 |
+
if os.path.exists(scene_dir):
|
| 184 |
+
image_files = sorted([f for f in os.listdir(scene_dir) if f.endswith('.png') or f.endswith('.jpg')])
|
| 185 |
+
for img_file in image_files:
|
| 186 |
+
try:
|
| 187 |
+
img_path = os.path.join(scene_dir, img_file)
|
| 188 |
+
img = Image.open(img_path).convert("RGB")
|
| 189 |
+
img = img.resize((frame_width, frame_height))
|
| 190 |
+
frames.append(np.array(img))
|
| 191 |
+
except Exception as e:
|
| 192 |
+
logger.error(f"Error loading image {img_file}: {str(e)}")
|
| 193 |
+
|
| 194 |
+
# If no frames were loaded, create a default colored frame with text
|
| 195 |
+
if not frames:
|
| 196 |
+
frame = np.ones((frame_height, frame_height, 3), dtype=np.uint8) * 100
|
| 197 |
+
# Add scene name as text
|
| 198 |
+
cv2.putText(frame, f"Scene: {scene_name}", (50, 180),
|
| 199 |
+
cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2)
|
| 200 |
+
frames.append(frame)
|
| 201 |
+
|
| 202 |
+
return frames
|
requirements.txt
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
|
|
|
|
|
| 1 |
torch>=2.4.0
|
| 2 |
torchvision>=0.19.0
|
| 3 |
opencv-python>=4.9.0.80
|
|
|
|
| 1 |
+
flash-attn @ https://github.com/Dao-AILab/flash-attention/releases/download/v2.7.4.post1/flash_attn-2.7.4.post1+cu12torch2.4cxx11abiFALSE-cp310-cp310-linux_x86_64.whl
|
| 2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 |
+
# -*- coding: utf-8 -*-
|
| 3 |
+
|
| 4 |
+
"""
|
| 5 |
+
Hugging Face Space launcher for MatrixGame WebSocket Server
|
| 6 |
+
This script launches the server with the appropriate configuration for Hugging Face Spaces.
|
| 7 |
+
"""
|
| 8 |
+
|
| 9 |
+
import os
|
| 10 |
+
import sys
|
| 11 |
+
import subprocess
|
| 12 |
+
import logging
|
| 13 |
+
import asyncio
|
| 14 |
+
from aiohttp import web
|
| 15 |
+
|
| 16 |
+
# Configure logging
|
| 17 |
+
logging.basicConfig(
|
| 18 |
+
level=logging.INFO,
|
| 19 |
+
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
|
| 20 |
+
)
|
| 21 |
+
logger = logging.getLogger(__name__)
|
| 22 |
+
|
| 23 |
+
def install_apex():
|
| 24 |
+
"""Install NVIDIA Apex at runtime with CUDA support"""
|
| 25 |
+
try:
|
| 26 |
+
logger.info("Installing NVIDIA Apex...")
|
| 27 |
+
|
| 28 |
+
# Clone the Apex repository
|
| 29 |
+
subprocess.check_call([
|
| 30 |
+
"git", "clone", "https://github.com/NVIDIA/apex"
|
| 31 |
+
])
|
| 32 |
+
|
| 33 |
+
# Change to apex directory and install
|
| 34 |
+
os.chdir("apex")
|
| 35 |
+
|
| 36 |
+
# Try to install with CUDA extensions first
|
| 37 |
+
try:
|
| 38 |
+
logger.info("Attempting to install Apex with CUDA extensions...")
|
| 39 |
+
subprocess.check_call([
|
| 40 |
+
sys.executable, "-m", "pip", "install", "-v",
|
| 41 |
+
"--disable-pip-version-check", "--no-cache-dir",
|
| 42 |
+
"--no-build-isolation", "--global-option=--cpp_ext",
|
| 43 |
+
"--global-option=--cuda_ext", "./"
|
| 44 |
+
])
|
| 45 |
+
logger.info("Apex installed successfully with CUDA extensions!")
|
| 46 |
+
except subprocess.CalledProcessError as e:
|
| 47 |
+
logger.warning(f"Failed to install Apex with CUDA extensions: {e}")
|
| 48 |
+
logger.info("Falling back to Python-only build...")
|
| 49 |
+
|
| 50 |
+
# Fall back to Python-only build
|
| 51 |
+
subprocess.check_call([
|
| 52 |
+
sys.executable, "-m", "pip", "install", "-v",
|
| 53 |
+
"--disable-pip-version-check", "--no-build-isolation",
|
| 54 |
+
"--no-cache-dir", "./"
|
| 55 |
+
])
|
| 56 |
+
logger.info("Apex installed successfully (Python-only build)!")
|
| 57 |
+
|
| 58 |
+
except subprocess.CalledProcessError as e:
|
| 59 |
+
logger.error(f"Failed to install Apex. Error: {e}")
|
| 60 |
+
# Don't fail the entire startup if Apex installation fails
|
| 61 |
+
logger.warning("Continuing without Apex...")
|
| 62 |
+
except Exception as e:
|
| 63 |
+
logger.error(f"Unexpected error during Apex installation: {e}")
|
| 64 |
+
logger.warning("Continuing without Apex...")
|
| 65 |
+
finally:
|
| 66 |
+
# Change back to original directory
|
| 67 |
+
os.chdir("..")
|
| 68 |
+
|
| 69 |
+
install_apex()
|
| 70 |
+
|
| 71 |
+
from api_server import init_app, parse_args
|
| 72 |
+
|
| 73 |
+
async def run_async():
|
| 74 |
+
"""Run the server using the async API directly for better stability"""
|
| 75 |
+
# Get the port from environment variable in Hugging Face Space
|
| 76 |
+
port = int(os.environ.get("PORT", 7860))
|
| 77 |
+
|
| 78 |
+
# Determine if this is running in a Hugging Face Space
|
| 79 |
+
is_hf_space = os.environ.get("SPACE_ID") is not None
|
| 80 |
+
|
| 81 |
+
# Set the base path if in a space
|
| 82 |
+
base_path = ""
|
| 83 |
+
if is_hf_space:
|
| 84 |
+
# In Hugging Face Spaces, we're usually behind a proxy
|
| 85 |
+
space_id = os.environ.get('SPACE_ID', '')
|
| 86 |
+
# Use empty base path for better WebSocket compatibility
|
| 87 |
+
# WebSockets often have trouble with subpaths in proxied environments
|
| 88 |
+
base_path = "" # or f"/{space_id}" if needed
|
| 89 |
+
logger.info(f"Running in Hugging Face Space {space_id}")
|
| 90 |
+
|
| 91 |
+
logger.info(f"Initializing application with base_path='{base_path}', port={port}")
|
| 92 |
+
|
| 93 |
+
# Parse default args and override for HF Space
|
| 94 |
+
args = parse_args()
|
| 95 |
+
args.port = port
|
| 96 |
+
args.host = "0.0.0.0"
|
| 97 |
+
args.path = base_path
|
| 98 |
+
|
| 99 |
+
# Initialize the application
|
| 100 |
+
app = await init_app(args, base_path=base_path)
|
| 101 |
+
|
| 102 |
+
# Log all routes for debugging
|
| 103 |
+
routes = sorted([f"{route.method} {route.resource}" for route in app.router.routes() if hasattr(route, 'resource')])
|
| 104 |
+
logger.info(f"Registered {len(routes)} routes:")
|
| 105 |
+
for route in routes:
|
| 106 |
+
logger.info(f" - {route}")
|
| 107 |
+
|
| 108 |
+
# Start the server
|
| 109 |
+
logger.info(f"Starting server on 0.0.0.0:{port}")
|
| 110 |
+
runner = web.AppRunner(app)
|
| 111 |
+
await runner.setup()
|
| 112 |
+
site = web.TCPSite(runner, '0.0.0.0', port)
|
| 113 |
+
await site.start()
|
| 114 |
+
|
| 115 |
+
# Keep the server running
|
| 116 |
+
logger.info("Server started, running indefinitely...")
|
| 117 |
+
while True:
|
| 118 |
+
await asyncio.sleep(3600) # Sleep for an hour
|
| 119 |
+
|
| 120 |
+
def main():
|
| 121 |
+
"""Run using the subprocess method (fallback)"""
|
| 122 |
+
# Get the port from environment variable in Hugging Face Space
|
| 123 |
+
port = int(os.environ.get("PORT", 7860))
|
| 124 |
+
|
| 125 |
+
# Determine if this is running in a Hugging Face Space
|
| 126 |
+
is_hf_space = os.environ.get("SPACE_ID") is not None
|
| 127 |
+
|
| 128 |
+
# Pass the appropriate path if in a space
|
| 129 |
+
path_arg = ""
|
| 130 |
+
if is_hf_space:
|
| 131 |
+
# In a space, we're usually behind a proxy, so we need to handle base path
|
| 132 |
+
# We use empty base path for better WebSocket compatibility
|
| 133 |
+
path_arg = "" # or f"--path /{os.environ.get('SPACE_ID', '')}" if needed
|
| 134 |
+
|
| 135 |
+
# Construct and run the command
|
| 136 |
+
cmd = f"{sys.executable} server.py --host 0.0.0.0 --port {port} {path_arg}"
|
| 137 |
+
print(f"Running command: {cmd}")
|
| 138 |
+
subprocess.run(cmd, shell=True)
|
| 139 |
+
|
| 140 |
+
if __name__ == "__main__":
|
| 141 |
+
# First try to run using the async API
|
| 142 |
+
try:
|
| 143 |
+
logger.info("Starting server using async API")
|
| 144 |
+
asyncio.run(run_async())
|
| 145 |
+
except Exception as e:
|
| 146 |
+
logger.error(f"Failed to run using async API: {e}", exc_info=True)
|
| 147 |
+
logger.info("Falling back to subprocess method")
|
| 148 |
+
main()
|