Skip to content

Commit

Permalink
integration with pre trained pipelines
Browse files Browse the repository at this point in the history
  • Loading branch information
jonasgabriel18 committed Dec 18, 2023
2 parents 13f1572 + d144445 commit 2f5b0ee
Show file tree
Hide file tree
Showing 8 changed files with 21 additions and 17 deletions.
18 changes: 11 additions & 7 deletions api/NbEmotionsModel.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,16 @@
#tfidf_vectorizer = TfidfVectorizer(use_idf=True)

def make_prediction(my_sentence):
with open("./models/nb_emotion.pkl", "rb") as f:
model = pickle.load(f)
model_file = "./models/emotions_pipeline.pkl"
try:
# Carregando o pipeline do arquivo .pkl
with open(model_file, 'rb') as model_file:
pipeline = pickle.load(model_file)

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

new_sentence = tfidf_vectorizer.transform([my_sentence])
prediction = model.predict(new_sentence)
return prediction[0]
return predictions[0]

except Exception as e:
return str(e)
Binary file added api/models/emotion_pipeline.pkl
Binary file not shown.
Binary file removed api/models/linear_reg.pkl
Binary file not shown.
Binary file removed api/models/nb_emotion.pkl
Binary file not shown.
File renamed without changes.
Binary file removed api/models/tfidf_vectorizer_em.pkl
Binary file not shown.
Binary file removed api/models/vectorizer_lin.pkl
Binary file not shown.
20 changes: 10 additions & 10 deletions api/models_code/nb_emotions.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
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 +13,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("../models/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("../models/tfidf_vectorizer_em.pkl", "wb") as f:
pickle.dump(tfidf_vectorizer, f)
# Salvando o pipeline em um arquivo .pkl
with open("../models/emotion_pipeline.pkl", "wb") as model_file:
pickle.dump(pipeline, model_file)

0 comments on commit 2f5b0ee

Please sign in to comment.