sharpify-ai-inference / space_app.py
suplo6669's picture
Add Sharpify Real-ESRGAN ZeroGPU inference service
a027087 verified
Raw History Blame Contribute Delete
7.51 kB
"""Hugging Face ZeroGPU adapter for the verified Sharpify inference service."""
# Gradio's cache path is set before its first file operation.
# ruff: noqa: I001
from __future__ import annotations
import logging
import os
import re
import tempfile
import threading
import time
import uuid
from pathlib import Path
from typing import cast
_temp_dir = Path(
os.environ.get(
"GRADIO_TEMP_DIR", str(Path(tempfile.gettempdir()) / "sharpify-gradio")
)
).resolve()
os.environ.setdefault("GRADIO_TEMP_DIR", str(_temp_dir))
_temp_dir.mkdir(parents=True, exist_ok=True)
import spaces
import torch
import gradio as gr
from app.config import Settings
from app.models.contracts import Scale
from app.services.enhancer import (
EnhancementError,
GPUOutOfMemoryError,
ImageEnhancer,
)
from app.utils.image_io import ImageInputError, encode_image_bytes
from scripts.download_weights import download
logger = logging.getLogger("sharpify.hosted")
MAX_UPLOAD_BYTES = 20_000_000
downloaded_weights = download()
hosted_settings = Settings(
device="cuda",
precision="fp16",
model_path=downloaded_weights,
tile_size=128,
tile_pad=32,
pre_pad=10,
cpu_threads=4,
max_upload_bytes=MAX_UPLOAD_BYTES,
max_input_pixels=4_000_000,
max_output_pixels=64_000_000,
)
# ZeroGPU requires CUDA placement during module initialization. The first real
# CUDA operation is deferred to an allocated call below.
enhancer = ImageEnhancer(
hosted_settings,
warm_up_on_init=False,
device_override="cuda",
)
_warmed_up = False
_warm_up_lock = threading.Lock()
_transient_cleanup_lock = threading.Lock()
_TRANSIENT_MAX_AGE_SECONDS = 900
def _cleanup_stale_transients() -> None:
cutoff = time.time() - _TRANSIENT_MAX_AGE_SECONDS
with _transient_cleanup_lock:
for candidate in _temp_dir.glob("sharpify-output-*"):
try:
if candidate.is_file() and candidate.stat().st_mtime < cutoff:
candidate.unlink(missing_ok=True)
except OSError:
logger.warning(
"Could not remove an expired transient image", exc_info=True
)
def _transient_cleanup_worker() -> None:
while True:
time.sleep(60)
_cleanup_stale_transients()
_cleanup_stale_transients()
threading.Thread(target=_transient_cleanup_worker, daemon=True).start()
def _safe_filename(filename: str, scale: Scale, output_format: str) -> str:
basename = Path(filename.replace("\\", "/")).name
stem = re.sub(r"[^A-Za-z0-9_-]+", "-", Path(basename).stem).strip("-_")[:80]
return f"{stem or 'image'}-enhanced-{scale}x.{output_format}"
def _error(code: str, message: str) -> tuple[None, dict[str, dict[str, str]]]:
return None, {"error": {"code": code, "message": message}}
def _save_transient(data: bytes, filename: str) -> str:
_cleanup_stale_transients()
destination = _temp_dir / f"sharpify-output-{uuid.uuid4().hex}-{filename}"
destination.write_bytes(data)
return str(destination)
@spaces.GPU(duration=120)
def enhance_image(
image_bytes: bytes,
filename: str,
scale_value: str,
output_format: str,
oauth_token: gr.OAuthToken,
) -> tuple[str | None, dict[str, object]]:
"""Enhance one bounded upload and return a transient file plus metadata."""
if oauth_token is None: # Protect against missing OAuth sessions at runtime.
return _error(
"auth_required", "Sign in with Hugging Face before enhancing images."
)
if not isinstance(image_bytes, bytes) or not image_bytes:
return _error("invalid_image", "Choose a non-empty JPEG, PNG, or WEBP image.")
if len(image_bytes) > MAX_UPLOAD_BYTES:
return _error("upload_too_large", "The upload exceeds the 20 MB limit.")
if scale_value not in {"2", "4"}:
return _error("invalid_request", "Choose either 2x or 4x upscaling.")
if output_format not in {"png", "jpeg", "webp"}:
return _error("invalid_request", "Choose PNG, JPEG, or WEBP output.")
if not isinstance(filename, str) or not filename.strip():
filename = "image"
global _warmed_up
try:
if not _warmed_up:
with _warm_up_lock:
if not _warmed_up:
enhancer._warm_up()
_warmed_up = True
scale = cast(Scale, int(scale_value))
result = enhancer.enhance_bytes(image_bytes, scale)
output = encode_image_bytes(result.image, None, output_format)
safe_name = _safe_filename(filename, scale, output_format)
path = _save_transient(output, safe_name)
metadata: dict[str, object] = {
"original_width": result.original_width,
"original_height": result.original_height,
"enhanced_width": result.enhanced_width,
"enhanced_height": result.enhanced_height,
"scale": scale,
"model": result.model,
"filename": safe_name,
"content_type": {
"png": "image/png",
"jpeg": "image/jpeg",
"webp": "image/webp",
}[output_format],
}
return path, metadata
except ImageInputError as error:
return _error("invalid_image", str(error))
except (GPUOutOfMemoryError, torch.cuda.OutOfMemoryError):
try:
torch.cuda.empty_cache()
except Exception:
logger.exception("Could not clear CUDA cache after an out-of-memory error")
return _error(
"gpu_out_of_memory",
"The GPU ran out of memory. Try a smaller image.",
)
except EnhancementError:
logger.exception("Real-ESRGAN could not complete an upload")
return _error(
"enhancement_failed",
"The image could not be enhanced with the available resources.",
)
except (MemoryError, OSError):
logger.exception("The hosted service could not allocate or save the result")
return _error(
"resource_unavailable",
"The service could not prepare this image. Try a smaller input.",
)
except Exception:
logger.exception("Unexpected hosted Real-ESRGAN inference failure")
return _error(
"inference_failed",
"Image enhancement failed. Please try again.",
)
with gr.Blocks(delete_cache=(60, 300), analytics_enabled=False) as demo:
upload = gr.File(
label="Image",
file_count="single",
file_types=[".jpg", ".jpeg", ".png", ".webp"],
type="binary",
)
filename_input = gr.Textbox(label="Filename", visible=False)
scale_input = gr.Radio(
choices=["2", "4"], value="4", label="Upscale factor", visible=False
)
format_input = gr.Radio(
choices=["png", "jpeg", "webp"],
value="png",
label="Output format",
visible=False,
)
enhance_button = gr.Button("Enhance", visible=False)
output_file = gr.File(label="Enhanced image", interactive=False)
output_metadata = gr.JSON(label="Image metadata")
enhance_button.click(
enhance_image,
inputs=[upload, filename_input, scale_input, format_input],
outputs=[output_file, output_metadata],
api_name="enhance",
concurrency_limit=1,
)
demo.queue(default_concurrency_limit=1, max_size=10)
if __name__ == "__main__":
demo.launch(max_file_size=MAX_UPLOAD_BYTES, show_error=False)