NeerAbhy commited on
Commit
b96b661
1 Parent(s): 02e5c28

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +70 -5
app.py CHANGED
@@ -4,11 +4,63 @@ import os
4
  import string
5
  import re
6
  import torch
7
- from transformers import pipeline
8
  from transformers import M2M100Tokenizer, M2M100ForConditionalGeneration
9
  import fasttext
10
  from huggingface_hub import hf_hub_download
11
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
 
13
  model_path = hf_hub_download(repo_id="cis-lmu/glotlid", filename="model.bin")
14
  identification_model = fasttext.load_model(model_path)
@@ -172,20 +224,33 @@ def print_s(source_lang, target_lang, text0):
172
  demo = gr.Blocks()
173
 
174
  with demo:
175
- gr.Markdown("Speech analyzer")
176
- audio = gr.Audio(type="filepath", label = "Upload a file")
177
  text0 = gr.Textbox()
178
  text = gr.Textbox()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
179
  source_lang = gr.Dropdown(label="Source lang", choices=list(lang_id.keys()), value=list(lang_id.keys())[0])
180
  target_lang = gr.Dropdown(label="target lang", choices=list(lang_id.keys()), value=list(lang_id.keys())[0])
181
 
182
  #gr.Examples(examples = list(lang_id.keys()),
183
  # inputs=[
184
  # source_lang])
185
- b1 = gr.Button("convert to text")
186
  b3 = gr.Button("translate")
187
  b3.click(translation_text, inputs = [source_lang, target_lang, text0], outputs = text)
188
- b1.click(audio_a_text, inputs=audio, outputs=text)
189
 
190
  b2 = gr.Button("Classification of language")
191
  b2.click(lang_ident,inputs = text0, outputs=text)
 
4
  import string
5
  import re
6
  import torch
7
+ from transformers import pipeline, AutoModelForSeq2SeqLM, AutoTokenizer, AutoConfig
8
  from transformers import M2M100Tokenizer, M2M100ForConditionalGeneration
9
  import fasttext
10
  from huggingface_hub import hf_hub_download
11
 
12
+ summarization_model_names = [
13
+ "google/bigbird-pegasus-large-arxiv",
14
+ "facebook/bart-large-cnn",
15
+ "google/t5-v1_1-large",
16
+ "sshleifer/distilbart-cnn-12-6",
17
+ "allenai/led-base-16384",
18
+ "google/pegasus-xsum",
19
+ "togethercomputer/LLaMA-2-7B-32K"
20
+ ]
21
+
22
+ # Placeholder for the summarizer pipeline, tokenizer, and maximum tokens
23
+ summarizer = None
24
+ tokenizer = None
25
+ max_tokens = None
26
+
27
+
28
+ # Function to load the selected model
29
+ def load_summarization_model(model_name):
30
+ global summarizer, tokenizer, max_tokens
31
+ try:
32
+ summarizer = pipeline("summarization", model=model_name, torch_dtype=torch.bfloat16)
33
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
34
+ config = AutoConfig.from_pretrained(model_name)
35
+
36
+ if max_tokens = config.max_position_embeddings
37
+ elif hasattr(config, 'n_positions'):
38
+ max_tokens = config.n_positions
39
+ elif hasattr(config, 'd_model'):
40
+ max_tokens = config.d_model # for T5 models, d_model is a rough proxy
41
+ else:
42
+ max_tokens = "Unknown"
43
+
44
+ return f"Model {model_name} loaded successfully! Max tokens: {max_tokens}"
45
+ except Exception as e:
46
+ return f"Failed to load model {model_name}. Error: {str(e)}"
47
+
48
+
49
+ def summarize_text(input, min_length, max_length):
50
+ if summarizer is None:
51
+ return "No model loaded!"
52
+
53
+ input_tokens = tokenizer.encode(input, return_tensors="pt")
54
+ num_tokens = input_tokens.shape[1]
55
+ if num_tokens > max_tokens:
56
+ return f"Error: The input text has {num_tokens} tokens, which exceeds the maximum allowed {max_tokens} tokens. Please enter shorter text."
57
+
58
+ min_summary_length = int(num_tokens * (min_length / 100))
59
+ max_summary_length = int(num_tokens * (max_length / 100))
60
+
61
+ output = summarizer(input, min_length=min_summary_length, max_length=max_summary_length)
62
+ return output[0]['summary_text']
63
+
64
 
65
  model_path = hf_hub_download(repo_id="cis-lmu/glotlid", filename="model.bin")
66
  identification_model = fasttext.load_model(model_path)
 
224
  demo = gr.Blocks()
225
 
226
  with demo:
 
 
227
  text0 = gr.Textbox()
228
  text = gr.Textbox()
229
+ #gr.Markdown("Speech analyzer")
230
+ #audio = gr.Audio(type="filepath", label = "Upload a file")
231
+ model_dropdown = gr.Dropdown(choices=model_names, label="Choose a model", value="sshleifer/distilbart-cnn-12-6")
232
+ load_message = gr.Textbox(label="Load Status", interactive=False)
233
+ b1 = gr.Button("Load Model")
234
+ min_length_slider = gr.Slider(minimum=0, maximum=100, step=1, label="Minimum Summary Length (%)", value=10)
235
+ max_length_slider = gr.Slider(minimum=0, maximum=100, step=1, label="Maximum Summary Length (%)", value=20)
236
+
237
+ summarize_button = gr.Button("Summarize Text")
238
+
239
+ b1.click(fn=load_model, inputs=model_dropdown, outputs=load_message)
240
+ summarize_button.click(fn=summarize_text, inputs=[text0, min_length_slider, max_length_slider],
241
+ outputs=text)
242
+
243
+
244
  source_lang = gr.Dropdown(label="Source lang", choices=list(lang_id.keys()), value=list(lang_id.keys())[0])
245
  target_lang = gr.Dropdown(label="target lang", choices=list(lang_id.keys()), value=list(lang_id.keys())[0])
246
 
247
  #gr.Examples(examples = list(lang_id.keys()),
248
  # inputs=[
249
  # source_lang])
250
+ #b1 = gr.Button("convert to text")
251
  b3 = gr.Button("translate")
252
  b3.click(translation_text, inputs = [source_lang, target_lang, text0], outputs = text)
253
+ #b1.click(audio_a_text, inputs=audio, outputs=text)
254
 
255
  b2 = gr.Button("Classification of language")
256
  b2.click(lang_ident,inputs = text0, outputs=text)