Spaces:
Running
Running
File size: 5,483 Bytes
4b8ee81 d4bb4d3 4b8ee81 40de55c 2857510 d4bb4d3 6e67c16 f2cece1 4b8ee81 d4bb4d3 f2cece1 4b8ee81 d4bb4d3 6e67c16 6b857b8 6e67c16 4b8ee81 6e67c16 4b8ee81 40de55c d4bb4d3 4b8ee81 6e67c16 4b8ee81 40de55c d4bb4d3 6e67c16 d4bb4d3 40de55c d4bb4d3 4b8ee81 6e67c16 6b857b8 4b8ee81 40de55c 4b8ee81 40de55c 26f2464 d4bb4d3 4b8ee81 26f2464 d4bb4d3 1f556d1 4b8ee81 40de55c 6e67c16 26f2464 6e67c16 d4bb4d3 6b857b8 26f2464 4b8ee81 26f2464 6e67c16 4b8ee81 6e67c16 |
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 |
import gradio as gr
import numpy as np
import requests
import json
from PIL import Image
from diffusers.utils import load_image
from io import BytesIO
from vars import base_url
# API endpoints
sdxl_inference_endpoint = f'{base_url}/api/v1/product-diffusion/sdxl_v0_lora_inference'
kandinsky_inpainting_inference = f'{base_url}/api/v1/product-diffusion/inpainting'
def generate_sdxl_lora_image(prompt, negative_prompt, num_inference_steps, guidance_scale, num_images, mode):
payload = {
"prompt": prompt,
"negative_prompt": negative_prompt,
"num_inference_steps": num_inference_steps,
"guidance_scale": guidance_scale,
"num_images": num_images,
"mode": mode
}
response = requests.post(sdxl_inference_endpoint, json=payload)
response.raise_for_status()
response_data = response.json()
url = response_data['url']
image = load_image(url)
return image
def generate_outpainting(prompt, negative_prompt, num_inference_steps, strength, guidance_scale, mode, num_images, image, width, height):
# Convert the image to bytes
img_byte_arr = BytesIO()
image.save(img_byte_arr, format='PNG')
img_byte_arr = img_byte_arr.getvalue()
# Prepare the files for multipart/form-data
files = {
'image': ('image.png', img_byte_arr, 'image/png')
}
# Prepare the request data
request_data = {
"prompt": prompt,
"negative_prompt": negative_prompt,
"num_inference_steps": num_inference_steps,
"strength": strength,
"guidance_scale": guidance_scale,
"mode": mode,
"num_images": num_images,
"width": width,
"height": height
}
# Convert request_data to a JSON string
request_data_json = json.dumps(request_data)
# Prepare the form data
form_data = {
'request_data': request_data_json
}
response = requests.post(kandinsky_inpainting_inference, files=files, data=form_data)
response.raise_for_status()
response_data = response.json()
image_url = response_data['image_url']
mask_url = response_data['mask_url']
outpainted_image = load_image(image_url)
mask_image = load_image(mask_url)
return outpainted_image, mask_image
with gr.Blocks(theme='VikramSingh178/Webui-Theme') as demo:
with gr.Tab("SdxL-Lora"):
with gr.Row():
with gr.Column():
with gr.Group():
prompt = gr.Textbox(label="Prompt", placeholder="Enter your prompt here")
negative_prompt = gr.Textbox(label="Negative Prompt", placeholder="Enter negative prompt here")
num_inference_steps = gr.Slider(minimum=1, maximum=1000, step=1, value=20, label="Inference Steps")
guidance_scale = gr.Slider(minimum=1.0, maximum=10.0, step=0.1, value=7.5, label="Guidance Scale")
num_images = gr.Slider(minimum=1, maximum=10, step=1, value=1, label="Number of Images")
mode = gr.Dropdown(choices=["s3_json", "b64_json"], value="s3_json", label="Mode")
generate_button = gr.Button("Generate Image", variant='primary')
with gr.Column(scale=1):
image_preview = gr.Image(label="Generated Image (SDXL-Lora)", show_download_button=True, show_share_button=True, container=True)
generate_button.click(generate_sdxl_lora_image, inputs=[prompt, negative_prompt, num_inference_steps, guidance_scale, num_images, mode], outputs=[image_preview])
with gr.Tab("Outpainting"):
with gr.Row():
with gr.Column():
with gr.Group():
input_image = gr.Image(label="Upload Image", type="pil")
prompt = gr.Textbox(label="Prompt", placeholder="Enter your prompt here")
negative_prompt= gr.Textbox(label="Negative Prompt", placeholder="Enter negative prompt here")
num_inference_steps = gr.Slider(minimum=1, maximum=100, step=1, value=20, label="Inference Steps")
strength = gr.Slider(minimum=0.1, maximum=1, step=0.1, value=0.8, label="Strength")
guidance_scale = gr.Slider(minimum=1.0, maximum=10.0, step=0.1, value=7.5, label="Guidance Scale")
num_images = gr.Slider(minimum=1, maximum=10, step=1, value=1, label="Number of Images")
mode_kandinsky = gr.Dropdown(choices=["s3_json", "b64_json"], value="s3_json", label="Mode")
width_slider = gr.Slider(minimum=512, maximum=1024, step=1, value=800, label="Image Width")
height_slider = gr.Slider(minimum=512, maximum=1024, step=1, value=800, label="Image Height")
generate_button = gr.Button("Generate Inpainting", variant='primary')
with gr.Column(scale=1):
outpainted_image_preview = gr.Image(label="Outpainted Image (Kandinsky)", show_download_button=True, show_share_button=True, container=True)
mask_image_preview = gr.Image(label="Generated Mask", show_download_button=True, show_share_button=True, container=True)
generate_button.click(generate_outpainting, inputs=[prompt, negative_prompt, num_inference_steps, strength, guidance_scale, mode_kandinsky, num_images, input_image, width_slider, height_slider], outputs=[outpainted_image_preview, mask_image_preview])
demo.launch()
|