-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
211 lines (177 loc) · 7.19 KB
/
app.py
File metadata and controls
211 lines (177 loc) · 7.19 KB
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
import os
from flask import Flask, request, jsonify, render_template, send_file, send_from_directory
from flask_cors import CORS
import replicate
import requests
from io import BytesIO
import uuid
from dotenv import load_dotenv
import json
import time
import asyncio
import concurrent.futures
import glob
from datetime import datetime
load_dotenv()
# Check if API token is set
if not os.getenv('REPLICATE_API_TOKEN'):
raise ValueError("REPLICATE_API_TOKEN is not set in .env file")
app = Flask(__name__)
CORS(app)
# Create a temporary directory for images if it doesn't exist
TEMP_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), "temp_images")
if not os.path.exists(TEMP_DIR):
os.makedirs(TEMP_DIR)
def generate_single_image(prompt, index):
"""Generate a single image using the model with a unique seed"""
try:
print(f"Starting image generation {index + 1} with prompt: {prompt}")
# Generate a different seed for each image based on the index
seed = 42 + index * 1000 # This ensures each image gets a different seed
model_input = {
"prompt": prompt,
"width": 1024,
"height": 683,
"guidance_scale": 7.5,
"negative_prompt": "",
"seed": seed # Use the unique seed
}
print(f"Creating prediction with model input: {model_input}")
prediction = replicate.run(
"black-forest-labs/flux-1.1-pro-ultra",
input=model_input
)
print(f"Prediction {index + 1} succeeded. Output: {prediction}")
return prediction
except Exception as e:
print(f"Error generating single image: {str(e)}")
raise e
@app.route('/')
def home():
return render_template('index.html')
@app.route('/gallery')
def gallery():
return render_template('gallery.html')
@app.route('/generate', methods=['POST'])
def generate_image():
try:
data = request.get_json()
prompt = data.get('prompt')
num_images = int(data.get('num_images', 1))
print(f"Starting image generation request - Prompt: {prompt}, Num Images: {num_images}")
# Generate multiple images in parallel
image_outputs = []
print("Starting parallel image generation...")
with concurrent.futures.ThreadPoolExecutor(max_workers=num_images) as executor:
# Create a list of futures for parallel execution
futures = [executor.submit(generate_single_image, prompt, i) for i in range(num_images)]
# Wait for all futures to complete
for future in concurrent.futures.as_completed(futures):
try:
output = future.result()
print(f"Received output from future: {output}")
if output:
if isinstance(output, str):
image_outputs.append(output)
elif isinstance(output, list):
image_outputs.extend(output)
elif isinstance(output, dict):
if 'images' in output:
image_outputs.extend(output['images'])
else:
image_outputs.append(output.get('image', ''))
except Exception as e:
print(f"Error in future: {str(e)}")
continue
print(f"Generated {len(image_outputs)} images. Image URLs: {image_outputs}")
# Download and save images
image_paths = []
for img_url in image_outputs:
if not img_url: # Skip empty URLs
continue
print(f"Downloading image from: {img_url}")
try:
# Download the image
response = requests.get(img_url)
response.raise_for_status()
# Generate a unique filename
filename = f"{uuid.uuid4()}.png"
filepath = os.path.join(TEMP_DIR, filename)
print(f"Saving image to: {filepath}")
# Save the image
with open(filepath, 'wb') as f:
f.write(response.content)
image_paths.append(filename)
print(f"Successfully saved image as: {filename}")
except requests.exceptions.RequestException as req_error:
print(f"Error downloading image: {str(req_error)}")
continue
if not image_paths:
raise Exception("Failed to generate or save any images")
print(f"Successfully processed {len(image_paths)} images. Returning filenames: {image_paths}")
return jsonify({
'images': image_paths
})
except Exception as e:
import traceback
error_msg = f"Error: {str(e)}\nTraceback: {traceback.format_exc()}"
print(error_msg)
return jsonify({
'error': str(e)
}), 500
@app.route('/api/gallery')
def get_gallery_images():
try:
page = int(request.args.get('page', 1))
per_page = 12
start = (page - 1) * per_page
# Get all image files from the temp directory
image_files = []
for ext in ['*.png', '*.jpg', '*.jpeg']:
image_files.extend(glob.glob(os.path.join(TEMP_DIR, ext)))
# Sort by creation time, newest first
image_files.sort(key=os.path.getctime, reverse=True)
# Paginate results
paginated_files = image_files[start:start + per_page]
images = []
for file in paginated_files:
created_at = datetime.fromtimestamp(os.path.getctime(file))
images.append({
'path': f'/temp_images/{os.path.basename(file)}',
'created_at': created_at.isoformat(),
'prompt': 'AI Generated Image' # You can store and retrieve actual prompts if needed
})
return jsonify({
'images': images,
'total': len(image_files),
'page': page,
'per_page': per_page
})
except Exception as e:
print(f"Error in gallery API: {str(e)}")
return jsonify({'error': str(e)}), 500
@app.route('/get-showcase-images')
def get_showcase_images():
try:
# Get list of all images in temp_images directory
image_files = [f for f in os.listdir(TEMP_DIR) if f.endswith('.png')]
# Sort by creation time, newest first
image_files.sort(key=lambda x: os.path.getctime(os.path.join(TEMP_DIR, x)), reverse=True)
return jsonify({'images': image_files})
except Exception as e:
return jsonify({'error': str(e)}), 500
@app.route('/temp_images/<path:filename>')
def serve_image(filename):
return send_from_directory(TEMP_DIR, filename)
@app.route('/download/<filename>')
def download_image(filename):
try:
return send_file(
os.path.join(TEMP_DIR, filename),
as_attachment=True,
download_name=filename
)
except Exception as e:
return jsonify({'error': str(e)}), 404
if __name__ == '__main__':
app.run(debug=True, port=5013)