File size: 4,511 Bytes
7cfdc4c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Shared Image Processing Utilities

Provides common image processing operations (upscale, remove background)
that can be used by both agent tools and API endpoints.

Models used:
- Upscale: recraft-ai/recraft-crisp-upscale (Replicate)
- Remove BG: 851-labs/background-remover (Replicate)
"""

import os
import base64
import replicate
from typing import Tuple
from core.utils.logger import logger
from core.utils.config import get_config


def get_replicate_token() -> str:
    """Get and set Replicate API token from config."""
    config = get_config()
    token = config.REPLICATE_API_TOKEN
    if not token:
        raise Exception("Replicate API token not configured. Add REPLICATE_API_TOKEN to your .env")
    os.environ["REPLICATE_API_TOKEN"] = token
    return token


def replicate_output_to_bytes(output, output_format: str = "png") -> Tuple[bytes, str]:
    """
    Convert Replicate FileOutput to bytes and mime type.
    
    Args:
        output: Replicate output (FileOutput or URL)
        output_format: Expected output format (png, webp, etc.)
        
    Returns:
        Tuple of (image_bytes, mime_type)
    """
    import urllib.request
    
    # FileOutput has .read() method for sync reading
    if hasattr(output, 'read'):
        result_bytes = output.read()
        mime = f"image/{output_format}"
        return result_bytes, mime
    
    # Fallback: fetch from URL if it's a URL string
    url = str(output.url) if hasattr(output, 'url') else str(output)
    with urllib.request.urlopen(url) as response:
        result_bytes = response.read()
        mime = f"image/{output_format}"
        return result_bytes, mime


def upscale_image_sync(image_bytes: bytes, mime_type: str = "image/png") -> Tuple[bytes, str]:
    """
    Upscale image using Replicate's recraft-ai/recraft-crisp-upscale.
    
    This is a synchronous function that blocks until completion.
    Use for agent tools where we want to wait for result.
    
    Args:
        image_bytes: Raw image bytes
        mime_type: MIME type of input image
        
    Returns:
        Tuple of (upscaled_image_bytes, output_mime_type)
        
    Raises:
        Exception: If upscale fails
    """
    get_replicate_token()
    
    # Convert bytes to data URL
    image_b64 = base64.b64encode(image_bytes).decode('utf-8')
    image_data_url = f"data:{mime_type};base64,{image_b64}"
    
    logger.info("Calling Replicate recraft-ai/recraft-crisp-upscale")
    
    try:
        output = replicate.run(
            "recraft-ai/recraft-crisp-upscale",
            input={"image": image_data_url}
        )
        # Output is webp format
        result_bytes, result_mime = replicate_output_to_bytes(output, "webp")
        logger.info(f"Upscale complete: {len(result_bytes)} bytes")
        return result_bytes, result_mime
    except Exception as e:
        logger.error(f"Replicate upscale error: {e}")
        raise Exception(f"Upscale failed: {str(e)}")


def remove_background_sync(image_bytes: bytes, mime_type: str = "image/png") -> Tuple[bytes, str]:
    """
    Remove background using Replicate's 851-labs/background-remover.
    
    This is a synchronous function that blocks until completion.
    Use for agent tools where we want to wait for result.
    
    Args:
        image_bytes: Raw image bytes
        mime_type: MIME type of input image
        
    Returns:
        Tuple of (result_image_bytes, output_mime_type) - PNG with transparency
        
    Raises:
        Exception: If background removal fails
    """
    get_replicate_token()
    
    # Convert bytes to data URL
    image_b64 = base64.b64encode(image_bytes).decode('utf-8')
    image_data_url = f"data:{mime_type};base64,{image_b64}"
    
    logger.info("Calling Replicate 851-labs/background-remover")
    
    try:
        output = replicate.run(
            "851-labs/background-remover:a029dff38972b5fda4ec5d75d7d1cd25aeff621d2cf4946a41055d7db66b80bc",
            input={"image": image_data_url}
        )
        # Output is PNG with transparency
        result_bytes, result_mime = replicate_output_to_bytes(output, "png")
        logger.info(f"Remove BG complete: {len(result_bytes)} bytes")
        return result_bytes, result_mime
    except Exception as e:
        logger.error(f"Replicate remove_bg error: {e}")
        raise Exception(f"Background removal failed: {str(e)}")


# Model identifiers for billing
UPSCALE_MODEL = "recraft-ai/recraft-crisp-upscale"
REMOVE_BG_MODEL = "851-labs/background-remover"