|
import gradio as gr |
|
from gradio_client import Client |
|
import os |
|
import numpy as np |
|
import random |
|
|
|
hf_token = os.environ.get("HF_TKN") |
|
MAX_SEED = np.iinfo(np.int32).max |
|
|
|
def get_caption(image_in): |
|
client = Client("https://fffiloni-moondream1.hf.space/", hf_token=hf_token) |
|
result = client.predict( |
|
image_in, |
|
"Describe the image", |
|
api_name="/predict" |
|
) |
|
print(result) |
|
return result |
|
|
|
def get_lcm(prompt): |
|
client = Client("https://latent-consistency-lcm-lora-for-sdxl.hf.space/") |
|
result = client.predict( |
|
prompt, |
|
0.3, |
|
8, |
|
0, |
|
True, |
|
api_name="/predict" |
|
) |
|
print(result) |
|
return result[0] |
|
|
|
def get_sdxl_lightning(prompt): |
|
client = Client("AP123/SDXL-Lightning") |
|
result = client.predict( |
|
prompt, |
|
"4-Step", |
|
api_name="/generate_image" |
|
) |
|
print(result) |
|
return result |
|
|
|
def get_turbo(prompt): |
|
seed = random.randint(0, MAX_SEED) |
|
print(f"SEED: {seed}") |
|
client = Client("https://diffusers-unofficial-sdxl-turbo-i2i-t2i.hf.space/") |
|
result = client.predict( |
|
None, |
|
prompt, |
|
0.7, |
|
8, |
|
seed, |
|
api_name="/predict" |
|
) |
|
print(result) |
|
return result |
|
|
|
def infer(image_in, chosen_method): |
|
caption = get_caption(image_in) |
|
if chosen_method == "LCM" : |
|
img_var = get_lcm(caption) |
|
elif chosen_method == "SDXL Lightning" : |
|
img_var = get_sdxl_lightning(caption) |
|
elif chosen_method == "SDXL Turbo" : |
|
img_var = get_turbo(caption) |
|
return img_var |
|
|
|
gr.Interface( |
|
title = "Supa Fast Image Variation", |
|
description = "Get quick image variation from image input, using <a href='https://huggingface.co/vikhyatk/moondream1' target='_blank'>moondream1</a> for caption, and <a href='https://huggingface.co/spaces/latent-consistency/lcm-lora-for-sdxl' target='_blank'>LCM SDXL</a>, <a href='https://huggingface.co/spaces/AP123/SDXL-Lightning' target='_blank'>SDXL Lightning</a> or <a href='https://huggingface.co/spaces/diffusers/unofficial-SDXL-Turbo-i2i-t2i' target='_blank'>SDXL Turbo</a> for image generation", |
|
fn = infer, |
|
inputs = [ |
|
gr.Image(type="filepath", label="Image input"), |
|
gr.Dropdown(label="Choose a model", choices=["LCM", "SDXL Lightning", "SDXL Turbo"], value="SDXL Lightning") |
|
], |
|
outputs = [ |
|
gr.Image(label="Image variation") |
|
], |
|
examples = [ |
|
["examples/frog_clean.jpg", "LCM"], |
|
["examples/martin_pecheur.jpeg", "SDXL Turbo"], |
|
["examples/forest_deer.png", "SDXL Lightning"] |
|
], |
|
cache_examples = False, |
|
concurrency_limit = 2 |
|
).queue(max_size=25).launch(show_api=False, show_error=True) |