-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathapp.py
61 lines (46 loc) · 1.83 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import streamlit as st
import requests as r
from os.path import dirname, join, realpath
import joblib
from langdetect import detect
# add banner image
st.header("Swahili Tweet Sentiment Analysis App")
st.image("images/sentiment-image.png")
st.subheader(
"""
A simple app to analyze the sentiment of Swahili tweets.
"""
)
# form to collect news content
my_form = st.form(key="news_form")
tweet = my_form.text_input("Input your swahili tweet here")
submit = my_form.form_submit_button(label="make prediction")
# load the model and count_vectorizer
with open(
join(dirname(realpath(__file__)), "model/swahili-sentiment-model.pkl"), "rb"
) as f:
model = joblib.load(f)
with open(join(dirname(realpath(__file__)), "preprocessing/vectorizer.pkl"), "rb") as f:
vectorizer = joblib.load(f)
sentiments = {0: "Neutral", 1: "Positive", -1: "Negative"}
if submit:
if detect(tweet) == "sw":
# transform the input
transformed_tweet = vectorizer.transform([tweet])
# perform prediction
prediction = model.predict(transformed_tweet)
output = int(prediction[0])
probas = model.predict_proba(transformed_tweet)
probability = "{:.2f}".format(float(probas[:, output]))
# Display results of the NLP task
st.header("Results")
if output == 1:
st.write("The sentiment of the tweet is {} 😊".format(sentiments[output]))
elif output == -1:
st.write("The sentiment of the tweet is {} 😡 ".format(sentiments[output]))
else:
st.write("The sentiment of the tweet is {} 😐".format(sentiments[output]))
else:
st.write(" ⚠️ The tweet is not in swahili language.Please make sure the input is in swahili language")
url = "https://twitter.com/Davis_McDavid"
st.write("Developed with ❤️ by [Davis David](%s)" % url)