Spaces:
Build error
Build error
| 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() | |