-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
38 lines (36 loc) · 1.45 KB
/
app.py
File metadata and controls
38 lines (36 loc) · 1.45 KB
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
import streamlit as st
import numpy as np
import requests
import json
st.title('Sentiment analysis of tweet :smile:')
def query(payload, API_URL):
API_TOKEN = st.secrets['ApiHf']
headers = {"Authorization": f"Bearer {API_TOKEN}"}
data = json.dumps(payload)
response = requests.request("POST", API_URL, headers=headers, data=data)
return json.loads(response.content.decode("utf-8"))
def predict(newDf):
endPoints = ['ProsusAI/finbert','finiteautomata/bertweet-base-sentiment-analysis','elozano/tweet_sentiment_eval']
bl=[]
for i in endPoints:
API_URL = "https://api-inference.huggingface.co/models/"+i
data = query({"inputs": newDf},API_URL)
print(data)
l=[]
key= [data[0][0]['label'][:3].upper(),data[0][1]['label'][:3].upper(),data[0][2]['label'][:3].upper()]
value = [data[0][0]['score'],data[0][1]['score'],data[0][2]['score']]
l.append(value[key.index('NEG')])
l.append(value[key.index('NEU')])
l.append(value[key.index('POS')])
bl.append(l)
finalA = np.array(bl)
return np.argmax(np.mean(finalA,axis=0))
text=st.text_input(max_chars=30,placeholder="Enter one sentence",label="Enter sentense to analysis ")
if (st.button('Predict sentiment ')):
v=predict(text)
if(v==0):
st.write("Negetive Tweet :thumbsdown:")
elif(v==1):
st.write("Neutral Tweet :neutral_face:")
else:
st.write("Positive Tweet :thumbsup:")