Update app.py
Browse files
app.py
CHANGED
|
@@ -1,27 +1,40 @@
|
|
| 1 |
import os
|
| 2 |
import io
|
| 3 |
import zipfile
|
| 4 |
-
import
|
| 5 |
-
import
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
import
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
# Get your HF Token from environment variables
|
| 12 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 13 |
-
client = InferenceClient(token=HF_TOKEN)
|
| 14 |
|
| 15 |
def enhance_lighting(pil_img):
|
| 16 |
"""Fix exposure and contrast using OpenCV CLAHE"""
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
def fix_aspect_ratio(pil_img, ratio_str):
|
| 27 |
"""Center-crop image to selected aspect ratio"""
|
|
@@ -42,36 +55,41 @@ def process_batch(files, ratio, rm_bg, prompt):
|
|
| 42 |
processed = []
|
| 43 |
|
| 44 |
for file in files:
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
# 2. Fix Aspect Ratio
|
| 51 |
-
img = fix_aspect_ratio(img, ratio)
|
| 52 |
-
|
| 53 |
-
# 3. Remove Background (local rembg on HF Space)
|
| 54 |
-
if rm_bg:
|
| 55 |
-
img = remove(img) # Returns RGBA with transparent bg
|
| 56 |
-
|
| 57 |
-
# 4. Generate AI Background (only if both prompt AND bg removal are selected)
|
| 58 |
-
if prompt and rm_bg:
|
| 59 |
-
# Generate background image via HF free inference API
|
| 60 |
-
bg_img = client.text_to_image(
|
| 61 |
-
f"Product photography background of {prompt}, elegant, soft studio light, photorealistic, 8k",
|
| 62 |
-
model="stabilityai/stable-diffusion-2-1"
|
| 63 |
-
)
|
| 64 |
-
# Resize background to match the jewelery size
|
| 65 |
-
bg_img = bg_img.resize(img.size).convert("RGBA")
|
| 66 |
|
| 67 |
-
#
|
| 68 |
-
|
| 69 |
-
final.paste(img, (0, 0), img) # img has alpha channel
|
| 70 |
-
img = final
|
| 71 |
|
| 72 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
|
| 74 |
-
# Save all images into a single ZIP
|
| 75 |
zip_buffer = io.BytesIO()
|
| 76 |
with zipfile.ZipFile(zip_buffer, "w") as zf:
|
| 77 |
for i, img in enumerate(processed):
|
|
@@ -83,7 +101,7 @@ def process_batch(files, ratio, rm_bg, prompt):
|
|
| 83 |
|
| 84 |
# Gradio UI Interface
|
| 85 |
with gr.Blocks(title="Jewelry Batch Processor") as demo:
|
| 86 |
-
gr.Markdown("## ✨ Free Jewelry Batch Processor
|
| 87 |
|
| 88 |
with gr.Row():
|
| 89 |
files = gr.Files(label="Upload Jewelry Images", file_count="multiple")
|
|
@@ -98,4 +116,6 @@ with gr.Blocks(title="Jewelry Batch Processor") as demo:
|
|
| 98 |
|
| 99 |
btn.click(process_batch, inputs=[files, ratio, rm_bg, prompt], outputs=output)
|
| 100 |
|
| 101 |
-
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
import io
|
| 3 |
import zipfile
|
| 4 |
+
import sys
|
| 5 |
+
import traceback
|
| 6 |
+
|
| 7 |
+
# CRITICAL FIX: Catch OpenCV import errors before they crash the app
|
| 8 |
+
try:
|
| 9 |
+
import cv2
|
| 10 |
+
import numpy as np
|
| 11 |
+
from PIL import Image
|
| 12 |
+
from rembg import remove
|
| 13 |
+
from huggingface_hub import InferenceClient
|
| 14 |
+
import gradio as gr
|
| 15 |
+
except Exception as e:
|
| 16 |
+
print("CRITICAL IMPORT ERROR:", e)
|
| 17 |
+
print(traceback.format_exc())
|
| 18 |
+
sys.exit(1)
|
| 19 |
|
| 20 |
# Get your HF Token from environment variables
|
| 21 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 22 |
+
client = InferenceClient(token=HF_TOKEN, model="stabilityai/stable-diffusion-2-1")
|
| 23 |
|
| 24 |
def enhance_lighting(pil_img):
|
| 25 |
"""Fix exposure and contrast using OpenCV CLAHE"""
|
| 26 |
+
try:
|
| 27 |
+
cv_img = cv2.cvtColor(np.array(pil_img), cv2.COLOR_RGB2BGR)
|
| 28 |
+
lab = cv2.cvtColor(cv_img, cv2.COLOR_BGR2LAB)
|
| 29 |
+
l, a, b = cv2.split(lab)
|
| 30 |
+
clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8,8))
|
| 31 |
+
l = clahe.apply(l)
|
| 32 |
+
lab = cv2.merge([l, a, b])
|
| 33 |
+
cv_img = cv2.cvtColor(lab, cv2.COLOR_LAB2BGR)
|
| 34 |
+
return Image.fromarray(cv2.cvtColor(cv_img, cv2.COLOR_BGR2RGB))
|
| 35 |
+
except Exception as e:
|
| 36 |
+
print(f"Lighting fix failed: {e}")
|
| 37 |
+
return pil_img
|
| 38 |
|
| 39 |
def fix_aspect_ratio(pil_img, ratio_str):
|
| 40 |
"""Center-crop image to selected aspect ratio"""
|
|
|
|
| 55 |
processed = []
|
| 56 |
|
| 57 |
for file in files:
|
| 58 |
+
try:
|
| 59 |
+
img = Image.open(file).convert("RGB")
|
| 60 |
+
|
| 61 |
+
# 1. Fix Lighting
|
| 62 |
+
img = enhance_lighting(img)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
|
| 64 |
+
# 2. Fix Aspect Ratio
|
| 65 |
+
img = fix_aspect_ratio(img, ratio)
|
|
|
|
|
|
|
| 66 |
|
| 67 |
+
# 3. Remove Background (local rembg)
|
| 68 |
+
if rm_bg:
|
| 69 |
+
img = remove(img)
|
| 70 |
+
|
| 71 |
+
# 4. Generate AI Background
|
| 72 |
+
if prompt and rm_bg:
|
| 73 |
+
print(f"Generating background for: {prompt}")
|
| 74 |
+
bg_img = client.text_to_image(
|
| 75 |
+
f"Product photography background of {prompt}, elegant, soft studio light, photorealistic, 8k",
|
| 76 |
+
model="stabilityai/stable-diffusion-2-1"
|
| 77 |
+
)
|
| 78 |
+
bg_img = bg_img.resize(img.size).convert("RGBA")
|
| 79 |
+
|
| 80 |
+
final = bg_img.copy()
|
| 81 |
+
final.paste(img, (0, 0), img)
|
| 82 |
+
img = final
|
| 83 |
+
|
| 84 |
+
processed.append(img)
|
| 85 |
+
except Exception as e:
|
| 86 |
+
print(f"Error processing file {file.name}: {e}")
|
| 87 |
+
continue
|
| 88 |
+
|
| 89 |
+
if not processed:
|
| 90 |
+
return None
|
| 91 |
|
| 92 |
+
# Save all images into a single ZIP
|
| 93 |
zip_buffer = io.BytesIO()
|
| 94 |
with zipfile.ZipFile(zip_buffer, "w") as zf:
|
| 95 |
for i, img in enumerate(processed):
|
|
|
|
| 101 |
|
| 102 |
# Gradio UI Interface
|
| 103 |
with gr.Blocks(title="Jewelry Batch Processor") as demo:
|
| 104 |
+
gr.Markdown("## ✨ Free Jewelry Batch Processor")
|
| 105 |
|
| 106 |
with gr.Row():
|
| 107 |
files = gr.Files(label="Upload Jewelry Images", file_count="multiple")
|
|
|
|
| 116 |
|
| 117 |
btn.click(process_batch, inputs=[files, ratio, rm_bg, prompt], outputs=output)
|
| 118 |
|
| 119 |
+
# CRITICAL FIX: This ensures Gradio launches correctly on Spaces
|
| 120 |
+
if __name__ == "__main__":
|
| 121 |
+
demo.launch(server_name="0.0.0.0", server_port=7860)
|