-
Notifications
You must be signed in to change notification settings - Fork 1
/
sy4_app_draft.py
99 lines (74 loc) · 3.34 KB
/
sy4_app_draft.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
"""
Simple Streamlit webserver application for serving developed classification
models.
Author: EDSA CPT Group 4.
Note:
---------------------------------------------------------------------
# create a Readme file
Plase follow the instructions provided within the README.md file
located within this directory for guidance on how to use this script
correctly.
---------------------------------------------------------------------
Description: This file is used to launch a streamlit web
application for the EDSA Climate Change Belief Analysis project. The
Funcitonality of this app was expended from a base app that was provided
by EDSA as part of the classification predict.
"""
# Streamlit dependencies
import streamlit as st
import joblib,os
# Data dependencies
import pandas as pd
# Vectorizer
news_vectorizer = open("resources/tfidfvect.pkl","rb")
tweet_cv = joblib.load(news_vectorizer) # loading your vectorizer from the pkl file
# Load your raw data
raw = pd.read_csv("resources/train.csv")
# The main function where we will build the actual app
def main():
"""Tweet Classifier App with Streamlit """
# Creates a main title and subheader on your page -
# these are static across all pages
st.title("Tweet Classifer")
st.subheader("Climate change tweet classification")
# Creating sidebar with selection box -
# you can create multiple pages this way
options = ["Information", "General","EDA","Prediction"]
selection = st.sidebar.selectbox("Choose Option", options)
# Building out the "Information" page-------
if selection == "Information":
st.info("General Information")
# You can read a markdown file from supporting resources folder---------
st.markdown(open("resources/info.md", 'r').read())
st.subheader("Raw Twitter data and label")
if st.checkbox('Show raw data'): # data is hidden if box is unchecked
st.write(raw[['sentiment', 'message']]) # will write the df to the page
# Building out the "General" page-------
if selection == "General":
st.info("Project Overview")
# You can read a markdown file from supporting resources folder---------
st.markdown(open("resources/generaloverview.md", 'r').read())
# Building out the "EDA" page----------
if selection == "EDA":
st.info("Eploratory Data Analysis")
# You can read a markdown file from supporting resources folder--------
st.markdown(open("resources/eda.md", 'r').read())
# Building out the predication page
if selection == "Prediction":
st.info("Prediction with ML Models")
# Creating a text box for user input
tweet_text = st.text_area("Enter Text","Type Here")
if st.button("Classify"):
# Transforming user input with vectorizer
vect_text = tweet_cv.transform([tweet_text]).toarray()
# Load your .pkl file with the model of your choice + make predictions
# Try loading in multiple models to give the user a choice
predictor = joblib.load(open(os.path.join("resources/Logistic_regression.pkl"),"rb"))
prediction = predictor.predict(vect_text)
# When model has successfully run, will print prediction
# You can use a dictionary or similar structure to make this output
# more human interpretable.
st.success("Text Categorized as: {}".format(prediction))
# Required to let Streamlit instantiate our web app.
if __name__ == '__main__':
main()