Skip to content

Commit 2f5b0ee

Browse files
integration with pre trained pipelines
2 parents 13f1572 + d144445 commit 2f5b0ee

File tree

8 files changed

+21
-17
lines changed

8 files changed

+21
-17
lines changed

api/NbEmotionsModel.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,16 @@
77
#tfidf_vectorizer = TfidfVectorizer(use_idf=True)
88

99
def make_prediction(my_sentence):
10-
with open("./models/nb_emotion.pkl", "rb") as f:
11-
model = pickle.load(f)
10+
model_file = "./models/emotions_pipeline.pkl"
11+
try:
12+
# Carregando o pipeline do arquivo .pkl
13+
with open(model_file, 'rb') as model_file:
14+
pipeline = pickle.load(model_file)
1215

13-
with open("./models/tfidf_vectorizer_em.pkl", 'rb') as f:
14-
tfidf_vectorizer = pickle.load(f)
16+
# Fazendo previsões para os textos
17+
predictions = pipeline.predict([texts])
1518

16-
new_sentence = tfidf_vectorizer.transform([my_sentence])
17-
prediction = model.predict(new_sentence)
18-
return prediction[0]
19+
return predictions[0]
20+
21+
except Exception as e:
22+
return str(e)

api/models/emotion_pipeline.pkl

9.33 MB
Binary file not shown.

api/models/linear_reg.pkl

-585 KB
Binary file not shown.

api/models/nb_emotion.pkl

-8.17 MB
Binary file not shown.

api/models/tfidf_vectorizer_em.pkl

-1.17 MB
Binary file not shown.

api/models/vectorizer_lin.pkl

-1010 KB
Binary file not shown.

api/models_code/nb_emotions.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import pandas as pd
22
import pickle
3+
from sklearn.pipeline import make_pipeline
4+
from sklearn.feature_extraction.text import CountVectorizer
5+
from sklearn.feature_extraction.text import TfidfVectorizer
36

47
# bag of words
58
from sklearn.feature_extraction.text import TfidfVectorizer
@@ -10,15 +13,12 @@
1013
df = pd.read_csv('../training_df/tweet_emotions.csv')
1114
train_data, test_data, train_target, test_target = train_test_split(df["content"], df["sentiment"], test_size=0.2, shuffle=True)
1215

13-
tfidf_vectorizer = TfidfVectorizer(use_idf=True)
14-
X_train_vectors_tfidf = tfidf_vectorizer.fit_transform(train_data)
15-
X_test_vectors_tfidf = tfidf_vectorizer.transform(test_data)
16-
17-
nb_tfidf = MultinomialNB(alpha = 0)
18-
nb_tfidf.fit(X_train_vectors_tfidf, train_target)
16+
# Criando um pipeline com o vetorizador TF-IDF e o classificador Multinomial Naive Bayes
17+
pipeline = make_pipeline(TfidfVectorizer(), MultinomialNB())
1918

20-
with open("../models/nb_emotion.pkl", "wb") as f:
21-
pickle.dump(nb_tfidf, f)
19+
# Ajustando o modelo ao conjunto de treinamento
20+
pipeline.fit(train_data, train_target)
2221

23-
with open("../models/tfidf_vectorizer_em.pkl", "wb") as f:
24-
pickle.dump(tfidf_vectorizer, f)
22+
# Salvando o pipeline em um arquivo .pkl
23+
with open("../models/emotion_pipeline.pkl", "wb") as model_file:
24+
pickle.dump(pipeline, model_file)

0 commit comments

Comments
 (0)