Spaces:
Running
Running
add app
Browse files
app.py
ADDED
|
@@ -0,0 +1,135 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import requests
|
| 3 |
+
import base64
|
| 4 |
+
import io
|
| 5 |
+
import time
|
| 6 |
+
import os
|
| 7 |
+
from PIL import Image
|
| 8 |
+
|
| 9 |
+
# Get SR API endpoint from environment
|
| 10 |
+
ENHANCE_API = os.getenv("img_enhance_api")
|
| 11 |
+
|
| 12 |
+
def apply_super_resolution(input_image, scale_factor):
|
| 13 |
+
"""Apply super-resolution to input image"""
|
| 14 |
+
if input_image is None:
|
| 15 |
+
return None, "β Please upload an image first", gr.update(visible=False)
|
| 16 |
+
|
| 17 |
+
try:
|
| 18 |
+
# Convert PIL image to bytes
|
| 19 |
+
img_buffer = io.BytesIO()
|
| 20 |
+
input_image.save(img_buffer, format='PNG')
|
| 21 |
+
img_bytes = img_buffer.getvalue()
|
| 22 |
+
|
| 23 |
+
# Call super-resolution API
|
| 24 |
+
response = requests.post(
|
| 25 |
+
f"{ENHANCE_API}/invocations",
|
| 26 |
+
headers={"Content-Type": "application/octet-stream"},
|
| 27 |
+
data=img_bytes,
|
| 28 |
+
params={
|
| 29 |
+
"model_name": "RealESRGAN_x4plus",
|
| 30 |
+
"outscale": scale_factor,
|
| 31 |
+
"tile": 0,
|
| 32 |
+
"fp32": False
|
| 33 |
+
},
|
| 34 |
+
timeout=300,
|
| 35 |
+
verify=False # Disable SSL verification for testing
|
| 36 |
+
)
|
| 37 |
+
|
| 38 |
+
if response.status_code == 200:
|
| 39 |
+
result = response.json()
|
| 40 |
+
# Decode base64 result
|
| 41 |
+
enhanced_data = base64.b64decode(result["prediction"])
|
| 42 |
+
enhanced_image = Image.open(io.BytesIO(enhanced_data))
|
| 43 |
+
|
| 44 |
+
# Create download file
|
| 45 |
+
timestamp = int(time.time())
|
| 46 |
+
download_path = f"enhanced_image_{timestamp}.png"
|
| 47 |
+
|
| 48 |
+
status = f"β
Enhancement successful!\n"
|
| 49 |
+
status += f"Model: {result.get('model', 'RealESRGAN_x4plus')}\n"
|
| 50 |
+
status += f"Scale: {result.get('outscale', 4.0)}x\n"
|
| 51 |
+
status += f"Input: {result.get('input_img_width', 0)}x{result.get('input_img_height', 0)}\n"
|
| 52 |
+
status += f"Output: {result.get('output_img_width', 0)}x{result.get('output_img_height', 0)}"
|
| 53 |
+
|
| 54 |
+
# Return tuple for ImageSlider: [original, enhanced]
|
| 55 |
+
return (input_image, enhanced_image), status, gr.update(visible=True, value=download_path)
|
| 56 |
+
else:
|
| 57 |
+
return None, f"β API Error: {response.status_code}\n{response.text}", gr.update(visible=False)
|
| 58 |
+
|
| 59 |
+
except Exception as e:
|
| 60 |
+
return None, f"β Error: {str(e)}", gr.update(visible=False)
|
| 61 |
+
|
| 62 |
+
def main():
|
| 63 |
+
with gr.Blocks(title="Image Enhancement App") as demo:
|
| 64 |
+
gr.Markdown("# π Image Enhancement App")
|
| 65 |
+
gr.Markdown("Upload an image and enhance it with AI-powered super-resolution")
|
| 66 |
+
|
| 67 |
+
# Row 1: Upload and Controls
|
| 68 |
+
with gr.Row():
|
| 69 |
+
with gr.Column(scale=3):
|
| 70 |
+
input_image = gr.Image(
|
| 71 |
+
label="π€ Upload Image",
|
| 72 |
+
type="pil",
|
| 73 |
+
height=300
|
| 74 |
+
)
|
| 75 |
+
|
| 76 |
+
with gr.Column(scale=1):
|
| 77 |
+
gr.Markdown("### Enhancement Settings")
|
| 78 |
+
scale_dropdown = gr.Dropdown(
|
| 79 |
+
choices=[1, 2, 4],
|
| 80 |
+
value=4,
|
| 81 |
+
label="Scale Factor",
|
| 82 |
+
info="How much to upscale the image"
|
| 83 |
+
)
|
| 84 |
+
|
| 85 |
+
enhance_button = gr.Button(
|
| 86 |
+
"β¨ Enhance Image",
|
| 87 |
+
variant="primary",
|
| 88 |
+
size="lg"
|
| 89 |
+
)
|
| 90 |
+
|
| 91 |
+
status_text = gr.Textbox(
|
| 92 |
+
label="Status",
|
| 93 |
+
lines=5,
|
| 94 |
+
value="Ready to enhance images!",
|
| 95 |
+
interactive=False
|
| 96 |
+
)
|
| 97 |
+
|
| 98 |
+
# Row 2: Before/After Comparison with Image Slider
|
| 99 |
+
with gr.Row():
|
| 100 |
+
gr.Markdown("### π Before vs After Comparison")
|
| 101 |
+
|
| 102 |
+
with gr.Row():
|
| 103 |
+
image_slider = gr.ImageSlider(
|
| 104 |
+
label="Original vs Enhanced",
|
| 105 |
+
height=500,
|
| 106 |
+
interactive=False
|
| 107 |
+
)
|
| 108 |
+
|
| 109 |
+
# Download button
|
| 110 |
+
with gr.Row():
|
| 111 |
+
download_button = gr.DownloadButton(
|
| 112 |
+
"π₯ Download Enhanced Image",
|
| 113 |
+
visible=False,
|
| 114 |
+
size="lg"
|
| 115 |
+
)
|
| 116 |
+
|
| 117 |
+
# Event handlers
|
| 118 |
+
enhance_button.click(
|
| 119 |
+
fn=apply_super_resolution,
|
| 120 |
+
inputs=[input_image, scale_dropdown],
|
| 121 |
+
outputs=[image_slider, status_text, download_button],
|
| 122 |
+
show_progress=True
|
| 123 |
+
)
|
| 124 |
+
|
| 125 |
+
# Clear results when new image is uploaded
|
| 126 |
+
input_image.change(
|
| 127 |
+
fn=lambda: (None, "Image uploaded! Ready to enhance.", gr.update(visible=False)),
|
| 128 |
+
outputs=[image_slider, status_text, download_button]
|
| 129 |
+
)
|
| 130 |
+
|
| 131 |
+
# Launch the app
|
| 132 |
+
demo.queue(default_concurrency_limit=3, max_size=10).launch()
|
| 133 |
+
|
| 134 |
+
if __name__ == "__main__":
|
| 135 |
+
main()
|