Spaces:
Runtime error
Runtime error
RICHARDMENSAH
commited on
Commit
•
f352d9e
1
Parent(s):
b1b8aca
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,70 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 input text
|
13 |
+
inputs = tokenizer(text, return_tensors="pt")
|
14 |
+
# Pass the tokenized input through the model
|
15 |
+
outputs = model(**inputs)
|
16 |
+
# Get the predicted class and return the corresponding sentiment
|
17 |
+
predicted_class = torch.argmax(outputs.logits, dim=-1).item()
|
18 |
+
if predicted_class == 0:
|
19 |
+
return "Negative"
|
20 |
+
elif predicted_class == 1:
|
21 |
+
return "Neutral"
|
22 |
+
else:
|
23 |
+
return "Positive"
|
24 |
+
|
25 |
+
# Setting the page configurations
|
26 |
+
st.set_page_config(
|
27 |
+
page_title="Sentiment Analysis App",
|
28 |
+
page_icon=":smile:",
|
29 |
+
layout="wide",
|
30 |
+
initial_sidebar_state="auto",
|
31 |
+
)
|
32 |
+
|
33 |
+
# Add description and title
|
34 |
+
st.write("""
|
35 |
+
# How Positive or Negative is your Text?
|
36 |
+
Enter some text and we'll tell you if it has a positive, negative, or neutral sentiment!
|
37 |
+
""")
|
38 |
+
|
39 |
+
|
40 |
+
# Add image
|
41 |
+
image = st.image("https://i0.wp.com/thedatascientist.com/wp-content/uploads/2018/10/sentiment-analysis.png", width=400)
|
42 |
+
|
43 |
+
# Get user input
|
44 |
+
text = st.text_input("Enter some text here:")
|
45 |
+
|
46 |
+
# Define the CSS style for the app
|
47 |
+
st.markdown(
|
48 |
+
"""
|
49 |
+
<style>
|
50 |
+
body {
|
51 |
+
background-color: #f5f5f5;
|
52 |
+
}
|
53 |
+
h1 {
|
54 |
+
color: #4e79a7;
|
55 |
+
}
|
56 |
+
</style>
|
57 |
+
""",
|
58 |
+
unsafe_allow_html=True
|
59 |
+
)
|
60 |
+
|
61 |
+
|
62 |
+
# Show sentiment output
|
63 |
+
if text:
|
64 |
+
sentiment = predict_sentiment(text)
|
65 |
+
if sentiment == "Positive":
|
66 |
+
st.success(f"The sentiment is {sentiment}!")
|
67 |
+
elif sentiment == "Negative":
|
68 |
+
st.error(f"The sentiment is {sentiment}.")
|
69 |
+
else:
|
70 |
+
st.warning(f"The sentiment is {sentiment}.")
|