kadirnar commited on
Commit
b13db2e
1 Parent(s): 00a1659

added whisper library

Browse files
Files changed (1) hide show
  1. app.py +91 -53
app.py CHANGED
@@ -1,9 +1,7 @@
1
- from transformers import pipeline, set_seed
2
- from transformers import BioGptTokenizer, BioGptForCausalLM
3
- from multilingual_translation import translate
4
  from utils import lang_ids
5
  import gradio as gr
6
- import torch
7
 
8
  biogpt_model_list = [
9
  "microsoft/biogpt",
@@ -16,27 +14,70 @@ lang_model_list = [
16
  "facebook/m2m100_418M"
17
  ]
18
 
 
 
 
 
 
 
 
19
  lang_list = list(lang_ids.keys())
20
 
21
- def translate_to_english(text, lang_model_id, base_lang):
 
 
 
 
 
 
 
22
  if base_lang == "English":
23
- return text
24
  else:
25
- base_lang = lang_ids[base_lang]
26
- new_text = translate(lang_model_id, text, base_lang, "en")
27
- return new_text[0]
 
 
 
 
 
28
 
29
- def biogpt(
 
30
  prompt: str,
31
  biogpt_model_id: str,
32
- max_length: str,
33
- num_return_sequences: int,
34
  base_lang: str,
35
- lang_model_id: str
36
  ):
37
-
38
  en_prompt = translate_to_english(prompt, lang_model_id, base_lang)
39
  generator = pipeline("text-generation", model=biogpt_model_id, device="cuda:0")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
40
  output = generator(en_prompt, max_length=max_length, num_return_sequences=num_return_sequences, do_sample=True)
41
  output_dict = {}
42
  for i in range(num_return_sequences):
@@ -46,50 +87,47 @@ def biogpt(
46
  for i in range(num_return_sequences):
47
  output_text += f'{output_dict[str(i+1)]}\n\n'
48
 
49
- if base_lang == "English":
50
- base_lang_output = output_text
51
-
52
- else:
53
- base_lang_output_ = ""
54
- for i in range(num_return_sequences):
55
- base_lang_output_ += f'{translate(lang_model_id, output_dict[str(i+1)], "en", lang_ids[base_lang])[0]}\n\n'
56
- base_lang_output = base_lang_output_
57
-
58
-
59
- return en_prompt, output_text, base_lang_output
60
-
61
-
62
- inputs = [
63
- gr.Textbox(lines=5, value="COVID-19 is", label="Prompt"),
64
- gr.Dropdown(biogpt_model_list, value="microsoft/biogpt", label="BioGPT Model ID"),
65
- gr.Slider(minumum=1, maximum=100, value=25, step=1, label="Max Length"),
66
- gr.Slider(minumum=1, maximum=10, value=2, step=1, label="Number of Outputs"),
67
- gr.Dropdown(lang_list, value="English", label="Base Language"),
68
- gr.Dropdown(lang_model_list, value="facebook/m2m100_418M", label="Language Model ID")
69
- ]
70
-
71
- outputs = [
72
- gr.outputs.Textbox(label="Prompt"),
73
- gr.outputs.Textbox(label="Output"),
74
- gr.outputs.Textbox(label="Translated Output")
75
- ]
76
 
77
  examples = [
78
- ["COVID-19 is", "microsoft/biogpt", 25, 2, "English", "facebook/m2m100_418M"],
79
- ["Kanser", "microsoft/biogpt", 25, 2, "Turkish", "facebook/m2m100_1.2B"]
80
  ]
81
 
82
  title = "M2M100 + BioGPT: Generative Pre-trained Transformer for Biomedical Text Generation and Mining"
83
 
84
  description = "BioGPT is a domain-specific generative pre-trained Transformer language model for biomedical text generation and mining. BioGPT follows the Transformer language model backbone, and is pre-trained on 15M PubMed abstracts from scratch. Github: github.com/microsoft/BioGPT Paper: https://arxiv.org/abs/2210.10341"
85
 
86
- demo_app = gr.Interface(
87
- biogpt,
88
- inputs,
89
- outputs,
90
- title=title,
91
- description=description,
92
- examples=examples,
93
- cache_examples=False,
94
- )
95
- demo_app.launch(debug=True, enable_queue=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import pipeline
2
+ from multilingual_translation import text_to_text_generation
 
3
  from utils import lang_ids
4
  import gradio as gr
 
5
 
6
  biogpt_model_list = [
7
  "microsoft/biogpt",
 
14
  "facebook/m2m100_418M"
15
  ]
16
 
17
+ whisper_model_list = [
18
+ "openai/whisper-small",
19
+ "openai/whisper-medium",
20
+ "openai/whisper-tiny",
21
+ "openai/whisper-large"
22
+ ]
23
+
24
  lang_list = list(lang_ids.keys())
25
 
26
+ def whisper_demo(input_audio, model_id):
27
+ pipe = pipeline(task="automatic-speech-recognition",model=model_id, device='cuda:0')
28
+ pipe.model.config.forced_decoder_ids = pipe.tokenizer.get_decoder_prompt_ids(language='en', task="transcribe")
29
+ output_text = pipe(input_audio)['text']
30
+ return output_text
31
+
32
+
33
+ def translate_to_english(prompt, lang_model_id, base_lang):
34
  if base_lang == "English":
35
+ return prompt
36
  else:
37
+ text_output = text_to_text_generation(
38
+ prompt=prompt,
39
+ model_id=lang_model_id,
40
+ device='cuda:0',
41
+ target_lang='en'
42
+ )
43
+
44
+ return text_output[0]
45
 
46
+
47
+ def biogpt_text(
48
  prompt: str,
49
  biogpt_model_id: str,
50
+ lang_model_id: str,
 
51
  base_lang: str,
 
52
  ):
53
+
54
  en_prompt = translate_to_english(prompt, lang_model_id, base_lang)
55
  generator = pipeline("text-generation", model=biogpt_model_id, device="cuda:0")
56
+ output = generator(en_prompt, max_length=250, num_return_sequences=1, do_sample=True)
57
+ output = output[0]['generated_text']
58
+ if base_lang == "English":
59
+ output_text = output
60
+
61
+ else:
62
+ output_text = text_to_text_generation(
63
+ prompt=output,
64
+ model_id=lang_model_id,
65
+ device='cuda:0',
66
+ target_lang=base_lang
67
+ )
68
+
69
+ return en_prompt, output, output_text
70
+
71
+
72
+ def biogpt_audio(
73
+ input_audio: str,
74
+ biogpt_model_id: str,
75
+ whisper_model_id: str,
76
+ max_length: str,
77
+ num_return_sequences: int
78
+ ):
79
+ en_prompt = whisper_demo(input_audio=input_audio, model_id=whisper_model_id)
80
+ generator = pipeline("text-generation", model=biogpt_model_id, device="cuda:0")
81
  output = generator(en_prompt, max_length=max_length, num_return_sequences=num_return_sequences, do_sample=True)
82
  output_dict = {}
83
  for i in range(num_return_sequences):
 
87
  for i in range(num_return_sequences):
88
  output_text += f'{output_dict[str(i+1)]}\n\n'
89
 
90
+ return en_prompt, output_text, output_text
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
91
 
92
  examples = [
93
+ ["COVID-19 is", biogpt_model_list[0], lang_model_list[1], "English"]
 
94
  ]
95
 
96
  title = "M2M100 + BioGPT: Generative Pre-trained Transformer for Biomedical Text Generation and Mining"
97
 
98
  description = "BioGPT is a domain-specific generative pre-trained Transformer language model for biomedical text generation and mining. BioGPT follows the Transformer language model backbone, and is pre-trained on 15M PubMed abstracts from scratch. Github: github.com/microsoft/BioGPT Paper: https://arxiv.org/abs/2210.10341"
99
 
100
+ app = gr.Blocks()
101
+ with app:
102
+ gr.Markdown("# **<p align='center'>AnimeSR: Learning Real-World Super-Resolution Models for Animation Videos</p>**")
103
+ gr.Markdown(
104
+ """
105
+ <p style='text-align: center'>
106
+ Follow me for more!
107
+ <br> <a href='https://twitter.com/kadirnar_ai' target='_blank'>twitter</a> | <a href='https://github.com/kadirnar' target='_blank'>github</a> | <a href='https://www.linkedin.com/in/kadir-nar/' target='_blank'>linkedin</a> |
108
+ </p>
109
+ """
110
+ )
111
+ with gr.Row():
112
+ with gr.Column():
113
+ with gr.Tab("Text"):
114
+ input_text = gr.Textbox(lines=3, value="COVID-19 is", label="Text")
115
+ input_text_button = gr.Button(value="Predict")
116
+ input_biogpt_model =gr.Dropdown(choices=biogpt_model_list, value=biogpt_model_list[0], label='BioGpt Model')
117
+ input_m2m100_model =gr.Dropdown(choices=lang_model_list, value=lang_model_list[1], label='Language Model')
118
+ input_base_lang = gr.Dropdown(lang_list, value="English", label="Base Language")
119
+
120
+ with gr.Tab("Audio"):
121
+ input_audio = gr.Microphone(label='Audio')
122
+ input_audio_button = gr.Button(value="Predict")
123
+
124
+ with gr.Column():
125
+ prompt_text = gr.Textbox(lines=3, label="Prompt")
126
+ output_text = gr.Textbox(lines=3, label="BioGpt Text")
127
+ translated_text = gr.Textbox(lines=3,label="Translated Text")
128
+
129
+ gr.Examples(examples, inputs=[input_text, input_biogpt_model, input_m2m100_model,input_base_lang], outputs=[prompt_text, output_text, translated_text], fn=biogpt_text, cache_examples=True)
130
+ input_text_button.click(biogpt_text, inputs=[input_text, input_biogpt_model, input_m2m100_model,input_base_lang], outputs=[prompt_text, output_text, translated_text])
131
+ input_audio_button.click(biogpt_audio, inputs=[input_audio, input_biogpt_model,input_m2m100_model,input_base_lang], outputs=[prompt_text, output_text, translated_text])
132
+
133
+ app.launch()