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
|
|
@@ -35,10 +35,9 @@ def process_images(image_files):
|
|
| 35 |
captions.append((os.path.basename(image_file), caption))
|
| 36 |
except Exception as e:
|
| 37 |
print(f"Skipping file {image_file}: {e}")
|
| 38 |
-
|
| 39 |
-
return
|
| 40 |
|
| 41 |
-
|
| 42 |
def process_zip_files(zip_file_paths):
|
| 43 |
# Create a directory to extract images
|
| 44 |
extract_dir = 'extracted_images'
|
|
@@ -60,8 +59,8 @@ def process_zip_files(zip_file_paths):
|
|
| 60 |
captions.append((file, caption))
|
| 61 |
except Exception as e:
|
| 62 |
print(f"Skipping file {file}: {e}")
|
| 63 |
-
|
| 64 |
-
return
|
| 65 |
|
| 66 |
def save_captions_to_csv(captions, csv_file_path='image_captions.csv'):
|
| 67 |
with open(csv_file_path, mode='w', newline='') as file:
|
|
@@ -71,34 +70,15 @@ def save_captions_to_csv(captions, csv_file_path='image_captions.csv'):
|
|
| 71 |
return csv_file_path
|
| 72 |
|
| 73 |
def gr_process(zip_files, image_files):
|
|
|
|
| 74 |
if zip_files:
|
| 75 |
zip_file_paths = [zip_file.name for zip_file in zip_files]
|
| 76 |
-
|
| 77 |
-
|
| 78 |
image_file_paths = [image_file.name for image_file in image_files]
|
| 79 |
-
|
| 80 |
-
else:
|
| 81 |
-
return None
|
| 82 |
-
|
| 83 |
-
def combine_csv_files(file1, file2, output_file='combined_captions.csv'):
|
| 84 |
-
with open(output_file, mode='w', newline='') as outfile:
|
| 85 |
-
writer = csv.writer(outfile)
|
| 86 |
-
writer.writerow(['Image Name', 'Caption'])
|
| 87 |
-
|
| 88 |
-
for file in [file1, file2]:
|
| 89 |
-
if os.path.exists(file):
|
| 90 |
-
with open(file, mode='r') as infile:
|
| 91 |
-
reader = csv.reader(infile)
|
| 92 |
-
next(reader) # Skip header row
|
| 93 |
-
for row in reader:
|
| 94 |
-
writer.writerow(row)
|
| 95 |
|
| 96 |
-
return
|
| 97 |
-
|
| 98 |
-
def add_files(zip_files, image_files, new_zip_files, new_image_files):
|
| 99 |
-
zip_files += new_zip_files
|
| 100 |
-
image_files += new_image_files
|
| 101 |
-
return zip_files, image_files
|
| 102 |
|
| 103 |
css = '''
|
| 104 |
h1#title {
|
|
@@ -125,7 +105,7 @@ demo = gr.Blocks(css=css)
|
|
| 125 |
|
| 126 |
with demo:
|
| 127 |
gr.Markdown('''<h1 id="title">Image Caption 🖼️</h1>''')
|
| 128 |
-
gr.Markdown('''Made by :
|
| 129 |
|
| 130 |
zip_files = gr.State([])
|
| 131 |
image_files = gr.State([])
|
|
@@ -133,21 +113,19 @@ with demo:
|
|
| 133 |
with gr.Row():
|
| 134 |
with gr.Column(scale=1):
|
| 135 |
new_zip_files = gr.File(label="Upload Zip Files", type="filepath", file_count="multiple")
|
| 136 |
-
generate_zip_captions_btn = gr.Button("Generate Zip Captions")
|
| 137 |
-
output_zip_file = gr.File(label="Download Zip Captions", interactive=False)
|
| 138 |
-
with gr.Column(scale=1):
|
| 139 |
new_image_files = gr.File(label="Upload Images", type="filepath", file_count="multiple")
|
| 140 |
-
|
| 141 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 142 |
|
| 143 |
-
with gr.Row():
|
| 144 |
-
add_files_btn = gr.Button("Add More Files")
|
| 145 |
-
combine_files_btn = gr.Button("Combine CSV Files")
|
| 146 |
-
combined_file = gr.File(label="Download Combined Captions", interactive=False)
|
| 147 |
-
|
| 148 |
add_files_btn.click(add_files, inputs=[zip_files, image_files, new_zip_files, new_image_files], outputs=[zip_files, image_files])
|
| 149 |
-
|
| 150 |
-
generate_image_captions_btn.click(fn=gr_process, inputs=image_files, outputs=output_image_file)
|
| 151 |
-
combine_files_btn.click(fn=combine_csv_files, inputs=[output_zip_file, output_image_file], outputs=combined_file)
|
| 152 |
|
| 153 |
demo.launch(share=True)
|
|
|
|
| 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
|
|
|
|
| 35 |
captions.append((os.path.basename(image_file), caption))
|
| 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
|
| 43 |
extract_dir = 'extracted_images'
|
|
|
|
| 59 |
captions.append((file, caption))
|
| 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:
|
|
|
|
| 70 |
return csv_file_path
|
| 71 |
|
| 72 |
def gr_process(zip_files, image_files):
|
| 73 |
+
captions = []
|
| 74 |
if zip_files:
|
| 75 |
zip_file_paths = [zip_file.name for zip_file in zip_files]
|
| 76 |
+
captions.extend(process_zip_files(zip_file_paths))
|
| 77 |
+
if image_files:
|
| 78 |
image_file_paths = [image_file.name for image_file in image_files]
|
| 79 |
+
captions.extend(process_images(image_file_paths))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
|
| 81 |
+
return save_captions_to_csv(captions)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
|
| 83 |
css = '''
|
| 84 |
h1#title {
|
|
|
|
| 105 |
|
| 106 |
with demo:
|
| 107 |
gr.Markdown('''<h1 id="title">Image Caption 🖼️</h1>''')
|
| 108 |
+
gr.Markdown('''Made by : No. Fa.''')
|
| 109 |
|
| 110 |
zip_files = gr.State([])
|
| 111 |
image_files = gr.State([])
|
|
|
|
| 113 |
with gr.Row():
|
| 114 |
with gr.Column(scale=1):
|
| 115 |
new_zip_files = gr.File(label="Upload Zip Files", type="filepath", file_count="multiple")
|
|
|
|
|
|
|
|
|
|
| 116 |
new_image_files = gr.File(label="Upload Images", type="filepath", file_count="multiple")
|
| 117 |
+
with gr.Column(scale=3):
|
| 118 |
+
output_file = gr.File(label="Download Caption File")
|
| 119 |
+
|
| 120 |
+
add_files_btn = gr.Button("Add More Files")
|
| 121 |
+
generate_captions_btn = gr.Button("Generate Captions")
|
| 122 |
+
|
| 123 |
+
def add_files(zip_files, image_files, new_zip_files, new_image_files):
|
| 124 |
+
zip_files.extend(new_zip_files)
|
| 125 |
+
image_files.extend(new_image_files)
|
| 126 |
+
return zip_files, image_files
|
| 127 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 128 |
add_files_btn.click(add_files, inputs=[zip_files, image_files, new_zip_files, new_image_files], outputs=[zip_files, image_files])
|
| 129 |
+
generate_captions_btn.click(fn=gr_process, inputs=[zip_files, image_files], outputs=output_file)
|
|
|
|
|
|
|
| 130 |
|
| 131 |
demo.launch(share=True)
|