Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from transformers import pipeline
|
3 |
+
|
4 |
+
# Load sentiment analysis model
|
5 |
+
@st.cache_resource
|
6 |
+
def load_model():
|
7 |
+
return pipeline(
|
8 |
+
"sentiment-analysis",
|
9 |
+
model="distilbert-base-uncased-finetuned-sst-2-english"
|
10 |
+
)
|
11 |
+
|
12 |
+
sentiment_model = load_model()
|
13 |
+
|
14 |
+
# Main app
|
15 |
+
def main():
|
16 |
+
# UI setup
|
17 |
+
st.title("Sentiment Analyzer")
|
18 |
+
st.write("Enter a sentence to analyze its sentiment (Positive/Negative)")
|
19 |
+
|
20 |
+
# Text input
|
21 |
+
user_input = st.text_area("Your Text", placeholder="Type your sentence here...", height=100)
|
22 |
+
|
23 |
+
# Analyze button and result display
|
24 |
+
if st.button("Analyze"):
|
25 |
+
if user_input:
|
26 |
+
try:
|
27 |
+
result = sentiment_model(user_input)[0]
|
28 |
+
sentiment = result['label']
|
29 |
+
confidence = result['score']
|
30 |
+
|
31 |
+
# Display results
|
32 |
+
st.success(f"Sentiment: {sentiment}")
|
33 |
+
st.info(f"Confidence: {confidence:.4f}")
|
34 |
+
|
35 |
+
# Footer shown only after analysis
|
36 |
+
st.markdown("""
|
37 |
+
<p style="font-size: small; color: grey; text-align: center;">
|
38 |
+
Developed By: Krishna Prakash
|
39 |
+
<a href="https://www.linkedin.com/in/krishnaprakash-profile/" target="_blank">
|
40 |
+
<img src="https://img.icons8.com/ios-filled/30/0077b5/linkedin.png" alt="LinkedIn" style="vertical-align: middle; margin: 0 5px;"/>
|
41 |
+
</a>
|
42 |
+
</p>
|
43 |
+
""", unsafe_allow_html=True)
|
44 |
+
|
45 |
+
except Exception as e:
|
46 |
+
st.error(f"An error occurred: {str(e)}")
|
47 |
+
else:
|
48 |
+
st.warning("Please enter some text to analyze!")
|
49 |
+
|
50 |
+
if __name__ == "__main__":
|
51 |
+
main()
|