Spaces:
Paused
Paused
Download app.py from Easyworkstation/caspr: direct link, hf CLI and curl.
- Browser
- Download file 4.3 kB
-
https://huggingface.co/spaces/Easyworkstation/caspr/resolve/fdbd781b2764558c63a2f904741bb721e13d364e/app.py
- Command line
-
hf download hf://spaces/Easyworkstation/caspr@fdbd781b2764558c63a2f904741bb721e13d364e/app.py
-
curl -L -o app.py https://huggingface.co/spaces/Easyworkstation/caspr/resolve/fdbd781b2764558c63a2f904741bb721e13d364e/app.py
4.3 kB
| import gradio as gr | |
| from transformers import AutoModelForSeq2SeqLM, AutoTokenizer | |
| from subprocess import run | |
| from faster_whisper import WhisperModel | |
| import json | |
| import tempfile | |
| import os | |
| import ffmpeg | |
| from zipfile import ZipFile | |
| import stat | |
| import uuid | |
| import subprocess | |
| ZipFile("ffmpeg.zip").extractall() | |
| st = os.stat('ffmpeg') | |
| os.chmod('ffmpeg', st.st_mode | stat.S_IEXEC) | |
| with open('language_codes.json', 'r') as f: | |
| lang_codes = json.load(f) | |
| tokenizer = AutoTokenizer.from_pretrained("facebook/nllb-200-distilled-600M") | |
| model = AutoModelForSeq2SeqLM.from_pretrained("facebook/nllb-200-distilled-600M") | |
| whisper_model = WhisperModel("large-v2", device="cuda", compute_type="float16") | |
| def process_video(Video, target_language): | |
| print("Iniciando process_video") | |
| print("Checking FFmpeg availability...") | |
| run(["ffmpeg", "-version"]) | |
| audio_file = f"{uuid.uuid4()}.wav" | |
| run(["ffmpeg", "-i", Video, audio_file]) | |
| print("Iniciando transcrição com Whisper") | |
| segments, _ = whisper_model.transcribe(audio_file, beam_size=5) | |
| segments = list(segments) | |
| transcript_file = f"{uuid.uuid4()}.srt" | |
| with open(transcript_file, "w", encoding="utf-8") as f: | |
| counter = 1 | |
| for segment in segments: | |
| start_minutes = int(segment.start // 60) | |
| start_seconds = int(segment.start % 60) | |
| start_milliseconds = int((segment.start - int(segment.start)) * 1000) | |
| end_minutes = int(segment.end // 60) | |
| end_seconds = int(segment.end % 60) | |
| end_milliseconds = int((segment.end - int(segment.end)) * 1000) | |
| formatted_start = f"{start_minutes:02d}:{start_seconds:02d},{start_milliseconds:03d}" | |
| formatted_end = f"{end_minutes:02d}:{end_seconds:02d},{end_milliseconds:03d}" | |
| f.write(f"{counter}\n") | |
| f.write(f"{formatted_start} --> {formatted_end}\n") | |
| f.write(f"{segment.text}\n\n") | |
| counter += 1 | |
| flores_code = lang_codes.get(target_language, "eng_Latn") | |
| translated_file = f"{uuid.uuid4()}.srt" | |
| with open(transcript_file, "r", encoding="utf-8") as infile, open(translated_file, "w", encoding="utf-8") as outfile: | |
| for line in infile: | |
| if line.strip().isnumeric() or "-->" in line: | |
| outfile.write(line) | |
| elif line.strip() != "": | |
| inputs = tokenizer(line.strip(), return_tensors="pt") | |
| translated_tokens = model.generate(**inputs, forced_bos_token_id=tokenizer.lang_code_to_id[flores_code], max_length=100) | |
| translated_text = tokenizer.batch_decode(translated_tokens, skip_special_tokens=True)[0] | |
| outfile.write(translated_text + "\n") | |
| else: | |
| outfile.write("\n") | |
| output_video = "output_video.mp4" | |
| # Debugging: Validate FFmpeg command for subtitle embedding | |
| print("Validating FFmpeg command for subtitle embedding...") | |
| print(f"Translated SRT file: {translated_file}") | |
| with open(translated_file, 'r', encoding='utf-8') as f: | |
| print(f"First few lines of translated SRT: {f.readlines()[:10]}") | |
| if os.path.exists(translated_file): | |
| print(f"{translated_file} exists.") | |
| else: | |
| print(f"{translated_file} does not exist.") | |
| try: | |
| translated_file_abs_path = os.path.abspath(translated_file) | |
| result = subprocess.run(["ffmpeg", "-i", Video, "-vf", f"subtitles={translated_file_abs_path}", output_video], capture_output=True, text=True) | |
| if result.returncode == 0: | |
| print("FFmpeg executed successfully.") | |
| else: | |
| print(f"FFmpeg failed with return code {result.returncode}.") | |
| print("Stdout:", result.stdout) | |
| print("Stderr:", result.stderr) | |
| except Exception as e: | |
| print(f"Exception occurred: {e}") | |
| print("process_video concluído com sucesso") | |
| os.unlink(audio_file) | |
| os.unlink(transcript_file) | |
| os.unlink(translated_file) | |
| return output_video | |
| iface = gr.Interface( | |
| fn=process_video, | |
| inputs=[ | |
| gr.Video(), | |
| gr.Dropdown(choices=list(lang_codes.keys()), label="Target Language for Dubbing", value="English"), | |
| ], | |
| outputs=gr.Video(), | |
| live=False, | |
| title="VIDEO TRANSCRIPTION AND TRANSLATION" | |
| ) | |
| iface.launch() | |