Spaces:
Runtime error
Runtime error
Commit ·
c752f17
1
Parent(s): b82c543
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
|
| 4 |
+
# Load sentiment analysis model
|
| 5 |
+
model = "cardiffnlp/twitter-roberta-base-sentiment-latest"
|
| 6 |
+
sentiment_analysis = pipeline("text-classification", model=model)
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
# Streamlit app
|
| 10 |
+
def main():
|
| 11 |
+
# Set app title and description
|
| 12 |
+
st.title("Sentiment Analysis App")
|
| 13 |
+
st.write("Enter text to predict sentiment.")
|
| 14 |
+
|
| 15 |
+
# User input
|
| 16 |
+
text = st.text_area("Text", "")
|
| 17 |
+
|
| 18 |
+
# Predict sentiment
|
| 19 |
+
if st.button("Predict"):
|
| 20 |
+
if text.strip() != "":
|
| 21 |
+
sentiment = predict_sentiment(text)
|
| 22 |
+
score = sentiment['score'] * 100
|
| 23 |
+
st.metric(label = f"Sentiment: {sentiment['label']}", value = f"Score: {score:.2f}%")
|
| 24 |
+
#st.write(f"Sentiment: {sentiment['label']}")
|
| 25 |
+
#st.write(f"Score: {sentiment['score']}")
|
| 26 |
+
else:
|
| 27 |
+
st.warning("Please enter some text.")
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
def predict_sentiment(text):
|
| 31 |
+
result = sentiment_analysis(text)[0]
|
| 32 |
+
return result
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
if __name__ == "__main__":
|
| 36 |
+
main()
|