Upload gradio_inference_t2i_lora.py
Browse files- gradio_inference_t2i_lora.py +59 -0
gradio_inference_t2i_lora.py
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
import os
|
| 3 |
+
import torch
|
| 4 |
+
import gradio as gr
|
| 5 |
+
import numpy as np
|
| 6 |
+
|
| 7 |
+
from PIL import Image
|
| 8 |
+
|
| 9 |
+
from diffusers import StableDiffusionPipeline,UNet2DConditionModel
|
| 10 |
+
|
| 11 |
+
NEGATIVE_PROMPT = "worst quality, low quality, bad anatomy, watermark, text, blurry, cartoon, unreal"
|
| 12 |
+
|
| 13 |
+
unet = UNet2DConditionModel.from_pretrained("runwayml/stable-diffusion-v1-5",subfolder='unet').to("cuda")
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
# unet.load_lora_weights("./exp_output/celeba_finetune/checkpoint-20000", weight_name="pytorch_lora_weights.safetensors")
|
| 18 |
+
|
| 19 |
+
pipeline = StableDiffusionPipeline.from_pretrained(
|
| 20 |
+
"runwayml/stable-diffusion-v1-5",
|
| 21 |
+
unet=unet)
|
| 22 |
+
|
| 23 |
+
pipeline.load_lora_weights("./exp_output/celeba_finetune/checkpoint-20000", weight_name="pytorch_lora_weights.safetensors")
|
| 24 |
+
|
| 25 |
+
# Define a function to process input and return output
|
| 26 |
+
def generate_image(text,num_batch,is_use_lora,num_inference_steps):
|
| 27 |
+
# Process text to generate image
|
| 28 |
+
if is_use_lora:
|
| 29 |
+
pipeline.enable_lora()
|
| 30 |
+
else:
|
| 31 |
+
pipeline.disable_lora()
|
| 32 |
+
|
| 33 |
+
print('begin inference with text:', text, 'is_use_lora:', is_use_lora)
|
| 34 |
+
image = pipeline(text,
|
| 35 |
+
num_inference_steps=num_inference_steps,
|
| 36 |
+
num_images_per_prompt=num_batch,
|
| 37 |
+
negative_prompt=NEGATIVE_PROMPT).images
|
| 38 |
+
return image
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
with gr.Blocks() as demo:
|
| 42 |
+
|
| 43 |
+
with gr.Row():
|
| 44 |
+
with gr.Column():
|
| 45 |
+
with gr.Row():
|
| 46 |
+
is_use_lora = gr.Checkbox(label="Use LoRA", value=False)
|
| 47 |
+
num_batch = gr.Number(value=4,label="Number of batch")
|
| 48 |
+
num_inference_steps = gr.Number(value=20,label="Number of inference steps")
|
| 49 |
+
|
| 50 |
+
text_input = gr.Textbox(lines=2, label="Input text", value="A young woman with long hair and a big smile.")
|
| 51 |
+
generate_button = gr.Button(value="Generate image")
|
| 52 |
+
|
| 53 |
+
# image_out = gr.Image(label="Output image", height=512,width=512)
|
| 54 |
+
image_out = gr.Gallery(label="Generated images", show_label=False, elem_id="gallery", object_fit="contain", height="512")
|
| 55 |
+
|
| 56 |
+
generate_button.click(generate_image, inputs=[text_input,num_batch,is_use_lora,num_inference_steps], outputs=image_out)
|
| 57 |
+
|
| 58 |
+
demo.launch(server_port=7861)
|
| 59 |
+
|