Spaces:
Sleeping
Sleeping
Commit
•
89e0900
1
Parent(s):
643be30
Update app.py
Browse files
app.py
CHANGED
@@ -1,39 +1,35 @@
|
|
1 |
import streamlit as st
|
2 |
-
from transformers import
|
|
|
3 |
|
4 |
-
# Load the
|
5 |
-
|
|
|
|
|
6 |
|
7 |
-
#
|
|
|
|
|
|
|
8 |
def analyze_sentiment(text):
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
return "ERROR", 0.0 # Handle errors gracefully
|
16 |
|
17 |
# Create a Streamlit app
|
18 |
st.title("Sentiment Analysis App")
|
19 |
|
20 |
# Get the user input
|
21 |
-
text = st.
|
22 |
-
|
23 |
-
if text:
|
24 |
-
# Perform sentiment analysis
|
25 |
-
sentiment, confidence = analyze_sentiment(text)
|
26 |
-
|
27 |
-
# Set a confidence threshold
|
28 |
-
confidence_threshold = 0.5
|
29 |
|
30 |
-
|
31 |
-
if
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
else:
|
37 |
-
st.info(f"Sentiment: Neutral, Confidence: {confidence:.2f}")
|
38 |
else:
|
39 |
-
st.warning("
|
|
|
1 |
import streamlit as st
|
2 |
+
from transformers import BertTokenizer, BertForSequenceClassification
|
3 |
+
import torch
|
4 |
|
5 |
+
# Load the fine-tuned model and tokenizer
|
6 |
+
model_path = 'path/to/your/fine-tuned/model'
|
7 |
+
tokenizer = BertTokenizer.from_pretrained(model_path)
|
8 |
+
model = BertForSequenceClassification.from_pretrained(model_path)
|
9 |
|
10 |
+
# Set the model to evaluation mode
|
11 |
+
model.eval()
|
12 |
+
|
13 |
+
# Function to perform sentiment analysis
|
14 |
def analyze_sentiment(text):
|
15 |
+
inputs = tokenizer(text, return_tensors='pt', truncation=True, padding=True)
|
16 |
+
with torch.no_grad():
|
17 |
+
outputs = model(**inputs)
|
18 |
+
logits = outputs.logits
|
19 |
+
predicted_class = logits.argmax().item()
|
20 |
+
return predicted_class
|
|
|
21 |
|
22 |
# Create a Streamlit app
|
23 |
st.title("Sentiment Analysis App")
|
24 |
|
25 |
# Get the user input
|
26 |
+
text = st.text_area("Enter text for sentiment analysis")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
|
28 |
+
if st.button("Analyze Sentiment"):
|
29 |
+
if text:
|
30 |
+
sentiment_class = analyze_sentiment(text)
|
31 |
+
sentiment_labels = ['Negative', 'Neutral', 'Positive'] # Adjust as needed
|
32 |
+
sentiment = sentiment_labels[sentiment_class]
|
33 |
+
st.write(f"Sentiment: {sentiment}")
|
|
|
|
|
34 |
else:
|
35 |
+
st.warning("Please enter text for analysis.")
|