Spaces:
Runtime error
Runtime error
Oliver Li
commited on
Commit
·
4b4d3c0
1
Parent(s):
2599dc4
added app and requirement files
Browse files- app.py +38 -0
- requirements.txt +3 -0
app.py
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from transformers import pipeline, AutoTokenizer, AutoModelForSequenceClassification
|
| 3 |
+
|
| 4 |
+
# Function to load the pre-trained model
|
| 5 |
+
def load_model(model_name):
|
| 6 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 7 |
+
model = AutoModelForSequenceClassification.from_pretrained(model_name)
|
| 8 |
+
sentiment_pipeline = pipeline("sentiment-analysis", tokenizer=tokenizer, model=model)
|
| 9 |
+
return sentiment_pipeline
|
| 10 |
+
|
| 11 |
+
# Streamlit app
|
| 12 |
+
st.title("Basic Sentiment Analysis App")
|
| 13 |
+
st.write("Enter a text and select a pre-trained model to get the sentiment analysis.")
|
| 14 |
+
|
| 15 |
+
# Input text
|
| 16 |
+
text = st.text_input("Enter your text:")
|
| 17 |
+
|
| 18 |
+
# Model selection
|
| 19 |
+
model_options = [
|
| 20 |
+
"distilbert-base-uncased-finetuned-sst-2-english",
|
| 21 |
+
"textattack/bert-base-uncased-SST-2",
|
| 22 |
+
"cardiffnlp/twitter-roberta-base-sentiment",
|
| 23 |
+
"nlptown/bert-base-multilingual-uncased-sentiment"
|
| 24 |
+
]
|
| 25 |
+
|
| 26 |
+
selected_model = st.selectbox("Choose a pre-trained model:", model_options)
|
| 27 |
+
|
| 28 |
+
# Load the model and perform sentiment analysis
|
| 29 |
+
if st.button("Analyze"):
|
| 30 |
+
if not text:
|
| 31 |
+
st.write("Please enter a text.")
|
| 32 |
+
else:
|
| 33 |
+
with st.spinner("Analyzing sentiment..."):
|
| 34 |
+
sentiment_pipeline = load_model(selected_model)
|
| 35 |
+
result = sentiment_pipeline(text)
|
| 36 |
+
st.write(f"Sentiment: {result[0]['label']} (confidence: {result[0]['score']:.2f})")
|
| 37 |
+
else:
|
| 38 |
+
st.write("Enter a text and click 'Analyze' to perform sentiment analysis.")
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
streamlit
|
| 2 |
+
torch
|
| 3 |
+
transformers
|