fffiloni commited on
Commit
5d92c6c
·
1 Parent(s): 32a98e8

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -0
app.py ADDED
@@ -0,0 +1,30 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from diffusers import StableDiffusionXLPipeline, UNet2DConditionModel
3
+ import torch
4
+
5
+ # load pipeline
6
+ model_id = "stabilityai/stable-diffusion-xl-base-1.0"
7
+ pipe = StableDiffusionXLPipeline.from_pretrained(model_id, torch_dtype=torch.float16, variant="fp16", use_safetensors=True).to("cuda")
8
+
9
+ # load finetuned model
10
+ unet_id = "mhdang/dpo-sdxl-text2image-v1"
11
+ unet = UNet2DConditionModel.from_pretrained(unet_id, subfolder="unet", torch_dtype=torch.float16)
12
+ pipe.unet = unet
13
+ pipe = pipe.to("cuda")
14
+
15
+ pipe.enable_model_cpu_offload()
16
+ pipe.enable_vae_slicing()
17
+
18
+ def infer(prompt):
19
+ image = pipe(prompt, guidance_scale=7.5).images[0].resize((512,512))
20
+ return image
21
+
22
+ gr.Interface(
23
+ fn = infer,
24
+ inputs = [
25
+ gr.Textbox(value="Two cats playing chess on a tree branch")
26
+ ],
27
+ outputs = [
28
+ gr.Image()
29
+ ]
30
+ ).launch()