curtpond commited on
Commit
3ae6ff6
·
1 Parent(s): bd45a60

Updated pickel files and app.py.

Browse files
Files changed (1) hide show
  1. app.py +13 -27
app.py CHANGED
@@ -12,12 +12,11 @@ from sklearn.feature_extraction.text import TfidfVectorizer
12
  from flair.data import Sentence
13
  from flair.models import SequenceTagger
14
 
15
- # file name
16
- #lr_filename = 'lr_021223.pkl'
17
-
18
- # Load model from pickle file
19
- # model = pickle.load(open(lr_filename, 'rb'))
20
-
21
 
22
  # Process input text, including removing stopwords, converting to lowercase, and removing punctuation
23
  stop = stopwords.words('english')
@@ -30,32 +29,19 @@ def process_text(text):
30
  text = " ".join(text.split())
31
  return text
32
 
33
- # Vectorize input text
34
- vec = CountVectorizer()
35
- '''
36
  def vectorize_text(text):
37
- #text = process_text(text)
38
- #text = vectorizer.fit_transform([text])
39
- #return text
40
- '''
41
 
42
  # Valid input for the model so number of features match
43
- def predict(text):
44
- # Load the pickled model
45
- filename = 'lr_021223.pkl'
46
- loaded_model = pickle.load(open(filename, 'rb'))
47
- text = vec.transform([text])
48
  text = process_text(text)
49
- prediction = loaded_model.predict(text)
 
50
  return prediction
51
 
52
- '''
53
- Prediction function
54
- #def predict(text):
55
- #text = vectorize_text(text)
56
- #prediction = model.predict(text)
57
- #return prediction
58
- '''
59
 
60
  # Specify NER model
61
  tagger = SequenceTagger.load('best-model.pt') # SequenceTagger.load('best-model.pt')
@@ -71,7 +57,7 @@ def run_ner(input_text):
71
 
72
  # Run both models, and return a tuple of their results
73
  def run_models(input_text):
74
- prediction = 0 # This "0" is a placeholder to avoid errors; once the LR model is working, use this instead: prediction = predict(input_text)
75
  entities = run_ner(input_text)
76
  return prediction, entities
77
 
 
12
  from flair.data import Sentence
13
  from flair.models import SequenceTagger
14
 
15
+ # Load pickled model and vectorizer
16
+ model = 'lr_021823.pkl'
17
+ model_loaded = pickle.load(open(model, 'rb'))
18
+ vectorizer = 'vectorizer_021823.pkl'
19
+ vectorizer_loaded = pickle.load(open(vectorizer, 'rb'))
 
20
 
21
  # Process input text, including removing stopwords, converting to lowercase, and removing punctuation
22
  stop = stopwords.words('english')
 
29
  text = " ".join(text.split())
30
  return text
31
 
32
+ # Vectorize text
 
 
33
  def vectorize_text(text):
34
+ text = process_text(text)
35
+ text = vectorizer_loaded.transform([text])
36
+ return text
 
37
 
38
  # Valid input for the model so number of features match
39
+ def class_predict(text):
 
 
 
 
40
  text = process_text(text)
41
+ vec = vectorizer_loaded.transform([text])
42
+ prediction = model_loaded.predict(vec)
43
  return prediction
44
 
 
 
 
 
 
 
 
45
 
46
  # Specify NER model
47
  tagger = SequenceTagger.load('best-model.pt') # SequenceTagger.load('best-model.pt')
 
57
 
58
  # Run both models, and return a tuple of their results
59
  def run_models(input_text):
60
+ prediction = class_predict(input_text)
61
  entities = run_ner(input_text)
62
  return prediction, entities
63