akhaliq HF staff commited on
Commit
01fec03
·
verified ·
1 Parent(s): 12222f8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -73
app.py CHANGED
@@ -1,3 +1,4 @@
 
1
  import gradio as gr
2
  import fal_client
3
  import time
@@ -5,107 +6,62 @@ import requests
5
  from io import BytesIO
6
  from PIL import Image
7
 
8
- def generate_image(api_key, prompt, image_size, num_images, enable_safety_checker, safety_tolerance):
 
 
 
 
9
  try:
10
- # Set the API key provided by the user
11
- fal_client.api_key = api_key
12
-
13
- # Map image size labels to API values
14
- image_size_mapping = {
15
- "Square HD": "square_hd",
16
- "Square": "square",
17
- "Portrait 4:3": "portrait_4_3",
18
- "Portrait 16:9": "portrait_16_9",
19
- "Landscape 4:3": "landscape_4_3",
20
- "Landscape 16:9": "landscape_16_9"
21
- }
22
 
23
- # Map safety tolerance labels to API values
24
- safety_tolerance_mapping = {
25
- "1 (Most Strict)": "1",
26
- "2": "2",
27
- "3": "3",
28
- "4": "4",
29
- "5 (Most Permissive)": "5"
30
- }
31
-
32
- # Print statements for debugging
33
- print(f"API Key: {api_key}")
34
- print(f"Prompt: {prompt}")
35
- print(f"Image Size: {image_size}")
36
- print(f"Number of Images: {num_images}")
37
- print(f"Enable Safety Checker: {enable_safety_checker}")
38
- print(f"Safety Tolerance: {safety_tolerance}")
39
-
40
  handler = fal_client.submit(
41
  "fal-ai/flux-pro/v1.1",
42
  arguments={
43
  "prompt": prompt,
44
- "image_size": image_size_mapping[image_size],
45
  "num_images": num_images,
46
  "enable_safety_checker": enable_safety_checker,
47
- "safety_tolerance": safety_tolerance_mapping[safety_tolerance]
48
  },
49
  )
50
 
51
  # Wait for the result
52
  while True:
53
  status = handler.status()
54
- print(f"Status: {status}")
55
  if status["status"] == "completed":
56
  break
57
  elif status["status"] == "failed":
58
- return (None, gr.update(value="The image generation failed.", visible=True))
59
  time.sleep(1) # Wait for 1 second before checking again
60
 
61
  result = handler.get()
62
- print(f"Handler Result: {result}")
63
- images = []
64
- for image_info in result.get("images", []):
65
- image_url = image_info.get("url")
66
- print(f"Image URL: {image_url}")
67
- # Download the image
68
- response = requests.get(image_url)
69
- if response.status_code == 200:
70
- image = Image.open(BytesIO(response.content))
71
- images.append(image)
72
- else:
73
- print(f"Failed to download image from {image_url}. Status code: {response.status_code}")
74
- raise Exception("Failed to download the generated image.")
75
 
76
- if not images:
77
- raise Exception("No images were returned by the API.")
78
 
79
- return (images if len(images) > 1 else images[0], gr.update(visible=False))
 
 
 
 
 
 
80
  except Exception as e:
81
- print(f"Exception occurred: {e}")
82
- return (None, gr.update(value=str(e), visible=True))
83
 
84
  # Set up the Gradio interface
85
  iface = gr.Interface(
86
  fn=generate_image,
87
- inputs=[
88
- gr.Textbox(label="API Key", placeholder="Enter your API key here...", type="password"),
89
- gr.Textbox(label="Prompt", placeholder="Enter your prompt here..."),
90
- gr.Dropdown(
91
- choices=["Landscape 4:3", "Landscape 16:9", "Portrait 4:3", "Portrait 16:9", "Square", "Square HD"],
92
- value="Landscape 4:3",
93
- label="Image Size"
94
- ),
95
- gr.Slider(minimum=1, maximum=4, step=1, value=1, label="Number of Images"),
96
- gr.Checkbox(value=True, label="Enable Safety Checker"),
97
- gr.Dropdown(
98
- choices=["1 (Most Strict)", "2", "3", "4", "5 (Most Permissive)"],
99
- value="2",
100
- label="Safety Tolerance"
101
- ),
102
- ],
103
- outputs=[
104
- gr.Gallery(label="Generated Images", columns=2),
105
- gr.Textbox(label="Error Message", visible=False)
106
- ],
107
  title="FLUX1.1 [pro] Image Generation",
108
- description="Generate images using the FLUX1.1 [pro] model by providing a text prompt and your API key."
109
  )
110
 
111
  iface.launch()
 
1
+ import os
2
  import gradio as gr
3
  import fal_client
4
  import time
 
6
  from io import BytesIO
7
  from PIL import Image
8
 
9
+ # Ensure the API key is set via environment variable
10
+ if "FAL_KEY" not in os.environ:
11
+ os.environ["FAL_KEY"] = "YOUR_API_KEY" # Replace with your actual API key
12
+
13
+ def generate_image(prompt):
14
  try:
15
+ # Map default parameters
16
+ image_size = "landscape_4_3"
17
+ num_images = 1
18
+ enable_safety_checker = True
19
+ safety_tolerance = "2"
 
 
 
 
 
 
 
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
  },
30
  )
31
 
32
  # Wait for the result
33
  while True:
34
  status = handler.status()
 
35
  if status["status"] == "completed":
36
  break
37
  elif status["status"] == "failed":
38
+ return "The image generation failed."
39
  time.sleep(1) # Wait for 1 second before checking again
40
 
41
  result = handler.get()
42
+ if not result["images"]:
43
+ return "No images were generated."
 
 
 
 
 
 
 
 
 
 
 
44
 
45
+ image_info = result["images"][0]
46
+ image_url = image_info["url"]
47
 
48
+ # Download the image
49
+ response = requests.get(image_url)
50
+ if response.status_code == 200:
51
+ image = Image.open(BytesIO(response.content))
52
+ return image
53
+ else:
54
+ return "Failed to download the generated image."
55
  except Exception as e:
56
+ return str(e)
 
57
 
58
  # Set up the Gradio interface
59
  iface = gr.Interface(
60
  fn=generate_image,
61
+ inputs=gr.Textbox(label="Prompt", placeholder="Enter your prompt here..."),
62
+ outputs=gr.Image(label="Generated Image"),
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
63
  title="FLUX1.1 [pro] Image Generation",
64
+ description="Generate an image using the FLUX1.1 [pro] model by providing a text prompt."
65
  )
66
 
67
  iface.launch()