Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -42,15 +42,13 @@ def upscale_image(image):
|
|
| 42 |
input_tensor = transform(image)
|
| 43 |
with torch.no_grad():
|
| 44 |
output_tensor = model(input_tensor)
|
| 45 |
-
output_image = transforms.ToPILImage()(output_tensor.squeeze
|
| 46 |
-
(0))
|
| 47 |
return output_image
|
| 48 |
|
| 49 |
def load_image_as_bytes(image_path):
|
| 50 |
"""Chuyển ảnh thành dữ liệu nhị phân"""
|
| 51 |
with Image.open(image_path) as img:
|
| 52 |
img = img.convert("RGB") # Đảm bảo ảnh là RGB
|
| 53 |
-
img = upscale_image(img) # SRCNN xử lý
|
| 54 |
img_bytes = BytesIO()
|
| 55 |
img.save(img_bytes, format="JPEG") # Lưu ảnh vào buffer
|
| 56 |
return img_bytes.getvalue() # Lấy dữ liệu nhị phân
|
|
@@ -72,7 +70,6 @@ async def generate_image(image_bytes, text_input):
|
|
| 72 |
for part in response.candidates[0].content.parts:
|
| 73 |
if part.inline_data is not None:
|
| 74 |
img = Image.open(BytesIO(part.inline_data.data))
|
| 75 |
-
img = upscale_image(img) # SRCNN sau khi nhận ảnh từ Gemini
|
| 76 |
images.append(img)
|
| 77 |
return images
|
| 78 |
|
|
@@ -83,8 +80,10 @@ async def process_request(image, text, num_requests):
|
|
| 83 |
results = await asyncio.gather(*tasks)
|
| 84 |
|
| 85 |
# Hợp nhất danh sách ảnh từ các request
|
| 86 |
-
|
| 87 |
-
|
|
|
|
|
|
|
| 88 |
|
| 89 |
def gradio_interface(image, text, num_requests):
|
| 90 |
"""Hàm Gradio xử lý yêu cầu và trả về ảnh"""
|
|
@@ -100,7 +99,7 @@ demo = gr.Interface(
|
|
| 100 |
],
|
| 101 |
outputs=gr.Gallery(label="Kết quả chỉnh sửa", columns=4),
|
| 102 |
title="Chỉnh sửa ảnh bằng Gemini AI + SRCNN",
|
| 103 |
-
description="Upload ảnh và nhập yêu cầu chỉnh sửa.
|
| 104 |
)
|
| 105 |
|
| 106 |
demo.launch()
|
|
|
|
| 42 |
input_tensor = transform(image)
|
| 43 |
with torch.no_grad():
|
| 44 |
output_tensor = model(input_tensor)
|
| 45 |
+
output_image = transforms.ToPILImage()(output_tensor.squeeze(0))
|
|
|
|
| 46 |
return output_image
|
| 47 |
|
| 48 |
def load_image_as_bytes(image_path):
|
| 49 |
"""Chuyển ảnh thành dữ liệu nhị phân"""
|
| 50 |
with Image.open(image_path) as img:
|
| 51 |
img = img.convert("RGB") # Đảm bảo ảnh là RGB
|
|
|
|
| 52 |
img_bytes = BytesIO()
|
| 53 |
img.save(img_bytes, format="JPEG") # Lưu ảnh vào buffer
|
| 54 |
return img_bytes.getvalue() # Lấy dữ liệu nhị phân
|
|
|
|
| 70 |
for part in response.candidates[0].content.parts:
|
| 71 |
if part.inline_data is not None:
|
| 72 |
img = Image.open(BytesIO(part.inline_data.data))
|
|
|
|
| 73 |
images.append(img)
|
| 74 |
return images
|
| 75 |
|
|
|
|
| 80 |
results = await asyncio.gather(*tasks)
|
| 81 |
|
| 82 |
# Hợp nhất danh sách ảnh từ các request
|
| 83 |
+
original_images = [img for result in results for img in result]
|
| 84 |
+
srcnn_images = [upscale_image(img) for img in original_images]
|
| 85 |
+
|
| 86 |
+
return original_images + srcnn_images # 4 ảnh gốc + 4 ảnh đã qua SRCNN
|
| 87 |
|
| 88 |
def gradio_interface(image, text, num_requests):
|
| 89 |
"""Hàm Gradio xử lý yêu cầu và trả về ảnh"""
|
|
|
|
| 99 |
],
|
| 100 |
outputs=gr.Gallery(label="Kết quả chỉnh sửa", columns=4),
|
| 101 |
title="Chỉnh sửa ảnh bằng Gemini AI + SRCNN",
|
| 102 |
+
description="Upload ảnh và nhập yêu cầu chỉnh sửa. Hiển thị 4 ảnh gốc từ API và 4 ảnh đã qua SRCNN.",
|
| 103 |
)
|
| 104 |
|
| 105 |
demo.launch()
|