Skip to content

Commit

Permalink
padronizando criacao de pkl
Browse files Browse the repository at this point in the history
  • Loading branch information
cmaloney111 committed Dec 18, 2023
1 parent 5bc313c commit d144445
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 22 deletions.
23 changes: 11 additions & 12 deletions api/NbEmotionsModel.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
import pandas as pd
import pickle

# bag of words
from sklearn.feature_extraction.text import TfidfVectorizer
def news_prediction(texts):
model_file = "./models/emotion_pipeline.pkl"
try:
# Carregando o pipeline do arquivo .pkl
with open(model_file, 'rb') as model_file:
pipeline = pickle.load(model_file)

#tfidf_vectorizer = TfidfVectorizer(use_idf=True)
# Fazendo previsões para os textos
predictions = pipeline.predict([texts])

def make_prediction(my_sentence):
with open("./models/nb_emotion.pkl", "rb") as f:
model = pickle.load(f)
return predictions[0]

with open("./models/tfidf_vectorizer_em.pkl", 'rb') as f:
tfidf_vectorizer = pickle.load(f)

new_sentence = tfidf_vectorizer.transform([my_sentence])
prediction = model.predict(new_sentence)
return prediction[0]
except Exception as e:
return str(e)
Binary file removed api/models/nb_emotion.pkl
Binary file not shown.
21 changes: 11 additions & 10 deletions api/models/nb_emotions.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import pandas as pd
import pickle
from sklearn.pipeline import make_pipeline
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.feature_extraction.text import TfidfVectorizer


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

tfidf_vectorizer = TfidfVectorizer(use_idf=True)
X_train_vectors_tfidf = tfidf_vectorizer.fit_transform(train_data)
X_test_vectors_tfidf = tfidf_vectorizer.transform(test_data)

nb_tfidf = MultinomialNB(alpha = 0)
nb_tfidf.fit(X_train_vectors_tfidf, train_target)
# Criando um pipeline com o vetorizador TF-IDF e o classificador Multinomial Naive Bayes
pipeline = make_pipeline(TfidfVectorizer(), MultinomialNB())

with open("nb_emotion.pkl", "wb") as f:
pickle.dump(nb_tfidf, f)
# Ajustando o modelo ao conjunto de treinamento
pipeline.fit(train_data, train_target)

with open("tfidf_vectorizer_em.pkl", "wb") as f:
pickle.dump(tfidf_vectorizer, f)
# Salvando o pipeline em um arquivo .pkl
with open("emotion_pipeline.pkl", "wb") as model_file:
pickle.dump(pipeline, model_file)
Binary file removed api/models/tfidf_vectorizer_em.pkl
Binary file not shown.

0 comments on commit d144445

Please sign in to comment.