File size: 3,363 Bytes
ad30d11
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
042efb0
ad30d11
 
 
042efb0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ad30d11
 
 
 
 
 
 
 
042efb0
ad30d11
 
 
 
3b40c15
ad30d11
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import os
import requests
import time
import gradio as gr
from PIL import Image
from io import BytesIO

def generate_image(prompt, width, height, api_key):
    headers = {
        'accept': 'application/json',
        'x-key': api_key,
        'Content-Type': 'application/json',
    }
    json_data = {
        'prompt': prompt,
        'width': int(width),
        'height': int(height),
    }
    # Create image generation request
    response = requests.post(
        'https://api.bfl.ml/v1/flux-pro-1.1',
        headers=headers,
        json=json_data,
    )
    if response.status_code != 200:
        return f"Error: {response.status_code} {response.text}"
    request = response.json()
    request_id = request.get("id")
    if not request_id:
        return "Failed to get request ID."

    # Poll for result
    while True:
        time.sleep(0.5)
        result_response = requests.get(
            'https://api.bfl.ml/v1/get_result',
            headers={
                'accept': 'application/json',
                'x-key': api_key,
            },
            params={
                'id': request_id,
            },
        )
        if result_response.status_code != 200:
            return f"Error: {result_response.status_code} {result_response.text}"
        result = result_response.json()
        status = result.get("status")
        if status == "Ready":
            image_url = result['result'].get('sample')
            if not image_url:
                return "No image URL found in the result."
            # Fetch the image from the URL
            image_response = requests.get(image_url)
            if image_response.status_code != 200:
                return f"Failed to fetch image: {image_response.status_code}"
            image = Image.open(BytesIO(image_response.content))
            return image
        elif status == "Failed":
            return "Image generation failed."
        elif status == "Pending":
            pass  # Continue polling
        else:
            return f"Unexpected status: {status}"

# Create Gradio interface with sliders for width and height
iface = gr.Interface(
    fn=generate_image,
    inputs=[
        gr.Textbox(
            label="Prompt",
            placeholder="Describe the image you want to generate",
            lines=3,
        ),
        gr.Slider(
            label="Width",
            minimum=256,
            maximum=1440,
            step=32,
            value=1024,
        ),
        gr.Slider(
            label="Height",
            minimum=256,
            maximum=1440,
            step=32,
            value=768,
        ),
        gr.Textbox(
            label="API Key",
            type="password",
            placeholder="Enter your BFL API key",
        ),
    ],
    outputs="image",
    title="BFL Image Generation Demo",
    description="""
    Generate images using the BFL FLUX 1.1 [pro] model.

    **Instructions:**
    1. Enter a descriptive prompt for the image you want to generate.
    2. Use the sliders to specify the width and height (in pixels) for the image. Values must be multiples of 32.
    3. Enter your BFL API key. You can obtain one from [BFL API Portal](https://api.bfl.ml).
    4. Click "Submit" to generate the image.

    **Note:** Ensure you have sufficient credits and adhere to the API usage limits.
    """
)

iface.launch()