Spaces:
Runtime error
Runtime error
Upload 2 files
Browse files- app/__init__.py +0 -0
- app/upscale.py +62 -0
app/__init__.py
ADDED
File without changes
|
app/upscale.py
ADDED
@@ -0,0 +1,62 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import replicate
|
2 |
+
from PIL import Image
|
3 |
+
import asyncio
|
4 |
+
import requests
|
5 |
+
from io import BytesIO
|
6 |
+
#from dotenv import load_dotenv
|
7 |
+
import os
|
8 |
+
|
9 |
+
#load_dotenv("../.env")
|
10 |
+
|
11 |
+
class ImageUpscaler():
|
12 |
+
|
13 |
+
|
14 |
+
def pil2byte(self, image):
|
15 |
+
byte_obj = BytesIO()
|
16 |
+
try:
|
17 |
+
image.save(byte_obj, format='PNG')
|
18 |
+
byte_obj.seek(0)
|
19 |
+
except Exception as e:
|
20 |
+
print(f"Unable to process the image: {e}")
|
21 |
+
return byte_obj
|
22 |
+
|
23 |
+
|
24 |
+
def img2byte(self, img_path):
|
25 |
+
with Image.open(img_path) as img:
|
26 |
+
|
27 |
+
img_byte_arr = BytesIO()
|
28 |
+
|
29 |
+
ext = os.path.splitext(img_path)[1].replace('.','').upper()
|
30 |
+
if ext == 'JPG':
|
31 |
+
ext = 'JPEG'
|
32 |
+
img.save(img_byte_arr, format=ext)
|
33 |
+
img_byte_arr.seek(0)
|
34 |
+
return img_byte_arr
|
35 |
+
|
36 |
+
|
37 |
+
|
38 |
+
async def upscaler(self, target_img=None, factor=4, byte_stream=True):
|
39 |
+
try:
|
40 |
+
model = replicate.models.get("nightmareai/real-esrgan")
|
41 |
+
version = model.versions.get("42fed1c4974146d4d2414e2be2c5277c7fcf05fcc3a73abf41610695738c1d7b")
|
42 |
+
|
43 |
+
if not byte_stream:
|
44 |
+
inputs = {
|
45 |
+
'image': open(target_img,'rb'),
|
46 |
+
|
47 |
+
}
|
48 |
+
else:
|
49 |
+
inputs = {
|
50 |
+
'image': target_img,
|
51 |
+
|
52 |
+
}
|
53 |
+
inputs['scale'] = factor
|
54 |
+
inputs['face_enhance'] = True
|
55 |
+
img_output = version.predict(**inputs)
|
56 |
+
upscaled_response = requests.get(img_output)
|
57 |
+
upscaled_bin = BytesIO(upscaled_response.content)
|
58 |
+
|
59 |
+
except Exception as e:
|
60 |
+
return f"An error occurred during face image generation: {str(e)}"
|
61 |
+
|
62 |
+
return upscaled_bin
|