added app
Browse files
app.py
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification,tokenzir
|
| 3 |
+
from scipy.special import softmax
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
# Load the fine-tuned model and tokenizer
|
| 7 |
+
model_dir = "./"
|
| 8 |
+
tokenizer = AutoTokenizer.from_pretrained(model_dir)
|
| 9 |
+
model = AutoModelForSequenceClassification.from_pretrained(model_dir)
|
| 10 |
+
|
| 11 |
+
# Create a Streamlit app
|
| 12 |
+
st.title('Sentiment Analysis with Fine Tuned Model')
|
| 13 |
+
st.write('Enter some text ')
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
text_input = st.text_input('Enter text here')
|
| 18 |
+
|
| 19 |
+
if st.button('Submit'):
|
| 20 |
+
|
| 21 |
+
# Tokenize the text
|
| 22 |
+
inputs = tokenizer(text_input, return_tensors="pt")
|
| 23 |
+
|
| 24 |
+
# Perform prediction
|
| 25 |
+
output = model(**inputs)
|
| 26 |
+
|
| 27 |
+
scores = output[0][0].detach().numpy()
|
| 28 |
+
scores = softmax(scores)
|
| 29 |
+
scores_dict = {
|
| 30 |
+
'Negative': scores[0],
|
| 31 |
+
'Neutral': scores[1],
|
| 32 |
+
'Positive': scores[2]
|
| 33 |
+
}
|
| 34 |
+
max_key = max(scores_dict, key=scores_dict.get)
|
| 35 |
+
|
| 36 |
+
# Get the maximum value
|
| 37 |
+
sentiment = str(scores_dict[max_key])
|
| 38 |
+
|
| 39 |
+
# Display the results
|
| 40 |
+
st.write(f'Sentiment is {data["sentiment"]}')
|
| 41 |
+
st.write(f'Score is {max_key}')
|