Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -4,6 +4,7 @@ import time
|
|
4 |
import requests
|
5 |
from io import BytesIO
|
6 |
from PIL import Image
|
|
|
7 |
|
8 |
def generate_image(api_key, prompt):
|
9 |
try:
|
@@ -15,6 +16,7 @@ def generate_image(api_key, prompt):
|
|
15 |
num_images = 1
|
16 |
enable_safety_checker = True
|
17 |
safety_tolerance = "2"
|
|
|
18 |
|
19 |
handler = fal_client.submit(
|
20 |
"fal-ai/flux-pro/v1.1",
|
@@ -24,34 +26,44 @@ def generate_image(api_key, prompt):
|
|
24 |
"num_images": num_images,
|
25 |
"enable_safety_checker": enable_safety_checker,
|
26 |
"safety_tolerance": safety_tolerance,
|
|
|
27 |
},
|
28 |
)
|
29 |
|
30 |
-
#
|
31 |
-
while True:
|
32 |
-
status = handler.status()
|
33 |
-
if status["status"] == "completed":
|
34 |
-
break
|
35 |
-
elif status["status"] == "failed":
|
36 |
-
return "The image generation failed."
|
37 |
-
time.sleep(1) # Wait for 1 second before checking again
|
38 |
-
|
39 |
result = handler.get()
|
40 |
-
|
41 |
-
|
|
|
|
|
|
|
|
|
42 |
|
43 |
image_info = result["images"][0]
|
44 |
-
image_url = image_info["url"]
|
45 |
|
46 |
-
#
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
51 |
else:
|
52 |
-
return "
|
53 |
except Exception as e:
|
54 |
-
|
|
|
|
|
55 |
|
56 |
# Set up the Gradio interface
|
57 |
iface = gr.Interface(
|
@@ -60,7 +72,10 @@ iface = gr.Interface(
|
|
60 |
gr.Textbox(label="API Key", placeholder="Enter your API key here...", type="password"),
|
61 |
gr.Textbox(label="Prompt", placeholder="Enter your prompt here...")
|
62 |
],
|
63 |
-
outputs=
|
|
|
|
|
|
|
64 |
title="FLUX1.1 [pro] Image Generation",
|
65 |
description="Generate an image using the FLUX1.1 [pro] model by providing your API key and a text prompt."
|
66 |
)
|
|
|
4 |
import requests
|
5 |
from io import BytesIO
|
6 |
from PIL import Image
|
7 |
+
import base64
|
8 |
|
9 |
def generate_image(api_key, prompt):
|
10 |
try:
|
|
|
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",
|
|
|
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 |
+
# For debugging, print the result
|
37 |
+
print("API Response Result:", result)
|
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(image_url)
|
56 |
+
if response.status_code == 200:
|
57 |
+
image = Image.open(BytesIO(response.content))
|
58 |
+
return image, None
|
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 |
+
# For debugging, print the exception
|
65 |
+
print("Exception occurred:", e)
|
66 |
+
return None, str(e)
|
67 |
|
68 |
# Set up the Gradio interface
|
69 |
iface = gr.Interface(
|
|
|
72 |
gr.Textbox(label="API Key", placeholder="Enter your API key here...", type="password"),
|
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 |
)
|