-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
68 lines (50 loc) · 1.81 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
62
63
64
65
66
67
68
#%%
import pandas as pd
import tweepy as tw
import numpy as np
from os import environ as env
import streamlit as st
from transformers import pipeline
import time
import psutil
import logging
@st.cache(
allow_output_mutation=True,
suppress_st_warning=True,
max_entries=1,
show_spinner=False,
)
def load_model():
return pipeline("zero-shot-classification", model="valhalla/distilbart-mnli-12-6")
def prettify_results(results):
return max(
list(zip(results["labels"], results["scores"])), key=lambda item: item[1]
)
tweet_text = """
The Bipartisan Infrastructure package delivers a
once-in-a-century investment to rebuild our nation in a stronger,
more sustainable way. Thanks to the leadership of @POTUS and work
of @HouseDemocrats, more jobs, safer roads, cleaner water and more
are on the way!"
"""
#%%
def main():
st.title("Tweets - Republican or Democrat?")
st.subheader("Which way does a tweet lean?")
tweet_text = st.text_area(
"Paste a tweet below:",
"The Bipartisan Infrastructure package delivers a once-in-a-century investment to rebuild our nation in a stronger, more sustainable way. Thanks to the leadership of @POTUS and work of @HouseDemocrats, more jobs, safer roads, cleaner water and more are on the way!",
)
np.random.seed(123)
labels = ["Republican", "Democrat"]
classifier = load_model()
if st.button("Get prediction"):
raw_results = classifier(tweet_text, labels)
pretty_results = prettify_results(raw_results)
st.text("Result: {}!".format(pretty_results[0]))
st.text("Probability score: {:.1%}".format(pretty_results[1]))
if __name__ == "__main__":
import transformers
transformers.logging.set_verbosity_debug()
logging.info(f"RAM memory % used: {psutil.virtual_memory()[2]}")
main()