Spaces:
Runtime error
Runtime error
Update app.py
Browse files
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-
|
13 |
-
feature_extractor = ViTImageProcessor.from_pretrained("NourFakih/Vit-GPT2-COCO2017Flickr-
|
14 |
-
tokenizer = AutoTokenizer.from_pretrained("NourFakih/Vit-GPT2-COCO2017Flickr-
|
15 |
|
16 |
def predict(image, max_length=64, num_beams=4):
|
17 |
# Process the input image
|
@@ -36,7 +36,14 @@ def process_images(image_files):
|
|
36 |
except Exception as e:
|
37 |
print(f"Skipping file {image_file}: {e}")
|
38 |
|
39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
40 |
|
41 |
def process_zip_files(zip_file_paths):
|
42 |
# Create a directory to extract images
|
@@ -60,23 +67,24 @@ def process_zip_files(zip_file_paths):
|
|
60 |
except Exception as e:
|
61 |
print(f"Skipping file {file}: {e}")
|
62 |
|
63 |
-
|
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
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
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)
|
@@ -135,8 +143,8 @@ with demo:
|
|
135 |
combine_files_btn = gr.Button("Combine CSV Files")
|
136 |
|
137 |
|
138 |
-
generate_zip_captions_btn.click(fn=
|
139 |
-
generate_image_captions_btn.click(fn=
|
140 |
combine_files_btn.click(fn=combine_csv_files, inputs=[output_zip_file, output_image_file], outputs=combined_file)
|
141 |
|
142 |
demo.launch()
|
|
|
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 |
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', '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 |
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 |
def combine_csv_files(file1, file2, output_file='combined_captions.csv'):
|
89 |
with open(output_file, mode='w', newline='') as outfile:
|
90 |
writer = csv.writer(outfile)
|
|
|
143 |
combine_files_btn = gr.Button("Combine CSV Files")
|
144 |
|
145 |
|
146 |
+
generate_zip_captions_btn.click(fn=gr_process, inputs=zip_files, outputs=output_zip_file)
|
147 |
+
generate_image_captions_btn.click(fn=gr_process, inputs=image_files, outputs=output_image_file)
|
148 |
combine_files_btn.click(fn=combine_csv_files, inputs=[output_zip_file, output_image_file], outputs=combined_file)
|
149 |
|
150 |
demo.launch()
|