Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -11,11 +11,12 @@ import json
|
|
11 |
import psutil
|
12 |
import sys
|
13 |
from pathlib import Path
|
|
|
14 |
|
15 |
# Workaround for torch.classes and Streamlit compatibility
|
16 |
-
# Exclude torch modules from Streamlit's path watcher
|
17 |
st._is_running_with_streamlit = True
|
18 |
-
sys.modules['torch']
|
|
|
19 |
|
20 |
st.set_page_config(layout="wide")
|
21 |
|
@@ -214,6 +215,7 @@ footer {
|
|
214 |
color: #ffffff;
|
215 |
padding: 3rem 2rem;
|
216 |
margin-top: 3rem;
|
|
|
217 |
border-radius: 1rem 1rem 0 0;
|
218 |
}
|
219 |
.footer-container {
|
@@ -335,7 +337,7 @@ def transcribe_audio(audio, sr, processor, model, device, start_time, language,
|
|
335 |
input_features = input_features.half()
|
336 |
generate_kwargs = {
|
337 |
"task": task,
|
338 |
-
"language": "urdu" if
|
339 |
"max_new_tokens": 128,
|
340 |
"return_timestamps": True,
|
341 |
"do_sample": False
|
@@ -357,7 +359,10 @@ def process_chunks(chunks, sr, processor, model, device, language, chunk_duratio
|
|
357 |
progress = TranscriptionProgress()
|
358 |
progress.init_progress()
|
359 |
if os.path.exists(transcript_file):
|
360 |
-
|
|
|
|
|
|
|
361 |
for i, chunk in enumerate(chunks):
|
362 |
progress.update((i + 1) / total_chunks, f"Processing chunk {i+1}/{total_chunks}...")
|
363 |
try:
|
@@ -387,7 +392,9 @@ def summarize_text(text, tokenizer, model, device, summarizer_type='bart'):
|
|
387 |
try:
|
388 |
inputs = tokenizer(text, return_tensors="pt", truncation=False)
|
389 |
input_ids = inputs["input_ids"].to(device)
|
390 |
-
attention_mask = inputs.get("attention_mask")
|
|
|
|
|
391 |
num_tokens = input_ids.shape[1]
|
392 |
st.write(f"Number of tokens in input: {num_tokens}")
|
393 |
if num_tokens < 50:
|
@@ -464,10 +471,10 @@ def merge_intervals(intervals):
|
|
464 |
return merged
|
465 |
|
466 |
def create_edited_video(video_path, transcript, keep_indices):
|
|
|
467 |
try:
|
468 |
intervals_to_keep = [(transcript[i][1], transcript[i][2]) for i in keep_indices]
|
469 |
merged_intervals = merge_intervals(intervals_to_keep)
|
470 |
-
temp_files = []
|
471 |
for j, (start, end) in enumerate(merged_intervals):
|
472 |
temp_file = f"temp_{j}.mp4"
|
473 |
ffmpeg.input(video_path, ss=start, to=end).output(temp_file, c='copy').run(overwrite_output=True, quiet=True)
|
@@ -484,9 +491,15 @@ def create_edited_video(video_path, transcript, keep_indices):
|
|
484 |
finally:
|
485 |
for temp_file in temp_files:
|
486 |
if os.path.exists(temp_file):
|
487 |
-
|
|
|
|
|
|
|
488 |
if os.path.exists("list.txt"):
|
489 |
-
|
|
|
|
|
|
|
490 |
|
491 |
def generate_srt(transcript, include_timeframe=True):
|
492 |
srt_content = ""
|
@@ -505,13 +518,16 @@ def cleanup_temp_files():
|
|
505 |
if os.path.exists(temp_file):
|
506 |
try:
|
507 |
os.remove(temp_file)
|
508 |
-
|
509 |
-
|
|
|
510 |
for temp_file in glob.glob("temp_*.mp4"):
|
511 |
-
|
512 |
-
|
513 |
-
|
514 |
-
|
|
|
|
|
515 |
|
516 |
# Main Function
|
517 |
def main():
|
@@ -570,7 +586,7 @@ def main():
|
|
570 |
st.markdown("<div id='upload'></div>", unsafe_allow_html=True)
|
571 |
st.markdown("<h3 style='text-align: center; color: black;'>Upload Your Video</h3>", unsafe_allow_html=True)
|
572 |
with st.form(key="upload_form"):
|
573 |
-
uploaded_file = st.file_uploader("
|
574 |
if st.form_submit_button("Upload") and uploaded_file:
|
575 |
video_path = save_uploaded_file(uploaded_file)
|
576 |
if video_path:
|
@@ -732,9 +748,17 @@ def main():
|
|
732 |
if st.session_state.get('video_path') and st.button("Reset"):
|
733 |
cleanup_temp_files()
|
734 |
if st.session_state['video_path'] and os.path.exists(st.session_state['video_path']):
|
735 |
-
|
|
|
|
|
|
|
|
|
736 |
if st.session_state['edited_video_path'] and os.path.exists(st.session_state['edited_video_path']):
|
737 |
-
|
|
|
|
|
|
|
|
|
738 |
st.session_state.clear()
|
739 |
st.rerun()
|
740 |
|
|
|
11 |
import psutil
|
12 |
import sys
|
13 |
from pathlib import Path
|
14 |
+
import glob
|
15 |
|
16 |
# Workaround for torch.classes and Streamlit compatibility
|
|
|
17 |
st._is_running_with_streamlit = True
|
18 |
+
if 'torch' in sys.modules and hasattr(sys.modules['torch'], '__path__'):
|
19 |
+
sys.modules['torch'].__path__ = []
|
20 |
|
21 |
st.set_page_config(layout="wide")
|
22 |
|
|
|
215 |
color: #ffffff;
|
216 |
padding: 3rem 2rem;
|
217 |
margin-top: 3rem;
|
218 |
+
Desk
|
219 |
border-radius: 1rem 1rem 0 0;
|
220 |
}
|
221 |
.footer-container {
|
|
|
337 |
input_features = input_features.half()
|
338 |
generate_kwargs = {
|
339 |
"task": task,
|
340 |
+
"language": "urdu" if language == "ur" else language,
|
341 |
"max_new_tokens": 128,
|
342 |
"return_timestamps": True,
|
343 |
"do_sample": False
|
|
|
359 |
progress = TranscriptionProgress()
|
360 |
progress.init_progress()
|
361 |
if os.path.exists(transcript_file):
|
362 |
+
try:
|
363 |
+
os.remove(transcript_file)
|
364 |
+
except Exception as e:
|
365 |
+
st.warning(f"Failed to remove {transcript_file}: {str(e)}")
|
366 |
for i, chunk in enumerate(chunks):
|
367 |
progress.update((i + 1) / total_chunks, f"Processing chunk {i+1}/{total_chunks}...")
|
368 |
try:
|
|
|
392 |
try:
|
393 |
inputs = tokenizer(text, return_tensors="pt", truncation=False)
|
394 |
input_ids = inputs["input_ids"].to(device)
|
395 |
+
attention_mask = inputs.get("attention_mask")
|
396 |
+
if attention_mask is not None:
|
397 |
+
attention_mask = attention_mask.to(device)
|
398 |
num_tokens = input_ids.shape[1]
|
399 |
st.write(f"Number of tokens in input: {num_tokens}")
|
400 |
if num_tokens < 50:
|
|
|
471 |
return merged
|
472 |
|
473 |
def create_edited_video(video_path, transcript, keep_indices):
|
474 |
+
temp_files = []
|
475 |
try:
|
476 |
intervals_to_keep = [(transcript[i][1], transcript[i][2]) for i in keep_indices]
|
477 |
merged_intervals = merge_intervals(intervals_to_keep)
|
|
|
478 |
for j, (start, end) in enumerate(merged_intervals):
|
479 |
temp_file = f"temp_{j}.mp4"
|
480 |
ffmpeg.input(video_path, ss=start, to=end).output(temp_file, c='copy').run(overwrite_output=True, quiet=True)
|
|
|
491 |
finally:
|
492 |
for temp_file in temp_files:
|
493 |
if os.path.exists(temp_file):
|
494 |
+
try:
|
495 |
+
os.remove(temp_file)
|
496 |
+
except Exception as e:
|
497 |
+
st.warning(f"Failed to remove {temp_file}: {str(e)}")
|
498 |
if os.path.exists("list.txt"):
|
499 |
+
try:
|
500 |
+
os.remove("list.txt")
|
501 |
+
except Exception as e:
|
502 |
+
st.warning(f"Failed to remove list.txt: {str(e)}")
|
503 |
|
504 |
def generate_srt(transcript, include_timeframe=True):
|
505 |
srt_content = ""
|
|
|
518 |
if os.path.exists(temp_file):
|
519 |
try:
|
520 |
os.remove(temp_file)
|
521 |
+
st.info(f"Removed temporary file: {temp_file}")
|
522 |
+
except Exception as e:
|
523 |
+
st.warning(f"Failed to remove {temp_file}: {str(e)}")
|
524 |
for temp_file in glob.glob("temp_*.mp4"):
|
525 |
+
if os.path.exists(temp_file):
|
526 |
+
try:
|
527 |
+
os.remove(temp_file)
|
528 |
+
st.info(f"Removed temporary file: {temp_file}")
|
529 |
+
except Exception as e:
|
530 |
+
st.warning(f"Failed to remove {temp_file}: {str(e)}")
|
531 |
|
532 |
# Main Function
|
533 |
def main():
|
|
|
586 |
st.markdown("<div id='upload'></div>", unsafe_allow_html=True)
|
587 |
st.markdown("<h3 style='text-align: center; color: black;'>Upload Your Video</h3>", unsafe_allow_html=True)
|
588 |
with st.form(key="upload_form"):
|
589 |
+
uploaded_file = st.file_uploader(" Succeeded uploading file: 1
|
590 |
if st.form_submit_button("Upload") and uploaded_file:
|
591 |
video_path = save_uploaded_file(uploaded_file)
|
592 |
if video_path:
|
|
|
748 |
if st.session_state.get('video_path') and st.button("Reset"):
|
749 |
cleanup_temp_files()
|
750 |
if st.session_state['video_path'] and os.path.exists(st.session_state['video_path']):
|
751 |
+
try:
|
752 |
+
os.remove(st.session_state['video_path'])
|
753 |
+
st.info(f"Removed video file: {st.session_state['video_path']}")
|
754 |
+
except Exception as e:
|
755 |
+
st.warning(f"Failed to remove video file: {str(e)}")
|
756 |
if st.session_state['edited_video_path'] and os.path.exists(st.session_state['edited_video_path']):
|
757 |
+
try:
|
758 |
+
os.remove(st.session_state['edited_video_path'])
|
759 |
+
st.info(f"Removed edited video file: {st.session_state['edited_video_path']}")
|
760 |
+
except Exception as e:
|
761 |
+
st.warning(f"Failed to remove edited video file: {str(e)}")
|
762 |
st.session_state.clear()
|
763 |
st.rerun()
|
764 |
|