Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -20,27 +20,30 @@ languages = {
|
|
20 |
"Russian (ru)": "ru"
|
21 |
}
|
22 |
|
23 |
-
def generate_subtitles(
|
24 |
try:
|
25 |
# Extract the target language code from the selected language name
|
26 |
target_language = languages[language_name]
|
27 |
|
28 |
-
#
|
29 |
-
|
|
|
|
|
|
|
30 |
|
31 |
-
|
|
|
|
|
32 |
video = mp.VideoFileClip(video_path)
|
33 |
audio = video.audio
|
34 |
audio.write_audiofile("temp_audio.wav", codec='pcm_s16le')
|
35 |
|
36 |
-
# Log transcription start
|
37 |
print("Starting speech-to-text transcription")
|
38 |
-
|
39 |
# Convert speech to text (ASR using Whisper)
|
40 |
with open("temp_audio.wav", "rb") as audio_file:
|
41 |
transcription = asr(audio_file)["text"]
|
42 |
|
43 |
-
# Log translation start
|
44 |
print("Starting translation")
|
45 |
|
46 |
# Translate transcription to the target language using M2M100
|
@@ -62,10 +65,9 @@ def generate_subtitles(video_path, language_name):
|
|
62 |
# Define Gradio interface
|
63 |
def subtitle_video(video_file, language_name):
|
64 |
try:
|
65 |
-
#
|
66 |
return generate_subtitles(video_file, language_name)
|
67 |
except Exception as e:
|
68 |
-
# Catch and display any high-level errors
|
69 |
print(f"Error in processing video: {e}")
|
70 |
return f"Error in processing video: {e}"
|
71 |
|
|
|
20 |
"Russian (ru)": "ru"
|
21 |
}
|
22 |
|
23 |
+
def generate_subtitles(video_file, language_name):
|
24 |
try:
|
25 |
# Extract the target language code from the selected language name
|
26 |
target_language = languages[language_name]
|
27 |
|
28 |
+
# Check if video_file is a file object or a file path string
|
29 |
+
if isinstance(video_file, str):
|
30 |
+
video_path = video_file # It's a file path
|
31 |
+
else:
|
32 |
+
video_path = video_file.name # It's a file object
|
33 |
|
34 |
+
print(f"Processing video from path: {video_path}")
|
35 |
+
|
36 |
+
# Extract audio from video using moviepy
|
37 |
video = mp.VideoFileClip(video_path)
|
38 |
audio = video.audio
|
39 |
audio.write_audiofile("temp_audio.wav", codec='pcm_s16le')
|
40 |
|
|
|
41 |
print("Starting speech-to-text transcription")
|
42 |
+
|
43 |
# Convert speech to text (ASR using Whisper)
|
44 |
with open("temp_audio.wav", "rb") as audio_file:
|
45 |
transcription = asr(audio_file)["text"]
|
46 |
|
|
|
47 |
print("Starting translation")
|
48 |
|
49 |
# Translate transcription to the target language using M2M100
|
|
|
65 |
# Define Gradio interface
|
66 |
def subtitle_video(video_file, language_name):
|
67 |
try:
|
68 |
+
# Handle both file-like objects and file paths
|
69 |
return generate_subtitles(video_file, language_name)
|
70 |
except Exception as e:
|
|
|
71 |
print(f"Error in processing video: {e}")
|
72 |
return f"Error in processing video: {e}"
|
73 |
|