Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,114 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from PIL import Image, ImageEnhance
|
| 3 |
+
import os
|
| 4 |
+
|
| 5 |
+
# --- 1. Processing Logic (Brain) ---
|
| 6 |
+
def process_image(image, split_count, should_enhance, format_type):
|
| 7 |
+
if image is None:
|
| 8 |
+
return None
|
| 9 |
+
|
| 10 |
+
# Step A: Enhancement (Magic Fix)
|
| 11 |
+
if should_enhance:
|
| 12 |
+
enhancer = ImageEnhance.Sharpness(image)
|
| 13 |
+
image = enhancer.enhance(1.5) # Thoda sharp karega
|
| 14 |
+
contrast = ImageEnhance.Contrast(image)
|
| 15 |
+
image = contrast.enhance(1.1) # Colors pop karenge
|
| 16 |
+
|
| 17 |
+
img_width, img_height = image.size
|
| 18 |
+
split_width = img_width // int(split_count)
|
| 19 |
+
output_files = []
|
| 20 |
+
|
| 21 |
+
if not os.path.exists("outputs"):
|
| 22 |
+
os.makedirs("outputs")
|
| 23 |
+
|
| 24 |
+
# Step B: Splitting
|
| 25 |
+
for i in range(int(split_count)):
|
| 26 |
+
left = i * split_width
|
| 27 |
+
right = (i + 1) * split_width
|
| 28 |
+
part = image.crop((left, 0, right, img_height))
|
| 29 |
+
|
| 30 |
+
# Transparency Fix
|
| 31 |
+
if part.mode in ('RGBA', 'LA') or (part.mode == 'P' and 'transparency' in part.info):
|
| 32 |
+
part = part.convert('RGB')
|
| 33 |
+
|
| 34 |
+
# Save as User Choice
|
| 35 |
+
ext = "png" if format_type == "PNG (High Quality)" else "jpg"
|
| 36 |
+
save_path = f"outputs/ts_part_{i+1}.{ext}"
|
| 37 |
+
part.save(save_path, quality=95)
|
| 38 |
+
output_files.append(save_path)
|
| 39 |
+
|
| 40 |
+
return output_files
|
| 41 |
+
|
| 42 |
+
# --- 2. Premium 3D Styling (CSS) ---
|
| 43 |
+
# Ye CSS poora look badal dega (Dark Mode + Glass Effect)
|
| 44 |
+
custom_css = """
|
| 45 |
+
body { background: linear-gradient(135deg, #0f0c29, #302b63, #24243e); }
|
| 46 |
+
.gradio-container { background-color: transparent !important; }
|
| 47 |
+
|
| 48 |
+
/* Main Glass Box */
|
| 49 |
+
#main_group {
|
| 50 |
+
background: rgba(255, 255, 255, 0.05);
|
| 51 |
+
backdrop-filter: blur(10px);
|
| 52 |
+
border: 1px solid rgba(255, 255, 255, 0.1);
|
| 53 |
+
border-radius: 20px;
|
| 54 |
+
padding: 20px;
|
| 55 |
+
box-shadow: 0 8px 32px 0 rgba(0, 0, 0, 0.37);
|
| 56 |
+
}
|
| 57 |
+
|
| 58 |
+
/* TS Logo Styling */
|
| 59 |
+
#logo_text {
|
| 60 |
+
font-family: 'Helvetica', sans-serif;
|
| 61 |
+
background: -webkit-linear-gradient(45deg, #00d2ff, #3a7bd5);
|
| 62 |
+
-webkit-background-clip: text;
|
| 63 |
+
-webkit-text-fill-color: transparent;
|
| 64 |
+
font-size: 40px !important;
|
| 65 |
+
font-weight: 900;
|
| 66 |
+
text-align: center;
|
| 67 |
+
margin-bottom: 0px;
|
| 68 |
+
text-shadow: 0px 4px 10px rgba(0,210,255,0.3);
|
| 69 |
+
}
|
| 70 |
+
|
| 71 |
+
/* 3D Button Effect */
|
| 72 |
+
button.primary-btn {
|
| 73 |
+
background: linear-gradient(90deg, #00d2ff 0%, #3a7bd5 100%) !important;
|
| 74 |
+
border: none !important;
|
| 75 |
+
color: white !important;
|
| 76 |
+
font-weight: bold;
|
| 77 |
+
transition: all 0.3s ease;
|
| 78 |
+
box-shadow: 0px 5px 15px rgba(0, 210, 255, 0.4);
|
| 79 |
+
}
|
| 80 |
+
button.primary-btn:hover {
|
| 81 |
+
transform: translateY(-2px); /* Upar uthega */
|
| 82 |
+
box-shadow: 0px 8px 20px rgba(0, 210, 255, 0.6);
|
| 83 |
+
}
|
| 84 |
+
"""
|
| 85 |
+
|
| 86 |
+
# --- 3. The Interface (UI) ---
|
| 87 |
+
with gr.Blocks(css=custom_css, theme=gr.themes.Soft()) as app:
|
| 88 |
+
|
| 89 |
+
# Header Area
|
| 90 |
+
with gr.Column(elem_id="main_group"):
|
| 91 |
+
gr.Markdown("# TS", elem_id="logo_text") # Sirf TS branding
|
| 92 |
+
gr.Markdown("<h3 style='text-align: center; color: #aaa; margin-top: -10px;'>Next-Gen AI Carousel Splitter</h3>")
|
| 93 |
+
|
| 94 |
+
with gr.Row():
|
| 95 |
+
# Left Side: Controls
|
| 96 |
+
with gr.Column():
|
| 97 |
+
inp_img = gr.Image(type="pil", label="Upload Image", elem_id="upload_box")
|
| 98 |
+
|
| 99 |
+
with gr.Accordion("⚙️ Advanced Settings", open=False):
|
| 100 |
+
enhance_opt = gr.Checkbox(label="Auto-Enhance (Fix Blurry Photos)", value=True)
|
| 101 |
+
fmt_opt = gr.Radio(["JPG (Standard)", "PNG (High Quality)"], label="Output Format", value="JPG (Standard)")
|
| 102 |
+
|
| 103 |
+
slider = gr.Slider(2, 5, value=3, step=1, label="Split Count (Parts)")
|
| 104 |
+
btn = gr.Button("✨ GENERATE CAROUSEL", variant="primary", elem_classes=["primary-btn"])
|
| 105 |
+
|
| 106 |
+
# Right Side: Result
|
| 107 |
+
with gr.Column():
|
| 108 |
+
out_gal = gr.Gallery(label="Ready to Post", columns=3, height=300)
|
| 109 |
+
|
| 110 |
+
# Action
|
| 111 |
+
btn.click(process_image, inputs=[inp_img, slider, enhance_opt, fmt_opt], outputs=out_gal)
|
| 112 |
+
|
| 113 |
+
# --- Launch ---
|
| 114 |
+
app.launch()
|