abiabidali commited on
Commit
dea192e
·
verified ·
1 Parent(s): 8de6861

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -41
app.py CHANGED
@@ -1,6 +1,7 @@
1
  import torch
2
  from PIL import Image
3
  from RealESRGAN import RealESRGAN
 
4
  import gradio as gr
5
  import numpy as np
6
  import io
@@ -11,6 +12,7 @@ import time
11
  # Set the device to CUDA if available, otherwise CPU
12
  device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
13
 
 
14
  def load_model(scale):
15
  model = RealESRGAN(device, scale=scale)
16
  weights_path = f'weights/RealESRGAN_x{scale}.pth'
@@ -21,44 +23,33 @@ def load_model(scale):
21
  print(f"Error loading weights for scale {scale}: {e}")
22
  return model
23
 
24
- # Load models for different scales
25
  model2 = load_model(2)
26
  model4 = load_model(4)
27
  model8 = load_model(8)
28
 
 
 
 
 
29
  def enhance_image(image, scale):
30
  try:
31
- print(f"Enhancing image with scale {scale}...")
32
- start_time = time.time()
33
  image_np = np.array(image.convert('RGB'))
34
  model = model2 if scale == '2x' else model4 if scale == '4x' else model8
35
  result = model.predict(image_np)
36
- enhanced_image = Image.fromarray(np.uint8(result))
37
- print(f"Image enhanced in {time.time() - start_time:.2f} seconds")
38
- return enhanced_image
39
  except Exception as e:
40
  print(f"Error enhancing image: {e}")
41
  return image
42
 
43
- def muda_dpi(image, dpi):
44
- try:
45
- with io.BytesIO() as output:
46
- image.save(output, format='JPEG', dpi=(dpi, dpi))
47
- return Image.open(output)
48
- except Exception as e:
49
- print(f"Error adjusting DPI: {e}")
50
- return image
51
 
52
- def resize_image(image, width, height):
53
- try:
54
- resized_image = image.resize((width, height))
55
- return resized_image
56
- except Exception as e:
57
- print(f"Error resizing image: {e}")
58
- return image
59
-
60
- def process_images(image_files, enhance, scale, adjust_dpi, dpi, resize, width, height):
61
  processed_images = []
 
62
  zip_buffer = io.BytesIO()
63
 
64
  for image_file in image_files:
@@ -67,13 +58,11 @@ def process_images(image_files, enhance, scale, adjust_dpi, dpi, resize, width,
67
  if enhance:
68
  image = enhance_image(image, scale)
69
 
70
- if adjust_dpi:
71
- image = muda_dpi(image, dpi)
72
-
73
- if resize:
74
- image = resize_image(image, width, height)
75
 
76
- # Save image to the in-memory ZIP buffer
77
  buffer = io.BytesIO()
78
  image.save(buffer, format='JPEG')
79
  processed_images.append(Image.open(io.BytesIO(buffer.getvalue())))
@@ -81,26 +70,23 @@ def process_images(image_files, enhance, scale, adjust_dpi, dpi, resize, width,
81
  zipf.writestr(os.path.basename(image_file.name), buffer.getvalue())
82
 
83
  zip_buffer.seek(0)
84
- return processed_images, zip_buffer
85
 
86
  iface = gr.Interface(
87
  fn=process_images,
88
  inputs=[
89
- gr.Files(label="Upload Image Files"), # Use gr.Files for multiple file uploads
90
  gr.Checkbox(label="Enhance Images (ESRGAN)"),
91
  gr.Radio(['2x', '4x', '8x'], type="value", value='2x', label='Resolution model'),
92
- gr.Checkbox(label="Adjust DPI"),
93
- gr.Number(label="DPI", value=300),
94
- gr.Checkbox(label="Resize"),
95
- gr.Number(label="Width", value=512),
96
- gr.Number(label="Height", value=512)
97
  ],
98
  outputs=[
99
- gr.Gallery(label="Final Images"), # Display the processed images
100
- gr.File(label="Download Final Images (ZIP)") # Provide a ZIP file for download
 
101
  ],
102
- title="Multi-Image Enhancer",
103
- description="Upload multiple images (.jpg, .png), enhance using AI, adjust DPI, resize, and download the final results as a ZIP file."
104
  )
105
 
106
- iface.launch(debug=True)
 
1
  import torch
2
  from PIL import Image
3
  from RealESRGAN import RealESRGAN
4
+ from transformers import BlipProcessor, BlipForConditionalGeneration # Example for Hugging Face model
5
  import gradio as gr
6
  import numpy as np
7
  import io
 
12
  # Set the device to CUDA if available, otherwise CPU
13
  device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
14
 
15
+ # Load the RealESRGAN models for enhancement
16
  def load_model(scale):
17
  model = RealESRGAN(device, scale=scale)
18
  weights_path = f'weights/RealESRGAN_x{scale}.pth'
 
23
  print(f"Error loading weights for scale {scale}: {e}")
24
  return model
25
 
 
26
  model2 = load_model(2)
27
  model4 = load_model(4)
28
  model8 = load_model(8)
29
 
30
+ # Load Hugging Face model and processor for image description
31
+ processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-base")
32
+ caption_model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-base").to(device)
33
+
34
  def enhance_image(image, scale):
35
  try:
 
 
36
  image_np = np.array(image.convert('RGB'))
37
  model = model2 if scale == '2x' else model4 if scale == '4x' else model8
38
  result = model.predict(image_np)
39
+ return Image.fromarray(np.uint8(result))
 
 
40
  except Exception as e:
41
  print(f"Error enhancing image: {e}")
42
  return image
43
 
44
+ def describe_image(image):
45
+ inputs = processor(image, return_tensors="pt").to(device)
46
+ generated_ids = caption_model.generate(**inputs)
47
+ description = processor.decode(generated_ids[0], skip_special_tokens=True)
48
+ return description
 
 
 
49
 
50
+ def process_images(image_files, enhance, scale, generate_description):
 
 
 
 
 
 
 
 
51
  processed_images = []
52
+ descriptions = []
53
  zip_buffer = io.BytesIO()
54
 
55
  for image_file in image_files:
 
58
  if enhance:
59
  image = enhance_image(image, scale)
60
 
61
+ if generate_description:
62
+ description = describe_image(image)
63
+ descriptions.append(description)
 
 
64
 
65
+ # Save enhanced image to ZIP in-memory buffer
66
  buffer = io.BytesIO()
67
  image.save(buffer, format='JPEG')
68
  processed_images.append(Image.open(io.BytesIO(buffer.getvalue())))
 
70
  zipf.writestr(os.path.basename(image_file.name), buffer.getvalue())
71
 
72
  zip_buffer.seek(0)
73
+ return processed_images, zip_buffer, descriptions
74
 
75
  iface = gr.Interface(
76
  fn=process_images,
77
  inputs=[
78
+ gr.Files(label="Upload Image Files"),
79
  gr.Checkbox(label="Enhance Images (ESRGAN)"),
80
  gr.Radio(['2x', '4x', '8x'], type="value", value='2x', label='Resolution model'),
81
+ gr.Checkbox(label="Generate Image Descriptions")
 
 
 
 
82
  ],
83
  outputs=[
84
+ gr.Gallery(label="Enhanced Images"),
85
+ gr.File(label="Download Enhanced Images (ZIP)"),
86
+ gr.Textbox(label="Generated Descriptions", lines=5)
87
  ],
88
+ title="Image Enhancer with Description Generator",
89
+ description="Upload multiple images, enhance using AI, generate descriptions using Hugging Face, and download results as a ZIP file."
90
  )
91
 
92
+ iface.launch()