Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,85 +1,27 @@
|
|
1 |
import os
|
2 |
-
import torch
|
3 |
import random
|
4 |
-
from huggingface_hub import snapshot_download
|
5 |
-
from kolors.pipelines.pipeline_stable_diffusion_xl_chatglm_256 import StableDiffusionXLPipeline
|
6 |
-
from kolors.models.modeling_chatglm import ChatGLMModel
|
7 |
-
from kolors.models.tokenization_chatglm import ChatGLMTokenizer
|
8 |
-
from diffusers import UNet2DConditionModel, AutoencoderKL
|
9 |
-
from diffusers import EulerDiscreteScheduler
|
10 |
import gradio as gr
|
|
|
|
|
11 |
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
# Function to load models
|
16 |
-
def load_models():
|
17 |
-
text_encoder = ChatGLMModel.from_pretrained(
|
18 |
-
os.path.join(ckpt_dir, 'text_encoder'),
|
19 |
-
torch_dtype=torch.float16).half()
|
20 |
-
tokenizer = ChatGLMTokenizer.from_pretrained(os.path.join(ckpt_dir, 'text_encoder'))
|
21 |
-
vae = AutoencoderKL.from_pretrained(os.path.join(ckpt_dir, "vae"), revision=None).half()
|
22 |
-
scheduler = EulerDiscreteScheduler.from_pretrained(os.path.join(ckpt_dir, "scheduler"))
|
23 |
-
unet = UNet2DConditionModel.from_pretrained(os.path.join(ckpt_dir, "unet"), revision=None).half()
|
24 |
-
|
25 |
-
return StableDiffusionXLPipeline(
|
26 |
-
vae=vae,
|
27 |
-
text_encoder=text_encoder,
|
28 |
-
tokenizer=tokenizer,
|
29 |
-
unet=unet,
|
30 |
-
scheduler=scheduler,
|
31 |
-
force_zeros_for_empty_prompt=False
|
32 |
-
)
|
33 |
-
|
34 |
-
# Create a global variable to hold the pipeline
|
35 |
-
pipe = load_models()
|
36 |
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
# Move the model to the CPU for inference and clear unnecessary variables
|
44 |
-
with torch.no_grad():
|
45 |
-
generator = torch.Generator().manual_seed(seed)
|
46 |
-
result = pipe(
|
47 |
-
prompt=prompt,
|
48 |
-
negative_prompt=negative_prompt,
|
49 |
-
height=height,
|
50 |
-
width=width,
|
51 |
-
num_inference_steps=num_inference_steps,
|
52 |
-
guidance_scale=guidance_scale,
|
53 |
-
num_images_per_prompt=num_images_per_prompt,
|
54 |
-
generator=generator
|
55 |
-
)
|
56 |
-
image = result.images
|
57 |
-
|
58 |
-
return image, seed
|
59 |
|
60 |
-
# Gradio interface
|
61 |
iface = gr.Interface(
|
62 |
-
fn=generate_image,
|
63 |
-
inputs=
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
additional_inputs=[
|
68 |
-
gr.Slider(512, 2048, 1024, step=64, label="Height"),
|
69 |
-
gr.Slider(512, 2048, 1024, step=64, label="Width"),
|
70 |
-
gr.Slider(20, 50, 20, step=1, label="Number of Inference Steps"),
|
71 |
-
gr.Slider(1, 20, 5, step=0.5, label="Guidance Scale"),
|
72 |
-
gr.Slider(1, 4, 1, step=1, label="Number of images per prompt"),
|
73 |
-
gr.Checkbox(label="Use Random Seed", value=True),
|
74 |
-
gr.Number(label="Seed", value=0, precision=0)
|
75 |
-
],
|
76 |
-
additional_inputs_accordion=gr.Accordion(label="Advanced settings", open=False),
|
77 |
-
outputs=[
|
78 |
-
gr.Gallery(label="Result", elem_id="gallery", show_label=False),
|
79 |
-
gr.Number(label="Seed Used")
|
80 |
-
],
|
81 |
-
title="Kolors",
|
82 |
-
theme='bethecloud/storj_theme',
|
83 |
)
|
84 |
|
85 |
-
|
|
|
|
1 |
import os
|
|
|
2 |
import random
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
import gradio as gr
|
4 |
+
import torch
|
5 |
+
from diffusers import DiffusionPipeline
|
6 |
|
7 |
+
pipe = DiffusionPipeline.from_pretrained("fluently/Fluently-XL-Final")
|
8 |
+
pipe.load_lora_weights("OEvortex/HelpingAI-PixelCraft")
|
9 |
+
pipe.to("cuda" if torch.cuda.is_available() else "cpu")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
|
11 |
+
# Function to generate image from prompt
|
12 |
+
def generate_image(prompt):
|
13 |
+
# Use the pipeline to generate an image from the text prompt
|
14 |
+
image = pipe(prompt).images[0]
|
15 |
+
return image
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
|
17 |
+
# Create Gradio interface
|
18 |
iface = gr.Interface(
|
19 |
+
fn=generate_image, # Function that generates the image
|
20 |
+
inputs=gr.Textbox(lines=2, placeholder="Enter your prompt"), # Textbox input for the prompt
|
21 |
+
outputs="image", # Output is an image
|
22 |
+
title="Kwai-Kolors Image Generator",
|
23 |
+
description="Generate images from text prompts using the Kwai-Kolors diffusion model."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
)
|
25 |
|
26 |
+
# Launch the app
|
27 |
+
iface.launch()
|