NourFakih commited on
Commit
5faae13
·
verified ·
1 Parent(s): c29228f

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +53 -30
app.py CHANGED
@@ -9,9 +9,9 @@ from PIL import Image
9
  device = 'cpu'
10
 
11
  # Load the pretrained model, feature extractor, and tokenizer
12
- model = VisionEncoderDecoderModel.from_pretrained("NourFakih/Vit-GPT2-COCO2017Flickr-02").to(device)
13
- feature_extractor = ViTImageProcessor.from_pretrained("NourFakih/Vit-GPT2-COCO2017Flickr-02")
14
- tokenizer = AutoTokenizer.from_pretrained("NourFakih/Vit-GPT2-COCO2017Flickr-02")
15
 
16
  def predict(image, max_length=64, num_beams=4):
17
  # Process the input image
@@ -36,14 +36,7 @@ def process_images(image_files):
36
  except Exception as e:
37
  print(f"Skipping file {image_file}: {e}")
38
 
39
- # Save the results to a CSV file
40
- csv_file_path = 'image_captions.csv'
41
- with open(csv_file_path, mode='w', newline='') as file:
42
- writer = csv.writer(file)
43
- writer.writerow(['Image Name', 'Caption'])
44
- writer.writerows(captions)
45
-
46
- return csv_file_path
47
 
48
  def process_zip_files(zip_file_paths):
49
  # Create a directory to extract images
@@ -67,24 +60,37 @@ def process_zip_files(zip_file_paths):
67
  except Exception as e:
68
  print(f"Skipping file {file}: {e}")
69
 
70
- # Save the results to a CSV file
71
- csv_file_path = 'zip_image_captions.csv'
 
72
  with open(csv_file_path, mode='w', newline='') as file:
73
  writer = csv.writer(file)
74
  writer.writerow(['Image Name', 'Caption'])
75
  writer.writerows(captions)
76
-
77
  return csv_file_path
78
 
79
- def gr_process(zip_files, image_files):
80
- if zip_files:
81
- zip_file_paths = [zip_file.name for zip_file in zip_files]
82
- return process_zip_files(zip_file_paths)
83
- elif image_files:
84
- image_file_paths = [image_file.name for image_file in image_files]
85
- return process_images(image_file_paths)
86
- else:
87
- return None
 
 
 
 
 
 
 
 
 
 
 
 
 
88
 
89
  css = '''
90
  h1#title {
@@ -113,14 +119,31 @@ with demo:
113
  gr.Markdown('''<h1 id="title">Image Caption 🖼️</h1>''')
114
  gr.Markdown('''Made by : No. Fa.''')
115
 
 
 
 
116
  with gr.Row():
117
  with gr.Column(scale=1):
118
- input_zip = gr.File(label="Upload Zip Files", type="filepath", file_count="multiple")
119
- input_images = gr.File(label="Upload Images", type="filepath", file_count="multiple")
120
  with gr.Column(scale=3):
121
- output_file = gr.File(label="Download Caption File")
122
-
123
- btn = gr.Button("Generate Captions")
124
- btn.click(fn=gr_process, inputs=[input_zip, input_images], outputs=output_file)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
125
 
126
- demo.launch()
 
9
  device = 'cpu'
10
 
11
  # Load the pretrained model, feature extractor, and tokenizer
12
+ model = VisionEncoderDecoderModel.from_pretrained("NourFakih/Vit-GPT2-COCO2017Flickr-01").to(device)
13
+ feature_extractor = ViTImageProcessor.from_pretrained("NourFakih/Vit-GPT2-COCO2017Flickr-01")
14
+ tokenizer = AutoTokenizer.from_pretrained("NourFakih/Vit-GPT2-COCO2017Flickr-01")
15
 
16
  def predict(image, max_length=64, num_beams=4):
17
  # Process the input image
 
36
  except Exception as e:
37
  print(f"Skipping file {image_file}: {e}")
38
 
39
+ return captions
 
 
 
 
 
 
 
40
 
41
  def process_zip_files(zip_file_paths):
42
  # Create a directory to extract images
 
60
  except Exception as e:
61
  print(f"Skipping file {file}: {e}")
62
 
63
+ return captions
64
+
65
+ def save_captions_to_csv(captions, csv_file_path='image_captions.csv'):
66
  with open(csv_file_path, mode='w', newline='') as file:
67
  writer = csv.writer(file)
68
  writer.writerow(['Image Name', 'Caption'])
69
  writer.writerows(captions)
 
70
  return csv_file_path
71
 
72
+ def gr_process_zip(zip_files):
73
+ captions = process_zip_files([zip_file.name for zip_file in zip_files])
74
+ return save_captions_to_csv(captions, 'zip_image_captions.csv')
75
+
76
+ def gr_process_images(image_files):
77
+ captions = process_images([image_file.name for image_file in image_files])
78
+ return save_captions_to_csv(captions, 'image_captions.csv')
79
+
80
+ def combine_csv_files(file1, file2, output_file='combined_captions.csv'):
81
+ with open(output_file, mode='w', newline='') as outfile:
82
+ writer = csv.writer(outfile)
83
+ writer.writerow(['Image Name', 'Caption'])
84
+
85
+ for file in [file1, file2]:
86
+ if os.path.exists(file):
87
+ with open(file, mode='r') as infile:
88
+ reader = csv.reader(infile)
89
+ next(reader) # Skip header row
90
+ for row in reader:
91
+ writer.writerow(row)
92
+
93
+ return output_file
94
 
95
  css = '''
96
  h1#title {
 
119
  gr.Markdown('''<h1 id="title">Image Caption 🖼️</h1>''')
120
  gr.Markdown('''Made by : No. Fa.''')
121
 
122
+ zip_files = gr.State([])
123
+ image_files = gr.State([])
124
+
125
  with gr.Row():
126
  with gr.Column(scale=1):
127
+ new_zip_files = gr.File(label="Upload Zip Files", type="filepath", file_count="multiple")
128
+ new_image_files = gr.File(label="Upload Images", type="filepath", file_count="multiple")
129
  with gr.Column(scale=3):
130
+ output_zip_file = gr.File(label="Download Zip Captions")
131
+ output_image_file = gr.File(label="Download Image Captions")
132
+ combined_file = gr.File(label="Download Combined Captions")
133
+
134
+ add_files_btn = gr.Button("Add More Files")
135
+ generate_zip_captions_btn = gr.Button("Generate Zip Captions")
136
+ generate_image_captions_btn = gr.Button("Generate Image Captions")
137
+ combine_files_btn = gr.Button("Combine CSV Files")
138
+
139
+ def add_files(zip_files, image_files, new_zip_files, new_image_files):
140
+ zip_files.extend(new_zip_files)
141
+ image_files.extend(new_image_files)
142
+ return zip_files, image_files
143
+
144
+ add_files_btn.click(add_files, inputs=[zip_files, image_files, new_zip_files, new_image_files], outputs=[zip_files, image_files])
145
+ generate_zip_captions_btn.click(fn=gr_process_zip, inputs=zip_files, outputs=output_zip_file)
146
+ generate_image_captions_btn.click(fn=gr_process_images, inputs=image_files, outputs=output_image_file)
147
+ combine_files_btn.click(fn=combine_csv_files, inputs=[output_zip_file, output_image_file], outputs=combined_file)
148
 
149
+ demo.launch(share=True)