File size: 3,067 Bytes
4f14c30
 
 
 
307c665
 
4f14c30
307c665
 
4f14c30
307c665
 
 
 
 
 
 
 
 
 
 
 
 
1c9e563
307c665
 
 
4f14c30
307c665
4f14c30
 
1c9e563
307c665
 
4f14c30
 
307c665
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4f14c30
 
307c665
 
 
 
 
4f14c30
307c665
4f14c30
307c665
 
 
 
 
 
1c9e563
4f14c30
 
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
import gradio as gr
import os
import uuid
from PIL import Image
from super_image import EdsrModel, ImageLoader
from torchvision.transforms.functional import to_pil_image

# Load pretrained model from Hugging Face
model = EdsrModel.from_pretrained('eugenesiow/edsr-base', scale=2)

# Ensure output directory exists
os.makedirs("upscaled_outputs", exist_ok=True)

def upscale_ai(image: Image.Image, resolution: str):
    # Convert to Super Image format
    scale = 2 if resolution == "2x" else 4
    model.config.scale = scale
    inputs = ImageLoader.load_image(image).unsqueeze(0)  # (1, 3, H, W)

    with torch.no_grad():
        preds = model(inputs)

    upscaled = to_pil_image(preds.squeeze(0).clamp(0, 1))
    filename = f"upscaled_{uuid.uuid4().hex[:6]}.png"
    path = os.path.join("upscaled_outputs", filename)
    upscaled.save(path)
    return image, upscaled  # Return both original and result

def process_images(images, resolution):
    results = []
    for img in images:
        image = Image.open(img).convert("RGB")
        before, after = upscale_ai(image, resolution)
        results.append([before, after])  # Side-by-side
    return results

# ------------------- UI Code with Styling -------------------

custom_theme = gr.themes.Base(
    primary_hue="blue",
    secondary_hue="fuchsia",
    neutral_hue="gray",
    spacing_size="sm",
    font=["ui-sans-serif", "Segoe UI", "sans-serif"]
).set(
    body_background_fill="#f3f4f6",
    body_text_color="#1f2937",
    button_primary_background_fill="#4f46e5",
    button_primary_text_color="white",
    button_primary_background_fill_hover="#4338ca",
    input_background_fill="#ffffff",
    input_border_color="#d1d5db",
    block_title_text_color="#1f2937"
)

with gr.Blocks(title="Ultra Pixel AI Image Upscaler", theme=custom_theme, css=".gr-button {font-size: 16px;}") as app:
    gr.Markdown(
        """
        <div style='text-align: center; padding: 1.5rem; color:#1f2937;'>
            <h1 style="font-size: 2.2rem; margin-bottom: 0.2em;">🧠 Ultra Pixel AI Image Upscaler</h1>
            <p style="font-size: 1.1rem;">
                Powerful deep learning model with side-by-side comparison.<br>
                Works beautifully across mobile, desktop, and tablets ✨
            </p>
        </div>
        """
    )

    with gr.Row():
        file_input = gr.File(label="πŸ“ Upload Images", file_types=["image"], file_count="multiple", interactive=True)
        resolution = gr.Dropdown(["2x", "4x"], value="2x", label="Choose Resolution", interactive=True)

    upscale_btn = gr.Button("πŸ”₯ Start AI Upscaling")
    output_comparison = gr.Gallery(label="πŸ” Before vs After", columns=2, show_label=False)

    upscale_btn.click(fn=process_images, inputs=[file_input, resolution], outputs=output_comparison)

    gr.Markdown(
        """
        <div style='text-align:center; margin-top: 2rem; font-size: 0.9rem; color: #6b7280;'>
            <p>Enhanced with ❀️ by Ultra Pixel | Powered by <code>super-image</code></p>
        </div>
        """
    )

app.launch()