Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -65,20 +65,32 @@ def process_zip_files(zip_file_paths):
|
|
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
|
69 |
writer.writerows(captions)
|
70 |
return csv_file_path
|
71 |
|
72 |
-
def
|
73 |
-
captions = []
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
80 |
|
81 |
-
return
|
82 |
|
83 |
css = '''
|
84 |
h1#title {
|
@@ -113,17 +125,25 @@ with demo:
|
|
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 |
-
zip_captions_btn = gr.Button("Generate Zip Captions")
|
117 |
-
output_zip_file = gr.File(label="Download Caption File")
|
118 |
-
with gr.Column(scale=1):
|
119 |
new_image_files = gr.File(label="Upload Images", type="filepath", file_count="multiple")
|
120 |
-
|
121 |
-
|
|
|
|
|
122 |
|
123 |
-
|
|
|
|
|
|
|
124 |
|
125 |
-
|
126 |
-
|
127 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
128 |
|
129 |
-
demo.launch(share=True)
|
|
|
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', '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 {
|
|
|
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)
|