-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
179 lines (158 loc) · 8.04 KB
/
app.py
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import streamlit as st
import replicate
import io
import base64
from zipfile import ZipFile
import requests
import asyncio
from PIL import Image
import logging
# Set up logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
# Set page title
st.set_page_config(page_title="Flux Image Generator", layout="wide")
# Title
st.title("Flux Image Generator")
# Select model
model = st.selectbox(
"Select Model",
["black-forest-labs/flux-schnell", "black-forest-labs/flux-dev"],
help="Choose the Flux model to use"
)
# Input prompt
prompt = st.text_area("Enter Prompt", help="Describe the image you want to generate")
# Upload txt file
uploaded_file = st.file_uploader("Upload Prompt File (one prompt per line)", type="txt")
# Set parameters
with st.expander("Advanced Settings"):
seed = st.number_input("Random Seed", min_value=0, help="Set a random seed for reproducible results")
num_outputs = st.slider("Number of Outputs per Prompt", min_value=1, max_value=4, value=1, help="Number of images to generate for each prompt")
aspect_ratio = st.selectbox(
"Aspect Ratio",
["16:9", "1:1", "21:9", "2:3", "3:2", "4:5", "5:4", "9:16", "9:21"],
index=0,
help="Aspect ratio of the generated images"
)
output_format = st.selectbox("Output Format", ["png", "webp", "jpg"], index=0, help="Format of the output images")
output_quality = st.slider("Output Quality", min_value=0, max_value=100, value=100, help="Quality of the output images, from 0 to 100. 100 is best quality, 0 is worst. Not relevant for .png output")
disable_safety_checker = st.checkbox("Disable Safety Checker", help="Disable the safety checker for generated images. This feature is only available through the API.")
async def generate_image_async(prompt, model, input_data, timeout=45):
try:
prediction = await replicate.predictions.async_create(
model=model,
input=input_data
)
start_time = asyncio.get_event_loop().time()
while prediction.status != "succeeded":
if asyncio.get_event_loop().time() - start_time > timeout:
raise asyncio.TimeoutError(f"Image generation timed out ({timeout} seconds)")
await asyncio.sleep(1)
prediction = await replicate.predictions.async_get(prediction.id)
logger.info(f"API response: {prediction.output}")
logger.info(f"Full API response: {prediction}")
if prediction.output:
if isinstance(prediction.output, list):
return prediction.output[0] if prediction.output else None
elif isinstance(prediction.output, str):
return prediction.output
else:
logger.error(f"Unknown API response format: {type(prediction.output)}")
return None
else:
logger.error("API response is empty")
return None
except asyncio.TimeoutError as e:
logger.error(str(e))
return None
except Exception as e:
logger.error(f"Error generating image: {str(e)}")
return None
def get_image_download_link(img_url, filename):
response = requests.get(img_url)
response.raise_for_status()
img_data = response.content
b64 = base64.b64encode(img_data).decode()
file_size = len(img_data)
logger.info(f"Downloaded image size: {file_size} bytes")
return f'<a href="data:image/png;base64,{b64}" download="{filename}">Download Image ({file_size/1024:.2f} KB)</a>'
# Generate button
if st.button("Generate Images"):
prompts = []
if uploaded_file:
prompts = [line.decode("utf-8").strip() for line in uploaded_file]
elif prompt:
prompts = [prompt]
if not prompts:
st.error("Please enter a prompt or upload a prompt file")
else:
with st.spinner(f"Generating {len(prompts) * num_outputs} images..."):
async def generate_all_images():
tasks = []
for current_prompt in prompts:
for _ in range(num_outputs):
input_data = {
"prompt": current_prompt,
"seed": seed if seed else None,
"aspect_ratio": aspect_ratio,
"output_format": output_format,
"output_quality": output_quality,
"disable_safety_checker": disable_safety_checker
}
input_data = {k: v for k, v in input_data.items() if v is not None}
tasks.append(generate_image_async(current_prompt, model, input_data, timeout=45))
return await asyncio.gather(*tasks)
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
generated_images = loop.run_until_complete(generate_all_images())
# Display generated images
for i, image_url in enumerate(generated_images):
if image_url:
logger.info(f"Processing image URL: {image_url}")
col1, col2 = st.columns([3, 1])
with col1:
try:
if not image_url.startswith(('http://', 'https://')):
raise ValueError(f"Invalid URL: {image_url}")
response = requests.get(image_url)
response.raise_for_status()
image = Image.open(io.BytesIO(response.content))
logger.info(f"Image {i+1} dimensions: {image.size}")
st.image(image, caption=f"Generated Image {i+1}", use_column_width=True)
logger.info(f"Successfully displayed image {i+1}")
except requests.RequestException as e:
logger.error(f"Error downloading image: {str(e)}")
st.error(f"Unable to load image {i+1}: {str(e)}")
except ValueError as e:
logger.error(str(e))
st.error(f"Invalid image URL {i+1}: {str(e)}")
except Exception as e:
logger.error(f"Unknown error processing image: {str(e)}")
st.error(f"Error processing image {i+1}: {str(e)}")
with col2:
st.markdown(get_image_download_link(image_url, f"generated_image_{i+1}.{output_format}"), unsafe_allow_html=True)
st.text_area("Prompt", prompts[i // num_outputs], height=100)
else:
logger.warning(f"URL for image {i+1} is empty")
# Batch download
if len(generated_images) > 1:
zip_buffer = io.BytesIO()
with ZipFile(zip_buffer, 'w') as zip_file:
for i, image_url in enumerate(generated_images):
if image_url:
response = requests.get(image_url)
zip_file.writestr(f"generated_image_{i+1}.{output_format}", response.content)
zip_buffer.seek(0)
b64 = base64.b64encode(zip_buffer.getvalue()).decode()
href = f'<a href="data:application/zip;base64,{b64}" download="generated_images.zip">Download All Images</a>'
st.markdown(href, unsafe_allow_html=True)
# Add instructions
st.markdown("""
## Instructions
- Select the Flux model you want to use.
- Enter a prompt describing the image you want to generate in the text box, or upload a txt file with multiple prompts (one per line).
- If needed, expand "Advanced Settings" to adjust other parameters.
- Click the "Generate Images" button to start the generation process.
- Generated images will be displayed on the page, and you can download each image individually or all images in batch.
Note: Make sure you have set the REPLICATE_API_TOKEN environment variable, otherwise the API call will fail.
""")