Spaces:
Runtime error
Runtime error
Commit
·
10ad0e5
1
Parent(s):
994310f
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,115 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from gtts import gTTS
|
2 |
+
import gradio as gr
|
3 |
+
from translate import Translator
|
4 |
+
|
5 |
+
auth_token = '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' # For accesing HugginFace Facebook Model.
|
6 |
+
|
7 |
+
class Languages:
|
8 |
+
""" Languages currently supported by the application. """
|
9 |
+
|
10 |
+
lang = {'Afrikaans': 'af','Arabic':'ar','Bulgarian':'bg','Bengali':'bn','Bosnian':'bs',
|
11 |
+
'Catalan':'ca','Czech':'cs','Danish':'da','German':'de','Greek':'el','English':'en',
|
12 |
+
'Spanish':'es','Estonian':'et','Finnish':'fi','French':'fr','Gujarati':'gu','Hindi':'hi',
|
13 |
+
'Croatian':'hr','Hungarian':'hu','Indonesian':'id','Icelandic':'is','Italian':'it',
|
14 |
+
'Hebrew':'iw','Japanese':'ja','Javanese':'jw','Khmer':'km','Kannada':'kn','Korean':'ko',
|
15 |
+
'Latin':'la','Latvian':'lv','Malayalam':'ml','Marathi':'mr','Malay':'ms',
|
16 |
+
'Myanmar (Burmese)':'my','Nepali':'ne', 'Dutch':'nl','Norwegian':'no',
|
17 |
+
'Polish':'pl','Portuguese':'pt','Romanian':'ro','Russian':'ru','Sinhala':'si',
|
18 |
+
'Slovak':'sk', 'Albanian':'sq','Serbian':'sr','Sundanese':'su','Swedish':'sv',
|
19 |
+
'Swahili':'sw','Tamil':'ta','Telugu':'te','Thai':'th','Filipino':'tl','Turkish':'tr',
|
20 |
+
'Ukrainian':'uk','Urdu':'ur','Vietnamese':'vi','Chinese (Simplified)':'zh-CN',
|
21 |
+
'Chinese (Mandarin/Taiwan)':'zh-TW',
|
22 |
+
'Chinese (Mandarin)':'zh'}
|
23 |
+
|
24 |
+
class TLD:
|
25 |
+
""" Depending on the top-level domain, gTTS can speak in different accents. """
|
26 |
+
|
27 |
+
tld = {'English(Australia)':'com.au', 'English (United Kingdom)':'co.uk',
|
28 |
+
'English (United States)':'us', 'English (Canada)':'ca','English (India)':'co.in',
|
29 |
+
'English (Ireland)':'ie','English (South Africa)':'co.za','French (Canada)':'ca',
|
30 |
+
'French (France)':'fr','Portuguese (Brazil)':'com.br','Portuguese (Portugal)':'pt',
|
31 |
+
'Spanish (Mexico)':'com.mx','Spanish (Spain)':'es','Spanish (United States)':'us'}
|
32 |
+
|
33 |
+
class TTSLayer():
|
34 |
+
""" Layer on top of gTTS - providing text to speech for """
|
35 |
+
|
36 |
+
def __init__(self, text, tld, lang) -> None:
|
37 |
+
""" [Constructor takes in text, the top-level domain and the language in which the text is : ] """
|
38 |
+
self.text = text
|
39 |
+
self.tld = tld
|
40 |
+
self.lang = lang
|
41 |
+
|
42 |
+
def tts(self):
|
43 |
+
""" [Converts the text to speech.] """
|
44 |
+
tts = gTTS(text=self.text,tld= TLD.tld[self.tld], lang=Languages.lang[self.lang])
|
45 |
+
tts.save('tts.mp3')
|
46 |
+
with open('tts.mp3') as fp:
|
47 |
+
return fp.name
|
48 |
+
|
49 |
+
langs = Languages()
|
50 |
+
top_level_domain = TLD()
|
51 |
+
|
52 |
+
""" [Utiility Functions] """
|
53 |
+
|
54 |
+
def T2TConversion(text, dest):
|
55 |
+
""" [(Utility Function) : Converts sentence from english to another language ] """
|
56 |
+
translator = Translator(to_lang=langs.lang[dest])
|
57 |
+
return translator.translate(text)
|
58 |
+
|
59 |
+
def convert_text(Text,Language, Accent):
|
60 |
+
""" [(Utility Function) : Performs Text-To-Speech provided language and accent.] """
|
61 |
+
tts = TTSLayer(Text,Accent, Language)
|
62 |
+
return tts.tts()
|
63 |
+
|
64 |
+
""" [Interface for Application] """
|
65 |
+
|
66 |
+
class GRadioInterface:
|
67 |
+
""" [Class for managing UI for the application.] """
|
68 |
+
def __init__(self, function) -> None:
|
69 |
+
""" [Interface for packaging GRadio Application] """
|
70 |
+
|
71 |
+
# Necessary for interface
|
72 |
+
self._function = function
|
73 |
+
self._inputs = [
|
74 |
+
gr.TextArea(label = 'The Text to be Converted to Audio'),
|
75 |
+
gr.Dropdown([key for key,_ in langs.lang.items()], label='Languages Available',),
|
76 |
+
gr.Dropdown([key for key,_ in top_level_domain.tld.items()])]
|
77 |
+
|
78 |
+
self.outputs = gr.Audio()
|
79 |
+
|
80 |
+
# Necessary for descriptive content
|
81 |
+
self.title = 'A Text-To-Speech Converter for Low Resource Languages'
|
82 |
+
self.description = 'Support over 50 languages !'
|
83 |
+
self.article = 'How does it work ? Just write a sentence (in target language) in the space provided and select the target language and accent and press submit. That is it. Wait and Enjoy.'
|
84 |
+
|
85 |
+
def start(self, share=False):
|
86 |
+
""" [Launching the interface in a tabbed manner.] """
|
87 |
+
|
88 |
+
it_1 = gr.Interface(fn=self._function, inputs=self._inputs,outputs=self.outputs,
|
89 |
+
title = self.title,
|
90 |
+
description= self.description,
|
91 |
+
article= self.article)
|
92 |
+
|
93 |
+
it_2 = gr.Interface(fn=T2TConversion,
|
94 |
+
inputs = [
|
95 |
+
gr.Text(label='Write a sentence in English'),
|
96 |
+
gr.Dropdown([key for key,_ in langs.lang.items()])],
|
97 |
+
outputs= gr.Text(label='The Converted Text'),
|
98 |
+
title = 'Translation from English',
|
99 |
+
description='Write a sentence in english and convert to other languages for speech synthesis',
|
100 |
+
article='What if you do not have a sentence in a particular language? Just write the sentence in english and let us do the magic.')
|
101 |
+
|
102 |
+
it_3 = gr.Interface.load(
|
103 |
+
"huggingface/facebook/wav2vec2-base-960h",
|
104 |
+
title="Automatic Speech Recognition",
|
105 |
+
inputs=gr.Audio(),
|
106 |
+
description="What you cookin' ?",
|
107 |
+
article='[Experimental]: Try uploading a audio file for transcription. Currently only working for English Language.',
|
108 |
+
api_key=auth_token
|
109 |
+
)
|
110 |
+
|
111 |
+
demo = gr.TabbedInterface([it_1, it_2, it_3],['Speech Synthesis', 'Sentence Translation', 'Automatic Speech Recognition'])
|
112 |
+
demo.launch(share=share)
|
113 |
+
|
114 |
+
demo_app = GRadioInterface(function=convert_text)
|
115 |
+
demo_app.start(share=True)
|