Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,175 +1,24 @@
|
|
| 1 |
-
|
| 2 |
-
import numpy as np
|
| 3 |
-
import random
|
| 4 |
-
|
| 5 |
-
# import spaces #[uncomment to use ZeroGPU]
|
| 6 |
-
from diffusers import DiffusionPipeline
|
| 7 |
import torch
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
#
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
buffer.data = buffer.data.to(torch.float32)
|
| 28 |
-
|
| 29 |
-
force_float32(pipe.text_encoder)
|
| 30 |
-
force_float32(pipe.vae)
|
| 31 |
-
force_float32(pipe.unet)
|
| 32 |
-
|
| 33 |
-
MAX_SEED = np.iinfo(np.int32).max
|
| 34 |
-
MAX_IMAGE_SIZE = 1024
|
| 35 |
-
|
| 36 |
-
# @spaces.GPU #[uncomment to use ZeroGPU]
|
| 37 |
-
def infer(
|
| 38 |
-
prompt,
|
| 39 |
-
negative_prompt,
|
| 40 |
-
seed,
|
| 41 |
-
randomize_seed,
|
| 42 |
-
width,
|
| 43 |
-
height,
|
| 44 |
-
guidance_scale,
|
| 45 |
-
num_inference_steps,
|
| 46 |
-
progress=gr.Progress(track_tqdm=True),
|
| 47 |
-
):
|
| 48 |
-
if randomize_seed:
|
| 49 |
-
seed = random.randint(0, MAX_SEED)
|
| 50 |
-
|
| 51 |
-
generator = torch.Generator(device).manual_seed(int(seed))
|
| 52 |
-
|
| 53 |
-
# Ensure text inputs are strings
|
| 54 |
-
prompt = str(prompt) if prompt else ""
|
| 55 |
-
negative_prompt = str(negative_prompt) if negative_prompt else ""
|
| 56 |
-
|
| 57 |
-
# Ensure text input IDs are of type LongTensor
|
| 58 |
-
if isinstance(prompt, torch.Tensor):
|
| 59 |
-
prompt = prompt.to(torch.long).tolist()
|
| 60 |
-
if isinstance(negative_prompt, torch.Tensor):
|
| 61 |
-
negative_prompt = negative_prompt.to(torch.long).tolist()
|
| 62 |
-
|
| 63 |
-
image = pipe(
|
| 64 |
-
prompt=prompt,
|
| 65 |
-
negative_prompt=negative_prompt,
|
| 66 |
-
guidance_scale=float(guidance_scale),
|
| 67 |
-
num_inference_steps=int(num_inference_steps),
|
| 68 |
-
width=int(width),
|
| 69 |
-
height=int(height),
|
| 70 |
-
generator=generator,
|
| 71 |
-
).images[0]
|
| 72 |
-
|
| 73 |
-
return image, seed
|
| 74 |
-
|
| 75 |
-
examples = [
|
| 76 |
-
"Astronaut in a jungle, cold color palette, muted colors, detailed, 8k",
|
| 77 |
-
"An astronaut riding a green horse",
|
| 78 |
-
"A delicious ceviche cheesecake slice",
|
| 79 |
-
]
|
| 80 |
-
|
| 81 |
-
css = """
|
| 82 |
-
#col-container {
|
| 83 |
-
margin: 0 auto;
|
| 84 |
-
max-width: 640px;
|
| 85 |
-
}
|
| 86 |
-
"""
|
| 87 |
-
|
| 88 |
-
with gr.Blocks(css=css) as demo:
|
| 89 |
-
with gr.Column(elem_id="col-container"):
|
| 90 |
-
gr.Markdown(" # Text-to-Image Gradio Template")
|
| 91 |
-
|
| 92 |
-
with gr.Row():
|
| 93 |
-
prompt = gr.Text(
|
| 94 |
-
label="Prompt",
|
| 95 |
-
show_label=False,
|
| 96 |
-
max_lines=1,
|
| 97 |
-
placeholder="Enter your prompt",
|
| 98 |
-
container=False,
|
| 99 |
-
)
|
| 100 |
-
|
| 101 |
-
run_button = gr.Button("Run", scale=0, variant="primary")
|
| 102 |
-
|
| 103 |
-
result = gr.Image(label="Result", show_label=False)
|
| 104 |
-
|
| 105 |
-
with gr.Accordion("Advanced Settings", open=False):
|
| 106 |
-
negative_prompt = gr.Text(
|
| 107 |
-
label="Negative prompt",
|
| 108 |
-
max_lines=1,
|
| 109 |
-
placeholder="Enter a negative prompt",
|
| 110 |
-
visible=False,
|
| 111 |
-
)
|
| 112 |
-
|
| 113 |
-
seed = gr.Slider(
|
| 114 |
-
label="Seed",
|
| 115 |
-
minimum=0,
|
| 116 |
-
maximum=MAX_SEED,
|
| 117 |
-
step=1,
|
| 118 |
-
value=0,
|
| 119 |
-
)
|
| 120 |
-
|
| 121 |
-
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
| 122 |
-
|
| 123 |
-
with gr.Row():
|
| 124 |
-
width = gr.Slider(
|
| 125 |
-
label="Width",
|
| 126 |
-
minimum=256,
|
| 127 |
-
maximum=MAX_IMAGE_SIZE,
|
| 128 |
-
step=32,
|
| 129 |
-
value=1024,
|
| 130 |
-
)
|
| 131 |
-
|
| 132 |
-
height = gr.Slider(
|
| 133 |
-
label="Height",
|
| 134 |
-
minimum=256,
|
| 135 |
-
maximum=MAX_IMAGE_SIZE,
|
| 136 |
-
step=32,
|
| 137 |
-
value=1024,
|
| 138 |
-
)
|
| 139 |
-
|
| 140 |
-
with gr.Row():
|
| 141 |
-
guidance_scale = gr.Slider(
|
| 142 |
-
label="Guidance scale",
|
| 143 |
-
minimum=0.0,
|
| 144 |
-
maximum=10.0,
|
| 145 |
-
step=0.1,
|
| 146 |
-
value=0.0,
|
| 147 |
-
)
|
| 148 |
-
|
| 149 |
-
num_inference_steps = gr.Slider(
|
| 150 |
-
label="Number of inference steps",
|
| 151 |
-
minimum=1,
|
| 152 |
-
maximum=50,
|
| 153 |
-
step=1,
|
| 154 |
-
value=2,
|
| 155 |
-
)
|
| 156 |
-
|
| 157 |
-
gr.Examples(examples=examples, inputs=[prompt])
|
| 158 |
-
gr.on(
|
| 159 |
-
triggers=[run_button.click, prompt.submit],
|
| 160 |
-
fn=infer,
|
| 161 |
-
inputs=[
|
| 162 |
-
prompt,
|
| 163 |
-
negative_prompt,
|
| 164 |
-
seed,
|
| 165 |
-
randomize_seed,
|
| 166 |
-
width,
|
| 167 |
-
height,
|
| 168 |
-
guidance_scale,
|
| 169 |
-
num_inference_steps,
|
| 170 |
-
],
|
| 171 |
-
outputs=[result, seed],
|
| 172 |
-
)
|
| 173 |
|
| 174 |
if __name__ == "__main__":
|
| 175 |
-
|
|
|
|
| 1 |
+
from diffusers import StableDiffusionPipeline
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
import torch
|
| 3 |
+
from flask import Flask, request, jsonify
|
| 4 |
+
|
| 5 |
+
app = Flask(__name__)
|
| 6 |
+
|
| 7 |
+
# Load the model
|
| 8 |
+
model_id = "ZB-Tech/Text-to-Image"
|
| 9 |
+
pipeline = StableDiffusionPipeline.from_pretrained(model_id, torch_dtype=torch.float16)
|
| 10 |
+
pipeline.to("cuda" if torch.cuda.is_available() else "cpu")
|
| 11 |
+
|
| 12 |
+
@app.route("/generate", methods=["POST"])
|
| 13 |
+
def generate_image():
|
| 14 |
+
data = request.get_json()
|
| 15 |
+
prompt = data.get("prompt", "A scenic landscape")
|
| 16 |
+
|
| 17 |
+
image = pipeline(prompt).images[0]
|
| 18 |
+
image_path = "generated_image.png"
|
| 19 |
+
image.save(image_path)
|
| 20 |
+
|
| 21 |
+
return jsonify({"image_url": image_path})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
if __name__ == "__main__":
|
| 24 |
+
app.run(host="0.0.0.0", port=7860)
|