Spaces:
Sleeping
Sleeping
File size: 1,381 Bytes
1616045 e73c41b 1616045 e73c41b 1616045 |
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 44 45 46 47 |
import requests
import json
from live_preview_helpers import preview_image
# Defina a URL da API do Hugging Face
API_URL = "https://api-inference.huggingface.co/models/stabilityai/stable-diffusion-2"
# Defina o cabeçalho da requisição com o token de autenticação
headers = {
"Authorization": "Bearer YOUR_HUGGINGFACE_API_TOKEN"
}
def query(payload):
response = requests.post(API_URL, headers=headers, json=payload)
return response
def generate_image(text_prompt, output_file="generated_image.png"):
data = {
"inputs": text_prompt,
"options": {
"wait_for_model": True,
"use_cpu": True,
"cpu_basic": {
"vCPU": 2,
"RAM": 16
}
}
}
response = query(data)
if response.status_code == 200:
with open(output_file, "wb") as f:
f.write(response.content)
print(f"Imagem gerada com sucesso e salva como '{output_file}'")
preview_image(output_file) # Chama a função de visualização ao vivo
else:
print(f"Erro na requisição: {response.status_code}")
print(f"Mensagem de erro: {response.text}")
if __name__ == "__main__":
# Defina o texto que será usado para gerar a imagem
text_prompt = "A beautiful sunset over the mountains"
# Gere a imagem
generate_image(text_prompt)
|