Spaces:
Runtime error
Runtime error
kasper-boy
commited on
Create app_gpu.py
Browse files- app_gpu.py +49 -0
app_gpu.py
ADDED
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import torch
|
3 |
+
from diffusers import DiffusionPipeline
|
4 |
+
from PIL import Image
|
5 |
+
|
6 |
+
# Load the model
|
7 |
+
model_id = "stabilityai/stable-diffusion-xl-base-1.0"
|
8 |
+
pipe = DiffusionPipeline.from_pretrained(
|
9 |
+
model_id,
|
10 |
+
torch_dtype=torch.float16, # Use float32 for CPU
|
11 |
+
use_safetensors=True
|
12 |
+
)
|
13 |
+
pipe.to("cuda")
|
14 |
+
|
15 |
+
def generate_image(prompt, negative_prompt, size):
|
16 |
+
if not prompt:
|
17 |
+
prompt = "a beautiful landscape"
|
18 |
+
if not negative_prompt:
|
19 |
+
negative_prompt = ""
|
20 |
+
|
21 |
+
width, height = map(int, size.split('x'))
|
22 |
+
generator = torch.Generator("cuda").manual_seed(42)
|
23 |
+
|
24 |
+
try:
|
25 |
+
result = pipe(prompt=prompt, height=height, width=width, negative_prompt=negative_prompt, generator=generator)
|
26 |
+
if result and hasattr(result, 'images') and len(result.images) > 0:
|
27 |
+
return result.images[0]
|
28 |
+
else:
|
29 |
+
print("Error: No images in the result or result is None")
|
30 |
+
return None
|
31 |
+
except Exception as e:
|
32 |
+
print(f"Error occurred: {e}")
|
33 |
+
return None
|
34 |
+
|
35 |
+
with gr.Blocks() as demo:
|
36 |
+
gr.Markdown("## Text to Image SDXL")
|
37 |
+
|
38 |
+
with gr.Row():
|
39 |
+
with gr.Column():
|
40 |
+
prompt = gr.Textbox(label="Prompt", placeholder="Enter the prompt here...")
|
41 |
+
negative_prompt = gr.Textbox(label="Negative Prompt", placeholder="Enter the negative prompt here...")
|
42 |
+
size = gr.Dropdown(choices=["512x512", "768x768", "1024x1024"], value="1024x1024", label="Size")
|
43 |
+
submit = gr.Button("Submit")
|
44 |
+
with gr.Column():
|
45 |
+
output = gr.Image(label="Output")
|
46 |
+
|
47 |
+
submit.click(generate_image, inputs=[prompt, negative_prompt, size], outputs=output)
|
48 |
+
|
49 |
+
demo.launch()
|