Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,10 +1,9 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
|
| 3 |
import whisper
|
| 4 |
from deep_translator import GoogleTranslator
|
| 5 |
import nltk
|
| 6 |
-
|
| 7 |
from nltk import sent_tokenize
|
|
|
|
| 8 |
nltk.download('punkt')
|
| 9 |
|
| 10 |
def transcribe_audio(audio, model_name, output_file):
|
|
@@ -14,15 +13,32 @@ def transcribe_audio(audio, model_name, output_file):
|
|
| 14 |
with open(output_file, "w", encoding='utf-8') as f:
|
| 15 |
f.write(result["text"])
|
| 16 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
def translate_transcript(transcript_file, target_language, output_file, max_chunk_length=4000):
|
| 18 |
-
print("translating into ",target_language)
|
| 19 |
translator = GoogleTranslator(source='auto', target=target_language)
|
| 20 |
|
| 21 |
with open(transcript_file, 'r', encoding='utf-8') as file:
|
| 22 |
content = file.read()
|
| 23 |
|
| 24 |
sentences = sent_tokenize(content)
|
| 25 |
-
|
| 26 |
translated_chunks = []
|
| 27 |
current_chunk = ""
|
| 28 |
|
|
@@ -30,45 +46,34 @@ def translate_transcript(transcript_file, target_language, output_file, max_chun
|
|
| 30 |
if len(current_chunk) + len(sentence) < max_chunk_length:
|
| 31 |
current_chunk += sentence + " "
|
| 32 |
else:
|
| 33 |
-
|
| 34 |
-
translated_chunk = translator.translate(current_chunk.strip())
|
| 35 |
-
translated_chunks.append(translated_chunk)
|
| 36 |
-
# Start a new chunk with the current sentence
|
| 37 |
current_chunk = sentence + " "
|
| 38 |
|
| 39 |
-
#
|
| 40 |
-
if current_chunk:
|
| 41 |
-
|
| 42 |
-
|
|
|
|
|
|
|
|
|
|
| 43 |
|
| 44 |
-
# Join all translated chunks into a single string
|
| 45 |
translated_text = ' '.join(translated_chunks)
|
| 46 |
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
return translated_text
|
| 51 |
|
| 52 |
-
# Example usage
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
def transcribe(audio, target_language):
|
| 57 |
-
# audio_file = "audios/large.ogg.mp3"
|
| 58 |
transcript_file = "transcript.txt"
|
| 59 |
translated_file = "translated_file.txt"
|
| 60 |
target_language = lang_name_to_code[target_language]
|
| 61 |
|
| 62 |
print("target_language", target_language)
|
| 63 |
-
# Transcribe audio and save the transcript
|
| 64 |
transcribe_audio(audio, model_name="base", output_file=transcript_file)
|
| 65 |
-
|
| 66 |
-
# Translate transcript to the target language
|
| 67 |
output = translate_transcript(transcript_file, target_language=target_language, output_file=translated_file)
|
| 68 |
-
|
| 69 |
return output
|
| 70 |
|
| 71 |
-
# List of top 10 widely used languages with their codes
|
| 72 |
top_languages = [
|
| 73 |
("English", "en"),
|
| 74 |
("Chinese", "zh"),
|
|
@@ -82,15 +87,8 @@ top_languages = [
|
|
| 82 |
("Punjabi", "pa"),
|
| 83 |
]
|
| 84 |
|
| 85 |
-
|
| 86 |
lang_name_to_code = {name: code for name, code in top_languages}
|
| 87 |
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
# Gradio interface
|
| 94 |
demo = gr.Interface(
|
| 95 |
fn=transcribe,
|
| 96 |
inputs=[
|
|
@@ -101,8 +99,3 @@ demo = gr.Interface(
|
|
| 101 |
)
|
| 102 |
|
| 103 |
demo.launch()
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
{'afrikaans': 'af', 'albanian': 'sq', 'amharic': 'am', 'arabic': 'ar', 'armenian': 'hy', 'assamese': 'as', 'aymara': 'ay', 'azerbaijani': 'az', 'bambara': 'bm', 'basque': 'eu', 'belarusian': 'be', 'bengali': 'bn', 'bhojpuri': 'bho', 'bosnian': 'bs', 'bulgarian': 'bg', 'catalan': 'ca', 'cebuano': 'ceb', 'chichewa': 'ny', 'chinese (simplified)': 'zh-CN', 'chinese (traditional)': 'zh-TW', 'corsican': 'co', 'croatian': 'hr', 'czech': 'cs', 'danish': 'da', 'dhivehi': 'dv', 'dogri': 'doi', 'dutch': 'nl', 'english': 'en', 'esperanto': 'eo', 'estonian': 'et', 'ewe': 'ee', 'filipino': 'tl', 'finnish': 'fi', 'french': 'fr', 'frisian': 'fy', 'galician': 'gl', 'georgian': 'ka', 'german': 'de', 'greek': 'el', 'guarani': 'gn', 'gujarati': 'gu', 'haitian creole': 'ht', 'hausa': 'ha', 'hawaiian': 'haw', 'hebrew': 'iw', 'hindi': 'hi', 'hmong': 'hmn', 'hungarian': 'hu', 'icelandic': 'is', 'igbo': 'ig', 'ilocano': 'ilo', 'indonesian': 'id', 'irish': 'ga', 'italian': 'it', 'japanese': 'ja', 'javanese': 'jw', 'kannada': 'kn', 'kazakh': 'kk', 'khmer': 'km', 'kinyarwanda': 'rw', 'konkani': 'gom', 'korean': 'ko', 'krio': 'kri', 'kurdish (kurmanji)': 'ku', 'kurdish (sorani)': 'ckb', 'kyrgyz': 'ky', 'lao': 'lo', 'latin': 'la', 'latvian': 'lv', 'lingala': 'ln', 'lithuanian': 'lt', 'luganda': 'lg', 'luxembourgish': 'lb', 'macedonian': 'mk', 'maithili': 'mai', 'malagasy': 'mg', 'malay': 'ms', 'malayalam': 'ml', 'maltese': 'mt', 'maori': 'mi', 'marathi': 'mr', 'meiteilon (manipuri)': 'mni-Mtei', 'mizo': 'lus', 'mongolian': 'mn', 'myanmar': 'my', 'nepali': 'ne', 'norwegian': 'no', 'odia (oriya)': 'or', 'oromo': 'om', 'pashto': 'ps', 'persian': 'fa', 'polish': 'pl', 'portuguese': 'pt', 'punjabi': 'pa', 'quechua': 'qu', 'romanian': 'ro', 'russian': 'ru', 'samoan': 'sm', 'sanskrit': 'sa', 'scots gaelic': 'gd', 'sepedi': 'nso', 'serbian': 'sr', 'sesotho': 'st', 'shona': 'sn', 'sindhi': 'sd', 'sinhala': 'si', 'slovak': 'sk', 'slovenian': 'sl', 'somali': 'so', 'spanish': 'es', 'sundanese': 'su', 'swahili': 'sw', 'swedish': 'sv', 'tajik': 'tg', 'tamil': 'ta', 'tatar': 'tt', 'telugu': 'te', 'thai': 'th', 'tigrinya': 'ti', 'tsonga': 'ts', 'turkish': 'tr', 'turkmen': 'tk', 'twi': 'ak', 'ukrainian': 'uk', 'urdu': 'ur', 'uyghur': 'ug', 'uzbek': 'uz', 'vietnamese': 'vi', 'welsh': 'cy', 'xhosa': 'xh', 'yiddish': 'yi', 'yoruba': 'yo', 'zulu': 'zu'}
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
import whisper
|
| 3 |
from deep_translator import GoogleTranslator
|
| 4 |
import nltk
|
|
|
|
| 5 |
from nltk import sent_tokenize
|
| 6 |
+
|
| 7 |
nltk.download('punkt')
|
| 8 |
|
| 9 |
def transcribe_audio(audio, model_name, output_file):
|
|
|
|
| 13 |
with open(output_file, "w", encoding='utf-8') as f:
|
| 14 |
f.write(result["text"])
|
| 15 |
|
| 16 |
+
def split_text(text, max_length=4000):
|
| 17 |
+
"""Split text into chunks of max_length, attempting to split at sentence boundaries."""
|
| 18 |
+
sentences = sent_tokenize(text)
|
| 19 |
+
chunks = []
|
| 20 |
+
current_chunk = ""
|
| 21 |
+
|
| 22 |
+
for sentence in sentences:
|
| 23 |
+
if len(current_chunk) + len(sentence) < max_length:
|
| 24 |
+
current_chunk += sentence + " "
|
| 25 |
+
else:
|
| 26 |
+
chunks.append(current_chunk.strip())
|
| 27 |
+
current_chunk = sentence + " "
|
| 28 |
+
|
| 29 |
+
if current_chunk:
|
| 30 |
+
chunks.append(current_chunk.strip())
|
| 31 |
+
|
| 32 |
+
return chunks
|
| 33 |
+
|
| 34 |
def translate_transcript(transcript_file, target_language, output_file, max_chunk_length=4000):
|
| 35 |
+
print("translating into ", target_language)
|
| 36 |
translator = GoogleTranslator(source='auto', target=target_language)
|
| 37 |
|
| 38 |
with open(transcript_file, 'r', encoding='utf-8') as file:
|
| 39 |
content = file.read()
|
| 40 |
|
| 41 |
sentences = sent_tokenize(content)
|
|
|
|
| 42 |
translated_chunks = []
|
| 43 |
current_chunk = ""
|
| 44 |
|
|
|
|
| 46 |
if len(current_chunk) + len(sentence) < max_chunk_length:
|
| 47 |
current_chunk += sentence + " "
|
| 48 |
else:
|
| 49 |
+
translated_chunks.append(translator.translate(current_chunk.strip()))
|
|
|
|
|
|
|
|
|
|
| 50 |
current_chunk = sentence + " "
|
| 51 |
|
| 52 |
+
# Handle last chunk
|
| 53 |
+
if len(current_chunk) >= max_chunk_length:
|
| 54 |
+
small_chunks = split_text(current_chunk, max_chunk_length)
|
| 55 |
+
for small_chunk in small_chunks:
|
| 56 |
+
translated_chunks.append(translator.translate(small_chunk.strip()))
|
| 57 |
+
else:
|
| 58 |
+
translated_chunks.append(translator.translate(current_chunk.strip()))
|
| 59 |
|
|
|
|
| 60 |
translated_text = ' '.join(translated_chunks)
|
| 61 |
|
| 62 |
+
with open(output_file, 'w', encoding='utf-8') as file:
|
| 63 |
+
file.write(translated_text)
|
| 64 |
+
|
| 65 |
return translated_text
|
| 66 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
def transcribe(audio, target_language):
|
|
|
|
| 68 |
transcript_file = "transcript.txt"
|
| 69 |
translated_file = "translated_file.txt"
|
| 70 |
target_language = lang_name_to_code[target_language]
|
| 71 |
|
| 72 |
print("target_language", target_language)
|
|
|
|
| 73 |
transcribe_audio(audio, model_name="base", output_file=transcript_file)
|
|
|
|
|
|
|
| 74 |
output = translate_transcript(transcript_file, target_language=target_language, output_file=translated_file)
|
|
|
|
| 75 |
return output
|
| 76 |
|
|
|
|
| 77 |
top_languages = [
|
| 78 |
("English", "en"),
|
| 79 |
("Chinese", "zh"),
|
|
|
|
| 87 |
("Punjabi", "pa"),
|
| 88 |
]
|
| 89 |
|
|
|
|
| 90 |
lang_name_to_code = {name: code for name, code in top_languages}
|
| 91 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
demo = gr.Interface(
|
| 93 |
fn=transcribe,
|
| 94 |
inputs=[
|
|
|
|
| 99 |
)
|
| 100 |
|
| 101 |
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|