miknad2319 commited on
Commit
f29cf14
1 Parent(s): eba120d

Create test-app.py

Browse files
Files changed (1) hide show
  1. test-app.py +56 -0
test-app.py ADDED
@@ -0,0 +1,56 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import requests
3
+ from transformers import pipeline
4
+
5
+ ############ 1. PAGE LAYOUT, TITLE
6
+ st.set_page_config(
7
+ layout="centered", page_title='Simple Sentiment Analysis App Using\n\
8
+ Hugging Face Model Library', page_icon="(y)"
9
+ )
10
+
11
+ c1,c2,c3 = st.columns([1,3,1])
12
+
13
+ with c1:
14
+ st.write("")
15
+ with c2:
16
+ st.image("images/emotions.png")
17
+ with c3:
18
+ st.write("")
19
+
20
+ # prepare a list of top sentiment analysis models including default
21
+ models = ["distilbert-base-uncased-finetuned-sst-2-english",#default
22
+ "bhadresh-savani/distilbert-base-uncased-emotion",#emotions
23
+ "ProsusAI/finbert",#finance
24
+ "finiteautomata/bertweet-base-sentiment-analysis",#tweets
25
+ "cardiffnlp/twitter-roberta-base-sentiment"#tweet2
26
+ ]
27
+ model_pointers = ["default: distilbert-base-uncased-finetuned-sst-2-english",
28
+ "emotion: bhadresh-savani/distilbert-base-uncased-emotion",
29
+ "finance: ProsusAI/finbert",
30
+ "tweets: finiteautomata/bertweet-base-sentiment-analysis",
31
+ "tweets2: cardiffnlp/twitter-roberta-base-sentiment"
32
+ ]
33
+
34
+ #Prompt User for input text for sentiment analysis, keep input and model selection in form to delay page refresh
35
+ with st.form(key="init_form"):
36
+ input_text = st.text_area("Input a sentence on which to perform sentiment\
37
+ analysis", value="I love Streamlit and I love Data Science!")
38
+ choice = st.selectbox("Choose Model", model_pointers)
39
+
40
+ # The index of choice in model_pointers will access the models list
41
+ # and select the Hugging Face model path at index.
42
+ user_picked_model = models[model_pointers.index(choice)]
43
+ with st.spinner("Downloading Model"):
44
+ sentiment_pipeline = pipeline(model=user_picked_model)
45
+
46
+ analyze = st.form_submit_button("Analyze")
47
+
48
+ if analyze:
49
+ with st.spinner("Analyzing..."):
50
+ sentiment_pipeline = pipeline(model=user_picked_model)
51
+ sentiment_results=sentiment_pipeline(input_text)
52
+ st.write(f"Sentiment: {sentiment_results[0]['label']}")
53
+ st.write(f"Score: {sentiment_results[0]['score']}")
54
+ else:
55
+ st.write("no input detected")
56
+