Spaces:
Running
on
CPU Upgrade
Running
on
CPU Upgrade
File size: 19,641 Bytes
6c02d81 015bcc4 ae0f04b 015bcc4 c8789f0 015bcc4 ae0f04b 23c539b 7bfcbea 0263e4d cf91b68 3efdd5e f9a7218 3efdd5e 10508b5 3efdd5e be2ac33 f9a7218 cd05370 3efdd5e f9a7218 3efdd5e 0263e4d 3efdd5e f9a7218 cd05370 3efdd5e 23c539b 5bc58f3 b292278 a2aedcc b292278 a2aedcc b292278 cd05370 0263e4d 9a071a1 b292278 3efdd5e ae0f04b c8789f0 015bcc4 dc396dd 015bcc4 3987e25 bb65891 015bcc4 3d57275 015bcc4 3987e25 c545b69 3987e25 015bcc4 647a13f c8789f0 0263e4d 75d43d9 0263e4d 75d43d9 c8789f0 c178277 1839681 c8789f0 dc396dd 3987e25 647a13f 3987e25 144fc2a 3987e25 75d43d9 0263e4d 647a13f c8789f0 2d1d47f 647a13f 015bcc4 75d43d9 015bcc4 50e935c 2d1d47f a885e1e a7d11dc 015bcc4 c8789f0 c47dbb1 015bcc4 3f11607 72afb79 015bcc4 72afb79 3f11607 72afb79 015bcc4 72afb79 3f11607 6c02d81 72afb79 3f11607 72afb79 3f11607 6c02d81 3f11607 72afb79 3f11607 72afb79 3f11607 6c02d81 3f11607 72afb79 3f11607 d693233 015bcc4 3f11607 0d7e6b0 3f11607 c178277 1ba90d0 ed7cd38 736e172 122e01d 0326165 1839681 3f11607 1839681 3f11607 1839681 3f11607 1839681 3f11607 ea9a3c9 e5f0808 015bcc4 ad78530 015bcc4 61a6f9a 015bcc4 3f11607 015bcc4 3f11607 c47dbb1 3f11607 ea9a3c9 3f11607 015bcc4 3987e25 c47dbb1 c8789f0 bb65891 4c82520 3f11607 4c82520 3f11607 ae0f04b 61a6f9a ae0f04b cd05370 cf91b68 ae0f04b cd05370 7a66ea4 3f11607 ae0f04b 3f11607 ae0f04b 3f11607 6c02d81 |
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 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 |
#version 2
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")
rmbgkey = os.getenv("RMBGKEY")
#fore, prompt, intensity, mode, refprompt, isrmbg
#erased this prompt
#"A car, on a high-tech city street at night, surrounded by neon lights and holographic billboards, with sleek skyscrapers and flying cars in the background, a glowing cyber-road beneath",
outputs_list = [["examples/out1.png"],["examples/out3.png"], ["examples/out4.png"],["examples/out5.png"],["examples/out6.png"],["examples/out7.png"],["examples/out2.png"]]
prompt_list = ["A Perfume Bottle, resting on a wooden table, surrounded by lavender leaves with droplets of morning dew glistening, in a serene garden, under the soft glow of a twilight sky, encircled by delicate purple petals",
"A PAir of Shoes, resting on a sleek white platform under a bright spotlight, with subtle pastel-colored reflections and delicate flower petals scattered around, creating a cheerful and elegant setting",
"A Perfume bottle, perched delicately on a rock by the ocean, with a breathtaking sunset casting a golden glow over the waves",
"A Bottle, placed on a rustic wooden table, viewed from a slight above angle, overlooking a serene beach, with seashells and driftwood artfully scattered around, bathed in the warm glow of the setting sun",
"A Sofa, In a snug, inviting room, a large sofa accompanied by plush cushions, a thick rug, book-filled shelves, potted plants, framed art, a coffee table with candles, a floor lamp, and gentle, warm lighting",
"Headphones, hovering gracefully against a swirling pastel galaxy, accented by glimmers of stardust, ethereal light beams, and floating musical notes, under a dreamy aurora borealis, with no visible wires"
]
example_list = [
[
"examples/in5.png",
"A Perfume bottle, perched delicately on a rock by the ocean, with a breathtaking sunset casting a golden glow over the waves",
3.5,
"full",
"(( A transparent perfume Bottle with a black cap )), and a yellow liquide, sunset, transparent",
False,
"examples/out5.png"
],
[
"examples/in6.png",
"A Sofa, In a snug, inviting room, a large sofa accompanied by plush cushions, a thick rug, book-filled shelves, potted plants, framed art, a coffee table with candles, a floor lamp, and gentle, warm lighting",
3.5,
"full",
"A light grey Sofa, studio light, shadows",
False,
"examples/out6.png"
],
#[
# "examples/in7.png",
# "A car, on a high-tech city street at night, surrounded by neon lights and holographic billboards, with sleek skyscrapers and flying cars in the background, a glowing cyber-road beneath",
# 3.0,
# "full",
# "(( A light grey Car )), vibrant, reflections",
# False,
# "examples/out7.png"
#],
[
"examples/in1.png",
"A Perfume Bottle, resting on a wooden table, surrounded by lavender leaves with droplets of morning dew glistening, in a serene garden, under the soft glow of a twilight sky, encircled by delicate purple petals",
3.0,
"full",
"A perfume Bottle with a purple liquide, studio light",
False,
"examples/out1.png"
],
[
"examples/in2.png",
"Headphones, hovering gracefully against a swirling pastel galaxy, accented by glimmers of stardust, ethereal light beams, and floating musical notes, under a dreamy aurora borealis, with no visible wires",
3.50,
"full",
"headphones, vibrant, colorful, ",
False,
"examples/out2.png"
],
[
"examples/in3.png",
"A PAir of Shoes, resting on a sleek white platform under a bright spotlight, with subtle pastel-colored reflections and delicate flower petals scattered around, creating a cheerful and elegant setting",
3.5,
"full",
"(( A Pair of Brown Shoes )), vibrant, Shadow",
False,
"examples/out3.png"
],
[
"examples/in4.png",
"A Bottle, placed on a rustic wooden table, viewed from a slight above angle, overlooking a serene beach, with seashells and driftwood artfully scattered around, bathed in the warm glow of the setting sun",
3.50,
"full",
"A transparent Bottle with a black cap and a semi transparent liquide, Sunset",
False,
"examples/out4.png"
]
]
def rmbg(pil_image):
# Convert PIL image to bytes
image_bytes = BytesIO()
pil_image.save(image_bytes, format='PNG')
image_bytes.seek(0)
# Send the image to the remove.bg API
response = requests.post(
'https://api.remove.bg/v1.0/removebg',
files={'image_file': ('filename.png', image_bytes, 'image/png')},
data={'size': 'auto'},
headers={'X-Api-Key': rmbgkey}
)
if response.status_code == 200:
# Convert the bytes response to a PIL image
result_image = Image.open(BytesIO(response.content))
return result_image
else:
return None
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 create_square_image(image):
"""
Create a new square image with the side length equal to the largest dimension
of the original image and paste the original image at the center on a transparent canvas.
:param image: A PIL image.
:return: A new square PIL image.
"""
original_width, original_height = image.size
new_side_length = max(original_width, original_height)
# Create a new square image with a transparent background
new_image = Image.new("RGBA", (new_side_length, new_side_length), (255, 255, 255, 0))
# Calculate the position to paste the original image on the new square canvas
paste_x = (new_side_length - original_width) // 2
paste_y = (new_side_length - original_height) // 2
# Paste the original image onto the new square canvas using the alpha channel as a mask
new_image.paste(image, (paste_x, paste_y), image)
return new_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":
gr.Info("Queued πΆπΆπΆπΆ!", duration=35)
if status != "COMPLETED":
# Polling with timeout for long-running tasks
output = run_request.output(timeout=120)
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, isrmbg):
max_size = (1500,1500)
if prompt in prompt_list:
max_size = (1500,1500)
if isrmbg:
try:
rmbgfore = rmbg(fore)
if rmbgfore is not None:
fore = rmbgfore.convert("RGBA")
print(f"Background removed!")
except:
pass
fore = create_square_image(fore)
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]>(max_size[0]*max_size[1]):
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))
fore.resize(max_size)
forestr = image_to_base64(fore.convert("RGBA"))
data = {
"foreground_image64": forestr,
"prompt" : prompt,
"mode" : mode,
"intensity" : float(intensity),
"width" : 1500,
"height" : 1500,
"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;
}
#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;
}
#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;
}
body {
font-family: Arial, sans-serif;
background-color: #f4f4f9;
margin: 0;
padding: 0;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
min-height: 100vh;
color: #333;
}
.custom-button {
background: linear-gradient(271.15deg, #00C7E2 0.27%, #00CC6A 48.52%, #70FF00 102.07%);
font-size: 30px;
color: white;
padding: 10px 20px;
border: none;
border-radius: 5px;
cursor: pointer;
transition: opacity 0.3s ease;
}
.custom-button:hover {
opacity: 0.8;
}
.custom-title {
font-size: 36px;
background: linear-gradient(271.15deg, #00C7E2 0.27%, #12C06D 102.07%);
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
font-weight: bold;
text-align: center;
margin-bottom: 20px;
}
h1 {
color: #222222;
}
a {
color: #4a90e2;
text-decoration: none;
font-weight: bold;
}
a:hover {
text-decoration: underline;
}
.emoji {
font-size: 1.5em;
}
@media (max-width: 768px) {
.custom-button {
font-size: 20px;
padding: 8px 16px;
}
.custom-title {
font-size: 28px;
}
#disp_image, .container {
padding: 0 1rem;
}
h1 {
font-size: 24px;
}
}
@media (max-width: 480px) {
.custom-button {
font-size: 16px;
padding: 6px 12px;
}
.custom-title {
font-size: 22px;
}
.container {
padding: 0 0.5rem;
}
h1 {
font-size: 20px;
}
#share-btn-container {
max-width: 100%;
padding: 0.5rem;
}
}
"""
block = gr.Blocks(css=css, title="## F.ai Fuzer").queue(default_concurrency_limit=12)
with block:
gr.HTML('''
<div class="container">
<center><h1 class="custom-title">Fotographer.ai Fuzer v0.1: Transcending image Generation Controlπ¨</h1></center>
<h2>Control Your Creativity with Unmatched Precision: </h2>
<p>Fotographer.ai Fuzer v0.1 empowers you to seamlessly blend foreground elements with any background, while maintaining the shape and style consistency of the foreground. This tool transcends traditional image generation methods by giving you unprecedented control over the final output.</p>
<p>Please check Us out on <strong>Twitter</strong>:
<a href="https://x.com/FotographerAI">Fotographer AI</a> and
<strong>Discord</strong>: <a href="https://discord.gg/7bKaeXfH">https://discord.gg/7bKaeXfH</a></p>
<h2>Step-by-Step Instructions</h2>
<p>Follow these instructions to control the generation of backgrounds while keeping the foreground's shape and style consistent:</p>
<ul>
<li>
<span class="emoji">π</span>
<strong>Step 1: Describe the Background</strong>
<p>Start by providing a detailed description of the background you want to create.</p>
<div class="example">
<p><strong>Example:</strong> "A Perfume Bottle nestled on a crystalline cliff of glistening snow, overlooking a serene, moonlit valley."</p>
</div>
</li>
<li>
<span class="emoji">π‘</span>
<strong>Step 2: Describe the Foreground</strong>
<p>Next, describe the texture, lighting, and style of the foreground element.</p>
<div class="example">
<p><strong>Example:</strong> "A transparent glass perfume bottle, vibrant, sunset lighting reflecting off its surface."</p>
</div>
</li>
<li>
<span class="emoji">ποΈ</span>
<strong>Step 3: Adjust the Intensity</strong>
<p>Decide how much change you want to apply to the image. Adjust the intensity to balance between keeping consistency and introducing new elements.</p>
</li>
</ul>
</div>
''')
gr.HTML("""
<center><h2><a href="https://fotographer.ai/api/top">π Check out our API!</a></h2></center>""")
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, width=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():
gr.HTML('''
<div class="container">
<h1>π For more freedom of usage, check out our API</h1>
<p>π€ You can test with free credits:</p>
<h2><a href="https://fotographer.ai/api/top">π API Dashboard</a></h2>
</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)
isrmbg = gr.Checkbox(label="Remove Background")
isrmbg.change(fn=update_value, inputs=isrmbg, outputs=isrmbg)
generate_button = gr.Button(value="Generate", elem_classes="custom-button")
gr.HTML('''
<div class="container">
<h2>Features:</h2>
<ul>
<li>πΌοΈ <strong>Consistent Foreground Style:</strong> Keep the foreground's shape and style intact across various backgrounds.</li>
<li>π <strong>Seamless Blending:</strong> Effortlessly blend foreground elements with any background for a natural look.</li>
<li>π¨ <strong>Custom Background Control:</strong> Choose or generate backgrounds that perfectly match your creative vision.</li>
<li>βοΈ <strong>Easy Integration:</strong> Integrates smoothly with your existing workflow and tools.</li>
</ul>
</div>
''')
gr.HTML("""
<center><h2><a href="https://fotographer.ai/">π Check Out our other Projects Here!</a></h2></center>""")
with gr.Row():
dummy_image_for_outputs = gr.Image(visible=False, label='Result', height=400, width=400)
gr.Examples(
fn=lambda *args: [args[-1]],
examples=example_list,
inputs=[
fore, prompt, intensity, mode, refprompt, isrmbg, dummy_image_for_outputs
]
)
'''
with gr.Column():
dummy_image_for_outputs = gr.Image(visible=False, label='Result')
gr.Examples(
examples=outputs_list,
inputs=[dummy_image_for_outputs],
)
'''
ins = [fore, prompt, intensity, mode, refprompt, isrmbg]
generate_button.click(fn=process_generate, inputs=ins, outputs=[result_gallery])
block.launch()
|