Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -5,7 +5,8 @@ import torch
|
|
5 |
import random
|
6 |
import os
|
7 |
import tempfile
|
8 |
-
from PIL import Image
|
|
|
9 |
|
10 |
# Import the pipeline from diffusers
|
11 |
from diffusers import FluxKontextPipeline
|
@@ -20,6 +21,9 @@ def load_model():
|
|
20 |
"""Load the model on CPU first, then move to GPU when needed"""
|
21 |
global pipe
|
22 |
if pipe is None:
|
|
|
|
|
|
|
23 |
# Get token from environment variable
|
24 |
hf_token = os.getenv("HF_TOKEN")
|
25 |
if hf_token:
|
@@ -56,7 +60,17 @@ def chat_fn(message, chat_history, seed, randomize_seed, guidance_scale, steps,
|
|
56 |
input_image = None
|
57 |
if files:
|
58 |
print(f"Received image: {files[0]}")
|
59 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
60 |
image = pipe(
|
61 |
image=input_image,
|
62 |
prompt=prompt,
|
|
|
5 |
import random
|
6 |
import os
|
7 |
import tempfile
|
8 |
+
from PIL import Image, ImageOps
|
9 |
+
import pillow_heif # For HEIF/AVIF support
|
10 |
|
11 |
# Import the pipeline from diffusers
|
12 |
from diffusers import FluxKontextPipeline
|
|
|
21 |
"""Load the model on CPU first, then move to GPU when needed"""
|
22 |
global pipe
|
23 |
if pipe is None:
|
24 |
+
# Register HEIF opener with PIL for AVIF/HEIF support
|
25 |
+
pillow_heif.register_heif_opener()
|
26 |
+
|
27 |
# Get token from environment variable
|
28 |
hf_token = os.getenv("HF_TOKEN")
|
29 |
if hf_token:
|
|
|
60 |
input_image = None
|
61 |
if files:
|
62 |
print(f"Received image: {files[0]}")
|
63 |
+
try:
|
64 |
+
# Try to open and convert the image
|
65 |
+
input_image = Image.open(files[0])
|
66 |
+
# Convert to RGB if needed (handles RGBA, P, etc.)
|
67 |
+
if input_image.mode != "RGB":
|
68 |
+
input_image = input_image.convert("RGB")
|
69 |
+
# Auto-orient the image based on EXIF data
|
70 |
+
input_image = ImageOps.exif_transpose(input_image)
|
71 |
+
except Exception as e:
|
72 |
+
raise gr.Error(f"Could not process the uploaded image: {str(e)}. Please try uploading a different image format (JPEG, PNG, WebP).")
|
73 |
+
|
74 |
image = pipe(
|
75 |
image=input_image,
|
76 |
prompt=prompt,
|