Spaces:
Runtime error
Runtime error
ikoghoemmanuell
commited on
Commit
•
4bc3168
1
Parent(s):
40c4abe
Upload 2 files
Browse files- app.py +73 -0
- requirements.txt +2 -0
app.py
ADDED
@@ -0,0 +1,73 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import transformers
|
3 |
+
import torch
|
4 |
+
|
5 |
+
# Load the model and tokenizer
|
6 |
+
model = transformers.AutoModelForSequenceClassification.from_pretrained("ikoghoemmanuell/finetuned_sentiment_model")
|
7 |
+
tokenizer = transformers.AutoTokenizer.from_pretrained("ikoghoemmanuell/finetuned_sentiment_tokenizer")
|
8 |
+
|
9 |
+
# Define the function for sentiment analysis
|
10 |
+
@st.cache_resource
|
11 |
+
def predict_sentiment(text):
|
12 |
+
# Tokenize the text
|
13 |
+
encoded_input = tokenizer(text, truncation=True, padding=True, return_tensors='pt')
|
14 |
+
|
15 |
+
# Forward pass through the model
|
16 |
+
output = model(**encoded_input)
|
17 |
+
logits = output.logits
|
18 |
+
|
19 |
+
# Compute probabilities and predicted label
|
20 |
+
probabilities = torch.softmax(logits, dim=1)
|
21 |
+
predicted_label = torch.argmax(probabilities, dim=1)
|
22 |
+
|
23 |
+
# Get sentiment label and score
|
24 |
+
sentiment_label = tokenizer.decode(predicted_label.squeeze().item())
|
25 |
+
sentiment_score = probabilities[0, predicted_label].item()
|
26 |
+
|
27 |
+
return sentiment_label, sentiment_score
|
28 |
+
|
29 |
+
# Setting the page configurations
|
30 |
+
st.set_page_config(
|
31 |
+
page_title="Sentiment Analysis App",
|
32 |
+
page_icon=":smile:",
|
33 |
+
layout="wide",
|
34 |
+
initial_sidebar_state="auto",
|
35 |
+
)
|
36 |
+
|
37 |
+
# Add description and title
|
38 |
+
st.write("""
|
39 |
+
# How Positive or Negative is your Text?
|
40 |
+
Enter some text and we'll tell you if it has a positive, negative, or neutral sentiment!
|
41 |
+
""")
|
42 |
+
|
43 |
+
|
44 |
+
# Add image
|
45 |
+
image = st.image("https://i0.wp.com/thedatascientist.com/wp-content/uploads/2018/10/sentiment-analysis.png", width=400)
|
46 |
+
|
47 |
+
# Get user input
|
48 |
+
text = st.text_input("Enter some text here:")
|
49 |
+
|
50 |
+
# Define the CSS style for the app
|
51 |
+
st.markdown(
|
52 |
+
"""
|
53 |
+
<style>
|
54 |
+
body {
|
55 |
+
background-color: #f5f5f5;
|
56 |
+
}
|
57 |
+
h1 {
|
58 |
+
color: #4e79a7;
|
59 |
+
}
|
60 |
+
</style>
|
61 |
+
""",
|
62 |
+
unsafe_allow_html=True
|
63 |
+
)
|
64 |
+
|
65 |
+
# Show sentiment output
|
66 |
+
if text:
|
67 |
+
sentiment, score = predict_sentiment(text)
|
68 |
+
if sentiment == "Positive":
|
69 |
+
st.success(f"The sentiment is {sentiment} with a score of {score*100:.2f}%!")
|
70 |
+
elif sentiment == "Negative":
|
71 |
+
st.error(f"The sentiment is {sentiment} with a score of {score*100:.2f}%!")
|
72 |
+
else:
|
73 |
+
st.warning(f"The sentiment is {sentiment} with a score of {score*100:.2f}%!")
|
requirements.txt
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
torch
|
2 |
+
transformers
|