Update app.py
Browse files
app.py
CHANGED
|
@@ -1,3 +1,90 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
from transformers import pipeline
|
| 3 |
import re
|
|
@@ -9,7 +96,7 @@ classifier = pipeline("text-classification", model="Mpavan45/Telugu_Sentimental_
|
|
| 9 |
labels = ["neutral", "positive", "negative"]
|
| 10 |
emojis = {"positive": "🤗", "negative": "😔", "neutral": "😐"}
|
| 11 |
|
| 12 |
-
#
|
| 13 |
st.markdown("""
|
| 14 |
<style>
|
| 15 |
.stApp {
|
|
@@ -18,7 +105,9 @@ st.markdown("""
|
|
| 18 |
background-position: center;
|
| 19 |
background-repeat: no-repeat;
|
| 20 |
background-attachment: fixed;
|
|
|
|
| 21 |
}
|
|
|
|
| 22 |
.radium-title {
|
| 23 |
font-size: 40px;
|
| 24 |
text-align: center;
|
|
@@ -27,7 +116,9 @@ st.markdown("""
|
|
| 27 |
border-radius: 10px;
|
| 28 |
background: linear-gradient(90deg, #8E2DE2, #4A00E0);
|
| 29 |
box-shadow: 0 0 20px #8E2DE2, 0 0 30px #4A00E0;
|
|
|
|
| 30 |
}
|
|
|
|
| 31 |
.radium-label {
|
| 32 |
font-size: 28px;
|
| 33 |
font-weight: bold;
|
|
@@ -38,10 +129,45 @@ st.markdown("""
|
|
| 38 |
display: inline-block;
|
| 39 |
margin-top: 20px;
|
| 40 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
</style>
|
| 42 |
""", unsafe_allow_html=True)
|
| 43 |
|
| 44 |
-
#
|
|
|
|
|
|
|
|
|
|
| 45 |
def is_mostly_telugu(text):
|
| 46 |
if not text.strip():
|
| 47 |
return False
|
|
@@ -54,34 +180,44 @@ def is_mostly_telugu(text):
|
|
| 54 |
valid_chars = telugu_chars + allowed_chars == total_chars
|
| 55 |
return telugu_ratio >= 0.7 and valid_chars
|
| 56 |
|
| 57 |
-
# Clean text
|
| 58 |
def clean_input(text):
|
| 59 |
cleaned_text = re.sub(r'[^a-zA-Z0-9\u0C00-\u0C7F\s?.!]', ' ', text)
|
| 60 |
cleaned_text = re.sub(r'([?.!])(?![?.!]\s|$)', '', cleaned_text)
|
| 61 |
return ' '.join(cleaned_text.split())
|
| 62 |
|
| 63 |
-
#
|
| 64 |
-
st.markdown(
|
|
|
|
|
|
|
| 65 |
|
| 66 |
# Input
|
| 67 |
-
user_input = st.text_area("
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
if
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# import streamlit as st
|
| 2 |
+
# from transformers import pipeline
|
| 3 |
+
# import re
|
| 4 |
+
|
| 5 |
+
# # Load the model
|
| 6 |
+
# classifier = pipeline("text-classification", model="Mpavan45/Telugu_Sentimental_Analysis")
|
| 7 |
+
|
| 8 |
+
# # Label mapping and emojis
|
| 9 |
+
# labels = ["neutral", "positive", "negative"]
|
| 10 |
+
# emojis = {"positive": "🤗", "negative": "😔", "neutral": "😐"}
|
| 11 |
+
|
| 12 |
+
# # Background and Title Styling
|
| 13 |
+
# st.markdown("""
|
| 14 |
+
# <style>
|
| 15 |
+
# .stApp {
|
| 16 |
+
# background-image: url('https://cdn-uploads.huggingface.co/production/uploads/675fab3a2d0851e23d23cad3/_YKXYHCbjM44ubGwnAKeQ.jpeg');
|
| 17 |
+
# background-size: cover;
|
| 18 |
+
# background-position: center;
|
| 19 |
+
# background-repeat: no-repeat;
|
| 20 |
+
# background-attachment: fixed;
|
| 21 |
+
# }
|
| 22 |
+
# .radium-title {
|
| 23 |
+
# font-size: 40px;
|
| 24 |
+
# text-align: center;
|
| 25 |
+
# color: #fff;
|
| 26 |
+
# padding: 10px;
|
| 27 |
+
# border-radius: 10px;
|
| 28 |
+
# background: linear-gradient(90deg, #8E2DE2, #4A00E0);
|
| 29 |
+
# box-shadow: 0 0 20px #8E2DE2, 0 0 30px #4A00E0;
|
| 30 |
+
# }
|
| 31 |
+
# .radium-label {
|
| 32 |
+
# font-size: 28px;
|
| 33 |
+
# font-weight: bold;
|
| 34 |
+
# color: white;
|
| 35 |
+
# padding: 10px 20px;
|
| 36 |
+
# border-radius: 12px;
|
| 37 |
+
# background: linear-gradient(90deg, #7F00FF, #E100FF);
|
| 38 |
+
# display: inline-block;
|
| 39 |
+
# margin-top: 20px;
|
| 40 |
+
# }
|
| 41 |
+
# </style>
|
| 42 |
+
# """, unsafe_allow_html=True)
|
| 43 |
+
|
| 44 |
+
# # Check Telugu text validity
|
| 45 |
+
# def is_mostly_telugu(text):
|
| 46 |
+
# if not text.strip():
|
| 47 |
+
# return False
|
| 48 |
+
# telugu_pattern = r'[\u0C00-\u0C7F]'
|
| 49 |
+
# allowed_pattern = r'[a-zA-Z0-9\s.,!?]'
|
| 50 |
+
# telugu_chars = len(re.findall(telugu_pattern, text))
|
| 51 |
+
# allowed_chars = len(re.findall(allowed_pattern, text))
|
| 52 |
+
# total_chars = len(text)
|
| 53 |
+
# telugu_ratio = telugu_chars / total_chars if total_chars > 0 else 0
|
| 54 |
+
# valid_chars = telugu_chars + allowed_chars == total_chars
|
| 55 |
+
# return telugu_ratio >= 0.7 and valid_chars
|
| 56 |
+
|
| 57 |
+
# # Clean text
|
| 58 |
+
# def clean_input(text):
|
| 59 |
+
# cleaned_text = re.sub(r'[^a-zA-Z0-9\u0C00-\u0C7F\s?.!]', ' ', text)
|
| 60 |
+
# cleaned_text = re.sub(r'([?.!])(?![?.!]\s|$)', '', cleaned_text)
|
| 61 |
+
# return ' '.join(cleaned_text.split())
|
| 62 |
+
|
| 63 |
+
# # Title
|
| 64 |
+
# st.markdown('<div class="radium-title">Telugu Sentiment Analysis</div>', unsafe_allow_html=True)
|
| 65 |
+
|
| 66 |
+
# # Input
|
| 67 |
+
# user_input = st.text_area("Enter your Telugu text:")
|
| 68 |
+
|
| 69 |
+
# if st.button("Predict"):
|
| 70 |
+
# if not user_input.strip():
|
| 71 |
+
# st.warning("Please enter some Telugu text.")
|
| 72 |
+
# else:
|
| 73 |
+
# cleaned = clean_input(user_input)
|
| 74 |
+
# if not is_mostly_telugu(cleaned):
|
| 75 |
+
# st.error("Please enter text primarily in Telugu script.")
|
| 76 |
+
# else:
|
| 77 |
+
# result = classifier(cleaned)[0]
|
| 78 |
+
# label = result['label']
|
| 79 |
+
# try:
|
| 80 |
+
# index = int(label.split('_')[-1])
|
| 81 |
+
# sentiment = labels[index]
|
| 82 |
+
# except (ValueError, IndexError):
|
| 83 |
+
# sentiment = label.lower() if label.lower() in labels else "neutral"
|
| 84 |
+
|
| 85 |
+
# # Display sentiment with emoji using radium label style
|
| 86 |
+
# sentiment_display = f'{sentiment.capitalize()} {emojis.get(sentiment, "")}'
|
| 87 |
+
# st.markdown(f'<div class="radium-label">{sentiment_display}</div>', unsafe_allow_html=True)
|
| 88 |
import streamlit as st
|
| 89 |
from transformers import pipeline
|
| 90 |
import re
|
|
|
|
| 96 |
labels = ["neutral", "positive", "negative"]
|
| 97 |
emojis = {"positive": "🤗", "negative": "😔", "neutral": "😐"}
|
| 98 |
|
| 99 |
+
# UI Styling
|
| 100 |
st.markdown("""
|
| 101 |
<style>
|
| 102 |
.stApp {
|
|
|
|
| 105 |
background-position: center;
|
| 106 |
background-repeat: no-repeat;
|
| 107 |
background-attachment: fixed;
|
| 108 |
+
font-family: 'Segoe UI', sans-serif;
|
| 109 |
}
|
| 110 |
+
|
| 111 |
.radium-title {
|
| 112 |
font-size: 40px;
|
| 113 |
text-align: center;
|
|
|
|
| 116 |
border-radius: 10px;
|
| 117 |
background: linear-gradient(90deg, #8E2DE2, #4A00E0);
|
| 118 |
box-shadow: 0 0 20px #8E2DE2, 0 0 30px #4A00E0;
|
| 119 |
+
margin-bottom: 30px;
|
| 120 |
}
|
| 121 |
+
|
| 122 |
.radium-label {
|
| 123 |
font-size: 28px;
|
| 124 |
font-weight: bold;
|
|
|
|
| 129 |
display: inline-block;
|
| 130 |
margin-top: 20px;
|
| 131 |
}
|
| 132 |
+
|
| 133 |
+
.radium-button > button {
|
| 134 |
+
font-size: 20px !important;
|
| 135 |
+
font-weight: bold !important;
|
| 136 |
+
color: white !important;
|
| 137 |
+
border: none !important;
|
| 138 |
+
padding: 12px 28px !important;
|
| 139 |
+
border-radius: 12px !important;
|
| 140 |
+
background: linear-gradient(90deg, #8E2DE2, #4A00E0) !important;
|
| 141 |
+
box-shadow: 0 0 10px #8E2DE2, 0 0 20px #4A00E0;
|
| 142 |
+
transition: all 0.3s ease-in-out;
|
| 143 |
+
}
|
| 144 |
+
|
| 145 |
+
.radium-button > button:hover {
|
| 146 |
+
box-shadow: 0 0 20px #fff, 0 0 30px #8E2DE2;
|
| 147 |
+
transform: scale(1.05);
|
| 148 |
+
}
|
| 149 |
+
|
| 150 |
+
textarea {
|
| 151 |
+
font-size: 20px !important;
|
| 152 |
+
line-height: 1.5 !important;
|
| 153 |
+
padding: 10px !important;
|
| 154 |
+
}
|
| 155 |
+
|
| 156 |
+
.example-box {
|
| 157 |
+
background-color: rgba(255, 255, 255, 0.1);
|
| 158 |
+
color: white;
|
| 159 |
+
padding: 10px 15px;
|
| 160 |
+
border-radius: 10px;
|
| 161 |
+
font-size: 18px;
|
| 162 |
+
margin-bottom: 10px;
|
| 163 |
+
}
|
| 164 |
</style>
|
| 165 |
""", unsafe_allow_html=True)
|
| 166 |
|
| 167 |
+
# Title
|
| 168 |
+
st.markdown('<div class="radium-title">Telugu Sentiment Analysis</div>', unsafe_allow_html=True)
|
| 169 |
+
|
| 170 |
+
# Functions
|
| 171 |
def is_mostly_telugu(text):
|
| 172 |
if not text.strip():
|
| 173 |
return False
|
|
|
|
| 180 |
valid_chars = telugu_chars + allowed_chars == total_chars
|
| 181 |
return telugu_ratio >= 0.7 and valid_chars
|
| 182 |
|
|
|
|
| 183 |
def clean_input(text):
|
| 184 |
cleaned_text = re.sub(r'[^a-zA-Z0-9\u0C00-\u0C7F\s?.!]', ' ', text)
|
| 185 |
cleaned_text = re.sub(r'([?.!])(?![?.!]\s|$)', '', cleaned_text)
|
| 186 |
return ' '.join(cleaned_text.split())
|
| 187 |
|
| 188 |
+
# Show examples
|
| 189 |
+
st.markdown("### 📝 You can only enter pure Telugu text. Try one of the examples below if you'd like:")
|
| 190 |
+
st.markdown('<div class="example-box">ఈ సినిమాను చూసిన తర్వాత నా హృదయం తేలిపోయింది, ఇది అద్భుతమైన అనుభవం!</div>', unsafe_allow_html=True)
|
| 191 |
+
st.markdown('<div class="example-box">ఈ రోజు నేను చాలా నిరాశతో ఉన్నాను. ఏది కూడా సరిగ్గా జరగడం లేదు.</div>', unsafe_allow_html=True)
|
| 192 |
|
| 193 |
# Input
|
| 194 |
+
user_input = st.text_area(" ", height=180, key="input_box")
|
| 195 |
+
|
| 196 |
+
# Buttons
|
| 197 |
+
col1, col2 = st.columns(2)
|
| 198 |
+
|
| 199 |
+
with col1:
|
| 200 |
+
if st.markdown('<div class="radium-button">', unsafe_allow_html=True):
|
| 201 |
+
if st.button("🔮 Predict"):
|
| 202 |
+
if not user_input.strip():
|
| 203 |
+
st.warning("Please enter some Telugu text.")
|
| 204 |
+
else:
|
| 205 |
+
cleaned = clean_input(user_input)
|
| 206 |
+
if not is_mostly_telugu(cleaned):
|
| 207 |
+
st.error("Please enter text primarily in Telugu script.")
|
| 208 |
+
else:
|
| 209 |
+
result = classifier(cleaned)[0]
|
| 210 |
+
label = result['label']
|
| 211 |
+
try:
|
| 212 |
+
index = int(label.split('_')[-1])
|
| 213 |
+
sentiment = labels[index]
|
| 214 |
+
except (ValueError, IndexError):
|
| 215 |
+
sentiment = label.lower() if label.lower() in labels else "neutral"
|
| 216 |
+
|
| 217 |
+
sentiment_display = f'{sentiment.capitalize()} {emojis.get(sentiment, "")}'
|
| 218 |
+
st.markdown(f'<div class="radium-label">{sentiment_display}</div>', unsafe_allow_html=True)
|
| 219 |
+
|
| 220 |
+
with col2:
|
| 221 |
+
if st.markdown('<div class="radium-button">', unsafe_allow_html=True):
|
| 222 |
+
if st.button("🧹 Clear"):
|
| 223 |
+
st.experimental_rerun()
|