saisi commited on
Commit
adc9432
1 Parent(s): b640d20

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +74 -0
app.py ADDED
@@ -0,0 +1,74 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import transformers
3
+ import torch
4
+ from transformers import AutoTokenizer, AutoModelForSequenceClassification
5
+
6
+ # Define the paths of the pre-trained models
7
+ model1_path = "saisi/finetuned-Sentiment-classfication-ROBERTA-Base-model"
8
+ model2_path = "saisi/finetuned-Sentiment-classfication-DISTILBERT-model"
9
+
10
+ # Initialize the tokenizer and models for sentiment analysis
11
+ tokenizer1 = AutoTokenizer.from_pretrained(model1_path)
12
+ model1 = AutoModelForSequenceClassification.from_pretrained(model1_path)
13
+ tokenizer2 = AutoTokenizer.from_pretrained(model2_path)
14
+ model2 = AutoModelForSequenceClassification.from_pretrained(model2_path)
15
+
16
+ # Define a function to preprocess the text data
17
+ def preprocess(text):
18
+     new_text = []
19
+     # Replace user mentions with '@user'
20
+     for t in text.split(" "):
21
+         t = '@user' if t.startswith('@') and len(t) > 1 else t
22
+         # Replace links with 'http'
23
+         t = 'http' if t.startswith('http') else t
24
+         new_text.append(t)
25
+     # Join the preprocessed text
26
+     return " ".join(new_text)
27
+
28
+ # Define a function to perform sentiment analysis on the input text using model 1
29
+ def sentiment_analysis_model1(text):
30
+     # Preprocess the input text
31
+     text = preprocess(text)
32
+     # Tokenize the input text using the pre-trained tokenizer
33
+     encoded_input = tokenizer1(text, return_tensors='pt')
34
+     # Feed the tokenized input to the pre-trained model and obtain output
35
+     output = model1(**encoded_input)
36
+     # Obtain the prediction scores for the output
37
+     scores_ = output[0][0].detach().numpy()
38
+     # Apply softmax activation function to obtain probability distribution over the labels
39
+     scores_ = torch.nn.functional.softmax(torch.from_numpy(scores_), dim=0).numpy()
40
+     # Format the output dictionary with the predicted scores
41
+     labels = ['Negative', 'Positive']
42
+     scores = {l:float(s) for (l,s) in zip(labels, scores_) }
43
+     # Return the scores
44
+     return scores
45
+
46
+ # Define a function to perform sentiment analysis on the input text using model 2
47
+ def sentiment_analysis_model2(text):
48
+     # Preprocess the input text
49
+     text = preprocess(text)
50
+     # Tokenize the input text using the pre-trained tokenizer
51
+     encoded_input = tokenizer2(text, return_tensors='pt')
52
+     # Feed the tokenized input to the pre-trained model and obtain output
53
+     output = model2(**encoded_input)
54
+     # Obtain the prediction scores for the output
55
+     scores_ = output[0][0].detach().numpy()
56
+     # Apply softmax activation function to obtain probability distribution over the labels
57
+     scores_ = torch.nn.functional.softmax(torch.from_numpy(scores_), dim=0).numpy()
58
+     # Format the output dictionary with the predicted scores
59
+     labels = ['Negative', 'Neutral', 'Positive']
60
+     scores = {l:float(s) for (l,s) in zip(labels, scores_) }
61
+     # Return the scores
62
+     return scores
63
+
64
+ # Define the Streamlit app
65
+ def app():
66
+     # Define the app title
67
+     st.title("Sentiment Analysis")
68
+     # Define the input field
69
+     text_input = st.text_input("Enter text:")
70
+     # Define the model selection dropdown
71
+     model_selection = st.selectbox("Select a model:", ["Model 1", "Model 2"])
72
+     # Perform sentiment analysis when the submit button is clicked
73
+     if st.button("Submit"):
74
+         if text_input