Spaces:
Build error
Build error
Shivam Singh commited on
Commit ·
100ec81
1
Parent(s): 7c66d21
update changes
Browse files
app.py
CHANGED
|
@@ -1,5 +1,3 @@
|
|
| 1 |
-
from pathlib import Path
|
| 2 |
-
|
| 3 |
import gradio as gr
|
| 4 |
import pillow_heif
|
| 5 |
import spaces
|
|
@@ -7,57 +5,17 @@ import torch
|
|
| 7 |
from gradio_imageslider import ImageSlider
|
| 8 |
from huggingface_hub import hf_hub_download
|
| 9 |
from PIL import Image
|
|
|
|
| 10 |
from refiners.fluxion.utils import manual_seed
|
| 11 |
from refiners.foundationals.latent_diffusion import Solver, solvers
|
| 12 |
|
| 13 |
from enhancer import ESRGANUpscaler, ESRGANUpscalerCheckpoints
|
| 14 |
|
|
|
|
| 15 |
pillow_heif.register_heif_opener()
|
| 16 |
pillow_heif.register_avif_opener()
|
| 17 |
|
| 18 |
-
|
| 19 |
-
<center>
|
| 20 |
-
|
| 21 |
-
<h1 style="font-size: 1.5rem; margin-bottom: 0.5rem;">
|
| 22 |
-
Image Enhancer Powered By Refiners
|
| 23 |
-
</h1>
|
| 24 |
-
|
| 25 |
-
<div style="
|
| 26 |
-
display: flex;
|
| 27 |
-
align-items: center;
|
| 28 |
-
justify-content: center;
|
| 29 |
-
gap: 0.5rem;
|
| 30 |
-
margin-bottom: 0.5rem;
|
| 31 |
-
font-size: 1.25rem;
|
| 32 |
-
flex-wrap: wrap;
|
| 33 |
-
">
|
| 34 |
-
<a href="https://blog.finegrain.ai/posts/reproducing-clarity-upscaler/" target="_blank">[Blog Post]</a>
|
| 35 |
-
<a href="https://github.com/finegrain-ai/refiners" target="_blank">[Refiners]</a>
|
| 36 |
-
<a href="https://finegrain.ai/" target="_blank">[Finegrain]</a>
|
| 37 |
-
<a href="https://huggingface.co/spaces/finegrain/finegrain-object-eraser" target="_blank">
|
| 38 |
-
[Finegrain Object Eraser]
|
| 39 |
-
</a>
|
| 40 |
-
<a href="https://huggingface.co/spaces/finegrain/finegrain-object-cutter" target="_blank">
|
| 41 |
-
[Finegrain Object Cutter]
|
| 42 |
-
</a>
|
| 43 |
-
</div>
|
| 44 |
-
|
| 45 |
-
<p>
|
| 46 |
-
Turn low resolution images into high resolution versions with added generated details (your image will be modified).
|
| 47 |
-
</p>
|
| 48 |
-
|
| 49 |
-
<p>
|
| 50 |
-
This space is powered by Refiners, our open source micro-framework for simple foundation model adaptation.
|
| 51 |
-
If you enjoyed it, please consider starring Refiners on GitHub!
|
| 52 |
-
</p>
|
| 53 |
-
|
| 54 |
-
<a href="https://github.com/finegrain-ai/refiners" target="_blank">
|
| 55 |
-
<img src="https://img.shields.io/github/stars/finegrain-ai/refiners?style=social" />
|
| 56 |
-
</a>
|
| 57 |
-
|
| 58 |
-
</center>
|
| 59 |
-
"""
|
| 60 |
-
|
| 61 |
CHECKPOINTS = ESRGANUpscalerCheckpoints(
|
| 62 |
unet=Path(
|
| 63 |
hf_hub_download(
|
|
@@ -120,12 +78,12 @@ CHECKPOINTS = ESRGANUpscalerCheckpoints(
|
|
| 120 |
},
|
| 121 |
)
|
| 122 |
|
| 123 |
-
#
|
| 124 |
DEVICE_CPU = torch.device("cpu")
|
| 125 |
DTYPE = torch.bfloat16 if torch.cuda.is_bf16_supported() else torch.float32
|
| 126 |
enhancer = ESRGANUpscaler(checkpoints=CHECKPOINTS, device=DEVICE_CPU, dtype=DTYPE)
|
| 127 |
|
| 128 |
-
#
|
| 129 |
DEVICE = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 130 |
enhancer.to(device=DEVICE, dtype=DTYPE)
|
| 131 |
|
|
@@ -167,20 +125,52 @@ def process(
|
|
| 167 |
|
| 168 |
return (input_image, enhanced_image)
|
| 169 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 170 |
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
with gr.Row():
|
| 175 |
-
with gr.Column():
|
| 176 |
-
input_image = gr.Image(type="pil", label="Input Image")
|
| 177 |
-
run_button = gr.ClearButton(components=None, value="Enhance Image")
|
| 178 |
-
with gr.Column():
|
| 179 |
-
output_slider = ImageSlider(label="Before / After")
|
| 180 |
-
run_button.add(output_slider)
|
| 181 |
|
|
|
|
| 182 |
def process_with_defaults(input_image):
|
| 183 |
-
# Set default values for the advanced options inside the function
|
| 184 |
prompt = "masterpiece, best quality, highres"
|
| 185 |
negative_prompt = "worst quality, low quality, normal quality"
|
| 186 |
seed = 42
|
|
@@ -194,7 +184,6 @@ with gr.Blocks() as demo:
|
|
| 194 |
num_inference_steps = 18
|
| 195 |
solver = "DDIM"
|
| 196 |
|
| 197 |
-
# Call the original process function with these constants
|
| 198 |
return process(
|
| 199 |
input_image,
|
| 200 |
prompt,
|
|
@@ -216,6 +205,6 @@ with gr.Blocks() as demo:
|
|
| 216 |
inputs=[input_image], # Only the image is user-provided
|
| 217 |
outputs=output_slider
|
| 218 |
)
|
| 219 |
-
|
| 220 |
|
|
|
|
| 221 |
demo.launch(share=False)
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import pillow_heif
|
| 3 |
import spaces
|
|
|
|
| 5 |
from gradio_imageslider import ImageSlider
|
| 6 |
from huggingface_hub import hf_hub_download
|
| 7 |
from PIL import Image
|
| 8 |
+
from pathlib import Path
|
| 9 |
from refiners.fluxion.utils import manual_seed
|
| 10 |
from refiners.foundationals.latent_diffusion import Solver, solvers
|
| 11 |
|
| 12 |
from enhancer import ESRGANUpscaler, ESRGANUpscalerCheckpoints
|
| 13 |
|
| 14 |
+
# Register HEIF and AVIF openers
|
| 15 |
pillow_heif.register_heif_opener()
|
| 16 |
pillow_heif.register_avif_opener()
|
| 17 |
|
| 18 |
+
# Download model checkpoints
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
CHECKPOINTS = ESRGANUpscalerCheckpoints(
|
| 20 |
unet=Path(
|
| 21 |
hf_hub_download(
|
|
|
|
| 78 |
},
|
| 79 |
)
|
| 80 |
|
| 81 |
+
# Initialize the enhancer on CPU
|
| 82 |
DEVICE_CPU = torch.device("cpu")
|
| 83 |
DTYPE = torch.bfloat16 if torch.cuda.is_bf16_supported() else torch.float32
|
| 84 |
enhancer = ESRGANUpscaler(checkpoints=CHECKPOINTS, device=DEVICE_CPU, dtype=DTYPE)
|
| 85 |
|
| 86 |
+
# Move the enhancer to the GPU, handled by Zero GPU
|
| 87 |
DEVICE = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 88 |
enhancer.to(device=DEVICE, dtype=DTYPE)
|
| 89 |
|
|
|
|
| 125 |
|
| 126 |
return (input_image, enhanced_image)
|
| 127 |
|
| 128 |
+
# Improved and interactive UI
|
| 129 |
+
with gr.Blocks(css=".input_image {border: 2px solid #6A5ACD;}") as demo:
|
| 130 |
+
|
| 131 |
+
# Title
|
| 132 |
+
gr.Markdown(
|
| 133 |
+
"""
|
| 134 |
+
<center>
|
| 135 |
+
<h1 style="font-size: 2rem;">🖼️ Image Enhancer Powered by Refiners</h1>
|
| 136 |
+
<p>Upload an image to enhance its quality with advanced AI-based upscaling!</p>
|
| 137 |
+
</center>
|
| 138 |
+
""")
|
| 139 |
+
|
| 140 |
+
with gr.Box():
|
| 141 |
+
with gr.Row():
|
| 142 |
+
with gr.Column(scale=1):
|
| 143 |
+
input_image = gr.Image(type="pil", label="Upload Your Image", elem_classes="input_image", interactive=True)
|
| 144 |
+
gr.Markdown(
|
| 145 |
+
"""
|
| 146 |
+
**Instructions:**
|
| 147 |
+
- Upload a low-resolution image.
|
| 148 |
+
- Click on "Enhance Image" to see the transformation.
|
| 149 |
+
""")
|
| 150 |
+
run_button = gr.Button("🚀 Enhance Image", elem_id="enhance_button")
|
| 151 |
+
|
| 152 |
+
with gr.Column(scale=1):
|
| 153 |
+
output_slider = ImageSlider(label="See the Difference (Before/After)")
|
| 154 |
+
|
| 155 |
+
with gr.Box():
|
| 156 |
+
gr.Markdown(
|
| 157 |
+
"""
|
| 158 |
+
<center>
|
| 159 |
+
<p style="font-size: 1.1rem;">
|
| 160 |
+
⚡ This application uses state-of-the-art AI models to upscale your images by adding new details!
|
| 161 |
+
<br><br>
|
| 162 |
+
Want to learn more? Check out our <a href="https://blog.finegrain.ai/posts/reproducing-clarity-upscaler/" target="_blank">Blog Post</a> or <a href="https://github.com/finegrain-ai/refiners" target="_blank">GitHub Repo</a> for more information.
|
| 163 |
+
</p>
|
| 164 |
+
</center>
|
| 165 |
+
"""
|
| 166 |
+
)
|
| 167 |
|
| 168 |
+
# Loading spinner during processing
|
| 169 |
+
with gr.Box():
|
| 170 |
+
gr.Markdown("<p><b>Note:</b> Processing may take a few seconds. Please wait...</p>")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 171 |
|
| 172 |
+
# Enhance the image using default values
|
| 173 |
def process_with_defaults(input_image):
|
|
|
|
| 174 |
prompt = "masterpiece, best quality, highres"
|
| 175 |
negative_prompt = "worst quality, low quality, normal quality"
|
| 176 |
seed = 42
|
|
|
|
| 184 |
num_inference_steps = 18
|
| 185 |
solver = "DDIM"
|
| 186 |
|
|
|
|
| 187 |
return process(
|
| 188 |
input_image,
|
| 189 |
prompt,
|
|
|
|
| 205 |
inputs=[input_image], # Only the image is user-provided
|
| 206 |
outputs=output_slider
|
| 207 |
)
|
|
|
|
| 208 |
|
| 209 |
+
# Launch the demo
|
| 210 |
demo.launch(share=False)
|