efobi commited on
Commit
9b7d933
1 Parent(s): bf388c4

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +63 -0
app.py ADDED
@@ -0,0 +1,63 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from transformers import AutoTokenizer, AutoModelForSequenceClassification
3
+ import torch
4
+ import numpy as np
5
+ from scipy.special import softmax
6
+
7
+
8
+ # Add description and title
9
+ st.write("""
10
+ # Sentiment Analysis App
11
+ """)
12
+
13
+
14
+ # Add image
15
+ image = st.image("images.png", width=200)
16
+
17
+
18
+ # Get user input
19
+ text = st.text_input("Type here:")
20
+ button = st.button('Analyze')
21
+
22
+ # Define the CSS style for the app
23
+ st.markdown(
24
+ """
25
+ <style>
26
+ body {
27
+ background-color: #f5f5f5;
28
+ }
29
+ h1 {
30
+ color: #4e79a7;
31
+ }
32
+ </style>
33
+ """,
34
+ unsafe_allow_html=True
35
+ )
36
+
37
+
38
+ def preprocess(text):
39
+ new_text = []
40
+ for t in text.split(" "):
41
+ t = '@user' if t.startswith('@') and len(t) > 1 else t
42
+ t = 'http' if t.startswith('http') else t
43
+ new_text.append(t)
44
+ return " ".join(new_text)
45
+
46
+ @st.cache_resource()
47
+ def get_model():
48
+ # Load the model and tokenizer
49
+ tokenizer = AutoTokenizer.from_pretrained("bert-base-uncased")
50
+ model = AutoModelForSequenceClassification.from_pretrained("MrDdz/bert-base-uncased")
51
+ return tokenizer,model
52
+ tokenizer, model = get_model()
53
+
54
+ if button:
55
+ text_sample = tokenizer(text, padding = 'max_length',return_tensors = 'pt')
56
+ # print(text_sample)
57
+ output = model(**text_sample)
58
+ scores_ = output[0][0].detach().numpy()
59
+ scores_ = softmax(scores_)
60
+
61
+ labels = ['Negative','Neutral','Positive']
62
+ scores = {l:float(s) for (l,s) in zip(labels,scores_)}
63
+ st.write("Prediction :",scores)