Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,112 +1,46 @@
|
|
1 |
-
|
2 |
-
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
<input type="number" id="guidance_scale" name="guidance_scale" step="0.1" value="7.5"><br><br>
|
44 |
-
|
45 |
-
<label for="steps">Número de Etapas (Inference Steps):</label>
|
46 |
-
<input type="number" id="steps" name="steps" value="50"><br><br>
|
47 |
-
|
48 |
-
<button type="submit">Gerar Imagem</button>
|
49 |
-
</form>
|
50 |
-
</body>
|
51 |
-
</html>
|
52 |
-
'''
|
53 |
-
|
54 |
-
@app.route("/generate", methods=["POST"])
|
55 |
-
def generate_image():
|
56 |
-
# Obter os parâmetros enviados pelo formulário
|
57 |
-
prompt = request.form.get("prompt")
|
58 |
-
seed = int(request.form.get("seed", 0))
|
59 |
-
randomize_seed = "randomize_seed" in request.form
|
60 |
-
width = int(request.form.get("width", 1024))
|
61 |
-
height = int(request.form.get("height", 1024))
|
62 |
-
guidance_scale = float(request.form.get("guidance_scale", 7.5))
|
63 |
-
steps = int(request.form.get("steps", 50))
|
64 |
-
|
65 |
-
# Ajustar o seed se for para randomizar
|
66 |
-
if randomize_seed:
|
67 |
-
import random
|
68 |
-
seed = random.randint(0, 999999)
|
69 |
-
|
70 |
-
# Requisição para o modelo Hugging Face
|
71 |
-
payload = {
|
72 |
-
"inputs": prompt,
|
73 |
-
"parameters": {
|
74 |
-
"width": width,
|
75 |
-
"height": height,
|
76 |
-
"guidance_scale": guidance_scale,
|
77 |
-
"num_inference_steps": steps,
|
78 |
-
"seed": seed
|
79 |
-
}
|
80 |
-
}
|
81 |
-
response = requests.post(
|
82 |
-
HUGGINGFACE_API_URL,
|
83 |
-
headers=headers,
|
84 |
-
json=payload
|
85 |
-
)
|
86 |
-
|
87 |
-
if response.status_code != 200:
|
88 |
-
return jsonify({"error": "Erro ao gerar a imagem", "details": response.text}), 500
|
89 |
-
|
90 |
-
# Converte a resposta para Base64
|
91 |
-
image_data = response.content
|
92 |
-
image_base64 = base64.b64encode(image_data).decode("utf-8")
|
93 |
-
|
94 |
-
# Retorna a imagem gerada
|
95 |
-
return f'''
|
96 |
-
<!DOCTYPE html>
|
97 |
-
<html>
|
98 |
-
<head>
|
99 |
-
<title>Imagem Gerada</title>
|
100 |
-
</head>
|
101 |
-
<body>
|
102 |
-
<h1>Imagem Gerada</h1>
|
103 |
-
<img src="data:image/png;base64,{image_base64}" alt="Generated Image">
|
104 |
-
<br><br>
|
105 |
-
<a href="/">Voltar</a>
|
106 |
-
</body>
|
107 |
-
</html>
|
108 |
-
'''
|
109 |
|
110 |
if __name__ == "__main__":
|
111 |
-
|
112 |
-
|
|
|
1 |
+
model_id = "stabilityai/stable-diffusion-xl-base-1.0"
|
2 |
+
scheduler = EulerAncestralDiscreteScheduler.from_pretrained(model_id, subfolder="scheduler")
|
3 |
+
pipe = StableDiffusionXLPipeline.from_pretrained(model_id, scheduler=scheduler, torch_dtype=torch.float16, revision="fp16")
|
4 |
+
pipe = pipe.to("cuda")
|
5 |
+
|
6 |
+
def generate_image(prompt: str, negative_prompt: str = "", height: int = 512, width: int = 512, num_inference_steps: int = 50, guidance_scale: float = 7.5, num_images_per_prompt: int = 1) -> Tuple[Image.Image, str]:
|
7 |
+
generator = torch.Generator(device="cuda").manual_seed(random.randint(0, 2**32 - 1))
|
8 |
+
image = pipe(
|
9 |
+
prompt=prompt,
|
10 |
+
negative_prompt=negative_prompt,
|
11 |
+
height=height,
|
12 |
+
width=width,
|
13 |
+
num_inference_steps=num_inference_steps,
|
14 |
+
guidance_scale=guidance_scale,
|
15 |
+
num_images_per_prompt=num_images_per_prompt,
|
16 |
+
generator=generator,
|
17 |
+
).images[0]
|
18 |
+
|
19 |
+
image_id = str(uuid.uuid4())
|
20 |
+
image_path = f"/tmp/{image_id}.png"
|
21 |
+
image.save(image_path)
|
22 |
+
|
23 |
+
return image, image_path
|
24 |
+
|
25 |
+
def gradio_interface():
|
26 |
+
with gr.Blocks(css=css) as demo:
|
27 |
+
gr.Markdown("## Gere imagens usando Stable Diffusion XL")
|
28 |
+
with gr.Row():
|
29 |
+
with gr.Column():
|
30 |
+
prompt = gr.Textbox(label="Prompt", placeholder="Digite o prompt aqui...")
|
31 |
+
negative_prompt = gr.Textbox(label="Negative Prompt", placeholder="Digite o negative prompt aqui...")
|
32 |
+
run_button = gr.Button("Gerar Imagem")
|
33 |
+
with gr.Column():
|
34 |
+
result = gr.Image(label="Imagem Gerada")
|
35 |
+
|
36 |
+
run_button.click(
|
37 |
+
fn=lambda p, np: generate_image(p, np)[0],
|
38 |
+
inputs=[prompt, negative_prompt],
|
39 |
+
outputs=result,
|
40 |
+
)
|
41 |
+
|
42 |
+
return demo
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
43 |
|
44 |
if __name__ == "__main__":
|
45 |
+
demo = gradio_interface()
|
46 |
+
demo.launch()
|