Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,35 +1,44 @@
|
|
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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import requests
|
2 |
+
import json
|
3 |
+
|
4 |
+
# Defina a URL da API do Hugging Face
|
5 |
+
API_URL = "https://api-inference.huggingface.co/models/stabilityai/stable-diffusion-2"
|
6 |
+
|
7 |
+
# Defina o cabeçalho da requisição com o token de autenticação
|
8 |
+
headers = {
|
9 |
+
"Authorization": "Bearer YOUR_HUGGINGFACE_API_TOKEN"
|
10 |
+
}
|
11 |
+
|
12 |
+
def query(payload):
|
13 |
+
response = requests.post(API_URL, headers=headers, json=payload)
|
14 |
+
return response
|
15 |
+
|
16 |
+
def generate_image(text_prompt, output_file="generated_image.png"):
|
17 |
+
data = {
|
18 |
+
"inputs": text_prompt,
|
19 |
+
"options": {
|
20 |
+
"wait_for_model": True,
|
21 |
+
"use_cpu": True,
|
22 |
+
"cpu_basic": {
|
23 |
+
"vCPU": 2,
|
24 |
+
"RAM": 16
|
25 |
+
}
|
26 |
+
}
|
27 |
+
}
|
28 |
+
|
29 |
+
response = query(data)
|
30 |
+
|
31 |
+
if response.status_code == 200:
|
32 |
+
with open(output_file, "wb") as f:
|
33 |
+
f.write(response.content)
|
34 |
+
print(f"Imagem gerada com sucesso e salva como '{output_file}'")
|
35 |
+
else:
|
36 |
+
print(f"Erro na requisição: {response.status_code}")
|
37 |
+
print(f"Mensagem de erro: {response.text}")
|
38 |
+
|
39 |
+
if __name__ == "__main__":
|
40 |
+
# Defina o texto que será usado para gerar a imagem
|
41 |
+
text_prompt = "A beautiful sunset over the mountains"
|
42 |
+
|
43 |
+
# Gere a imagem
|
44 |
+
generate_image(text_prompt)
|