Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -57,7 +57,8 @@ def load_image_as_bytes(image_path):
|
|
| 57 |
async def generate_image(image_bytes_list, text_input):
|
| 58 |
"""Gửi request và nhận kết quả từ Gemini API"""
|
| 59 |
image_parts = [types.Part(inline_data=types.Blob(data=img, mime_type="image/jpeg")) for img in image_bytes_list if img]
|
| 60 |
-
contents = [text_input, image_parts] if image_parts else
|
|
|
|
| 61 |
|
| 62 |
response = await asyncio.to_thread(
|
| 63 |
client.models.generate_content,
|
|
@@ -74,35 +75,32 @@ async def generate_image(image_bytes_list, text_input):
|
|
| 74 |
images.append(img)
|
| 75 |
return images
|
| 76 |
|
| 77 |
-
async def process_request(
|
| 78 |
"""Chạy nhiều request song song"""
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
results = await asyncio.gather(*tasks)
|
| 82 |
|
| 83 |
# Hợp nhất danh sách ảnh từ các request
|
| 84 |
-
|
| 85 |
-
resized_images = [img.resize((2560, int(img.height * (2560 / img.width)))), Image.LANCZOS) for img in original_images] # Resize trước khi upscale
|
| 86 |
# srcnn_images = [upscale_image(img) for img in resized_images]
|
| 87 |
|
| 88 |
-
return resized_images
|
| 89 |
-
# return resized_images + srcnn_images # 4 ảnh gốc + 4 ảnh đã qua SRCNN
|
| 90 |
|
| 91 |
-
def gradio_interface(
|
| 92 |
"""Hàm Gradio xử lý yêu cầu và trả về ảnh"""
|
| 93 |
-
return asyncio.run(process_request(
|
| 94 |
|
| 95 |
# Tạo giao diện Gradio
|
| 96 |
demo = gr.Interface(
|
| 97 |
fn=gradio_interface,
|
| 98 |
inputs=[
|
| 99 |
-
gr.
|
| 100 |
gr.Textbox(label="Nhập yêu cầu chỉnh sửa hình ảnh"),
|
| 101 |
gr.Slider(minimum=1, maximum=4, step=1, value=4, label="Số lượng ảnh cần tạo")
|
| 102 |
],
|
| 103 |
outputs=gr.Gallery(label="Kết quả chỉnh sửa", columns=4),
|
| 104 |
title="Chỉnh sửa ảnh bằng Gemini AI + SRCNN",
|
| 105 |
-
description="Upload ảnh và nhập yêu cầu chỉnh sửa. Hiển thị
|
| 106 |
)
|
| 107 |
|
| 108 |
demo.launch()
|
|
|
|
| 57 |
async def generate_image(image_bytes_list, text_input):
|
| 58 |
"""Gửi request và nhận kết quả từ Gemini API"""
|
| 59 |
image_parts = [types.Part(inline_data=types.Blob(data=img, mime_type="image/jpeg")) for img in image_bytes_list if img]
|
| 60 |
+
contents = [text_input, image_parts] if image_parts else
|
| 61 |
+
[text_input]
|
| 62 |
|
| 63 |
response = await asyncio.to_thread(
|
| 64 |
client.models.generate_content,
|
|
|
|
| 75 |
images.append(img)
|
| 76 |
return images
|
| 77 |
|
| 78 |
+
async def process_request(images, text, num_requests):
|
| 79 |
"""Chạy nhiều request song song"""
|
| 80 |
+
image_bytes_list = [load_image_as_bytes(image) if image else None for image in images]
|
| 81 |
+
results = await generate_image(image_bytes_list, text)
|
|
|
|
| 82 |
|
| 83 |
# Hợp nhất danh sách ảnh từ các request
|
| 84 |
+
resized_images = [img.resize((2560, int(img.height * (2560 / img.width))), Image.LANCZOS) for img in results] # Resize giữ nguyên tỉ lệ chiều cao
|
|
|
|
| 85 |
# srcnn_images = [upscale_image(img) for img in resized_images]
|
| 86 |
|
| 87 |
+
return resized_images
|
|
|
|
| 88 |
|
| 89 |
+
def gradio_interface(images, text, num_requests):
|
| 90 |
"""Hàm Gradio xử lý yêu cầu và trả về ảnh"""
|
| 91 |
+
return asyncio.run(process_request(images, text, num_requests))
|
| 92 |
|
| 93 |
# Tạo giao diện Gradio
|
| 94 |
demo = gr.Interface(
|
| 95 |
fn=gradio_interface,
|
| 96 |
inputs=[
|
| 97 |
+
gr.File(file_types=["image"], label="Upload hình ảnh", multiple=True),
|
| 98 |
gr.Textbox(label="Nhập yêu cầu chỉnh sửa hình ảnh"),
|
| 99 |
gr.Slider(minimum=1, maximum=4, step=1, value=4, label="Số lượng ảnh cần tạo")
|
| 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 nhiều ảnh và nhập yêu cầu chỉnh sửa. Hiển thị ảnh gốc từ API và ảnh đã qua SRCNN.",
|
| 104 |
)
|
| 105 |
|
| 106 |
demo.launch()
|