Spaces:
Running on Zero
Running on Zero
File size: 14,007 Bytes
f1f0cc8 0062fa7 2866f2d 9a17de0 f1f0cc8 2866f2d f1f0cc8 0f088a7 1c3f461 77cc6e5 1c3f461 f1f0cc8 770f17c f1f0cc8 2866f2d f1f0cc8 2866f2d 0b3743c abca38d 0b3743c f1f0cc8 32967e1 0062fa7 abca38d f1f0cc8 abca38d 0b3743c abca38d 2866f2d f1f0cc8 77cc6e5 f1f0cc8 77cc6e5 288b222 2866f2d 0b3743c 2866f2d abca38d 770f17c f1f0cc8 2866f2d f1f0cc8 0b3743c fb0d17a abca38d fb0d17a 2866f2d 0b3743c c438e0e 0062fa7 c438e0e 0062fa7 0b3743c 28c1246 0b3743c 2866f2d 10871c8 c438e0e 0062fa7 2866f2d 28c1246 f1f0cc8 0062fa7 10871c8 bca5d00 10871c8 f1f0cc8 2866f2d f1f0cc8 abca38d 2866f2d abca38d 2866f2d 0062fa7 2866f2d abca38d 2866f2d f1f0cc8 0b3743c 77cc6e5 f1f0cc8 77cc6e5 1c3f461 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 | import os
import torch
import re
import av
import uuid
import copy
import threading
import time
import shutil
from PIL import Image
from transformers import AutoProcessor, MiniCPMV4_6ForConditionalGeneration, TextIteratorStreamer
from gradio import Server
from gradio.data_classes import FileData
from fastapi.responses import HTMLResponse
import logging
# Silence asyncio noise from ZeroGPU cleanup
logging.getLogger("asyncio").setLevel(logging.CRITICAL)
from starlette.middleware import Middleware
import hashlib
import base64
import json
# ---------- Logging Middleware ----------
def _headers_from_asgi(raw_headers) -> list[dict]:
headers = []
for raw_key, raw_value in raw_headers or []:
headers.append({
"name": raw_key.decode("latin-1", errors="replace"),
"value": raw_value.decode("latin-1", errors="replace"),
})
return headers
def _header_value(headers: list[dict], name: str) -> str:
name = name.lower()
for header in headers:
if header["name"].lower() == name:
return header["value"]
return ""
def _body_text(data: bytes, content_type: str) -> str | None:
if not data: return ""
lower_type = (content_type or "").lower()
if "text/" in lower_type or "json" in lower_type or "x-www-form-urlencoded" in lower_type:
return data.decode("utf-8", errors="replace")
return None
def _body_record(data: bytes, content_type: str) -> dict:
return {
"size": len(data),
"sha256": hashlib.sha256(data).hexdigest() if data else "",
"base64": base64.b64encode(data).decode("ascii") if data else "",
"text": _body_text(data, content_type),
}
def _append_http_log(record: dict) -> None:
os.makedirs(os.path.dirname(HTTP_LOG_FILE), exist_ok=True)
line = json.dumps(record, ensure_ascii=False, separators=(",", ":"))
with HTTP_LOG_LOCK:
with open(HTTP_LOG_FILE, "a", encoding="utf-8") as f:
f.write(line + "\n")
class HTTPRequestLogMiddleware:
def __init__(self, app):
self.app = app
async def __call__(self, scope, receive, send):
if scope.get("type") != "http":
await self.app(scope, receive, send)
return
started = time.time()
request_id = uuid.uuid4().hex[:12]
request_body = bytearray()
response_headers = []
response_body = bytearray()
status_code = None
async def receive_wrapper():
message = await receive()
if message.get("type") == "http.request":
chunk = message.get("body", b"")
if chunk: request_body.extend(chunk)
return message
async def send_wrapper(message):
nonlocal status_code, response_headers
if message.get("type") == "http.response.start":
status_code = message.get("status")
response_headers = _headers_from_asgi(message.get("headers", []))
elif message.get("type") == "http.response.body":
chunk = message.get("body", b"")
if chunk: response_body.extend(chunk)
await send(message)
try:
await self.app(scope, receive_wrapper, send_wrapper)
finally:
request_headers = _headers_from_asgi(scope.get("headers", []))
client = scope.get("client") or (None, None)
record = {
"ts": time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime(started)),
"request_id": request_id,
"client_id": _header_value(request_headers, "x-v46-client-id"),
"method": scope.get("method"),
"path": scope.get("path"),
"status_code": status_code,
"duration_ms": round((time.time() - started) * 1000, 2),
}
try:
_append_http_log(record)
except Exception as e:
print(f"Logging error: {e}")
import spaces
from typing import Generator
# ---------- Globals & Model Loading ----------
MODEL_ID = "openbmb/MiniCPM-V-4.6"
print(f"Loading processor: {MODEL_ID}")
processor = AutoProcessor.from_pretrained(MODEL_ID, trust_remote_code=True)
print(f"Loading model: {MODEL_ID}")
model = MiniCPMV4_6ForConditionalGeneration.from_pretrained(
MODEL_ID,
torch_dtype=torch.bfloat16,
attn_implementation="sdpa",
trust_remote_code=True,
device_map="cuda"
).eval()
# ---------- Logging & Helper Functions ----------
PROJECT_ROOT = os.path.dirname(os.path.abspath(__file__))
LOG_DIR = os.path.join(PROJECT_ROOT, "logs")
UPLOAD_LOG_DIR = os.path.join(LOG_DIR, "uploads")
HTTP_LOG_FILE = os.path.join(LOG_DIR, "http_requests.jsonl")
RAW_OUTPUT_LOG_FILE = os.path.join(LOG_DIR, "raw_model_outputs.jsonl")
HTTP_LOG_LOCK = threading.Lock()
RAW_OUTPUT_LOG_LOCK = threading.Lock()
def _append_raw_output_log(record: dict) -> None:
os.makedirs(os.path.dirname(RAW_OUTPUT_LOG_FILE), exist_ok=True)
line = json.dumps(record, ensure_ascii=False, separators=(",", ":"))
with RAW_OUTPUT_LOG_LOCK:
with open(RAW_OUTPUT_LOG_FILE, "a", encoding="utf-8") as f:
f.write(line + "\n")
def log_raw_model_output(session_id: str, **record) -> None:
payload = {
"ts": time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime()),
"session_id": session_id,
**record,
}
try:
_append_raw_output_log(payload)
except Exception as e:
print(f"Logging error: {e}")
def load_video(video_path, max_frames=64):
"""Fast video loading using PyAV timestamp seeking."""
try:
container = av.open(video_path)
stream = container.streams.video[0]
stream.thread_count = 8
duration = stream.duration
if duration is None or duration <= 0:
frames = [f.to_image() for f in container.decode(video=0)]
if len(frames) > max_frames:
indices = [int(i * len(frames) / max_frames) for i in range(max_frames)]
return [frames[i] for i in indices]
return frames
indices = [int(i * duration / max_frames) for i in range(max_frames)]
frames = []
for ts in indices:
container.seek(ts, stream=stream)
for frame in container.decode(video=0):
frames.append(frame.to_image())
break
container.close()
return frames
except Exception as e:
print(f"Error loading video: {e}")
return None
def persist_uploaded_files(files: list, session_id: str) -> list:
"""Copy Gradio temp uploads into the project log directory."""
if not files: return []
dest_dir = os.path.join(UPLOAD_LOG_DIR, session_id or "session")
os.makedirs(dest_dir, exist_ok=True)
persisted = []
for f in files:
src = f["path"] if isinstance(f, dict) else f
if not os.path.isfile(src):
persisted.append(src)
continue
base = os.path.basename(src)
stamp = time.strftime("%Y%m%dT%H%M%SZ", time.gmtime())
dest = os.path.join(dest_dir, f"{stamp}-{uuid.uuid4().hex[:8]}-{base}")
shutil.copy2(src, dest)
persisted.append(dest)
return persisted
def normalize_response_text(text: str) -> str:
"""Robust conversion of literal \n to newlines while protecting code/LaTeX."""
if not isinstance(text, str) or "\\" not in text:
return text
protected = {}
counter = [0]
def _convert(v):
v = re.sub(r"(?<!\\)(?:\\r\\n|\\n|\\r){2,}", lambda m: "\n" * len(re.findall(r"\\n|\\r", m.group(0))), v)
v = re.sub(r"(?<!\\)\\r\\n", "\n", v)
v = re.sub(r"(?<!\\)\\n(?![a-zA-Z])", "\n", v)
return v
def _protect(m):
key = f"\x00P{counter[0]}\x00"
counter[0] += 1
protected[key] = m.group(0)
return key
res = text
res = re.sub(r"```[\s\S]*?```", lambda m: _protect(re.match(r"```[\s\S]*?```", _convert(m.group(0)))), res) # Simplified for parity
res = re.sub(r"`[^`]+`", _protect, res)
res = _convert(res)
for k, v in protected.items(): res = res.replace(k, v)
return res
# ---------- Inference Endpoint ----------
demo = Server()
@demo.api()
@spaces.GPU(duration=120)
def predict(
message: str,
history: list[list] = None,
files: list[FileData] = None,
thinking_mode: bool = True,
max_new_tokens: int = 1024,
temperature: float = 0.7,
top_p: float = 0.8,
top_k: int = 100,
max_frames: int = 64,
generation_mode: str = "Sampling"
) -> Generator[str, None, None]:
"""
Streaming inference endpoint with history support.
"""
session_id = str(uuid.uuid4())
# Persist files in background to avoid blocking user (parity audit)
if files:
threading.Thread(target=persist_uploaded_files, args=(files, session_id), daemon=True).start()
messages = []
# Process history
if history:
for turn in history:
# history turn is [user_text, assistant_text, [optional_file_paths]]
user_text = turn[0]
assistant_text = turn[1]
turn_files = turn[2] if len(turn) > 2 else []
h_content = []
if turn_files:
for f_path in turn_files:
# In history, we don't have mime_type, so we check extension
ext = os.path.splitext(f_path)[1].lower()
if ext in {".mp4", ".mkv", ".mov", ".avi", ".webm"}:
v_frames = load_video(f_path, max_frames=max_frames)
if v_frames:
h_content.append({"type": "video", "video": v_frames})
else:
h_content.append({"type": "video", "path": f_path})
else:
try:
img = Image.open(f_path).convert("RGB")
h_content.append({"type": "image", "image": img})
except Exception:
v_frames = load_video(f_path, max_frames=max_frames)
if v_frames:
h_content.append({"type": "video", "video": v_frames})
else:
h_content.append({"type": "video", "path": f_path})
if user_text:
h_content.append({"type": "text", "text": user_text})
if h_content:
messages.append({"role": "user", "content": h_content})
if assistant_text:
messages.append({"role": "assistant", "content": [{"type": "text", "text": assistant_text}]})
content = []
if files:
for f in files:
file_path = f["path"]
try:
# Try image first
img = Image.open(file_path).convert("RGB")
content.append({"type": "image", "image": img})
except Exception:
# Fallback to manual video frame extraction (bypasses broken torchvision)
v_frames = load_video(file_path, max_frames=max_frames)
if v_frames:
content.append({"type": "video", "video": v_frames})
else:
print(f"Failed to load video: {file_path}")
if message:
content.append({"type": "text", "text": message})
if content:
messages.append({"role": "user", "content": content})
# Prepare inputs with Advanced Parameters for MiniCPM-V 4.6
with torch.no_grad():
inputs = processor.apply_chat_template(
messages,
add_generation_prompt=True,
tokenize=True,
return_dict=True,
return_tensors="pt",
enable_thinking=thinking_mode,
processor_kwargs={
"downsample_mode": "16x",
"max_slice_nums": 1 if any(it.get("type") == "video" for msg in messages for it in msg["content"]) else 9,
"use_image_id": False if any(it.get("type") == "video" for msg in messages for it in msg["content"]) else True,
"videos_kwargs": {
"max_num_frames": max_frames,
"do_sample_frames": False, # Frames are already sampled by load_video
"stack_frames": 1,
}
}
).to(model.device)
for k, v in inputs.items():
if isinstance(v, torch.Tensor) and torch.is_floating_point(v):
inputs[k] = v.to(dtype=torch.bfloat16)
streamer = TextIteratorStreamer(
processor.tokenizer,
skip_prompt=True,
skip_special_tokens=True,
)
sampling = (generation_mode == "Sampling")
generate_kwargs = {
**inputs,
"max_new_tokens": max_new_tokens,
"do_sample": sampling,
"streamer": streamer,
"downsample_mode": "16x"
}
if sampling:
generate_kwargs.update({
"temperature": temperature,
"top_p": top_p,
"top_k": top_k,
})
else:
generate_kwargs.update({"num_beams": 1})
thread = threading.Thread(target=model.generate, kwargs=generate_kwargs)
thread.start()
full_text = ""
for new_text in streamer:
full_text += new_text
yield normalize_response_text(full_text)
log_raw_model_output(session_id, message=message, response=full_text, variant="thinking" if thinking_mode else "instruct")
@demo.get("/", response_class=HTMLResponse)
async def homepage():
html_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), "index.html")
with open(html_path, "r", encoding="utf-8") as f:
return f.read()
if __name__ == "__main__":
demo.launch(
show_error=True,
app_kwargs={"middleware": [Middleware(HTTPRequestLogMiddleware)]}
)
|