Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,139 +1,112 @@
|
|
1 |
-
import
|
2 |
-
import
|
3 |
-
import
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
taef1 = AutoencoderTiny.from_pretrained("madebyollin/taef1", torch_dtype=dtype).to(device)
|
14 |
-
good_vae = AutoencoderKL.from_pretrained("black-forest-labs/FLUX.1-dev", subfolder="vae", torch_dtype=dtype).to(device)
|
15 |
-
pipe = DiffusionPipeline.from_pretrained("black-forest-labs/FLUX.1-dev", torch_dtype=dtype, vae=taef1).to(device)
|
16 |
-
torch.cuda.empty_cache()
|
17 |
-
|
18 |
-
MAX_SEED = np.iinfo(np.int32).max
|
19 |
-
MAX_IMAGE_SIZE = 2048
|
20 |
-
|
21 |
-
pipe.flux_pipe_call_that_returns_an_iterable_of_images = flux_pipe_call_that_returns_an_iterable_of_images.__get__(pipe)
|
22 |
-
|
23 |
-
@spaces.GPU(duration=75)
|
24 |
-
def infer(prompt, seed=42, randomize_seed=False, width=1024, height=1024, guidance_scale=3.5, num_inference_steps=28, progress=gr.Progress(track_tqdm=True)):
|
25 |
-
if randomize_seed:
|
26 |
-
seed = random.randint(0, MAX_SEED)
|
27 |
-
generator = torch.Generator().manual_seed(seed)
|
28 |
-
|
29 |
-
for img in pipe.flux_pipe_call_that_returns_an_iterable_of_images(
|
30 |
-
prompt=prompt,
|
31 |
-
guidance_scale=guidance_scale,
|
32 |
-
num_inference_steps=num_inference_steps,
|
33 |
-
width=width,
|
34 |
-
height=height,
|
35 |
-
generator=generator,
|
36 |
-
output_type="pil",
|
37 |
-
good_vae=good_vae,
|
38 |
-
):
|
39 |
-
yield img, seed
|
40 |
-
|
41 |
-
examples = [
|
42 |
-
"a tiny astronaut hatching from an egg on the moon",
|
43 |
-
"a cat holding a sign that says hello world",
|
44 |
-
"an anime illustration of a wiener schnitzel",
|
45 |
-
]
|
46 |
-
|
47 |
-
css="""
|
48 |
-
#col-container {
|
49 |
-
margin: 0 auto;
|
50 |
-
max-width: 520px;
|
51 |
}
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
gr.Examples(
|
125 |
-
examples = examples,
|
126 |
-
fn = infer,
|
127 |
-
inputs = [prompt],
|
128 |
-
outputs = [result, seed],
|
129 |
-
cache_examples="lazy"
|
130 |
-
)
|
131 |
-
|
132 |
-
gr.on(
|
133 |
-
triggers=[run_button.click, prompt.submit],
|
134 |
-
fn = infer,
|
135 |
-
inputs = [prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps],
|
136 |
-
outputs = [result, seed]
|
137 |
)
|
138 |
|
139 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from flask import Flask, request, jsonify
|
2 |
+
import requests
|
3 |
+
import base64
|
4 |
+
|
5 |
+
app = Flask(__name__)
|
6 |
+
|
7 |
+
# URL do modelo Hugging Face e seu token de API
|
8 |
+
HUGGINGFACE_API_URL = "https://api-inference.huggingface.co/models/stabilityai/stable-diffusion-2"
|
9 |
+
HUGGINGFACE_API_TOKEN = "SEU_TOKEN_AQUI"
|
10 |
+
|
11 |
+
headers = {
|
12 |
+
"Authorization": f"Bearer {HUGGINGFACE_API_TOKEN}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
}
|
14 |
+
|
15 |
+
@app.route("/")
|
16 |
+
def home():
|
17 |
+
return '''
|
18 |
+
<!DOCTYPE html>
|
19 |
+
<html>
|
20 |
+
<head>
|
21 |
+
<title>Gerador de Imagens</title>
|
22 |
+
</head>
|
23 |
+
<body>
|
24 |
+
<h1>Gerador de Imagens com Texto</h1>
|
25 |
+
<form action="/generate" method="post">
|
26 |
+
<label for="prompt">Descrição da imagem:</label><br>
|
27 |
+
<input type="text" id="prompt" name="prompt" required><br><br>
|
28 |
+
|
29 |
+
<h3>Configurações Avançadas</h3>
|
30 |
+
<label for="seed">Seed:</label>
|
31 |
+
<input type="number" id="seed" name="seed" value="0"><br><br>
|
32 |
+
|
33 |
+
<label for="randomize_seed">Randomizar Seed:</label>
|
34 |
+
<input type="checkbox" id="randomize_seed" name="randomize_seed" checked><br><br>
|
35 |
+
|
36 |
+
<label for="width">Largura (Width):</label>
|
37 |
+
<input type="number" id="width" name="width" value="1024"><br><br>
|
38 |
+
|
39 |
+
<label for="height">Altura (Height):</label>
|
40 |
+
<input type="number" id="height" name="height" value="1024"><br><br>
|
41 |
+
|
42 |
+
<label for="guidance_scale">Escala de Orientação (Guidance Scale):</label>
|
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 |
+
from waitress import serve
|
112 |
+
serve(app, host="0.0.0.0", port=8080)
|