File size: 1,408 Bytes
58126da
b8ec2f8
fe1b250
 
58126da
b5ee36a
 
 
b8ec2f8
 
 
b5ee36a
58126da
b8ec2f8
bfd543d
b8ec2f8
fe1b250
 
 
b8ec2f8
fe1b250
bfd543d
b8ec2f8
bfd543d
b8ec2f8
 
bfd543d
 
 
 
 
 
 
 
b8ec2f8
 
bfd543d
 
 
 
d8e57d4
 
bfd543d
 
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
import gradio as gr
import requests
from PIL import Image
from io import BytesIO

# Substitua 'YOUR_HUGGINGFACE_API_KEY' pela sua chave de API real
API_KEY = "YOUR_HUGGINGFACE_API_KEY"

# Função para gerar imagem usando a API do Hugging Face
def generate_image(prompt: str):
    API_URL = "https://api-inference.huggingface.co/models/CompVis/stable-diffusion-v1-4"
    headers = {"Authorization": f"Bearer {API_KEY}"}

    response = requests.post(API_URL, headers=headers, json={"inputs": prompt})

    if response.status_code == 200:
        image_data = response.content
        image = Image.open(BytesIO(image_data))
        return image
    else:
        raise gr.Error(f"Erro ao gerar imagem: {response.status_code} - {response.text}")

# Função para criar a interface do Gradio
def gradio_interface():
    with gr.Blocks() as demo:
        gr.Markdown("## Gere imagens usando Stable Diffusion")
        with gr.Row():
            with gr.Column():
                prompt = gr.Textbox(label="Prompt", placeholder="Digite o prompt aqui...")
                run_button = gr.Button("Gerar Imagem")
            with gr.Column():
                result = gr.Image(label="Imagem Gerada")

        run_button.click(
            fn=generate_image,
            inputs=prompt,
            outputs=result,
        )

    return demo

if __name__ == "__main__":
    demo = gradio_interface()
    demo.launch()