Merlintxu commited on
Commit
38047c4
·
1 Parent(s): cadb09a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -6
app.py CHANGED
@@ -1,5 +1,5 @@
1
  ## app.py ##
2
- from transformers import pipeline
3
  from gradio import Interface
4
  import gradio as gr
5
 
@@ -8,14 +8,32 @@ MODELS = {
8
  "T5": "lmsys/fastchat-t5-3b-v1.0",
9
  "Bert": "bert-base-multilingual-cased",
10
  "GPT2": "datificate/gpt2-small-spanish",
11
- "bloom":"bigscience/bloom"
12
  }
13
 
 
 
 
 
 
 
 
 
 
 
14
  # Define your function
15
  def generate_and_analyze(model_name, input_text):
16
  # Load the model from the dictionary using the selected model name
17
- model = MODELS[model_name]
18
- text_generator = pipeline('text-generation', model=model, device=0) # Use GPU if available
 
 
 
 
 
 
 
 
 
19
  result = text_generator(input_text, max_length=250, do_sample=True)[0]
20
  return result['generated_text']
21
 
@@ -23,10 +41,9 @@ def generate_and_analyze(model_name, input_text):
23
  iface = gr.Interface(
24
  fn=generate_and_analyze,
25
  inputs=[
26
- gr.inputs.Dropdown(choices=list(MODELS.keys()), label="Model"),
27
  gr.inputs.Textbox(lines=2, label="Input Text")
28
  ],
29
  outputs="text"
30
  )
31
  iface.launch()
32
-
 
1
  ## app.py ##
2
+ from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
3
  from gradio import Interface
4
  import gradio as gr
5
 
 
8
  "T5": "lmsys/fastchat-t5-3b-v1.0",
9
  "Bert": "bert-base-multilingual-cased",
10
  "GPT2": "datificate/gpt2-small-spanish",
 
11
  }
12
 
13
+ TOKENIZERS = {
14
+ "T5": None,
15
+ "Bert": None,
16
+ "GPT2": None,
17
+ }
18
+
19
+ # Load Bloom model separately with memory optimizations
20
+ model_bloom = AutoModelForCausalLM.from_pretrained("bigscience/bloom", low_cpu_mem_usage=True)
21
+ tokenizer_bloom = AutoTokenizer.from_pretrained("bigscience/bloom")
22
+
23
  # Define your function
24
  def generate_and_analyze(model_name, input_text):
25
  # Load the model from the dictionary using the selected model name
26
+ if model_name == "bloom":
27
+ model = model_bloom
28
+ tokenizer = tokenizer_bloom
29
+ else:
30
+ model = MODELS[model_name]
31
+ tokenizer = TOKENIZERS[model_name]
32
+ if tokenizer is None: # Load tokenizer if not already done
33
+ tokenizer = AutoTokenizer.from_pretrained(model)
34
+ TOKENIZERS[model_name] = tokenizer
35
+
36
+ text_generator = pipeline('text-generation', model=model, tokenizer=tokenizer, device=0) # Use GPU if available
37
  result = text_generator(input_text, max_length=250, do_sample=True)[0]
38
  return result['generated_text']
39
 
 
41
  iface = gr.Interface(
42
  fn=generate_and_analyze,
43
  inputs=[
44
+ gr.inputs.Dropdown(choices=list(MODELS.keys()) + ["bloom"], label="Model"),
45
  gr.inputs.Textbox(lines=2, label="Input Text")
46
  ],
47
  outputs="text"
48
  )
49
  iface.launch()