Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,83 +1,54 @@
|
|
1 |
import gradio as gr
|
2 |
import fal_client
|
3 |
-
import time
|
4 |
import requests
|
5 |
-
from io import BytesIO
|
6 |
from PIL import Image
|
7 |
-
import
|
8 |
|
9 |
-
def generate_image(api_key, prompt):
|
10 |
try:
|
11 |
-
# Set the API key
|
12 |
fal_client.api_key = api_key
|
13 |
|
14 |
-
# Default parameters
|
15 |
-
image_size = "landscape_4_3"
|
16 |
-
num_images = 1
|
17 |
-
enable_safety_checker = True
|
18 |
-
safety_tolerance = "2"
|
19 |
-
sync_mode = True # Enable sync_mode to get the image directly
|
20 |
-
|
21 |
handler = fal_client.submit(
|
22 |
"fal-ai/flux-pro/v1.1",
|
23 |
arguments={
|
24 |
"prompt": prompt,
|
25 |
"image_size": image_size,
|
26 |
"num_images": num_images,
|
27 |
-
"enable_safety_checker": enable_safety_checker,
|
28 |
-
"safety_tolerance": safety_tolerance,
|
29 |
-
"sync_mode": sync_mode,
|
30 |
},
|
31 |
)
|
32 |
-
|
33 |
-
# Since sync_mode is True, handler.get() will wait for the result
|
34 |
result = handler.get()
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
if not result.get("images"):
|
40 |
-
return None, "No images were generated."
|
41 |
-
|
42 |
-
image_info = result["images"][0]
|
43 |
-
|
44 |
-
# Check if image data is in the 'data' field (base64 encoded)
|
45 |
-
if 'data' in image_info:
|
46 |
-
# Image data is base64 encoded
|
47 |
-
image_data = image_info['data']
|
48 |
-
image_bytes = base64.b64decode(image_data)
|
49 |
-
image = Image.open(BytesIO(image_bytes))
|
50 |
-
return image, None
|
51 |
-
elif 'url' in image_info:
|
52 |
-
image_url = image_info['url']
|
53 |
-
print("Image URL:", image_url)
|
54 |
# Download the image
|
55 |
-
response = requests.get(
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
else:
|
60 |
-
return None, f"Failed to download the generated image. Status code: {response.status_code}"
|
61 |
-
else:
|
62 |
-
return None, "Image data not found in API response."
|
63 |
except Exception as e:
|
64 |
-
|
65 |
-
|
66 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
67 |
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
gr.Textbox(label="Prompt", placeholder="Enter your prompt here...")
|
74 |
-
],
|
75 |
-
outputs=[
|
76 |
-
gr.Image(label="Generated Image"),
|
77 |
-
gr.Textbox(label="Error Message")
|
78 |
-
],
|
79 |
-
title="FLUX1.1 [pro] Image Generation",
|
80 |
-
description="Generate an image using the FLUX1.1 [pro] model by providing your API key and a text prompt."
|
81 |
-
)
|
82 |
|
83 |
-
|
|
|
1 |
import gradio as gr
|
2 |
import fal_client
|
|
|
3 |
import requests
|
|
|
4 |
from PIL import Image
|
5 |
+
from io import BytesIO
|
6 |
|
7 |
+
def generate_image(api_key, prompt, image_size='landscape_4_3', num_images=1):
|
8 |
try:
|
9 |
+
# Set the API key for the fal_client
|
10 |
fal_client.api_key = api_key
|
11 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
handler = fal_client.submit(
|
13 |
"fal-ai/flux-pro/v1.1",
|
14 |
arguments={
|
15 |
"prompt": prompt,
|
16 |
"image_size": image_size,
|
17 |
"num_images": num_images,
|
|
|
|
|
|
|
18 |
},
|
19 |
)
|
|
|
|
|
20 |
result = handler.get()
|
21 |
+
images = []
|
22 |
+
for img_info in result['images']:
|
23 |
+
img_url = img_info['url']
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
# Download the image
|
25 |
+
response = requests.get(img_url)
|
26 |
+
img = Image.open(BytesIO(response.content))
|
27 |
+
images.append(img)
|
28 |
+
return images
|
|
|
|
|
|
|
|
|
29 |
except Exception as e:
|
30 |
+
return [f"Error: {e}"]
|
31 |
+
|
32 |
+
with gr.Blocks() as demo:
|
33 |
+
gr.Markdown("# FLUX1.1 [pro] Text-to-Image Generator")
|
34 |
+
with gr.Row():
|
35 |
+
api_key = gr.Textbox(label="API Key", type="password", placeholder="Enter your API key here")
|
36 |
+
with gr.Row():
|
37 |
+
prompt = gr.Textbox(label="Prompt", lines=2, placeholder="Enter your prompt here")
|
38 |
+
with gr.Row():
|
39 |
+
image_size = gr.Dropdown(
|
40 |
+
label="Image Size",
|
41 |
+
choices=["square_hd", "square", "portrait_4_3", "portrait_16_9", "landscape_4_3", "landscape_16_9"],
|
42 |
+
value="landscape_4_3"
|
43 |
+
)
|
44 |
+
num_images = gr.Slider(label="Number of Images", minimum=1, maximum=4, step=1, value=1)
|
45 |
+
generate_btn = gr.Button("Generate Image")
|
46 |
+
output_gallery = gr.Gallery(label="Generated Images")
|
47 |
|
48 |
+
generate_btn.click(
|
49 |
+
fn=generate_image,
|
50 |
+
inputs=[api_key, prompt, image_size, num_images],
|
51 |
+
outputs=output_gallery
|
52 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
53 |
|
54 |
+
demo.launch()
|