ysharma HF staff commited on
Commit
b31d4fb
1 Parent(s): ee1afde
Files changed (1) hide show
  1. app.py +74 -16
app.py CHANGED
@@ -5,32 +5,70 @@ import requests
5
  import tempfile
6
  from neon_tts_plugin_coqui import CoquiTTS
7
 
 
 
 
8
  # Whisper: Speech-to-text
9
  model = whisper.load_model("base")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
 
11
  # LLM : Bloom as inference
12
  API_URL = "https://api-inference.huggingface.co/models/bigscience/bloom"
13
  HF_TOKEN = os.environ["HF_TOKEN"]
14
  headers = {"Authorization": f"Bearer {HF_TOKEN}"}
15
- #Language covered in Bloom : en, fr, esp, arb, hn, portu, Indonesian, Vietnamese, Chinese, tamil, telugu, bengali
 
 
16
 
17
  # Text-to-Speech
18
  LANGUAGES = list(CoquiTTS.langs.keys())
 
19
  print(f"Languages for Coqui are: {LANGUAGES}")
20
  #Languages for Coqui are: ['en', 'es', 'fr', 'de', 'pl', 'uk', 'ro', 'hu', 'el', 'bg', 'nl', 'fi', 'sl', 'lv', 'ga']
21
- coquiTTS = CoquiTTS()
 
 
22
 
23
 
24
  # Driver function
25
  def driver_fun(audio) :
26
- text1, lang = whisper_stt(audio)
27
  #text1 = model.transcribe(audio)["text"]
28
- text2 = lang_model_response(text1, lang)
29
- speech = tts(text2, lang) #'en')
30
- return text1, text2, speech
 
 
 
 
 
 
 
31
 
32
 
33
- # Whisper - speeech-to-text
34
  def whisper_stt(audio):
35
  print("Inside Whisper TTS")
36
  # load audio and pad/trim it to fit 30 seconds
@@ -46,17 +84,28 @@ def whisper_stt(audio):
46
  print(f"Detected language: {max(probs, key=probs.get)}")
47
 
48
  # decode the audio
49
- options = whisper.DecodingOptions(fp16 = False, language=lang)
50
- result = whisper.decode(model, mel, options)
 
 
 
 
 
 
 
 
 
 
51
 
52
  # print the recognized text
53
- print(f"transcript is : {result.text}")
54
- return result.text, lang
 
55
 
56
 
57
  # LLM - Bloom Response
58
  def lang_model_response(prompt, language):
59
- print(f"*****Inside lang_model_response - Prompt is :{prompt}")
60
  p = """Question: How are you doing today?
61
  Answer: I am doing good, thanks.
62
  Question: """
@@ -65,8 +114,10 @@ def lang_model_response(prompt, language):
65
  Answer: Sure, I am here for you.
66
  Question: """
67
 
68
- prompt = p + prompt + "\n" + "Answer: "
69
-
 
 
70
  json_ = {"inputs": prompt,
71
  "parameters":
72
  {
@@ -85,7 +136,10 @@ def lang_model_response(prompt, language):
85
  output = response.json()
86
  output_tmp = output[0]['generated_text']
87
  print(f"Bloom API Response is : {output_tmp}")
88
- solution = output_tmp.split("Answer: ")[2].split("\n")[0]
 
 
 
89
  print(f"Final Bloom Response after splits is: {solution}")
90
  return solution
91
 
@@ -99,7 +153,11 @@ def tts(text, language):
99
  coquiTTS.get_tts(text, fp, speaker = {"language" : language})
100
  return fp.name
101
 
 
 
 
102
 
 
103
  gr.Interface(
104
  title = 'Testing Whisper',
105
  fn=driver_fun,
@@ -108,6 +166,6 @@ gr.Interface(
108
  # "state"
109
  ],
110
  outputs=[
111
- "textbox", "textbox", "audio",
112
  ],
113
  live=True).launch()
 
5
  import tempfile
6
  from neon_tts_plugin_coqui import CoquiTTS
7
 
8
+ # Language common in all three multilingual models - English, Chinese, Spanish, and French
9
+ # So it would make sense to test the App on these four prominently
10
+
11
  # Whisper: Speech-to-text
12
  model = whisper.load_model("base")
13
+ model_medium = whisper.load_model("medium")
14
+ # Languages covered in Whisper - (exhaustive list) :
15
+ #"en": "english", "zh": "chinese", "de": "german", "es": "spanish", "ru": "russian",
16
+ #"ko": "korean", "fr": "french", "ja": "japanese", "pt": "portuguese", "tr": "turkish",
17
+ #"pl": "polish", "ca": "catalan", "nl": "dutch", "ar": "arabic", "sv": "swedish",
18
+ #"it": "italian", "id": "indonesian", "hi": "hindi", "fi": "finnish", "vi": "vietnamese",
19
+ #"iw": "hebrew", "uk": "ukrainian", "el": "greek", "ms": "malay", "cs": "czech",
20
+ #"ro": "romanian", "da": "danish", "hu": "hungarian", "ta": "tamil", "no": "norwegian",
21
+ #"th": "thai", "ur": "urdu", "hr": "croatian", "bg": "bulgarian", "lt": "lithuanian",
22
+ #"la": "latin", "mi": "maori", "ml": "malayalam", "cy": "welsh", "sk": "slovak",
23
+ #"te": "telugu", "fa": "persian", "lv": "latvian", "bn": "bengali", "sr": "serbian",
24
+ #"az": "azerbaijani", "sl": "slovenian", "kn": "kannada", "et": "estonian",
25
+ #"mk": "macedonian", "br": "breton", "eu": "basque", "is": "icelandic", "hy": "armenian",
26
+ #"ne": "nepali", "mn": "mongolian", "bs": "bosnian", "kk": "kazakh", "sq": "albanian",
27
+ #"sw": "swahili", "gl": "galician", "mr": "marathi", "pa": "punjabi", "si": "sinhala",
28
+ #"km": "khmer", "sn": "shona", "yo": "yoruba", "so": "somali", "af": "afrikaans",
29
+ #"oc": "occitan", "ka": "georgian", "be": "belarusian", "tg": "tajik", "sd": "sindhi",
30
+ #"gu": "gujarati", "am": "amharic", "yi": "yiddish", "lo": "lao", "uz": "uzbek",
31
+ #"fo": "faroese", "ht": "haitian creole", "ps": "pashto", "tk": "turkmen", "nn": "nynorsk",
32
+ #"mt": "maltese", "sa": "sanskrit", "lb": "luxembourgish", "my": "myanmar", "bo": "tibetan",
33
+ #"tl": "tagalog", "mg": "malagasy", "as": "assamese", "tt": "tatar", "haw": "hawaiian",
34
+ #"ln": "lingala", "ha": "hausa", "ba": "bashkir", "jw": "javanese", "su": "sundanese",
35
+
36
 
37
  # LLM : Bloom as inference
38
  API_URL = "https://api-inference.huggingface.co/models/bigscience/bloom"
39
  HF_TOKEN = os.environ["HF_TOKEN"]
40
  headers = {"Authorization": f"Bearer {HF_TOKEN}"}
41
+ # Main Languages covered in Bloom are (not exhaustive list):
42
+ # English, Chinese, French, Spanish, Portuguese, Arabic, Hindi, Vietnamese, Indonesian, Bengali, Tamil, Telugu
43
+
44
 
45
  # Text-to-Speech
46
  LANGUAGES = list(CoquiTTS.langs.keys())
47
+ coquiTTS = CoquiTTS()
48
  print(f"Languages for Coqui are: {LANGUAGES}")
49
  #Languages for Coqui are: ['en', 'es', 'fr', 'de', 'pl', 'uk', 'ro', 'hu', 'el', 'bg', 'nl', 'fi', 'sl', 'lv', 'ga']
50
+ # en - Engish, es - Spanish, fr - French, de - German, pl - Polish
51
+ # uk - Ukrainian, ro - Romanian, hu - Hungarian, el - Greek, bg - Bulgarian,
52
+ # nl - dutch, fi - finnish, sl - slovenian, lv - latvian, ga - ??
53
 
54
 
55
  # Driver function
56
  def driver_fun(audio) :
57
+ transcribe, translation, lang = whisper_stt(audio)
58
  #text1 = model.transcribe(audio)["text"]
59
+
60
+ #For now only taking in English text for Bloom prompting as inference model is not high spec
61
+ text_generated = lang_model_response(transcribe, lang)
62
+ text_generate_en = lang_model_response(translation, 'en')
63
+
64
+ if lang in ['es', 'fr']:
65
+ speech = tts(text_generated, lang)
66
+ else:
67
+ speech = tts(text_generated_en, 'en') #'en')
68
+ return transcribe, translation, text_generate, text_generate_en, speech
69
 
70
 
71
+ # Whisper - speech-to-text
72
  def whisper_stt(audio):
73
  print("Inside Whisper TTS")
74
  # load audio and pad/trim it to fit 30 seconds
 
84
  print(f"Detected language: {max(probs, key=probs.get)}")
85
 
86
  # decode the audio
87
+ options_transc = whisper.DecodingOptions(fp16 = False, language=lang, task='transcribe') #lang
88
+ options_transl = whisper.DecodingOptions(fp16 = False, language='en', task='translate') #lang
89
+ result_transc = whisper.decode(model_med, mel, options_transc)
90
+ result_transl = whisper.decode(model_med, mel, options_transl)
91
+
92
+ # print the recognized text
93
+ print(f"transcript is : {result_transc.text}")
94
+ print(f"translation is : {result_transl.text}")
95
+
96
+ # decode the audio
97
+ #options = whisper.DecodingOptions(fp16 = False, language='en') #lang
98
+ #result = whisper.decode(model, mel, options)
99
 
100
  # print the recognized text
101
+ # print(f"transcript is : {result.text}")
102
+ # return result.text, lang
103
+ return result_transc.text, result_transl.text, lang
104
 
105
 
106
  # LLM - Bloom Response
107
  def lang_model_response(prompt, language):
108
+ print(f"Inside lang_model_response - Prompt is :{prompt}")
109
  p = """Question: How are you doing today?
110
  Answer: I am doing good, thanks.
111
  Question: """
 
114
  Answer: Sure, I am here for you.
115
  Question: """
116
 
117
+ if language = 'en':
118
+ prompt = p + prompt + "\n" + "Answer: "
119
+ #else:
120
+
121
  json_ = {"inputs": prompt,
122
  "parameters":
123
  {
 
136
  output = response.json()
137
  output_tmp = output[0]['generated_text']
138
  print(f"Bloom API Response is : {output_tmp}")
139
+ if language == 'en':
140
+ solution = output_tmp.split("Answer: ")[2].split("\n")[0]
141
+ else:
142
+ output_tmp.split(".")[1]
143
  print(f"Final Bloom Response after splits is: {solution}")
144
  return solution
145
 
 
153
  coquiTTS.get_tts(text, fp, speaker = {"language" : language})
154
  return fp.name
155
 
156
+ #demo = gr.Blocks()
157
+ #with demo:
158
+ # gr.Markdown("<h1><center>Testing</center></h1>")
159
 
160
+
161
  gr.Interface(
162
  title = 'Testing Whisper',
163
  fn=driver_fun,
 
166
  # "state"
167
  ],
168
  outputs=[
169
+ "textbox", "textbox", "textbox", "textbox", "audio",
170
  ],
171
  live=True).launch()