Update app.py
Browse files
app.py
CHANGED
|
@@ -1,25 +1,33 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
-
|
| 3 |
|
| 4 |
-
# Load the
|
| 5 |
-
model
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
# Perform prediction
|
| 17 |
-
|
| 18 |
-
sentiment =
|
|
|
|
|
|
|
|
|
|
| 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 |
import streamlit as st
|
| 2 |
+
from transformers import pipeline
|
| 3 |
|
| 4 |
+
# Load the sentiment analysis model from Hugging Face
|
| 5 |
+
@st.cache_resource # Cache the model for faster loading
|
| 6 |
+
def load_model():
|
| 7 |
+
return pipeline('sentiment-analysis', model='cardiffnlp/twitter-roberta-base-sentiment')
|
| 8 |
+
|
| 9 |
+
model = load_model()
|
| 10 |
|
| 11 |
# Streamlit UI
|
| 12 |
st.title("Sentiment Analysis App using GenAI Models")
|
| 13 |
|
| 14 |
# Text input from the user
|
| 15 |
+
user_input = st.text_area("Enter text to analyze sentiment:")
|
| 16 |
|
| 17 |
# Prediction button
|
| 18 |
if st.button("Analyze"):
|
| 19 |
if user_input:
|
| 20 |
# Perform prediction
|
| 21 |
+
result = model(user_input) # Hugging Face pipeline returns a dictionary
|
| 22 |
+
sentiment = result[0]['label'] # Get sentiment label (Positive/Negative/Neutral)
|
| 23 |
+
confidence = result[0]['score'] # Confidence score
|
| 24 |
+
|
| 25 |
+
# Display the sentiment and confidence score
|
| 26 |
st.write(f"**Predicted Sentiment:** {sentiment}")
|
| 27 |
+
st.write(f"**Confidence Score:** {confidence:.2f}")
|
| 28 |
else:
|
| 29 |
st.warning("Please enter some text to analyze.")
|
| 30 |
|
| 31 |
# Optional: Footer
|
| 32 |
st.write("---")
|
| 33 |
+
st.caption("Built with Streamlit and Hugging Face's GenAI models.")
|