Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -53,15 +53,15 @@ STYLES = {
|
|
| 53 |
'prompt': 'fix the jpeg compression',
|
| 54 |
'lora_url': 'https://huggingface.co/fofr/flux-kontext-dev-jpeg-compression-fix-lora/resolve/main/flux-kontext-dev-jpeg-compression-fix-lora.safetensors'
|
| 55 |
},
|
| 56 |
-
|
| 57 |
'prompt': 'render this image into a gfx image, fill in the background perfect and edit the image to look like awesome graphic gfx image',
|
| 58 |
'lora_url': 'https://huggingface.co/jerrrycans/gfx/resolve/main/flux-kontext-gfx-lora.safetensors'
|
| 59 |
},
|
| 60 |
-
|
| 61 |
'prompt': 'remove all the watermarks from this image, all watermarks that are over this image',
|
| 62 |
'lora_url': 'https://huggingface.co/jerrrycans/watermark20000/resolve/main/flux-kontext-watermark20000-lora.safetensors'
|
| 63 |
},
|
| 64 |
-
|
| 65 |
'prompt': 'render this image into a gfx image, fill in the background perfect and edit the image to look like awesome graphic gfx image',
|
| 66 |
'lora_url': 'https://huggingface.co/jerrrycans/gfx20000/resolve/main/flux-kontext-gfx20000-lora.safetensors'
|
| 67 |
},
|
|
@@ -86,11 +86,9 @@ STYLES = {
|
|
| 86 |
def upload_base64_image(base64_data):
|
| 87 |
"""Upload base64 image to jerrrycans-file.hf.space"""
|
| 88 |
try:
|
| 89 |
-
# Convert base64 to bytes
|
| 90 |
header, data = base64_data.split(',', 1)
|
| 91 |
image_data = base64.b64decode(data)
|
| 92 |
|
| 93 |
-
# Upload to jerrrycans
|
| 94 |
files = {'file': ('generated_image.png', io.BytesIO(image_data), 'image/png')}
|
| 95 |
headers = {
|
| 96 |
'Origin': 'https://jerrrycans-file.hf.space',
|
|
@@ -108,19 +106,20 @@ def upload_base64_image(base64_data):
|
|
| 108 |
print(f"Upload error: {e}")
|
| 109 |
return None
|
| 110 |
|
| 111 |
-
def get_style_config(style, custom_lora_url=None):
|
| 112 |
-
"""Get style configuration"""
|
| 113 |
style_config = STYLES.get(style.lower())
|
| 114 |
|
| 115 |
if style_config:
|
|
|
|
| 116 |
return {
|
| 117 |
-
'prompt': style_config['prompt'],
|
| 118 |
'lora_url': custom_lora_url or style_config['lora_url']
|
| 119 |
}
|
| 120 |
else:
|
| 121 |
-
#
|
| 122 |
return {
|
| 123 |
-
'prompt': style,
|
| 124 |
'lora_url': custom_lora_url
|
| 125 |
}
|
| 126 |
|
|
@@ -133,7 +132,7 @@ def process_stream_response(response):
|
|
| 133 |
if chunk:
|
| 134 |
buffer += chunk
|
| 135 |
lines = buffer.split('\n')
|
| 136 |
-
buffer = lines.pop()
|
| 137 |
|
| 138 |
for line in lines:
|
| 139 |
if line.startswith('data: ') and len(line) > 6:
|
|
@@ -144,7 +143,6 @@ def process_stream_response(response):
|
|
| 144 |
|
| 145 |
data = json.loads(data_content)
|
| 146 |
|
| 147 |
-
# Check for progress or completed data with images
|
| 148 |
if (data.get('json', {}).get('type') in ['progress', 'completed'] and
|
| 149 |
data.get('json', {}).get('data', {}).get('images')):
|
| 150 |
|
|
@@ -161,32 +159,27 @@ def process_stream_response(response):
|
|
| 161 |
@app.route('/api/transform', methods=['POST'])
|
| 162 |
def transform_image():
|
| 163 |
try:
|
| 164 |
-
# Get JSON data from request
|
| 165 |
data = request.get_json()
|
| 166 |
|
| 167 |
if not data:
|
| 168 |
return jsonify({'success': False, 'error': 'No JSON data provided'}), 400
|
| 169 |
|
| 170 |
-
# Extract required fields
|
| 171 |
image_url = data.get('image_url')
|
| 172 |
style = data.get('style')
|
|
|
|
| 173 |
custom_lora_url = data.get('lora_url')
|
| 174 |
|
| 175 |
-
# Validate required fields
|
| 176 |
if not image_url:
|
| 177 |
return jsonify({'success': False, 'error': 'image_url is required'}), 400
|
| 178 |
|
| 179 |
if not style:
|
| 180 |
return jsonify({'success': False, 'error': 'style is required'}), 400
|
| 181 |
|
| 182 |
-
# Validate image URL
|
| 183 |
if not image_url.startswith(('http://', 'https://')):
|
| 184 |
return jsonify({'success': False, 'error': 'Invalid image URL'}), 400
|
| 185 |
|
| 186 |
-
|
| 187 |
-
style_config = get_style_config(style, custom_lora_url)
|
| 188 |
|
| 189 |
-
# Prepare generation parameters
|
| 190 |
generate_params = {
|
| 191 |
'json': {
|
| 192 |
'imageUrl': image_url,
|
|
@@ -197,20 +190,19 @@ def transform_image():
|
|
| 197 |
if style_config['lora_url']:
|
| 198 |
generate_params['json']['loraUrl'] = style_config['lora_url']
|
| 199 |
|
| 200 |
-
# Make request to FAL API
|
| 201 |
generate_url = f"https://fal-kontext-demo.vercel.app/api/trpc/generateImageStream?input={requests.utils.quote(json.dumps(generate_params))}"
|
| 202 |
|
| 203 |
response = requests.get(generate_url, stream=True)
|
| 204 |
if response.status_code != 200:
|
| 205 |
return jsonify({'success': False, 'error': 'Failed to generate styled image'}), 500
|
| 206 |
|
| 207 |
-
# Process streaming response
|
| 208 |
uploaded_images = process_stream_response(response)
|
| 209 |
|
| 210 |
return jsonify({
|
| 211 |
'success': True,
|
| 212 |
'originalImage': image_url,
|
| 213 |
'style': style,
|
|
|
|
| 214 |
'generatedImages': uploaded_images,
|
| 215 |
'count': len(uploaded_images)
|
| 216 |
})
|
|
@@ -222,7 +214,8 @@ def transform_image():
|
|
| 222 |
def index():
|
| 223 |
return jsonify({
|
| 224 |
'message': 'Image Style Transfer API',
|
| 225 |
-
'usage': 'POST /api/transform with JSON body: {"image_url": "...", "style": "...", "lora_url": "
|
|
|
|
| 226 |
'styles': list(STYLES.keys())
|
| 227 |
})
|
| 228 |
|
|
|
|
| 53 |
'prompt': 'fix the jpeg compression',
|
| 54 |
'lora_url': 'https://huggingface.co/fofr/flux-kontext-dev-jpeg-compression-fix-lora/resolve/main/flux-kontext-dev-jpeg-compression-fix-lora.safetensors'
|
| 55 |
},
|
| 56 |
+
'gfx': {
|
| 57 |
'prompt': 'render this image into a gfx image, fill in the background perfect and edit the image to look like awesome graphic gfx image',
|
| 58 |
'lora_url': 'https://huggingface.co/jerrrycans/gfx/resolve/main/flux-kontext-gfx-lora.safetensors'
|
| 59 |
},
|
| 60 |
+
'watermarkremover': {
|
| 61 |
'prompt': 'remove all the watermarks from this image, all watermarks that are over this image',
|
| 62 |
'lora_url': 'https://huggingface.co/jerrrycans/watermark20000/resolve/main/flux-kontext-watermark20000-lora.safetensors'
|
| 63 |
},
|
| 64 |
+
'gfxultra': {
|
| 65 |
'prompt': 'render this image into a gfx image, fill in the background perfect and edit the image to look like awesome graphic gfx image',
|
| 66 |
'lora_url': 'https://huggingface.co/jerrrycans/gfx20000/resolve/main/flux-kontext-gfx20000-lora.safetensors'
|
| 67 |
},
|
|
|
|
| 86 |
def upload_base64_image(base64_data):
|
| 87 |
"""Upload base64 image to jerrrycans-file.hf.space"""
|
| 88 |
try:
|
|
|
|
| 89 |
header, data = base64_data.split(',', 1)
|
| 90 |
image_data = base64.b64decode(data)
|
| 91 |
|
|
|
|
| 92 |
files = {'file': ('generated_image.png', io.BytesIO(image_data), 'image/png')}
|
| 93 |
headers = {
|
| 94 |
'Origin': 'https://jerrrycans-file.hf.space',
|
|
|
|
| 106 |
print(f"Upload error: {e}")
|
| 107 |
return None
|
| 108 |
|
| 109 |
+
def get_style_config(style, custom_prompt=None, custom_lora_url=None):
|
| 110 |
+
"""Get style configuration, allowing custom prompt to override style prompt."""
|
| 111 |
style_config = STYLES.get(style.lower())
|
| 112 |
|
| 113 |
if style_config:
|
| 114 |
+
# Style found: use its LoRA, but allow prompt to be overridden
|
| 115 |
return {
|
| 116 |
+
'prompt': custom_prompt or style_config['prompt'],
|
| 117 |
'lora_url': custom_lora_url or style_config['lora_url']
|
| 118 |
}
|
| 119 |
else:
|
| 120 |
+
# No style found: treat 'style' as the prompt (legacy behavior)
|
| 121 |
return {
|
| 122 |
+
'prompt': custom_prompt or style,
|
| 123 |
'lora_url': custom_lora_url
|
| 124 |
}
|
| 125 |
|
|
|
|
| 132 |
if chunk:
|
| 133 |
buffer += chunk
|
| 134 |
lines = buffer.split('\n')
|
| 135 |
+
buffer = lines.pop()
|
| 136 |
|
| 137 |
for line in lines:
|
| 138 |
if line.startswith('data: ') and len(line) > 6:
|
|
|
|
| 143 |
|
| 144 |
data = json.loads(data_content)
|
| 145 |
|
|
|
|
| 146 |
if (data.get('json', {}).get('type') in ['progress', 'completed'] and
|
| 147 |
data.get('json', {}).get('data', {}).get('images')):
|
| 148 |
|
|
|
|
| 159 |
@app.route('/api/transform', methods=['POST'])
|
| 160 |
def transform_image():
|
| 161 |
try:
|
|
|
|
| 162 |
data = request.get_json()
|
| 163 |
|
| 164 |
if not data:
|
| 165 |
return jsonify({'success': False, 'error': 'No JSON data provided'}), 400
|
| 166 |
|
|
|
|
| 167 |
image_url = data.get('image_url')
|
| 168 |
style = data.get('style')
|
| 169 |
+
custom_prompt = data.get('custom_prompt') # New field
|
| 170 |
custom_lora_url = data.get('lora_url')
|
| 171 |
|
|
|
|
| 172 |
if not image_url:
|
| 173 |
return jsonify({'success': False, 'error': 'image_url is required'}), 400
|
| 174 |
|
| 175 |
if not style:
|
| 176 |
return jsonify({'success': False, 'error': 'style is required'}), 400
|
| 177 |
|
|
|
|
| 178 |
if not image_url.startswith(('http://', 'https://')):
|
| 179 |
return jsonify({'success': False, 'error': 'Invalid image URL'}), 400
|
| 180 |
|
| 181 |
+
style_config = get_style_config(style, custom_prompt, custom_lora_url)
|
|
|
|
| 182 |
|
|
|
|
| 183 |
generate_params = {
|
| 184 |
'json': {
|
| 185 |
'imageUrl': image_url,
|
|
|
|
| 190 |
if style_config['lora_url']:
|
| 191 |
generate_params['json']['loraUrl'] = style_config['lora_url']
|
| 192 |
|
|
|
|
| 193 |
generate_url = f"https://fal-kontext-demo.vercel.app/api/trpc/generateImageStream?input={requests.utils.quote(json.dumps(generate_params))}"
|
| 194 |
|
| 195 |
response = requests.get(generate_url, stream=True)
|
| 196 |
if response.status_code != 200:
|
| 197 |
return jsonify({'success': False, 'error': 'Failed to generate styled image'}), 500
|
| 198 |
|
|
|
|
| 199 |
uploaded_images = process_stream_response(response)
|
| 200 |
|
| 201 |
return jsonify({
|
| 202 |
'success': True,
|
| 203 |
'originalImage': image_url,
|
| 204 |
'style': style,
|
| 205 |
+
'promptUsed': style_config['prompt'],
|
| 206 |
'generatedImages': uploaded_images,
|
| 207 |
'count': len(uploaded_images)
|
| 208 |
})
|
|
|
|
| 214 |
def index():
|
| 215 |
return jsonify({
|
| 216 |
'message': 'Image Style Transfer API',
|
| 217 |
+
'usage': 'POST /api/transform with JSON body: {"image_url": "...", "style": "...", "custom_prompt": "(optional)", "lora_url": "(optional)"}',
|
| 218 |
+
'note': 'If "custom_prompt" is provided, it will override the default prompt for the selected "style", while still using the style\'s LoRA.',
|
| 219 |
'styles': list(STYLES.keys())
|
| 220 |
})
|
| 221 |
|