Spaces:
Running on Zero
Running on Zero
File size: 10,139 Bytes
082393b | 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 | """V1 helper API endpoints — browser-memory storage mode.
Inference endpoints accept the image directly in the request body (base64 JSON).
No session state lookup needed.
On ZeroGPU Spaces, inference is routed through @spaces.GPU-decorated functions
from gradio_endpoints so a real GPU is allocated for each call.
"""
from __future__ import annotations
import asyncio
import base64
import json
import logging
import os
import re
from collections.abc import AsyncGenerator, Generator
from io import BytesIO
from typing import Any
from fastapi import HTTPException, Request
from fastapi.responses import StreamingResponse
from PIL import Image as PILImage
logger = logging.getLogger(__name__)
def _is_zerogpu() -> bool:
"""Detect HuggingFace ZeroGPU Spaces."""
return bool(os.environ.get("SPACE_ID")) and bool(os.environ.get("ZERO_GPU"))
def _decode_request_image(image_b64: str) -> PILImage.Image:
"""Decode a base64-encoded image from the request body."""
img_bytes = base64.b64decode(image_b64)
return PILImage.open(BytesIO(img_bytes)).convert("RGB")
def _sse_event(data: dict[str, str]) -> str:
"""Format a dict as an SSE data line."""
return f"data: {json.dumps(data)}\n\n"
async def _sse_stream_v1(
task_label: str,
token_generator: Generator[str, None, None],
post_process: Any | None = None,
) -> AsyncGenerator[str, None]:
"""Wrap a blocking token generator as SSE events (no session check needed).
The generator yields token deltas. Each delta is sent as a chunk event.
"""
yield _sse_event({"type": "status", "message": task_label})
full_text = ""
try:
for delta in token_generator:
if delta:
full_text += delta
yield _sse_event({"type": "chunk", "content": delta})
await asyncio.sleep(0)
if post_process is not None:
processed = post_process(full_text)
if processed != full_text:
yield _sse_event({"type": "replace", "content": processed})
yield _sse_event({"type": "done"})
except Exception as e: # noqa: BLE001
logger.error("V1 streaming error: %s", e, exc_info=True)
yield _sse_event({"type": "error", "message": str(e)})
_GPU_MAX_RETRIES = 3
_GPU_BASE_DELAY = 2.0 # seconds
async def _sse_gpu_call(
task_label: str,
gpu_fn: Any,
image_b64: str,
post_process: Any | None = None,
) -> AsyncGenerator[str, None]:
"""Call a @spaces.GPU function in a thread and yield the result as SSE.
Retries up to _GPU_MAX_RETRIES times on 429 / rate-limit errors with
exponential backoff.
"""
yield _sse_event({"type": "status", "message": task_label})
last_exc: Exception | None = None
loop = asyncio.get_event_loop()
for attempt in range(_GPU_MAX_RETRIES):
try:
result = await loop.run_in_executor(None, gpu_fn, image_b64)
if post_process is not None:
result = post_process(result)
yield _sse_event({"type": "replace", "content": result})
yield _sse_event({"type": "done"})
return
except Exception as e: # noqa: BLE001
exc_str = str(e).lower()
if "429" in exc_str or "too many requests" in exc_str or "queue" in exc_str or ("exceeded" in exc_str and "gpu quota" in exc_str):
last_exc = e
delay = _GPU_BASE_DELAY * (2**attempt)
logger.warning(
"ZeroGPU rate-limited (attempt %d/%d), retrying in %.1fs: %s",
attempt + 1,
_GPU_MAX_RETRIES,
delay,
e,
)
yield _sse_event({"type": "status", "message": "Waiting for GPU to become available..."})
await asyncio.sleep(delay)
else:
logger.error("V1 GPU inference error: %s", e, exc_info=True)
yield _sse_event({"type": "error", "message": str(e)})
return
logger.error("V1 GPU inference failed after %d retries: %s", _GPU_MAX_RETRIES, last_exc)
yield _sse_event({"type": "error", "message": f"GPU rate-limited after {_GPU_MAX_RETRIES} retries: {last_exc}"})
_STREAM_HEADERS = {
"Cache-Control": "no-cache",
"Connection": "keep-alive",
"X-Accel-Buffering": "no",
}
async def _get_image_from_body(request: Request) -> PILImage.Image:
"""Extract and decode the image from a JSON request body."""
image_b64 = await _get_image_b64_from_body(request)
try:
return _decode_request_image(image_b64)
except Exception as e: # noqa: BLE001
raise HTTPException(status_code=400, detail=f"Invalid image data: {e}") from e
async def _get_image_b64_from_body(request: Request) -> str:
"""Extract the raw base64 image string from a JSON request body."""
try:
body = await request.json()
except Exception as e: # noqa: BLE001
raise HTTPException(status_code=400, detail=f"Invalid JSON body: {e}") from e
image_b64 = body.get("image")
if not image_b64:
raise HTTPException(status_code=400, detail="Missing 'image' field in request body")
return image_b64
def create_helper_routes_v1(app: Any) -> None:
"""Register v1 helper API routes (image in request body)."""
zerogpu = _is_zerogpu()
@app.post("/api/v1/helpers/chart2summary/stream")
async def api_v1_chart2summary_stream(request: Request) -> StreamingResponse:
if zerogpu:
from gradio_endpoints import infer_chart2summary_sync
image_b64 = await _get_image_b64_from_body(request)
return StreamingResponse(
_sse_gpu_call("Generating summary...", infer_chart2summary_sync, image_b64),
media_type="text/event-stream",
headers=_STREAM_HEADERS,
)
from infer_vision_qa import answer_question_stream
image = await _get_image_from_body(request)
gen = answer_question_stream(image, "<chart2summary>", [], None)
return StreamingResponse(
_sse_stream_v1("Generating summary...", gen),
media_type="text/event-stream",
headers=_STREAM_HEADERS,
)
@app.post("/api/v1/helpers/chart2csv/stream")
async def api_v1_chart2csv_stream(request: Request) -> StreamingResponse:
if zerogpu:
from gradio_endpoints import infer_chart2csv_sync
image_b64 = await _get_image_b64_from_body(request)
return StreamingResponse(
_sse_gpu_call("Extracting CSV...", infer_chart2csv_sync, image_b64),
media_type="text/event-stream",
headers=_STREAM_HEADERS,
)
from infer_chart2csv import extract_csv_stream
image = await _get_image_from_body(request)
gen = extract_csv_stream(image)
return StreamingResponse(
_sse_stream_v1("Extracting CSV...", gen),
media_type="text/event-stream",
headers=_STREAM_HEADERS,
)
@app.post("/api/v1/helpers/chart2code/stream")
async def api_v1_chart2code_stream(request: Request) -> StreamingResponse:
if zerogpu:
from gradio_endpoints import infer_chart2code_sync
image_b64 = await _get_image_b64_from_body(request)
return StreamingResponse(
_sse_gpu_call("Generating code...", infer_chart2code_sync, image_b64),
media_type="text/event-stream",
headers=_STREAM_HEADERS,
)
from app import PROMPT_TEXT_CODE
from infer_vision_qa import answer_question_stream
image = await _get_image_from_body(request)
gen = answer_question_stream(image, PROMPT_TEXT_CODE, [], None)
return StreamingResponse(
_sse_stream_v1("Generating code...", gen),
media_type="text/event-stream",
headers=_STREAM_HEADERS,
)
@app.post("/api/v1/helpers/table-extract/stream")
async def api_v1_table_extract_stream(request: Request) -> StreamingResponse:
if zerogpu:
from gradio_endpoints import infer_table_extract_sync
image_b64 = await _get_image_b64_from_body(request)
return StreamingResponse(
_sse_gpu_call("Extracting table...", infer_table_extract_sync, image_b64),
media_type="text/event-stream",
headers=_STREAM_HEADERS,
)
from infer_vision_qa import answer_question_stream
def _clean_table_html(text: str) -> str:
text = re.sub(r"^```(?:html)?\s*", "", text.strip())
text = re.sub(r"\s*```$", "", text.strip())
text = re.sub(r"^\[\s*", "", text.strip())
text = re.sub(r"\s*\]$", "", text.strip())
return text
image = await _get_image_from_body(request)
gen = answer_question_stream(image, "<tables_html>", [], None)
return StreamingResponse(
_sse_stream_v1("Extracting table...", gen, post_process=_clean_table_html),
media_type="text/event-stream",
headers=_STREAM_HEADERS,
)
@app.post("/api/v1/helpers/describe-image/stream")
async def api_v1_describe_image_stream(request: Request) -> StreamingResponse:
if zerogpu:
from gradio_endpoints import infer_describe_image_sync
image_b64 = await _get_image_b64_from_body(request)
return StreamingResponse(
_sse_gpu_call("Describing image...", infer_describe_image_sync, image_b64),
media_type="text/event-stream",
headers=_STREAM_HEADERS,
)
from infer_vision_qa import answer_question_stream
image = await _get_image_from_body(request)
gen = answer_question_stream(image, "Describe this image in detail", [], None)
return StreamingResponse(
_sse_stream_v1("Describing image...", gen),
media_type="text/event-stream",
headers=_STREAM_HEADERS,
)
|