Nathan Butters commited on
Commit
87d9953
1 Parent(s): af2e26f

optimize computation

Browse files
Files changed (2) hide show
  1. .ipynb_checkpoints/app-checkpoint.py +11 -6
  2. app.py +9 -11
.ipynb_checkpoints/app-checkpoint.py CHANGED
@@ -1,8 +1,6 @@
1
  #Import the libraries we know we'll need for the Generator.
2
- import pandas as pd, spacy, nltk, numpy as np, re
3
  from spacy.matcher import Matcher
4
- !python -m spacy download en_core_web_lg
5
- nlp = spacy.load("en_core_web_lg")
6
  from nltk.corpus import wordnet
7
 
8
  #Import the libraries to support the model and predictions.
@@ -29,10 +27,17 @@ def set_up_explainer():
29
 
30
  @st.experimental_singleton
31
  def prepare_model():
 
 
 
 
 
 
 
32
  tokenizer = AutoTokenizer.from_pretrained("distilbert-base-uncased-finetuned-sst-2-english")
33
  model = AutoModelForSequenceClassification.from_pretrained("distilbert-base-uncased-finetuned-sst-2-english")
34
  pipe = TextClassificationPipeline(model=model, tokenizer=tokenizer, return_all_scores=True)
35
- return tokenizer, model, pipe
36
 
37
  @st.experimental_singleton
38
  def prepare_lists():
@@ -80,7 +85,7 @@ st.subheader(f'Current Layout: {layout}')
80
  text = st.text_input('Provide a sentence you want to evaluate.', placeholder = "I like you. I love you.", key="input")
81
 
82
  #Prepare the model, data, and Lime. Set starting variables.
83
- tokenizer, model, pipe = prepare_model()
84
  countries, professions, word_lists = prepare_lists()
85
  explainer = set_up_explainer()
86
  text2 = ""
@@ -347,4 +352,4 @@ if layout == 'VizNLC':
347
  size=alt.Size('seed:O'),
348
  tooltip=('Categories','text','pred')
349
  ).mark_circle(opacity=.5).properties(width=450, height=450).add_selection(single_nearest)
350
- st.altair_chart(full)
 
1
  #Import the libraries we know we'll need for the Generator.
2
+ import pandas as pd, spacy, nltk, numpy as np, re, os
3
  from spacy.matcher import Matcher
 
 
4
  from nltk.corpus import wordnet
5
 
6
  #Import the libraries to support the model and predictions.
 
27
 
28
  @st.experimental_singleton
29
  def prepare_model():
30
+ #Attempting to fix the issue with spacy model in a more intuitive way.
31
+ try:
32
+ nlp = spacy.load("en_core_web_lg")
33
+ except:
34
+ script = "python -m spacy download en_core_web_lg"
35
+ os.system("bash -c '%s'" % script)
36
+ nlp = spacy.load("en_core_web_lg")
37
  tokenizer = AutoTokenizer.from_pretrained("distilbert-base-uncased-finetuned-sst-2-english")
38
  model = AutoModelForSequenceClassification.from_pretrained("distilbert-base-uncased-finetuned-sst-2-english")
39
  pipe = TextClassificationPipeline(model=model, tokenizer=tokenizer, return_all_scores=True)
40
+ return tokenizer, model, pipe, nlp
41
 
42
  @st.experimental_singleton
43
  def prepare_lists():
 
85
  text = st.text_input('Provide a sentence you want to evaluate.', placeholder = "I like you. I love you.", key="input")
86
 
87
  #Prepare the model, data, and Lime. Set starting variables.
88
+ tokenizer, model, pipe, nlp = prepare_model()
89
  countries, professions, word_lists = prepare_lists()
90
  explainer = set_up_explainer()
91
  text2 = ""
 
352
  size=alt.Size('seed:O'),
353
  tooltip=('Categories','text','pred')
354
  ).mark_circle(opacity=.5).properties(width=450, height=450).add_selection(single_nearest)
355
+ st.altair_chart(full)
app.py CHANGED
@@ -3,15 +3,6 @@ import pandas as pd, spacy, nltk, numpy as np, re, os
3
  from spacy.matcher import Matcher
4
  from nltk.corpus import wordnet
5
 
6
- #Attempting to fix the issue with spacy model in a more intuitive way.
7
- try:
8
- nlp = spacy.load("en_core_web_lg")
9
- except:
10
- script = "python -m spacy download en_core_web_lg"
11
- os.system("bash -c '%s'" % script)
12
-
13
- nlp = spacy.load("en_core_web_lg")
14
-
15
  #Import the libraries to support the model and predictions.
16
  from transformers import AutoTokenizer, AutoModelForSequenceClassification, TextClassificationPipeline
17
  import lime
@@ -36,10 +27,17 @@ def set_up_explainer():
36
 
37
  @st.experimental_singleton
38
  def prepare_model():
 
 
 
 
 
 
 
39
  tokenizer = AutoTokenizer.from_pretrained("distilbert-base-uncased-finetuned-sst-2-english")
40
  model = AutoModelForSequenceClassification.from_pretrained("distilbert-base-uncased-finetuned-sst-2-english")
41
  pipe = TextClassificationPipeline(model=model, tokenizer=tokenizer, return_all_scores=True)
42
- return tokenizer, model, pipe
43
 
44
  @st.experimental_singleton
45
  def prepare_lists():
@@ -87,7 +85,7 @@ st.subheader(f'Current Layout: {layout}')
87
  text = st.text_input('Provide a sentence you want to evaluate.', placeholder = "I like you. I love you.", key="input")
88
 
89
  #Prepare the model, data, and Lime. Set starting variables.
90
- tokenizer, model, pipe = prepare_model()
91
  countries, professions, word_lists = prepare_lists()
92
  explainer = set_up_explainer()
93
  text2 = ""
 
3
  from spacy.matcher import Matcher
4
  from nltk.corpus import wordnet
5
 
 
 
 
 
 
 
 
 
 
6
  #Import the libraries to support the model and predictions.
7
  from transformers import AutoTokenizer, AutoModelForSequenceClassification, TextClassificationPipeline
8
  import lime
 
27
 
28
  @st.experimental_singleton
29
  def prepare_model():
30
+ #Attempting to fix the issue with spacy model in a more intuitive way.
31
+ try:
32
+ nlp = spacy.load("en_core_web_lg")
33
+ except:
34
+ script = "python -m spacy download en_core_web_lg"
35
+ os.system("bash -c '%s'" % script)
36
+ nlp = spacy.load("en_core_web_lg")
37
  tokenizer = AutoTokenizer.from_pretrained("distilbert-base-uncased-finetuned-sst-2-english")
38
  model = AutoModelForSequenceClassification.from_pretrained("distilbert-base-uncased-finetuned-sst-2-english")
39
  pipe = TextClassificationPipeline(model=model, tokenizer=tokenizer, return_all_scores=True)
40
+ return tokenizer, model, pipe, nlp
41
 
42
  @st.experimental_singleton
43
  def prepare_lists():
 
85
  text = st.text_input('Provide a sentence you want to evaluate.', placeholder = "I like you. I love you.", key="input")
86
 
87
  #Prepare the model, data, and Lime. Set starting variables.
88
+ tokenizer, model, pipe, nlp = prepare_model()
89
  countries, professions, word_lists = prepare_lists()
90
  explainer = set_up_explainer()
91
  text2 = ""