Skip to content

Instantly share code, notes, and snippets.

@Davisy
Created June 25, 2021 08:06
Show Gist options
  • Save Davisy/798e4d0f51664bd326548627e89502b2 to your computer and use it in GitHub Desktop.
Save Davisy/798e4d0f51664bd326548627e89502b2 to your computer and use it in GitHub Desktop.
@app.get("/predict-review")
def predict_sentiment(review: str):
"""
A simple function that receive a review content and predict the sentiment of the content.
:param review:
:return: prediction, probabilities
"""
# clean the review
cleaned_review = text_cleaning(review)
# perform prediction
prediction = model.predict([cleaned_review])
output = int(prediction[0])
probas = model.predict_proba([cleaned_review])
output_probability = "{:.2f}".format(float(probas[:, output]))
# output dictionary
sentiments = {0: "Negative", 1: "Positive"}
# show results
result = {"prediction": sentiments[output], "Probability": output_probability}
return result
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment