Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,83 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import fal_client
|
3 |
+
import time
|
4 |
+
|
5 |
+
def generate_image(api_key, prompt, image_size, num_images, enable_safety_checker, safety_tolerance):
|
6 |
+
try:
|
7 |
+
# Set the API key provided by the user
|
8 |
+
fal_client.api_key = api_key
|
9 |
+
|
10 |
+
# Map image size labels to API values
|
11 |
+
image_size_mapping = {
|
12 |
+
"Square HD": "square_hd",
|
13 |
+
"Square": "square",
|
14 |
+
"Portrait 4:3": "portrait_4_3",
|
15 |
+
"Portrait 16:9": "portrait_16_9",
|
16 |
+
"Landscape 4:3": "landscape_4_3",
|
17 |
+
"Landscape 16:9": "landscape_16_9"
|
18 |
+
}
|
19 |
+
|
20 |
+
# Map safety tolerance labels to API values
|
21 |
+
safety_tolerance_mapping = {
|
22 |
+
"1 (Most Strict)": "1",
|
23 |
+
"2": "2",
|
24 |
+
"3": "3",
|
25 |
+
"4": "4",
|
26 |
+
"5 (Most Permissive)": "5"
|
27 |
+
}
|
28 |
+
|
29 |
+
handler = fal_client.submit(
|
30 |
+
"fal-ai/flux-pro/v1.1",
|
31 |
+
arguments={
|
32 |
+
"prompt": prompt,
|
33 |
+
"image_size": image_size_mapping[image_size],
|
34 |
+
"num_images": num_images,
|
35 |
+
"enable_safety_checker": enable_safety_checker,
|
36 |
+
"safety_tolerance": safety_tolerance_mapping[safety_tolerance]
|
37 |
+
},
|
38 |
+
)
|
39 |
+
|
40 |
+
# Wait for the result
|
41 |
+
while True:
|
42 |
+
status = handler.status()
|
43 |
+
if status["status"] == "completed":
|
44 |
+
break
|
45 |
+
elif status["status"] == "failed":
|
46 |
+
return "The image generation failed."
|
47 |
+
time.sleep(1) # Wait for 1 second before checking again
|
48 |
+
|
49 |
+
result = handler.get()
|
50 |
+
images = []
|
51 |
+
for image_info in result["images"]:
|
52 |
+
image_url = image_info["url"]
|
53 |
+
images.append(image_url)
|
54 |
+
|
55 |
+
return images if len(images) > 1 else images[0]
|
56 |
+
except Exception as e:
|
57 |
+
return str(e)
|
58 |
+
|
59 |
+
# Set up the Gradio interface
|
60 |
+
iface = gr.Interface(
|
61 |
+
fn=generate_image,
|
62 |
+
inputs=[
|
63 |
+
gr.Textbox(label="API Key", placeholder="Enter your API key here...", type="password"),
|
64 |
+
gr.Textbox(label="Prompt", placeholder="Enter your prompt here..."),
|
65 |
+
gr.Dropdown(
|
66 |
+
choices=["Landscape 4:3", "Landscape 16:9", "Portrait 4:3", "Portrait 16:9", "Square", "Square HD"],
|
67 |
+
value="Landscape 4:3",
|
68 |
+
label="Image Size"
|
69 |
+
),
|
70 |
+
gr.Slider(minimum=1, maximum=4, step=1, value=1, label="Number of Images"),
|
71 |
+
gr.Checkbox(value=True, label="Enable Safety Checker"),
|
72 |
+
gr.Dropdown(
|
73 |
+
choices=["1 (Most Strict)", "2", "3", "4", "5 (Most Permissive)"],
|
74 |
+
value="2",
|
75 |
+
label="Safety Tolerance"
|
76 |
+
),
|
77 |
+
],
|
78 |
+
outputs=gr.Gallery(label="Generated Images").style(grid=[2], height="auto"),
|
79 |
+
title="FLUX1.1 [pro] Image Generation",
|
80 |
+
description="Generate images using the FLUX1.1 [pro] model by providing a text prompt and your API key."
|
81 |
+
)
|
82 |
+
|
83 |
+
iface.launch()
|