Upload 2 files
Browse files- app.py +10 -39
- requirements.txt +0 -3
app.py
CHANGED
|
@@ -54,7 +54,7 @@ def encode_video(video_path, max_num_frames=32):
|
|
| 54 |
return []
|
| 55 |
|
| 56 |
def load_model():
|
| 57 |
-
"""Load MiniCPM-o 2.6 model and tokenizer with
|
| 58 |
global model, tokenizer
|
| 59 |
|
| 60 |
if model is not None and tokenizer is not None:
|
|
@@ -69,14 +69,9 @@ def load_model():
|
|
| 69 |
|
| 70 |
print(f"Loading on device: {device} with dtype: {torch_dtype}")
|
| 71 |
|
| 72 |
-
#
|
| 73 |
-
attn_implementation = 'sdpa'
|
| 74 |
-
|
| 75 |
-
import flash_attn
|
| 76 |
-
attn_implementation = 'flash_attention_2'
|
| 77 |
-
print("✅ Using Flash Attention 2 for optimal performance")
|
| 78 |
-
except ImportError:
|
| 79 |
-
print("⚠️ Flash Attention not available, using SDPA (still good performance)")
|
| 80 |
|
| 81 |
# Load model with memory optimization for Spaces
|
| 82 |
model = AutoModel.from_pretrained(
|
|
@@ -94,38 +89,12 @@ def load_model():
|
|
| 94 |
trust_remote_code=True
|
| 95 |
)
|
| 96 |
|
| 97 |
-
print("✅ Model loaded successfully!")
|
| 98 |
return model, tokenizer
|
| 99 |
|
| 100 |
except Exception as e:
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
print("⚠️ Flash Attention failed, retrying with SDPA...")
|
| 104 |
-
try:
|
| 105 |
-
model = AutoModel.from_pretrained(
|
| 106 |
-
model_path,
|
| 107 |
-
trust_remote_code=True,
|
| 108 |
-
attn_implementation='sdpa',
|
| 109 |
-
torch_dtype=torch_dtype,
|
| 110 |
-
device_map="auto",
|
| 111 |
-
offload_buffers=True,
|
| 112 |
-
low_cpu_mem_usage=True
|
| 113 |
-
)
|
| 114 |
-
|
| 115 |
-
tokenizer = AutoTokenizer.from_pretrained(
|
| 116 |
-
model_path,
|
| 117 |
-
trust_remote_code=True
|
| 118 |
-
)
|
| 119 |
-
|
| 120 |
-
print("✅ Model loaded successfully with SDPA fallback!")
|
| 121 |
-
return model, tokenizer
|
| 122 |
-
|
| 123 |
-
except Exception as fallback_e:
|
| 124 |
-
print(f"❌ Error loading model with fallback: {fallback_e}")
|
| 125 |
-
raise fallback_e
|
| 126 |
-
else:
|
| 127 |
-
print(f"❌ Error loading model: {e}")
|
| 128 |
-
raise e
|
| 129 |
|
| 130 |
def analyze_video(video_file, prompt, max_frames):
|
| 131 |
"""Analyze video using MiniCPM-o 2.6"""
|
|
@@ -178,6 +147,7 @@ def analyze_video(video_file, prompt, max_frames):
|
|
| 178 |
**Processing Time:** {processing_time:.2f} seconds
|
| 179 |
**Frames Analyzed:** {len(frames)}
|
| 180 |
**Model:** MiniCPM-o 2.6
|
|
|
|
| 181 |
|
| 182 |
### Analysis:
|
| 183 |
{answer}
|
|
@@ -243,6 +213,7 @@ def create_interface():
|
|
| 243 |
- 🖼️ Frame-by-frame understanding
|
| 244 |
- 📝 Detailed descriptions
|
| 245 |
- 🎨 Creative and marketing insights
|
|
|
|
| 246 |
|
| 247 |
**Supported formats:** MP4, AVI, MOV, WebM
|
| 248 |
""")
|
|
@@ -333,7 +304,7 @@ def create_interface():
|
|
| 333 |
|
| 334 |
- **Model:** [openbmb/MiniCPM-o-2_6](https://huggingface.co/openbmb/MiniCPM-o-2_6)
|
| 335 |
- **Code:** Based on exact official sample implementation
|
| 336 |
-
- **GPU:** Optimized for Hugging Face Spaces GPU
|
| 337 |
|
| 338 |
**Note:** Processing time depends on video length and complexity.
|
| 339 |
""")
|
|
|
|
| 54 |
return []
|
| 55 |
|
| 56 |
def load_model():
|
| 57 |
+
"""Load MiniCPM-o 2.6 model and tokenizer with optimized attention implementation"""
|
| 58 |
global model, tokenizer
|
| 59 |
|
| 60 |
if model is not None and tokenizer is not None:
|
|
|
|
| 69 |
|
| 70 |
print(f"Loading on device: {device} with dtype: {torch_dtype}")
|
| 71 |
|
| 72 |
+
# Use SDPA attention which provides excellent performance without compilation issues
|
| 73 |
+
attn_implementation = 'sdpa'
|
| 74 |
+
print("🚀 Using SDPA (Scaled Dot Product Attention) for optimal compatibility and performance")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
|
| 76 |
# Load model with memory optimization for Spaces
|
| 77 |
model = AutoModel.from_pretrained(
|
|
|
|
| 89 |
trust_remote_code=True
|
| 90 |
)
|
| 91 |
|
| 92 |
+
print("✅ Model loaded successfully with SDPA!")
|
| 93 |
return model, tokenizer
|
| 94 |
|
| 95 |
except Exception as e:
|
| 96 |
+
print(f"❌ Error loading model: {e}")
|
| 97 |
+
raise e
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
|
| 99 |
def analyze_video(video_file, prompt, max_frames):
|
| 100 |
"""Analyze video using MiniCPM-o 2.6"""
|
|
|
|
| 147 |
**Processing Time:** {processing_time:.2f} seconds
|
| 148 |
**Frames Analyzed:** {len(frames)}
|
| 149 |
**Model:** MiniCPM-o 2.6
|
| 150 |
+
**Attention:** SDPA (Optimized)
|
| 151 |
|
| 152 |
### Analysis:
|
| 153 |
{answer}
|
|
|
|
| 213 |
- 🖼️ Frame-by-frame understanding
|
| 214 |
- 📝 Detailed descriptions
|
| 215 |
- 🎨 Creative and marketing insights
|
| 216 |
+
- 🚀 SDPA optimized for reliable performance
|
| 217 |
|
| 218 |
**Supported formats:** MP4, AVI, MOV, WebM
|
| 219 |
""")
|
|
|
|
| 304 |
|
| 305 |
- **Model:** [openbmb/MiniCPM-o-2_6](https://huggingface.co/openbmb/MiniCPM-o-2_6)
|
| 306 |
- **Code:** Based on exact official sample implementation
|
| 307 |
+
- **GPU:** Optimized for Hugging Face Spaces GPU with SDPA
|
| 308 |
|
| 309 |
**Note:** Processing time depends on video length and complexity.
|
| 310 |
""")
|
requirements.txt
CHANGED
|
@@ -3,9 +3,6 @@ torch==2.3.1
|
|
| 3 |
torchaudio==2.3.1
|
| 4 |
torchvision==0.18.1
|
| 5 |
|
| 6 |
-
# Flash Attention - needs torch to be installed first
|
| 7 |
-
flash-attn==2.8.0.post2
|
| 8 |
-
|
| 9 |
# Core ML libraries
|
| 10 |
transformers==4.44.2
|
| 11 |
accelerate==1.2.1
|
|
|
|
| 3 |
torchaudio==2.3.1
|
| 4 |
torchvision==0.18.1
|
| 5 |
|
|
|
|
|
|
|
|
|
|
| 6 |
# Core ML libraries
|
| 7 |
transformers==4.44.2
|
| 8 |
accelerate==1.2.1
|