Update app.py
Browse files
app.py
CHANGED
|
@@ -1,25 +1,31 @@
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
-
import
|
| 3 |
|
| 4 |
-
# Load
|
| 5 |
-
|
|
|
|
| 6 |
|
| 7 |
-
# Streamlit UI
|
| 8 |
st.title("Sentiment Analysis App using GenAI Models")
|
| 9 |
|
| 10 |
# Text input from the user
|
| 11 |
-
user_input = st.text_area("Enter text to analyze sentiment:"
|
| 12 |
|
| 13 |
# Prediction button
|
| 14 |
if st.button("Analyze"):
|
| 15 |
if user_input:
|
| 16 |
-
#
|
| 17 |
-
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
st.write(f"**Predicted Sentiment:** {sentiment}")
|
| 20 |
else:
|
| 21 |
st.warning("Please enter some text to analyze.")
|
| 22 |
-
|
| 23 |
-
# Optional: Footer
|
| 24 |
-
st.write("---")
|
| 25 |
-
st.caption("Built with Streamlit and GenAI models.")
|
|
|
|
| 1 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
| 2 |
import streamlit as st
|
| 3 |
+
import torch
|
| 4 |
|
| 5 |
+
# Load tokenizer and model from Hugging Face Hub
|
| 6 |
+
tokenizer = AutoTokenizer.from_pretrained("cardiffnlp/twitter-roberta-base-sentiment")
|
| 7 |
+
model = AutoModelForSequenceClassification.from_pretrained("cardiffnlp/twitter-roberta-base-sentiment")
|
| 8 |
|
| 9 |
+
# Streamlit UI setup
|
| 10 |
st.title("Sentiment Analysis App using GenAI Models")
|
| 11 |
|
| 12 |
# Text input from the user
|
| 13 |
+
user_input = st.text_area("Enter text to analyze sentiment:")
|
| 14 |
|
| 15 |
# Prediction button
|
| 16 |
if st.button("Analyze"):
|
| 17 |
if user_input:
|
| 18 |
+
# Tokenize the user input
|
| 19 |
+
inputs = tokenizer(user_input, return_tensors="pt")
|
| 20 |
+
|
| 21 |
+
# Perform inference
|
| 22 |
+
with torch.no_grad():
|
| 23 |
+
outputs = model(**inputs)
|
| 24 |
+
|
| 25 |
+
# Interpret the results
|
| 26 |
+
predicted_class = torch.argmax(outputs.logits, dim=1).item()
|
| 27 |
+
sentiment = ["Negative", "Neutral", "Positive"][predicted_class] # Assuming 3 classes
|
| 28 |
+
|
| 29 |
st.write(f"**Predicted Sentiment:** {sentiment}")
|
| 30 |
else:
|
| 31 |
st.warning("Please enter some text to analyze.")
|
|
|
|
|
|
|
|
|
|
|
|