kadirnar commited on
Commit
c29b21d
1 Parent(s): 04558a8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -18
app.py CHANGED
@@ -28,7 +28,7 @@ def whisper_demo(input_audio, model_id):
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":
@@ -73,21 +73,24 @@ 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):
84
- output_dict[str(i+1)] = output[i]['generated_text']
85
-
86
- output_text = ""
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"]
@@ -95,7 +98,7 @@ examples = [
95
 
96
  app = gr.Blocks()
97
  with app:
98
- gr.Markdown("# **<h4 align='center'>Whisper + M2M100 + BioGPT: Generative Pre-trained Transformer for Biomedical Text Generation and Mining</h4>**")
99
  gr.Markdown(
100
  """
101
  <p style='text-align: center'>
@@ -109,12 +112,16 @@ with app:
109
  with gr.Tab("Text"):
110
  input_text = gr.Textbox(lines=3, value="COVID-19 is", label="Text")
111
  input_text_button = gr.Button(value="Predict")
112
- input_biogpt_model =gr.Dropdown(choices=biogpt_model_list, value=biogpt_model_list[0], label='BioGpt Model')
113
- input_m2m100_model =gr.Dropdown(choices=lang_model_list, value=lang_model_list[1], label='Language Model')
114
  input_base_lang = gr.Dropdown(lang_list, value="English", label="Base Language")
115
 
116
  with gr.Tab("Audio"):
117
- input_audio = gr.Microphone(label='Audio')
 
 
 
 
118
  input_audio_button = gr.Button(value="Predict")
119
 
120
  with gr.Column():
@@ -124,6 +131,6 @@ with app:
124
 
125
  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=False)
126
  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])
127
- 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])
128
 
129
  app.launch()
 
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":
 
73
  input_audio: str,
74
  biogpt_model_id: str,
75
  whisper_model_id: str,
76
+ base_lang: str,
77
+ lang_model_id: str,
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=250, num_return_sequences=1, do_sample=True)
82
+ if base_lang == "English":
83
+ output_text = output
84
+
85
+ else:
86
+ output_text = text_to_text_generation(
87
+ prompt=output,
88
+ model_id=lang_model_id,
89
+ device='cuda:0',
90
+ target_lang=lang_ids[base_lang]
91
+ )
92
+
93
+ return en_prompt, output, output_text
94
 
95
  examples = [
96
  ["COVID-19 is", biogpt_model_list[0], lang_model_list[1], "English"]
 
98
 
99
  app = gr.Blocks()
100
  with app:
101
+ gr.Markdown("# **<h4 align='center'>Whisper + M2M100 + BioGPT: Generative Pre-trained Transformer for Biomedical Text Generation and Mining<h4>**")
102
  gr.Markdown(
103
  """
104
  <p style='text-align: center'>
 
112
  with gr.Tab("Text"):
113
  input_text = gr.Textbox(lines=3, value="COVID-19 is", label="Text")
114
  input_text_button = gr.Button(value="Predict")
115
+ input_biogpt_model = gr.Dropdown(choices=biogpt_model_list, value=biogpt_model_list[0], label='BioGpt Model')
116
+ input_m2m100_model = gr.Dropdown(choices=lang_model_list, value=lang_model_list[1], label='Language Model')
117
  input_base_lang = gr.Dropdown(lang_list, value="English", label="Base Language")
118
 
119
  with gr.Tab("Audio"):
120
+ input_audio = gr.Audio(source="microphone", type="filepath")
121
+ input_biogpt_model = gr.Dropdown(choices=biogpt_model_list, value=biogpt_model_list[0], label='BioGpt Model')
122
+ input_whisper_model = gr.Dropdown(choices=whisper_model_list, value=whisper_model_list[0], label='Audio Model')
123
+ input_base_lang = gr.Dropdown(lang_list, value="English", label="Base Language")
124
+ input_m2m100_model = gr.Dropdown(choices=lang_model_list, value=lang_model_list[1], label='Language Model')
125
  input_audio_button = gr.Button(value="Predict")
126
 
127
  with gr.Column():
 
131
 
132
  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=False)
133
  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])
134
+ input_audio_button.click(biogpt_audio, inputs=[input_audio, input_biogpt_model,input_whisper_model,input_base_lang], outputs=[prompt_text, output_text, translated_text])
135
 
136
  app.launch()