Neprox commited on
Commit
142a301
1 Parent(s): 13b3459

Add translation part

Browse files
Files changed (2) hide show
  1. app.py +26 -13
  2. requirements.txt +1 -0
app.py CHANGED
@@ -5,7 +5,16 @@ from pytube import YouTube
5
  from datasets import Dataset, Audio
6
  from moviepy.editor import AudioFileClip
7
 
 
 
 
8
  pipe = pipeline(model="Neprox/model")
 
 
 
 
 
 
9
 
10
  def download_from_youtube(url):
11
  """
@@ -65,19 +74,21 @@ def divide_into_30s_segments(audio_fpath, seconds_max):
65
 
66
  return segment_paths, segment_start_times
67
 
68
- def get_translation(text):
69
  """
70
- Translates the given Swedish text to English.
71
  """
72
- # TODO: Make API call to Google Translate to get English translation
73
- return "..."
 
 
74
 
75
- def transcribe(audio, url, seconds_max):
76
  """
77
- Transcribes a YouTube video if a url is specified and returns the transcription.
78
- If not url is specified, it transcribes the audio file as passed by Gradio.
79
  :param audio: Audio file as passed by Gradio. Only used if no url is specified.
80
- :param url: YouTube URL to transcribe.
81
  :param seconds_max: Maximum number of seconds to consider. If the audio file is longer than this, it will be truncated.
82
  """
83
  if url:
@@ -91,7 +102,8 @@ def transcribe(audio, url, seconds_max):
91
  for i, (seconds, output) in enumerate(zip(segment_start_times, pred)):
92
  text += f"[Segment {i+1}/{n_segments}, start time {get_timestamp(seconds)}]\n"
93
  text += f"{output['text']}\n"
94
- text += f"[Translation]\n{get_translation(output['text'])}\n\n"
 
95
  return text
96
 
97
  else:
@@ -99,11 +111,12 @@ def transcribe(audio, url, seconds_max):
99
  return text
100
 
101
  iface = gr.Interface(
102
- fn=transcribe,
103
  inputs=[
104
- gr.Audio(source="microphone", type="filepath", label="Transcribe from Microphone"),
105
- gr.Text(max_lines=1, placeholder="Enter YouTube Link with Swedish speech to be transcribed", label="Transcribe from YouTube URL"),
106
- gr.Slider(minimum=30, maximum=300, value=30, step=30, label="Number of seconds to transcribe from YouTube URL")
 
107
  ],
108
  outputs="text",
109
  title="Whisper Small Swedish",
 
5
  from datasets import Dataset, Audio
6
  from moviepy.editor import AudioFileClip
7
 
8
+ import googletrans
9
+ from googletrans import Translator
10
+
11
  pipe = pipeline(model="Neprox/model")
12
+ translator = Translator()
13
+
14
+ # Get languages available for translation
15
+ languages = []
16
+ for code, name in googletrans.LANGUAGES.items():
17
+ languages.append((code, name.capitalize()))
18
 
19
  def download_from_youtube(url):
20
  """
 
74
 
75
  return segment_paths, segment_start_times
76
 
77
+ def get_translation(text, dest="en"):
78
  """
79
+ Translates the given Swedish text to the language specified.
80
  """
81
+ dest_text = dest[0]
82
+ result = translator.translate(text, dest_text, 'sv')
83
+ return result.text
84
+
85
 
86
+ def translate(audio, url, seconds_max, dest_language):
87
  """
88
+ Translates a YouTube video if a url is specified and returns the transcription.
89
+ If not url is specified, it translates the audio file as passed by Gradio.
90
  :param audio: Audio file as passed by Gradio. Only used if no url is specified.
91
+ :param url: URL of the YouTube video to translate.
92
  :param seconds_max: Maximum number of seconds to consider. If the audio file is longer than this, it will be truncated.
93
  """
94
  if url:
 
102
  for i, (seconds, output) in enumerate(zip(segment_start_times, pred)):
103
  text += f"[Segment {i+1}/{n_segments}, start time {get_timestamp(seconds)}]\n"
104
  text += f"{output['text']}\n"
105
+ text += f"[Translation ({dest_language})]\n"
106
+ text += f"{get_translation(output['text'], dest_language)}\n\n"
107
  return text
108
 
109
  else:
 
111
  return text
112
 
113
  iface = gr.Interface(
114
+ fn=translate,
115
  inputs=[
116
+ gr.Audio(source="microphone", type="filepath", label="Translate from Microphone"),
117
+ gr.Text(max_lines=1, placeholder="Enter YouTube Link with Swedish speech to be translated", label="Translate from YouTube URL"),
118
+ gr.Slider(minimum=30, maximum=300, value=30, step=30, label="Number of seconds to translate from YouTube URL"),
119
+ gr.Dropdown(languages, label="Destination language")
120
  ],
121
  outputs="text",
122
  title="Whisper Small Swedish",
requirements.txt CHANGED
@@ -7,3 +7,4 @@ torch
7
  torchaudio
8
  moviepy
9
  git+https://github.com/pytube/pytube
 
 
7
  torchaudio
8
  moviepy
9
  git+https://github.com/pytube/pytube
10
+ googletrans-py==4.0.0rc1