akhaliq HF staff commited on
Commit
874da76
1 Parent(s): 5d29ce0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -62
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 base64
8
 
9
- def generate_image(api_key, prompt):
10
  try:
11
- # Set the API key provided by the user
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
- # 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(
70
- fn=generate_image,
71
- inputs=[
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
- )
82
 
83
- iface.launch()
 
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()