File size: 2,577 Bytes
13fabf7
9d3588e
 
 
 
 
13fabf7
9d3588e
 
 
 
 
 
 
 
 
 
 
 
 
9aad24a
9d3588e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import os
import gradio as gr
import requests
import json

# Configurar a chave API
FAL_KEY = os.environ.get("FAL_KEY")

def generate_image(prompt, negative_prompt, image_size, format):
    headers = {
        "Authorization": f"Key {FAL_KEY}",
        "Content-Type": "application/json"
    }
    
    data = {
        "prompt": prompt,
        "negative_prompt": negative_prompt,
        "image_size": image_size,
        "num_inference_steps": 25,
        "guidance_scale": 7.5,
        "num_images": 1,
        "safety_checker": False,
        "seed": 0,  # Adicionando um seed fixo para reprodutibilidade
        "format": format
    }
    
    print("Sending data to API:", json.dumps(data, indent=2))  # Debug print
    
    try:
        response = requests.post(
            "https://110602490-fast-sdxl.gateway.alpha.fal.ai/",
            headers=headers,
            json=data  # Usando json= em vez de data=json.dumps(data)
        )
        
        print("API Response Status:", response.status_code)  # Debug print
        print("API Response Content:", response.text)  # Debug print
        
        response.raise_for_status()
        
        result = response.json()
        image_url = result['images'][0]['url']
        
        return image_url
    except requests.RequestException as e:
        return f"Error in API request: {str(e)}\nResponse: {e.response.text if e.response else 'No response'}"
    except Exception as e:
        return f"Unexpected error: {str(e)}"

def run_interface(prompt, negative_prompt, image_size, format):
    result = generate_image(prompt, negative_prompt, image_size, format)
    if result.startswith("Error") or result.startswith("Unexpected error"):
        return None, result  # Retorna None para a imagem e a mensagem de erro como texto
    else:
        return result, None  # Retorna a URL da imagem e None para a mensagem de erro

# Criar a interface Gradio
iface = gr.Interface(
    fn=run_interface,
    inputs=[
        gr.Textbox(label="Prompt"),
        gr.Textbox(label="Negative Prompt"),
        gr.Dropdown(
            choices=["square_hd", "square", "portrait_4_3", "portrait_16_9", "landscape_4_3", "landscape_16_9"],
            label="Image Size",
            value="square_hd"
        ),
        gr.Radio(["jpeg", "png"], label="Format", value="jpeg")
    ],
    outputs=[
        gr.Image(label="Generated Image"),
        gr.Textbox(label="Error Message")
    ],
    title="Gerador de Imagens",
    description="Model: Stable Diffusion XL Image Generator"
)

# Executar o aplicativo
iface.launch()