sunbv56 commited on
Commit
3a7aae2
·
verified ·
1 Parent(s): d9f0003

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -3
app.py CHANGED
@@ -1,6 +1,9 @@
1
  import gradio as gr
2
  import asyncio
3
  import os
 
 
 
4
  from google import genai
5
  from google.genai import types
6
  from PIL import Image
@@ -9,15 +12,31 @@ from io import BytesIO
9
  # Cấu hình API Key
10
  api_key = os.getenv("GEMINI_API_KEY")
11
  if not api_key:
12
-
13
  raise ValueError("⚠️ GEMINI_API_KEY is missing!")
14
 
15
  client = genai.Client(api_key=api_key)
16
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
  def load_image_as_bytes(image_path):
18
  """Chuyển ảnh thành dữ liệu nhị phân"""
19
  with Image.open(image_path) as img:
20
  img = img.convert("RGB") # Đảm bảo ảnh là RGB
 
21
  img_bytes = BytesIO()
22
  img.save(img_bytes, format="JPEG") # Lưu ảnh vào buffer
23
  return img_bytes.getvalue() # Lấy dữ liệu nhị phân
@@ -39,6 +58,7 @@ async def generate_image(image_bytes, text_input):
39
  for part in response.candidates[0].content.parts:
40
  if part.inline_data is not None:
41
  img = Image.open(BytesIO(part.inline_data.data))
 
42
  images.append(img)
43
  return images
44
 
@@ -65,8 +85,8 @@ demo = gr.Interface(
65
  gr.Slider(minimum=1, maximum=4, step=1, value=4, label="Số lượng ảnh cần tạo")
66
  ],
67
  outputs=gr.Gallery(label="Kết quả chỉnh sửa", columns=4),
68
- title="Chỉnh sửa ảnh bằng Gemini AI",
69
- description="Upload ảnh và nhập yêu cầu chỉnh sửa. Chọn số lượng ảnh muốn tạo (1-4).",
70
  )
71
 
72
  demo.launch()
 
1
  import gradio as gr
2
  import asyncio
3
  import os
4
+ import torch
5
+ import torchvision.transforms as transforms
6
+ from torchvision.utils import save_image
7
  from google import genai
8
  from google.genai import types
9
  from PIL import Image
 
12
  # Cấu hình API Key
13
  api_key = os.getenv("GEMINI_API_KEY")
14
  if not api_key:
 
15
  raise ValueError("⚠️ GEMINI_API_KEY is missing!")
16
 
17
  client = genai.Client(api_key=api_key)
18
 
19
+ # Load SRCNN từ Torch Hub
20
+ model = torch.hub.load('pytorch/vision:v0.10.0', 'srcnn', pretrained=True)
21
+ model.eval()
22
+
23
+ def upscale_image(image):
24
+ """Nâng cấp độ phân giải ảnh bằng SRCNN"""
25
+ transform = transforms.Compose([
26
+ transforms.ToTensor(),
27
+ transforms.Lambda(lambda x: x.unsqueeze(0)) # Thêm batch dimension
28
+ ])
29
+ img_tensor = transform(image)
30
+ with torch.no_grad():
31
+ upscaled_tensor = model(img_tensor)
32
+ upscaled_image = transforms.ToPILImage()(upscaled_tensor.squeeze(0))
33
+ return upscaled_image
34
+
35
  def load_image_as_bytes(image_path):
36
  """Chuyển ảnh thành dữ liệu nhị phân"""
37
  with Image.open(image_path) as img:
38
  img = img.convert("RGB") # Đảm bảo ảnh là RGB
39
+ img = upscale_image(img) # SRCNN trước khi gửi đi
40
  img_bytes = BytesIO()
41
  img.save(img_bytes, format="JPEG") # Lưu ảnh vào buffer
42
  return img_bytes.getvalue() # Lấy dữ liệu nhị phân
 
58
  for part in response.candidates[0].content.parts:
59
  if part.inline_data is not None:
60
  img = Image.open(BytesIO(part.inline_data.data))
61
+ img = upscale_image(img) # SRCNN sau khi nhận ảnh từ Gemini
62
  images.append(img)
63
  return images
64
 
 
85
  gr.Slider(minimum=1, maximum=4, step=1, value=4, label="Số lượng ảnh cần tạo")
86
  ],
87
  outputs=gr.Gallery(label="Kết quả chỉnh sửa", columns=4),
88
+ title="Chỉnh sửa ảnh bằng Gemini AI + SRCNN",
89
+ description="Upload ảnh và nhập yêu cầu chỉnh sửa. Ảnh được nâng cấp độ phân giải trước và sau khi xử lý.",
90
  )
91
 
92
  demo.launch()