Update app.py
Browse files
app.py
CHANGED
|
@@ -4,43 +4,17 @@ from transformers import pipeline
|
|
| 4 |
|
| 5 |
# Load the model
|
| 6 |
classifier = pipeline("text-classification", model="Mpavan45/Telugu_Sentimental_Analysis")
|
|
|
|
|
|
|
| 7 |
st.markdown("""
|
| 8 |
<style>
|
| 9 |
-
/* Background image for the entire app */
|
| 10 |
.stApp {
|
| 11 |
-
background-image: url('https://cdn-uploads.huggingface.co/production/uploads/675fab3a2d0851e23d23cad3/
|
| 12 |
background-size: cover;
|
| 13 |
background-position: center;
|
| 14 |
background-repeat: no-repeat;
|
| 15 |
background-attachment: fixed;
|
| 16 |
}
|
| 17 |
-
/* Radium title styling */
|
| 18 |
-
.radium-title {
|
| 19 |
-
font-size: 40px;
|
| 20 |
-
text-align: center;
|
| 21 |
-
color: #fff;
|
| 22 |
-
padding: 10px;
|
| 23 |
-
border-radius: 10px;
|
| 24 |
-
background: linear-gradient(90deg, #ff416c, #ff4b2b);
|
| 25 |
-
box-shadow: 0 0 20px #ff416c, 0 0 30px #ff4b2b;
|
| 26 |
-
}
|
| 27 |
-
/* Radium label styling */
|
| 28 |
-
.radium-label {
|
| 29 |
-
font-size: 24px;
|
| 30 |
-
font-weight: bold;
|
| 31 |
-
color: white;
|
| 32 |
-
padding: 10px;
|
| 33 |
-
border-radius: 8px;
|
| 34 |
-
background: linear-gradient(90deg, #36d1dc, #5b86e5);
|
| 35 |
-
display: inline-block;
|
| 36 |
-
margin-top: 10px;
|
| 37 |
-
}
|
| 38 |
-
</style>
|
| 39 |
-
""", unsafe_allow_html=True)
|
| 40 |
-
|
| 41 |
-
# CSS for radium effect
|
| 42 |
-
st.markdown("""
|
| 43 |
-
<style>
|
| 44 |
.radium-title {
|
| 45 |
font-size: 40px;
|
| 46 |
text-align: center;
|
|
@@ -64,10 +38,10 @@ st.markdown("""
|
|
| 64 |
""", unsafe_allow_html=True)
|
| 65 |
|
| 66 |
# Title
|
| 67 |
-
st.markdown('<div class="radium-title">Sentiment Analysis with BERT</div>', unsafe_allow_html=True)
|
| 68 |
st.write("This app uses a fine-tuned BERT model to classify Telugu text as Positive, Negative, or Neutral.")
|
| 69 |
|
| 70 |
-
#
|
| 71 |
label_map = {
|
| 72 |
"LABEL_0": ("Negative", "😞"),
|
| 73 |
"LABEL_1": ("Neutral", "😐"),
|
|
@@ -76,17 +50,16 @@ label_map = {
|
|
| 76 |
|
| 77 |
# Telugu input checker
|
| 78 |
def is_telugu_text(text):
|
| 79 |
-
|
| 80 |
-
cleaned_text = re.sub(r'[\u0C00-\u0C7F\s\.\,\!\?]', '', text)
|
| 81 |
return len(cleaned_text) == 0
|
| 82 |
|
| 83 |
-
# Session state
|
| 84 |
if "text_input" not in st.session_state:
|
| 85 |
st.session_state.text_input = ""
|
| 86 |
if "result_shown" not in st.session_state:
|
| 87 |
st.session_state.result_shown = False
|
| 88 |
|
| 89 |
-
#
|
| 90 |
st.subheader("Try one of the following examples:")
|
| 91 |
examples = [
|
| 92 |
"ఈ ఆహారం చాలా చెడుగా ఉంది",
|
|
@@ -97,7 +70,6 @@ examples = [
|
|
| 97 |
"ఈ వాతావరణం నాకు చాలా ఉష్ణంగా ఉంది"
|
| 98 |
]
|
| 99 |
|
| 100 |
-
# Display examples in 3 rows × 2 columns
|
| 101 |
for i in range(0, len(examples), 2):
|
| 102 |
cols = st.columns(2)
|
| 103 |
for j in range(2):
|
|
@@ -105,26 +77,26 @@ for i in range(0, len(examples), 2):
|
|
| 105 |
example = examples[i + j]
|
| 106 |
if cols[j].button(example[:30] + "..."):
|
| 107 |
st.session_state.text_input = example
|
| 108 |
-
st.session_state.result_shown =
|
| 109 |
|
| 110 |
-
# Text
|
| 111 |
-
input_text = st.text_area("Enter text to analyze sentiment:",
|
| 112 |
|
| 113 |
# Analyze button
|
| 114 |
if st.button("Analyze Sentiment"):
|
| 115 |
-
|
| 116 |
-
st.session_state.result_shown = True
|
| 117 |
-
|
| 118 |
-
# Output
|
| 119 |
-
if st.session_state.result_shown:
|
| 120 |
-
if not st.session_state.text_input.strip():
|
| 121 |
st.warning("Please enter some text to analyze!")
|
| 122 |
-
|
| 123 |
-
|
|
|
|
| 124 |
st.session_state.result_shown = False
|
| 125 |
else:
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
# Load the model
|
| 6 |
classifier = pipeline("text-classification", model="Mpavan45/Telugu_Sentimental_Analysis")
|
| 7 |
+
|
| 8 |
+
# Background & Style
|
| 9 |
st.markdown("""
|
| 10 |
<style>
|
|
|
|
| 11 |
.stApp {
|
| 12 |
+
background-image: url('https://cdn-uploads.huggingface.co/production/uploads/675fab3a2d0851e23d23cad3/zMmw55ImaQKf-ExZcw9w4.jpeg');
|
| 13 |
background-size: cover;
|
| 14 |
background-position: center;
|
| 15 |
background-repeat: no-repeat;
|
| 16 |
background-attachment: fixed;
|
| 17 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
.radium-title {
|
| 19 |
font-size: 40px;
|
| 20 |
text-align: center;
|
|
|
|
| 38 |
""", unsafe_allow_html=True)
|
| 39 |
|
| 40 |
# Title
|
| 41 |
+
st.markdown('<div class="radium-title"> Telugu Sentiment Analysis with BERT</div>', unsafe_allow_html=True)
|
| 42 |
st.write("This app uses a fine-tuned BERT model to classify Telugu text as Positive, Negative, or Neutral.")
|
| 43 |
|
| 44 |
+
# Label mapping
|
| 45 |
label_map = {
|
| 46 |
"LABEL_0": ("Negative", "😞"),
|
| 47 |
"LABEL_1": ("Neutral", "😐"),
|
|
|
|
| 50 |
|
| 51 |
# Telugu input checker
|
| 52 |
def is_telugu_text(text):
|
| 53 |
+
cleaned_text = re.sub(r'[\u0C00-\u0C7F0-9\s\.\,\!\?]', '', text)
|
|
|
|
| 54 |
return len(cleaned_text) == 0
|
| 55 |
|
| 56 |
+
# Session state setup
|
| 57 |
if "text_input" not in st.session_state:
|
| 58 |
st.session_state.text_input = ""
|
| 59 |
if "result_shown" not in st.session_state:
|
| 60 |
st.session_state.result_shown = False
|
| 61 |
|
| 62 |
+
# Examples
|
| 63 |
st.subheader("Try one of the following examples:")
|
| 64 |
examples = [
|
| 65 |
"ఈ ఆహారం చాలా చెడుగా ఉంది",
|
|
|
|
| 70 |
"ఈ వాతావరణం నాకు చాలా ఉష్ణంగా ఉంది"
|
| 71 |
]
|
| 72 |
|
|
|
|
| 73 |
for i in range(0, len(examples), 2):
|
| 74 |
cols = st.columns(2)
|
| 75 |
for j in range(2):
|
|
|
|
| 77 |
example = examples[i + j]
|
| 78 |
if cols[j].button(example[:30] + "..."):
|
| 79 |
st.session_state.text_input = example
|
| 80 |
+
st.session_state.result_shown = False
|
| 81 |
|
| 82 |
+
# Text Area for user input
|
| 83 |
+
input_text = st.text_area("Enter text to analyze sentiment:", height=150)
|
| 84 |
|
| 85 |
# Analyze button
|
| 86 |
if st.button("Analyze Sentiment"):
|
| 87 |
+
if not input_text.strip():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
st.warning("Please enter some text to analyze!")
|
| 89 |
+
st.session_state.result_shown = False
|
| 90 |
+
elif not is_telugu_text(input_text):
|
| 91 |
+
st.error("Please enter valid **Telugu** text only.")
|
| 92 |
st.session_state.result_shown = False
|
| 93 |
else:
|
| 94 |
+
st.session_state.text_input = input_text
|
| 95 |
+
st.session_state.result_shown = True
|
| 96 |
+
|
| 97 |
+
# Output
|
| 98 |
+
if st.session_state.result_shown:
|
| 99 |
+
result = classifier(st.session_state.text_input)
|
| 100 |
+
raw_label = result[0]['label']
|
| 101 |
+
sentiment, emoji = label_map.get(raw_label, (raw_label, ""))
|
| 102 |
+
st.markdown(f'<div class="radium-label">Sentiment: {sentiment} {emoji}</div>', unsafe_allow_html=True)
|