Upload folder using huggingface_hub
Browse files- README.md +2 -8
- image.py +95 -0
- requirements.txt +5 -0
README.md
CHANGED
@@ -1,12 +1,6 @@
|
|
1 |
---
|
2 |
-
title:
|
3 |
-
|
4 |
-
colorFrom: blue
|
5 |
-
colorTo: indigo
|
6 |
sdk: gradio
|
7 |
sdk_version: 5.38.0
|
8 |
-
app_file: app.py
|
9 |
-
pinned: false
|
10 |
---
|
11 |
-
|
12 |
-
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
1 |
---
|
2 |
+
title: image_generator_using_sd_turbo
|
3 |
+
app_file: image.py
|
|
|
|
|
4 |
sdk: gradio
|
5 |
sdk_version: 5.38.0
|
|
|
|
|
6 |
---
|
|
|
|
image.py
ADDED
@@ -0,0 +1,95 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import torch
|
3 |
+
from diffusers import AutoPipelineForText2Image
|
4 |
+
import time
|
5 |
+
import numpy as np # The only library we need for this fix
|
6 |
+
|
7 |
+
# --- 1. Load the SD-Turbo Model (Optimized for CPU) ---
|
8 |
+
# (No changes here)
|
9 |
+
print("Loading the SD-Turbo model for CPU...")
|
10 |
+
pipe = AutoPipelineForText2Image.from_pretrained("stabilityai/sd-turbo")
|
11 |
+
device = "cpu"
|
12 |
+
pipe = pipe.to(device)
|
13 |
+
print("Model loaded successfully on CPU!")
|
14 |
+
|
15 |
+
# --- 2. Pre-warm the Model ---
|
16 |
+
# (No changes here)
|
17 |
+
print("Pre-warming the pipeline...")
|
18 |
+
_ = pipe(prompt="A photo of a cat", width=512, height=512, num_inference_steps=1).images[0]
|
19 |
+
print("Pipeline is warmed up and ready!")
|
20 |
+
|
21 |
+
|
22 |
+
# --- 3. The NumPy Array Solution ---
|
23 |
+
def generate_and_return_numpy(prompt, seed, width, height):
|
24 |
+
"""
|
25 |
+
Generates an image and returns it as a raw NumPy array. This is the most
|
26 |
+
stable method to avoid Gradio/Windows bugs, though a UI delay will exist.
|
27 |
+
"""
|
28 |
+
start_time = time.time()
|
29 |
+
try:
|
30 |
+
width = int(width)
|
31 |
+
height = int(height)
|
32 |
+
generator = torch.Generator(device=pipe.device).manual_seed(int(seed))
|
33 |
+
|
34 |
+
# The model generates the PIL Image
|
35 |
+
pil_image = pipe(
|
36 |
+
prompt=prompt,
|
37 |
+
width=width,
|
38 |
+
height=height,
|
39 |
+
num_inference_steps=2,
|
40 |
+
guidance_scale=0.0,
|
41 |
+
generator=generator,
|
42 |
+
).images[0]
|
43 |
+
|
44 |
+
# --- THE FIX: Convert to NumPy Array ---
|
45 |
+
numpy_array = np.array(pil_image)
|
46 |
+
# --- END OF FIX ---
|
47 |
+
|
48 |
+
end_time = time.time()
|
49 |
+
# This time will be the FAST backend time. The UI will take longer.
|
50 |
+
generation_time = f"Backend generation time: {end_time - start_time:.2f} seconds"
|
51 |
+
|
52 |
+
# We return the raw array. Gradio will handle the slow encoding now.
|
53 |
+
return numpy_array, generation_time, None
|
54 |
+
|
55 |
+
except Exception as e:
|
56 |
+
print(f"An error occurred: {e}")
|
57 |
+
return None, "Generation failed", str(e)
|
58 |
+
|
59 |
+
|
60 |
+
# --- 4. Create the Gradio Interface ---
|
61 |
+
# The UI code is identical. gr.Image can handle NumPy arrays.
|
62 |
+
with gr.Blocks(theme=gr.themes.Soft()) as app:
|
63 |
+
gr.Markdown(
|
64 |
+
"""
|
65 |
+
# 💯 Stable CPU Generator (NumPy Version) 💯
|
66 |
+
### This is the most robust version to prevent crashes on Windows.
|
67 |
+
"""
|
68 |
+
)
|
69 |
+
with gr.Row():
|
70 |
+
with gr.Column(scale=3):
|
71 |
+
prompt_input = gr.Textbox(
|
72 |
+
label="Prompt", placeholder="A wizard casting a spell", lines=3,
|
73 |
+
)
|
74 |
+
with gr.Row():
|
75 |
+
width_slider = gr.Slider(
|
76 |
+
label="Width", minimum=256, maximum=768, value=512, step=64,
|
77 |
+
)
|
78 |
+
height_slider = gr.Slider(
|
79 |
+
label="Height", minimum=256, maximum=768, value=512, step=64,
|
80 |
+
)
|
81 |
+
seed_input = gr.Number(label="Seed", value=100)
|
82 |
+
generate_button = gr.Button("Generate", variant="primary")
|
83 |
+
with gr.Column(scale=2):
|
84 |
+
image_output = gr.Image(label="Generated Image", show_label=False)
|
85 |
+
info_output = gr.Textbox(label="Status", show_label=False, interactive=False)
|
86 |
+
error_output = gr.Textbox(label="Error", visible=False)
|
87 |
+
|
88 |
+
generate_button.click(
|
89 |
+
fn=generate_and_return_numpy,
|
90 |
+
inputs=[prompt_input, seed_input, width_slider, height_slider],
|
91 |
+
outputs=[image_output, info_output, error_output],
|
92 |
+
)
|
93 |
+
|
94 |
+
# --- 5. Launch the App ---
|
95 |
+
app.launch(share=True)
|
requirements.txt
ADDED
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
gradio
|
2 |
+
torch
|
3 |
+
diffusers
|
4 |
+
numpy
|
5 |
+
accelerate
|