|
import gradio as gr |
|
from gradio_client import Client |
|
from PIL import Image |
|
import os |
|
import traceback |
|
import random |
|
import time |
|
|
|
|
|
clients = [ |
|
Client("hsuwill000/LCM_SoteMix_OpenVINO_CPU_Space_TAESD"), |
|
Client("HelloSun/LCM_Dreamshaper_v7-int8-ov") |
|
] |
|
|
|
|
|
count = 0 |
|
|
|
|
|
def infer_gradio(prompt: str): |
|
global count |
|
random.seed(time.time()) |
|
|
|
client = random.choice(clients) |
|
|
|
|
|
inputs = { |
|
"prompt": prompt, |
|
"num_inference_steps": 10 |
|
} |
|
|
|
try: |
|
|
|
result = client.predict(inputs, api_name="/infer") |
|
|
|
|
|
image = Image.open(result) |
|
|
|
|
|
filename = f"img_{count:08d}.jpg" |
|
while os.path.exists(filename): |
|
count += 1 |
|
filename = f"img_{count:08d}.jpg" |
|
|
|
|
|
image.save(filename) |
|
print(f"Saved image as {filename}") |
|
|
|
|
|
return image |
|
|
|
except Exception as e: |
|
|
|
print(f"An exception occurred: {str(e)}") |
|
print("Stack trace:") |
|
traceback.print_exc() |
|
return None |
|
|
|
|
|
with gr.Blocks() as demo: |
|
with gr.Row(): |
|
prompt_input = gr.Textbox( |
|
label="Enter Your Prompt", |
|
show_label=False, |
|
placeholder="Type your prompt for image generation here", |
|
lines=1, |
|
interactive=True |
|
) |
|
|
|
|
|
run_button = gr.Button("RUN") |
|
|
|
|
|
output_image = gr.Image(label="Generated Image") |
|
|
|
|
|
run_button.click(infer_gradio, inputs=prompt_input, outputs=output_image) |
|
|
|
demo.launch() |
|
|