Spaces:
Runtime error
Runtime error
add files
Browse files- .DS_Store +0 -0
- .gitattributes +2 -0
- README.md +7 -4
- app.py +215 -0
- examples/.DS_Store +0 -0
- examples/anime_1.png +3 -0
- examples/anime_2.jpg +3 -0
- examples/bus1.png +3 -0
- examples/dogs.jpg +3 -0
- examples/festival_3.png +3 -0
- examples/general_2.png +3 -0
- examples/mid65_general.png +3 -0
- examples/portrait1.png +3 -0
- examples/vintage_family_photo.jpg +3 -0
- requirements.txt +5 -0
.DS_Store
ADDED
|
Binary file (6.15 kB). View file
|
|
|
.gitattributes
CHANGED
|
@@ -33,3 +33,5 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
|
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
| 36 |
+
*.png filter=lfs diff=lfs merge=lfs -text
|
| 37 |
+
*.jpg filter=lfs diff=lfs merge=lfs -text
|
README.md
CHANGED
|
@@ -1,13 +1,16 @@
|
|
| 1 |
---
|
| 2 |
-
title: Puncta Lite
|
| 3 |
-
emoji:
|
| 4 |
-
colorFrom:
|
| 5 |
-
colorTo:
|
| 6 |
sdk: gradio
|
| 7 |
sdk_version: 5.29.1
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
| 10 |
short_description: High quality image upscaler
|
|
|
|
|
|
|
|
|
|
| 11 |
---
|
| 12 |
|
| 13 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
|
| 1 |
---
|
| 2 |
+
title: Puncta Upscaler Lite
|
| 3 |
+
emoji: 🔎
|
| 4 |
+
colorFrom: green
|
| 5 |
+
colorTo: indigo
|
| 6 |
sdk: gradio
|
| 7 |
sdk_version: 5.29.1
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
| 10 |
short_description: High quality image upscaler
|
| 11 |
+
tags:
|
| 12 |
+
- upscaler
|
| 13 |
+
- super-resolution
|
| 14 |
---
|
| 15 |
|
| 16 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
app.py
ADDED
|
@@ -0,0 +1,215 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import io
|
| 3 |
+
import requests
|
| 4 |
+
from uuid import uuid4
|
| 5 |
+
|
| 6 |
+
import boto3
|
| 7 |
+
from botocore.client import Config
|
| 8 |
+
from PIL import Image
|
| 9 |
+
import gradio as gr
|
| 10 |
+
|
| 11 |
+
from dotenv import load_dotenv
|
| 12 |
+
load_dotenv()
|
| 13 |
+
|
| 14 |
+
css = """
|
| 15 |
+
#col-container {
|
| 16 |
+
margin: 0 auto;
|
| 17 |
+
max-width: 512px;
|
| 18 |
+
}
|
| 19 |
+
"""
|
| 20 |
+
|
| 21 |
+
MAX_PIXEL_BUDGET = 1024 * 1024
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
def upload_to_r2(file_path, object_key, content_type):
|
| 25 |
+
s3 = boto3.client(
|
| 26 |
+
's3',
|
| 27 |
+
endpoint_url=os.getenv('R2_ENDPOINT'),
|
| 28 |
+
aws_access_key_id=os.getenv('R2_ACCESS_KEY_ID'),
|
| 29 |
+
aws_secret_access_key=os.getenv('R2_SECRET_ACCESS_KEY'),
|
| 30 |
+
config=Config(signature_version='s3v4'),
|
| 31 |
+
region_name='auto'
|
| 32 |
+
)
|
| 33 |
+
|
| 34 |
+
with open(file_path, 'rb') as f:
|
| 35 |
+
s3.put_object(
|
| 36 |
+
Bucket=os.getenv('R2_BUCKET'),
|
| 37 |
+
Key=object_key,
|
| 38 |
+
Body=f,
|
| 39 |
+
ContentType=content_type
|
| 40 |
+
)
|
| 41 |
+
|
| 42 |
+
download_url = s3.generate_presigned_url(
|
| 43 |
+
'get_object',
|
| 44 |
+
Params={
|
| 45 |
+
'Bucket': os.getenv('R2_BUCKET'),
|
| 46 |
+
'Key': object_key
|
| 47 |
+
},
|
| 48 |
+
ExpiresIn=3600 # url expiration time in seconds
|
| 49 |
+
)
|
| 50 |
+
|
| 51 |
+
return download_url
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
def process_input(input_image, upscale_factor):
|
| 55 |
+
w, h = input_image.size
|
| 56 |
+
w_original, h_original = w, h
|
| 57 |
+
aspect_ratio = w / h
|
| 58 |
+
|
| 59 |
+
was_resized = False
|
| 60 |
+
|
| 61 |
+
# compute minimum dimension after upscaling
|
| 62 |
+
min_dimension = min(w, h) * upscale_factor
|
| 63 |
+
|
| 64 |
+
# if minimum dimension is above 1024, adjust scale factor
|
| 65 |
+
if min_dimension > 1024:
|
| 66 |
+
new_scale = 1024 / min(w, h)
|
| 67 |
+
upscale_factor = min(2, new_scale) # cap at 2x if needed
|
| 68 |
+
gr.Info(f'Adjusted scale factor to {upscale_factor}x to maintain minimum dimension of 1024 pixels')
|
| 69 |
+
|
| 70 |
+
if w * h * upscale_factor**2 > MAX_PIXEL_BUDGET:
|
| 71 |
+
gr.Info(
|
| 72 |
+
f'Requested output image is too large. Resizing input to fit within pixel budget.'
|
| 73 |
+
)
|
| 74 |
+
input_image = input_image.resize(
|
| 75 |
+
(
|
| 76 |
+
int(aspect_ratio * MAX_PIXEL_BUDGET**0.5 // upscale_factor),
|
| 77 |
+
int(MAX_PIXEL_BUDGET**0.5 // aspect_ratio // upscale_factor),
|
| 78 |
+
)
|
| 79 |
+
)
|
| 80 |
+
was_resized = True
|
| 81 |
+
|
| 82 |
+
return input_image, w_original, h_original, was_resized, upscale_factor
|
| 83 |
+
|
| 84 |
+
def infer(
|
| 85 |
+
input_image,
|
| 86 |
+
upscale_factor,
|
| 87 |
+
image_category,
|
| 88 |
+
):
|
| 89 |
+
true_input_image = input_image
|
| 90 |
+
input_image, w_original, h_original, was_resized, adjusted_scale = process_input(
|
| 91 |
+
input_image, upscale_factor
|
| 92 |
+
)
|
| 93 |
+
|
| 94 |
+
temp_input_path = 'temp_input.png'
|
| 95 |
+
input_image.save(temp_input_path)
|
| 96 |
+
image_url = upload_to_r2(temp_input_path, str(uuid4()), 'image/png')
|
| 97 |
+
gr.Info('Upscaling image...')
|
| 98 |
+
|
| 99 |
+
try:
|
| 100 |
+
resp = requests.get(
|
| 101 |
+
os.getenv('ENDPOINT') ,
|
| 102 |
+
headers={
|
| 103 |
+
'Modal-Key': os.getenv('AUTH_KEY'),
|
| 104 |
+
'Modal-Secret': os.getenv('AUTH_SECRET'),
|
| 105 |
+
},
|
| 106 |
+
params={
|
| 107 |
+
'image_url': image_url,
|
| 108 |
+
'image_category': image_category,
|
| 109 |
+
'scale_factor': adjusted_scale,
|
| 110 |
+
'output_format': 'png',
|
| 111 |
+
'upload_to_r2': False
|
| 112 |
+
}
|
| 113 |
+
)
|
| 114 |
+
|
| 115 |
+
if resp.status_code != 200:
|
| 116 |
+
raise gr.Error(f'API request failed with status {resp.status_code}: {resp.text}')
|
| 117 |
+
|
| 118 |
+
# save the response image
|
| 119 |
+
output_path = 'output.png'
|
| 120 |
+
with open(output_path, 'wb') as f:
|
| 121 |
+
f.write(resp.content)
|
| 122 |
+
|
| 123 |
+
output_image = Image.open(output_path)
|
| 124 |
+
|
| 125 |
+
if was_resized:
|
| 126 |
+
gr.Info(
|
| 127 |
+
f'Resizing output image to targeted {w_original * adjusted_scale}x{h_original * adjusted_scale} size.'
|
| 128 |
+
)
|
| 129 |
+
output_image = output_image.resize((int(w_original * adjusted_scale), int(h_original * adjusted_scale)))
|
| 130 |
+
|
| 131 |
+
return output_image
|
| 132 |
+
|
| 133 |
+
except Exception as e:
|
| 134 |
+
raise gr.Error(f'Error during upscaling: {str(e)}')
|
| 135 |
+
finally:
|
| 136 |
+
if os.path.exists(temp_input_path):
|
| 137 |
+
os.remove(temp_input_path)
|
| 138 |
+
if os.path.exists(output_path):
|
| 139 |
+
os.remove(output_path)
|
| 140 |
+
|
| 141 |
+
with gr.Blocks(css=css) as demo:
|
| 142 |
+
gr.Markdown(
|
| 143 |
+
"""
|
| 144 |
+
# 🚀 Puncta Lite - AI Image Upscaler
|
| 145 |
+
|
| 146 |
+
This is a lite version of Puncta's AI image upscaler. For more advanced features and higher quality results, visit [puncta.ai](https://www.puncta.ai/).
|
| 147 |
+
|
| 148 |
+
*Note*: This demo is limited to a maximum output resolution of 1024x1024 pixels. For higher resolution upscaling, please visit our full version at puncta.ai.
|
| 149 |
+
"""
|
| 150 |
+
)
|
| 151 |
+
|
| 152 |
+
with gr.Row():
|
| 153 |
+
with gr.Column(scale=1):
|
| 154 |
+
input_im = gr.Image(label='Input Image', type='pil')
|
| 155 |
+
with gr.Column(scale=1):
|
| 156 |
+
output_im = gr.Image(label='Output Image', type='pil')
|
| 157 |
+
|
| 158 |
+
with gr.Row():
|
| 159 |
+
with gr.Column(scale=1):
|
| 160 |
+
upscale_factor = gr.Slider(
|
| 161 |
+
label='Upscale Factor',
|
| 162 |
+
minimum=2,
|
| 163 |
+
maximum=4,
|
| 164 |
+
step=1,
|
| 165 |
+
value=2,
|
| 166 |
+
)
|
| 167 |
+
with gr.Column(scale=1):
|
| 168 |
+
image_category = gr.Dropdown(
|
| 169 |
+
label='Image Category',
|
| 170 |
+
choices=['general', 'portrait', 'outdoor', 'digital art'],
|
| 171 |
+
value='general'
|
| 172 |
+
)
|
| 173 |
+
|
| 174 |
+
with gr.Row():
|
| 175 |
+
run_button = gr.Button(value='Upscale Image', variant='primary"')
|
| 176 |
+
|
| 177 |
+
examples = gr.Examples(
|
| 178 |
+
examples=[
|
| 179 |
+
['examples/dogs.jpg', 2, 'outdoor'],
|
| 180 |
+
['examples/portrait1.png', 3, 'portrait'],
|
| 181 |
+
['examples/anime_1.png', 2, 'digital art'],
|
| 182 |
+
['examples/vintage_family_photo.jpg', 2, 'portrait'],
|
| 183 |
+
['examples/bus1.png', 3, 'general'],
|
| 184 |
+
['examples/festival_3.png', 2, 'general'],
|
| 185 |
+
['examples/general_2.png', 2, 'general'],
|
| 186 |
+
['examples/anime_2.jpg', 3, 'digital art'],
|
| 187 |
+
],
|
| 188 |
+
inputs=[
|
| 189 |
+
input_im,
|
| 190 |
+
upscale_factor,
|
| 191 |
+
image_category,
|
| 192 |
+
],
|
| 193 |
+
fn=infer,
|
| 194 |
+
outputs=output_im,
|
| 195 |
+
cache_examples=True,
|
| 196 |
+
)
|
| 197 |
+
|
| 198 |
+
gr.Markdown("**Disclaimer:**")
|
| 199 |
+
gr.Markdown(
|
| 200 |
+
"This demo is for testing purposes only. For commercial use and higher quality results, please visit [puncta.ai](https://www.puncta.ai/)."
|
| 201 |
+
)
|
| 202 |
+
|
| 203 |
+
gr.on(
|
| 204 |
+
[run_button.click],
|
| 205 |
+
fn=infer,
|
| 206 |
+
inputs=[
|
| 207 |
+
input_im,
|
| 208 |
+
upscale_factor,
|
| 209 |
+
image_category,
|
| 210 |
+
],
|
| 211 |
+
outputs=output_im,
|
| 212 |
+
show_api=False,
|
| 213 |
+
)
|
| 214 |
+
|
| 215 |
+
demo.queue().launch(share=False, show_api=False)
|
examples/.DS_Store
ADDED
|
Binary file (6.15 kB). View file
|
|
|
examples/anime_1.png
ADDED
|
Git LFS Details
|
examples/anime_2.jpg
ADDED
|
Git LFS Details
|
examples/bus1.png
ADDED
|
Git LFS Details
|
examples/dogs.jpg
ADDED
|
Git LFS Details
|
examples/festival_3.png
ADDED
|
Git LFS Details
|
examples/general_2.png
ADDED
|
Git LFS Details
|
examples/mid65_general.png
ADDED
|
Git LFS Details
|
examples/portrait1.png
ADDED
|
Git LFS Details
|
examples/vintage_family_photo.jpg
ADDED
|
Git LFS Details
|
requirements.txt
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio==4.29.0
|
| 2 |
+
gradio_imageslider
|
| 3 |
+
requests
|
| 4 |
+
Pillow
|
| 5 |
+
boto3
|