onnew commited on
Commit
1616045
·
verified ·
1 Parent(s): 0cc401b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -35
app.py CHANGED
@@ -1,35 +1,44 @@
1
- from PIL import Image, ImageDraw, ImageFont
2
-
3
- # Dimensões da imagem
4
- width, height = 576, 1024
5
-
6
- # Criar uma imagem branca
7
- image = Image.new('RGB', (width, height), color='white')
8
-
9
- # Criar um objeto de desenho
10
- draw = ImageDraw.Draw(image)
11
-
12
- # Carregar uma fonte (certifique-se de que a fonte está disponível no seu sistema)
13
- try:
14
- font = ImageFont.truetype("arial.ttf", 40)
15
- except IOError:
16
- font = ImageFont.load_default()
17
-
18
- # Texto a ser adicionado à imagem
19
- text = "Este é um exemplo de texto em uma imagem."
20
-
21
- # Calcular a posição do texto para centralizá-lo
22
- bbox = draw.textbbox((0, 0), text, font=font)
23
- text_width = bbox[2] - bbox[0]
24
- text_height = bbox[3] - bbox[1]
25
- text_x = (width - text_width) / 2
26
- text_y = (height - text_height) / 2
27
-
28
- # Adicionar texto à imagem
29
- draw.text((text_x, text_y), text, font=font, fill='black')
30
-
31
- # Salvar a imagem
32
- image.save('text_image.png')
33
-
34
- # Exibir a imagem
35
- image.show()
 
 
 
 
 
 
 
 
 
 
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)