Spaces:
Sleeping
Sleeping
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) | |