Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,90 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import requests
|
3 |
+
import time
|
4 |
+
import gradio as gr
|
5 |
+
from PIL import Image
|
6 |
+
from io import BytesIO
|
7 |
+
|
8 |
+
def generate_image(prompt, width, height, api_key):
|
9 |
+
headers = {
|
10 |
+
'accept': 'application/json',
|
11 |
+
'x-key': api_key,
|
12 |
+
'Content-Type': 'application/json',
|
13 |
+
}
|
14 |
+
json_data = {
|
15 |
+
'prompt': prompt,
|
16 |
+
'width': int(width),
|
17 |
+
'height': int(height),
|
18 |
+
}
|
19 |
+
# Create image generation request
|
20 |
+
response = requests.post(
|
21 |
+
'https://api.bfl.ml/v1/flux-pro-1.1',
|
22 |
+
headers=headers,
|
23 |
+
json=json_data,
|
24 |
+
)
|
25 |
+
if response.status_code != 200:
|
26 |
+
return f"Error: {response.status_code} {response.text}"
|
27 |
+
request = response.json()
|
28 |
+
request_id = request.get("id")
|
29 |
+
if not request_id:
|
30 |
+
return "Failed to get request ID."
|
31 |
+
|
32 |
+
# Poll for result
|
33 |
+
while True:
|
34 |
+
time.sleep(0.5)
|
35 |
+
result_response = requests.get(
|
36 |
+
'https://api.bfl.ml/v1/get_result',
|
37 |
+
headers={
|
38 |
+
'accept': 'application/json',
|
39 |
+
'x-key': api_key,
|
40 |
+
},
|
41 |
+
params={
|
42 |
+
'id': request_id,
|
43 |
+
},
|
44 |
+
)
|
45 |
+
if result_response.status_code != 200:
|
46 |
+
return f"Error: {result_response.status_code} {result_response.text}"
|
47 |
+
result = result_response.json()
|
48 |
+
status = result.get("status")
|
49 |
+
if status == "Ready":
|
50 |
+
image_url = result['result'].get('sample')
|
51 |
+
if not image_url:
|
52 |
+
return "No image URL found in the result."
|
53 |
+
# Fetch the image from the URL
|
54 |
+
image_response = requests.get(image_url)
|
55 |
+
if image_response.status_code != 200:
|
56 |
+
return f"Failed to fetch image: {image_response.status_code}"
|
57 |
+
image = Image.open(BytesIO(image_response.content))
|
58 |
+
return image
|
59 |
+
elif status == "Failed":
|
60 |
+
return "Image generation failed."
|
61 |
+
elif status == "Pending":
|
62 |
+
pass # Continue polling
|
63 |
+
else:
|
64 |
+
return f"Unexpected status: {status}"
|
65 |
+
|
66 |
+
# Create Gradio interface
|
67 |
+
iface = gr.Interface(
|
68 |
+
fn=generate_image,
|
69 |
+
inputs=[
|
70 |
+
gr.Textbox(label="Prompt", placeholder="Describe the image you want to generate"),
|
71 |
+
gr.Number(label="Width", value=512),
|
72 |
+
gr.Number(label="Height", value=512),
|
73 |
+
gr.Textbox(label="API Key", type="password", placeholder="Enter your BFL API key"),
|
74 |
+
],
|
75 |
+
outputs="image",
|
76 |
+
title="BFL Image Generation Demo",
|
77 |
+
description="""
|
78 |
+
Generate images using the BFL FLUX 1.1 [pro] model.
|
79 |
+
|
80 |
+
**Instructions:**
|
81 |
+
1. Enter a descriptive prompt for the image you want to generate.
|
82 |
+
2. Specify the width and height (in pixels) for the image.
|
83 |
+
3. Enter your BFL API key. You can obtain one from [BFL API Portal](https://api.bfl.ml).
|
84 |
+
4. Click "Submit" to generate the image.
|
85 |
+
|
86 |
+
**Note:** Ensure you have sufficient credits and adhere to the API usage limits.
|
87 |
+
""",
|
88 |
+
)
|
89 |
+
|
90 |
+
iface.launch()
|