NourFakih commited on
Commit
7896ee4
·
verified ·
1 Parent(s): d169772

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +8 -18
app.py CHANGED
@@ -31,6 +31,7 @@ model.config.eos_token_id = tokenizer.eos_token_id
31
  model.config.decoder_start_token_id = tokenizer.bos_token_id
32
  model.config.pad_token_id = tokenizer.pad_token_id
33
 
 
34
  model_sum_name = "google-t5/t5-base"
35
  tokenizer_sum = AutoTokenizer.from_pretrained("google-t5/t5-base")
36
  model_sum = AutoModelForSeq2SeqLM.from_pretrained("google-t5/t5-base")
@@ -98,19 +99,16 @@ with st.sidebar:
98
  input_option = st.selectbox("Select input method:", ["Folder Path", "Upload Video", "Upload ZIP"])
99
 
100
  video_files = []
101
- original_names = []
102
 
103
  if input_option == "Folder Path":
104
  folder_path = st.text_input("Enter the folder path containing videos:")
105
  if folder_path and os.path.isdir(folder_path):
106
  video_files = [os.path.join(folder_path, f) for f in os.listdir(folder_path) if f.lower().endswith(('mp4', 'avi', 'mov', 'mkv'))]
107
- original_names = [f for f in os.listdir(folder_path) if f.lower().endswith(('mp4', 'avi', 'mov', 'mkv'))]
108
 
109
  elif input_option == "Upload Video":
110
  uploaded_files = st.file_uploader("Upload video files", type=["mp4", "avi", "mov", "mkv"], accept_multiple_files=True)
111
  if uploaded_files:
112
  for uploaded_file in uploaded_files:
113
- original_names.append(uploaded_file.name)
114
  with tempfile.NamedTemporaryFile(delete=False) as temp_file:
115
  temp_file.write(uploaded_file.read())
116
  video_files.append(temp_file.name)
@@ -123,23 +121,22 @@ elif input_option == "Upload ZIP":
123
  with zipfile.ZipFile(temp_file.name, 'r') as zip_ref:
124
  zip_ref.extractall("/tmp/videos")
125
  video_files = [os.path.join("/tmp/videos", f) for f in zip_ref.namelist() if f.lower().endswith(('mp4', 'avi', 'mov', 'mkv'))]
126
- original_names = [f for f in zip_ref.namelist() if f.lower().endswith(('mp4', 'avi', 'mov', 'mkv'))]
127
 
128
  if video_files:
129
  captions = {}
130
- for video_file, original_name in zip(video_files, original_names):
131
  frames, captions_df = process_video(video_file, frame_interval=20)
132
 
133
  if frames and not captions_df.empty:
134
  generated_captions = ' '.join(captions_df['Caption'])
135
  summary = summarize_pipe(generated_captions)[0]['summary_text']
136
- captions[original_name] = summary
137
 
138
  # Display videos in a 4-column grid
139
  cols = st.columns(4)
140
  for idx, (video_path, summary) in enumerate(captions.items()):
141
  with cols[idx % 4]:
142
- st.video(video_files[idx])
143
  st.caption(summary)
144
 
145
  if query:
@@ -149,15 +146,8 @@ if video_files:
149
  st.video(video_path)
150
  st.caption(summary)
151
 
152
- # Save captions to Excel and provide a download button
153
- if st.button("Generate Excel"):
154
  df = pd.DataFrame(list(captions.items()), columns=['Video', 'Caption'])
155
- excel_buffer = io.BytesIO()
156
- with pd.ExcelWriter(excel_buffer, engine='xlsxwriter') as writer:
157
- df.to_excel(writer, index=False, sheet_name='Captions')
158
- st.download_button(
159
- label="Download captions as Excel",
160
- data=excel_buffer,
161
- file_name="captions.xlsx",
162
- mime="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
163
- )
 
31
  model.config.decoder_start_token_id = tokenizer.bos_token_id
32
  model.config.pad_token_id = tokenizer.pad_token_id
33
 
34
+
35
  model_sum_name = "google-t5/t5-base"
36
  tokenizer_sum = AutoTokenizer.from_pretrained("google-t5/t5-base")
37
  model_sum = AutoModelForSeq2SeqLM.from_pretrained("google-t5/t5-base")
 
99
  input_option = st.selectbox("Select input method:", ["Folder Path", "Upload Video", "Upload ZIP"])
100
 
101
  video_files = []
 
102
 
103
  if input_option == "Folder Path":
104
  folder_path = st.text_input("Enter the folder path containing videos:")
105
  if folder_path and os.path.isdir(folder_path):
106
  video_files = [os.path.join(folder_path, f) for f in os.listdir(folder_path) if f.lower().endswith(('mp4', 'avi', 'mov', 'mkv'))]
 
107
 
108
  elif input_option == "Upload Video":
109
  uploaded_files = st.file_uploader("Upload video files", type=["mp4", "avi", "mov", "mkv"], accept_multiple_files=True)
110
  if uploaded_files:
111
  for uploaded_file in uploaded_files:
 
112
  with tempfile.NamedTemporaryFile(delete=False) as temp_file:
113
  temp_file.write(uploaded_file.read())
114
  video_files.append(temp_file.name)
 
121
  with zipfile.ZipFile(temp_file.name, 'r') as zip_ref:
122
  zip_ref.extractall("/tmp/videos")
123
  video_files = [os.path.join("/tmp/videos", f) for f in zip_ref.namelist() if f.lower().endswith(('mp4', 'avi', 'mov', 'mkv'))]
 
124
 
125
  if video_files:
126
  captions = {}
127
+ for video_file in video_files:
128
  frames, captions_df = process_video(video_file, frame_interval=20)
129
 
130
  if frames and not captions_df.empty:
131
  generated_captions = ' '.join(captions_df['Caption'])
132
  summary = summarize_pipe(generated_captions)[0]['summary_text']
133
+ captions[video_file] = summary
134
 
135
  # Display videos in a 4-column grid
136
  cols = st.columns(4)
137
  for idx, (video_path, summary) in enumerate(captions.items()):
138
  with cols[idx % 4]:
139
+ st.video(video_path)
140
  st.caption(summary)
141
 
142
  if query:
 
146
  st.video(video_path)
147
  st.caption(summary)
148
 
149
+ # Save captions to CSV and provide a download button
150
+ if st.button("Generate CSV"):
151
  df = pd.DataFrame(list(captions.items()), columns=['Video', 'Caption'])
152
+ csv = df.to_csv(index=False)
153
+ st.download_button(label="Download captions as CSV", data=csv, file_name="captions.csv", mime="text/csv")