Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
| 4 |
+
import torch
|
| 5 |
+
import numpy as np
|
| 6 |
+
# Load sentiment analysis model
|
| 7 |
+
@st.cache_resource
|
| 8 |
+
def load_model():
|
| 9 |
+
model_name = "distilbert-base-uncased-finetuned-sst-2-english"
|
| 10 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 11 |
+
model = AutoModelForSequenceClassification.from_pretrained(model_name)
|
| 12 |
+
return pipeline('sentiment-analysis', model=model, tokenizer=tokenizer)
|
| 13 |
+
|
| 14 |
+
classifier = load_model()
|
| 15 |
+
|
| 16 |
+
# Streamlit UI
|
| 17 |
+
st.title("Sentiment Analysis App")
|
| 18 |
+
st.header("Analyze Text Sentiment")
|
| 19 |
+
|
| 20 |
+
user_input = st.text_area("Enter text to analyze:", "I love using Hugging face! It's awesome.")
|
| 21 |
+
|
| 22 |
+
if st.button("Analyze"):
|
| 23 |
+
if user_input:
|
| 24 |
+
result = classifier(user_input)
|
| 25 |
+
sentiment = result[0]['label']
|
| 26 |
+
confidence = result[0]['score']
|
| 27 |
+
|
| 28 |
+
st.subheader("Result:")
|
| 29 |
+
if sentiment == 'POSITIVE':
|
| 30 |
+
st.success(f"Positive sentiment (confidence: {confidence:.2%})")
|
| 31 |
+
else:
|
| 32 |
+
st.error(f"Negative sentiment (confidence: {confidence:.2%})")
|
| 33 |
+
else:
|
| 34 |
+
st.warning("Please enter some text to analyze!")
|