File size: 13,145 Bytes
015bcc4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3987e25
 
015bcc4
 
 
 
 
 
 
 
 
3987e25
 
c545b69
3987e25
 
015bcc4
 
 
 
 
 
 
 
 
 
647a13f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7eb88b3
3987e25
647a13f
 
 
3987e25
 
 
 
 
647a13f
 
dde8c61
2d1d47f
647a13f
015bcc4
 
 
 
 
 
 
 
 
2d1d47f
 
a885e1e
 
 
 
 
 
 
 
 
 
a7d11dc
015bcc4
 
 
 
c47dbb1
 
015bcc4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ad78530
015bcc4
 
58edf8e
015bcc4
 
 
 
 
 
 
 
 
 
ad78530
015bcc4
7eb88b3
015bcc4
 
 
 
 
 
 
 
 
 
 
c47dbb1
015bcc4
c545b69
 
 
 
 
 
 
 
 
 
 
015bcc4
 
3987e25
c47dbb1
015bcc4
 
 
 
 
647a13f
015bcc4
 
 
 
 
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
import os
import math
import gradio as gr
import numpy as np
import requests
import json
import base64
from PIL import Image
from io import BytesIO
import runpod
from enum import Enum


api_key = os.getenv("FAI_API_KEY")
api = os.getenv("FAI_API")


def image_to_base64(image):
    # Open the image file
    with image:
        # Create a buffer to hold the binary data
        buffered = BytesIO()
        # Save the image in its original format to the buffer
        #print(image.format)
        image.save(buffered, format="PNG")
        # Get the byte data from the buffer
        binary_image_data = buffered.getvalue()
        # Encode the binary data to a base64 string
        base64_image = base64.b64encode(binary_image_data).decode("utf-8")
        return base64_image
    
     
def process(data, api, api_key):
    
    runpod.api_key = api_key
    input_payload = {"input": data }

    try:
        endpoint = runpod.Endpoint(api)
        run_request = endpoint.run(input_payload)

        # Initial check without blocking, useful for quick tasks
        status = run_request.status()
        print(f"Initial job status: {status}")
        if status=="IN_QUEUE":
            raise gr.Info("Queued 🚶🚶🚶🚶!", duration=15)

        if status != "COMPLETED":
            # Polling with timeout for long-running tasks
            output = run_request.output(timeout=60)
        else:
            output = run_request.output()
        print(f"Job output: {output}")
    except Exception as e:
        print(f"An error occurred: {e}")
        status = run_request.status()
        if status=="FAILED":
            raise gr.Error(f"An error occured 💥! {e}", duration=5)
        if status=="TIMED_OUT":
            raise gr.Error("Sorry we could not secure a worker for you ⏳! Try again", duration=5)
        

    image_data = output['image']
    # Decode the Base64 string
    image_bytes = base64.b64decode(image_data)
    # Convert binary data to image
    image = Image.open(BytesIO(image_bytes))
    
    return image

def resize_to_fit(max_size, original_size):
    """
    Calculate the new size for an image to fit within max_size while maintaining the aspect ratio.

    :param max_size: Maximum allowed size as a tuple (width, height).
    :param original_size: Original size of the image as a tuple (width, height).
    :return: New size as a tuple (new_width, new_height) that fits within max_size while maintaining the aspect ratio.
    """
    original_width, original_height = original_size
    max_width, max_height = max_size

    # Calculate the scaling factor to maintain aspect ratio
    width_ratio = max_width / original_width
    height_ratio = max_height / original_height
    scaling_factor = min(width_ratio, height_ratio)

    # Calculate the new size while maintaining the aspect ratio
    new_width = int(original_width * scaling_factor)
    new_height = int(original_height * scaling_factor)

    return new_width, new_height

    
def process_generate(fore, prompt, intensity, mode, refprompt, bg):    
        
    size = fore.size
    image_width = size[0]
    image_height = size[1]
    
    if size[0]*size[1]<=(768*768):
        gr.Warning("ℹ️ The input image resolution is low, it might lead to some deformation!")
        
    if size[0]*size[1]>(1500*1500):
        gr.Warning("ℹ️ The input image size is too big, I will lower it!")
        image_width, image_height = resize_to_fit((1500,1500), (image_width, image_height))


    
    forestr = image_to_base64(fore.convert("RGBA"))    
    data = {
        "foreground_image64": forestr,
        "prompt" : prompt,
        "mode" : mode,
        "intensity" : float(intensity),
        "width" : int(image_width),
        "height" : int(image_height),
        "refprompt" : refprompt
    }
    print(f"DATA: {data}")
    
    '''
    data = {
        "foreground_image64": forestr,
        "prompt" : "There is Perfume, nestled on a crystalline cliff of glistening snow, under a celestial night sky adorned with constellations and swirling galaxies, framed by ethereal, blue flames that dance gracefully in the icy air",
        "mode" : "full", #refiner, full
        "intensity" : 3.0,
        "width" : 1000,
        "height" : 1000,
        "refprompt" : " transparent glass "    
    }
    '''    
    image = process(data, api, api_key)

    return image

def update_value(val):
  return val

class Stage(Enum):
    FIRST_STAGE = "first-stage"
    SECOND_STAGE = "refiner"
    FULL = "full"

css="""#disp_image {
    text-align: center; /* Horizontally center the content */
}
#share-btn-container {padding-left: 0.5rem !important; padding-right: 0.5rem !important; background-color: #000000; justify-content: center; align-items: center; border-radius: 9999px !important; max-width: 13rem; margin-left: auto;}
div#share-btn-container > div {flex-direction: row;background: black;align-items: center}
#share-btn-container:hover {background-color: #060606}
#share-btn {all: initial; color: #ffffff;font-weight: 600; cursor:pointer; font-family: 'IBM Plex Sans', sans-serif; margin-left: 0.5rem !important; padding-top: 0.5rem !important; padding-bottom: 0.5rem !important;right:0;}
#share-btn * {all: unset}
#share-btn-container div:nth-child(-n+2){width: auto !important;min-height: 0px !important;}
#share-btn-container .wrap {display: none !important}
#share-btn-container.hidden {display: none!important}
#duplicate-button {
    margin-left: auto;
    color: #fff;
    background: #1565c0;
  }
        """
block = gr.Blocks(css=css, title="## F.ai Fuzer").queue()
with block:
    gr.HTML("""
            <center><h1 style="color:#000">Fotographer AI Fuzer</h1></center>""")
    
    gr.HTML('''
        <div>
        <a style="display:inline-block; margin-left: .5em" href="https://app.fotographer.ai/home"><img src="https://img.shields.io/badge/2310.15110-f9f7f7?logo=data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADcAAABMCAYAAADJPi9EAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAAuIwAALiMBeKU/dgAAABl0RVh0U29mdHdhcmUAd3d3Lmlua3NjYXBlLm9yZ5vuPBoAAAa2SURBVHja3Zt7bBRFGMAXUCDGF4rY7m7bAwuhlggKStFgLBgFEkCIIRJEEoOBYHwRFYKilUgEReVNJEGCJJpehHI3M9vZvd3bUP1DjNhEIRQQsQgSHiJgQZ5dv7krWEvvdmZ7d7vHJN+ft/f99pv5XvOtJMFCqvoCUpTdIEeRLC+L9Ox5i3Q9LACaCeK0kXoSChVcD3C/tQPHpAEsquQ73IkUcEz2kcLCknyGW5MGjkljRFVL8xJOKyi4CwCOuQAeAkfTP1+tNxLkogvgEbDgffkJqKqvuMA5ifOpqg/5qWecRstNg7xoUTI1Fovdxg8oy2s5AP8CGeYHmGngeZaOL4I4LXLcpHg4149/GDz4xqgsb+UAbMKKUpkrqHA43MUyyJpWUK0EHeG2YKRXr7tB+QMcgGewLD+ebTDbtrtbBt7UPlhS4rV4IvcDI7J8P1OeA/AcAI7LHljN7aB8XTowJmZt9EFRD/o0SDMH4HlwMhMyDWZZSAHFf3YDs3RS49WDLuaAY3IJq+qzmQKLxXAZKN7oDoYbdV3v5elPqiSpMyiOuAEVZVqHXb1OhloUH+MA+ztO0cAO/RkrfyBE7OAEbAZvO8vzVtTRWFD6DAfY5biBM3PWiaL0a4lvXICwnV8WjmE6ntYmhqX2jjp5LbMZjCw/wbYeN6CizOa2GMVzQOlmHjB4Ceuyk6LJ8huccEm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nE5xilYKAobZ77LpGZ8TAIun+/iCKQoF71IxQDI3K2CCd+ARNvXg9sykBcnHAoCZG4u66hlDoQLe6QV4CRtFSxZQ+D0BwNO2jgdkzoGoah1nj3FVlSR19taTSYxI8QLut23U8dsgzqHulJNCQpcqBnpTALCuQ6NSYLHpmR5i42gZzuIdcrMMvMJbQlxe3jXxyZnLACl7ARm/FjPIDOY8ODtpM71sxwfcZpvBeUzKWmfNINM5AS+wO0Khh7dMqKccu4+qatarZjYAwDlgetzStHtEt+XedsBOQtU9XMrRgjg4KTnc5nr+dmqadit/4C4uLm8DuA9koJTj1TL7fI5nDL+qqoo/FLGAzL7dYT17PzvAcQONYSUQRxW/QMrHZVIyik0ZuQA2mzp+Ji8BW4YM3Mbzm9inaHkJCGfrUZZjujiYailfFwA8DHIy3acwUj4v9vUVa+SmgNsl5fuyDTKovW9/IAmfLV0Pi2UncA515kjYdrwC9i9rpuHiq3JwtAAAAABJRU5ErkJggg=="></a>
        <a style="display:inline-block; margin-left: .5em" href='https://app.fotographer.ai/home'><img src='https://img.shields.io/github/stars/SUDO-AI-3D/zero123plus?style=social' /></a>
        Check out our App<a href="https://app.fotographer.ai/home">Fotographer.ai</a>! 
        </div>
        ''')

    with gr.Row():
        gr.Markdown("### F.ai Fuzer: Real Composite Photography in 2 minutes!")
    with gr.Row():
        fore = gr.Image(image_mode='RGBA', type="pil", label="Foreground Image", height=400)
        with gr.Column():
            
            result_gallery = gr.Image(label='Output')  #gr.Gallery(height=400, object_fit='contain', label='Outputs')
    with gr.Row():
        prompt = gr.Textbox(label="Prompt")
        with gr.Column():
            refprompt = gr.Textbox(label="Refiner Prompt")
    with gr.Row():
        mode = gr.Radio(choices=[e.value for e in Stage],
                            value=Stage.FULL.value,
                            label="Generation Mode", type='value')
        mode.change(fn=update_value, inputs=mode, outputs=mode)
        with gr.Column():
            bg = gr.Checkbox(info="Remove Backgroung")
            bg.change(fn=update_value, inputs=bg, outputs=bg)
            
        with gr.Column():
            gr.HTML('''
                <div>
                <a style="display:inline-block; margin-left: .5em" href="https://app.fotographer.ai/home"><img src="https://img.shields.io/badge/2310.15110-f9f7f7?logo=data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADcAAABMCAYAAADJPi9EAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAAuIwAALiMBeKU/dgAAABl0RVh0U29mdHdhcmUAd3d3Lmlua3NjYXBlLm9yZ5vuPBoAAAa2SURBVHja3Zt7bBRFGMAXUCDGF4rY7m7bAwuhlggKStFgLBgFEkCIIRJEEoOBYHwRFYKilUgEReVNJEGCJJpehHI3M9vZvd3bUP1DjNhEIRQQsQgSHiJgQZ5dv7krWEvvdmZ7d7vHJN+ft/f99pv5XvOtJMFCqvoCUpTdIEeRLC+L9Ox5i3Q9LACaCeK0kXoSChVcD3C/tQPHpAEsquQ73IkUcEz2kcLCknyGW5MGjkljRFVL8xJOKyi4CwCOuQAeAkfTP1+tNxLkogvgEbDgffkJqKqvuMA5ifOpqg/5qWecRstNg7xoUTI1Fovdxg8oy2s5AP8CGeYHmGngeZaOL4I4LXLcpHg4149/GDz4xqgsb+UAbMKKUpkrqHA43MUyyJpWUK0EHeG2YKRXr7tB+QMcgGewLD+ebTDbtrtbBt7UPlhS4rV4IvcDI7J8P1OeA/AcAI7LHljN7aB8XTowJmZt9EFRD/o0SDMH4HlwMhMyDWZZSAHFf3YDs3RS49WDLuaAY3IJq+qzmQKLxXAZKN7oDoYbdV3v5elPqiSpMyiOuAEVZVqHXb1OhloUH+MA+ztO0cAO/RkrfyBE7OAEbAZvO8vzVtTRWFD6DAfY5biBM3PWiaL0a4lvXICwnV8WjmE6ntYmhqX2jjp5LbMZjCw/wbYeN6CizOa2GMVzQOlmHjB4Ceuyk6LJ8huccEmR5Xddg7OOV/NAtchW+E3XbOag60QA4Qwuarca0bRuEJyr+cFQwzcY98huxhAKdQelt4kAQpj4qJ3gvFXAYn+aJumXk1yPlpQUgtIHhbYoFMUstNRRWgjnpl4A7IKlayNymqFHFa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sgzqHulJNCQpcqBnpTALCuQ6NSYLHpmR5i42gZzuIdcrMMvMJbQlxe3jXxyZnLACl7ARm/FjPIDOY8ODtpM71sxwfcZpvBeUzKWmfNINM5AS+wO0Khh7dMqKccu4+qatarZjYAwDlgetzStHtEt+XedsBOQtU9XMrRgjg4KTnc5nr+dmqadit/4C4uLm8DuA9koJTj1TL7fI5nDL+qqoo/FLGAzL7dYT17PzvAcQONYSUQRxW/QMrHZVIyik0ZuQA2mzp+Ji8BW4YM3Mbzm9inaHkJCGfrUZZjujiYailfFwA8DHIy3acwUj4v9vUVa+SmgNsl5fuyDTKovW9/IAmfLV0Pi2UncA515kjYdrwC9i9rpuHiq3JwtAAAAABJRU5ErkJggg=="></a>
                <a style="display:inline-block; margin-left: .5em" href='https://app.fotographer.ai/home'><img src='https://img.shields.io/github/stars/SUDO-AI-3D/zero123plus?style=social' /></a>
                Check out our App<a href="https://app.fotographer.ai/home">Fotographer.ai</a>! 
                </div>
                ''')
            
    with gr.Row():
        intensity = gr.Slider(label="Refiner Strength", minimum=1.0, maximum=7.0, value=3.0, step=0.5)
        intensity.change(fn=update_value, inputs=intensity, outputs=intensity)
        generate_button = gr.Button(value="Generate")
        



    ips = [fore, prompt, intensity, mode, refprompt, bg]
    generate_button.click(fn=process_generate, inputs=ips, outputs=[result_gallery])


block.launch()