abiabidali commited on
Commit
d4208db
·
verified ·
1 Parent(s): c874c8f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -22
app.py CHANGED
@@ -1,16 +1,19 @@
1
 
 
 
2
  from PIL import Image
3
  from RealESRGAN import RealESRGAN
4
  import gradio as gr
5
  import numpy as np
6
  import tempfile
7
  import time
8
- import zipfile
9
  import os
 
10
 
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'
@@ -27,6 +30,10 @@ model2 = load_model(2)
27
  model4 = load_model(4)
28
  model8 = load_model(8)
29
 
 
 
 
 
30
  def enhance_image(image, scale):
31
  try:
32
  print(f"Enhancing image with scale {scale}...")
@@ -48,6 +55,18 @@ def enhance_image(image, scale):
48
  print(f"Error enhancing image: {e}")
49
  return image
50
 
 
 
 
 
 
 
 
 
 
 
 
 
51
  def muda_dpi(input_image, dpi):
52
  dpi_tuple = (dpi, dpi)
53
  image = Image.fromarray(input_image.astype('uint8'), 'RGB')
@@ -56,6 +75,7 @@ def muda_dpi(input_image, dpi):
56
  temp_file.close()
57
  return Image.open(temp_file.name)
58
 
 
59
  def resize_image(input_image, width, height):
60
  image = Image.fromarray(input_image.astype('uint8'), 'RGB')
61
  resized_image = image.resize((width, height))
@@ -64,9 +84,11 @@ def resize_image(input_image, width, height):
64
  temp_file.close()
65
  return Image.open(temp_file.name)
66
 
 
67
  def process_images(image_files, enhance, scale, adjust_dpi, dpi, resize, width, height):
68
  processed_images = []
69
- temp_dir = tempfile.mkdtemp()
 
70
 
71
  for image_file in image_files:
72
  input_image = np.array(Image.open(image_file).convert('RGB'))
@@ -81,23 +103,27 @@ def process_images(image_files, enhance, scale, adjust_dpi, dpi, resize, width,
81
  if resize:
82
  original_image = resize_image(np.array(original_image), width, height)
83
 
84
- # Save each image as JPEG, preserving the original filename
85
- file_name = os.path.basename(image_file.name)
86
- output_path = os.path.join(temp_dir, file_name)
 
 
 
 
 
 
 
 
 
 
87
  original_image.save(output_path, format='JPEG')
88
- processed_images.append(output_path)
89
-
90
- # Create a ZIP file with all processed images
91
- zip_path = os.path.join(temp_dir, 'processed_images.zip')
92
- with zipfile.ZipFile(zip_path, 'w') as zipf:
93
- for file_path in processed_images:
94
- zipf.write(file_path, os.path.basename(file_path))
95
-
96
- # Load images for display in the gallery
97
- display_images = [Image.open(img_path) for img_path in processed_images]
98
 
99
- return display_images, zip_path
100
 
 
101
  iface = gr.Interface(
102
  fn=process_images,
103
  inputs=[
@@ -111,11 +137,12 @@ iface = gr.Interface(
111
  gr.Number(label="Height", value=512)
112
  ],
113
  outputs=[
114
- gr.Gallery(label="Final Images"), # Display the processed images
115
- gr.File(label="Download Final Images (ZIP)") # Provide a ZIP file for download
 
116
  ],
117
- title="bulk image upscaler",
118
- description="Upload multiple images (.jpg, .png), enhance using AI, adjust DPI, resize, and download the final results as a ZIP file."
119
  )
120
 
121
- iface.launch(debug=True)
 
1
 
2
+
3
+ import torch
4
  from PIL import Image
5
  from RealESRGAN import RealESRGAN
6
  import gradio as gr
7
  import numpy as np
8
  import tempfile
9
  import time
 
10
  import os
11
+ from transformers import pipeline # For Hugging Face image description generation
12
 
13
+ # Check for GPU availability
14
  device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
15
 
16
+ # Load RealESRGAN model with specified scale
17
  def load_model(scale):
18
  model = RealESRGAN(device, scale=scale)
19
  weights_path = f'weights/RealESRGAN_x{scale}.pth'
 
30
  model4 = load_model(4)
31
  model8 = load_model(8)
32
 
33
+ # Hugging Face image description pipeline
34
+ description_generator = pipeline("image-to-text", model="nlpconnect/vit-gpt2-image-captioning")
35
+
36
+ # Enhance image based on selected scale
37
  def enhance_image(image, scale):
38
  try:
39
  print(f"Enhancing image with scale {scale}...")
 
55
  print(f"Error enhancing image: {e}")
56
  return image
57
 
58
+ # Generate image description using Hugging Face Transformers
59
+ def generate_description(image):
60
+ try:
61
+ print("Generating description for the image...")
62
+ description = description_generator(image)[0]['generated_text']
63
+ print(f"Description generated: {description}")
64
+ return description
65
+ except Exception as e:
66
+ print(f"Error generating description: {e}")
67
+ return "Description unavailable."
68
+
69
+ # Adjust DPI of an image
70
  def muda_dpi(input_image, dpi):
71
  dpi_tuple = (dpi, dpi)
72
  image = Image.fromarray(input_image.astype('uint8'), 'RGB')
 
75
  temp_file.close()
76
  return Image.open(temp_file.name)
77
 
78
+ # Resize an image to specified dimensions
79
  def resize_image(input_image, width, height):
80
  image = Image.fromarray(input_image.astype('uint8'), 'RGB')
81
  resized_image = image.resize((width, height))
 
84
  temp_file.close()
85
  return Image.open(temp_file.name)
86
 
87
+ # Process a list of images with various options
88
  def process_images(image_files, enhance, scale, adjust_dpi, dpi, resize, width, height):
89
  processed_images = []
90
+ file_paths = []
91
+ descriptions = [] # List to store descriptions
92
 
93
  for image_file in image_files:
94
  input_image = np.array(Image.open(image_file).convert('RGB'))
 
103
  if resize:
104
  original_image = resize_image(np.array(original_image), width, height)
105
 
106
+ # Generate description
107
+ description = generate_description(original_image)
108
+ descriptions.append(description)
109
+
110
+ # Sanitize the base filename
111
+ base_name = os.path.basename(image_file.name)
112
+ file_name, _ = os.path.splitext(base_name)
113
+
114
+ # Remove any characters that aren't alphanumeric, spaces, underscores, or hyphens
115
+ file_name = ''.join(e for e in file_name if e.isalnum() or e in (' ', '_', '-')).strip().replace(' ', '_')
116
+
117
+ # Create a final file path without unnecessary suffixes
118
+ output_path = os.path.join(tempfile.gettempdir(), f"{file_name}.jpg")
119
  original_image.save(output_path, format='JPEG')
120
+
121
+ processed_images.append(original_image)
122
+ file_paths.append(output_path)
 
 
 
 
 
 
 
123
 
124
+ return processed_images, file_paths, descriptions
125
 
126
+ # Set up Gradio interface with share=True for public access
127
  iface = gr.Interface(
128
  fn=process_images,
129
  inputs=[
 
137
  gr.Number(label="Height", value=512)
138
  ],
139
  outputs=[
140
+ gr.Gallery(label="Final Images"), # Use gr.Gallery to display multiple images
141
+ gr.Files(label="Download Final Images"),
142
+ gr.Textbox(label="Image Descriptions", lines=5) # Display generated descriptions
143
  ],
144
+ title="Multi-Image Enhancer with Hugging Face Descriptions",
145
+ description="Upload multiple images (.jpg, .png), enhance using AI, adjust DPI, resize, generate descriptions, and download the final results."
146
  )
147
 
148
+ iface.launch(debug=True, share=True)