Adityaganesh's picture
Update app.py
95af976 verified
raw
history blame
8.12 kB
# import streamlit as st
# from transformers import pipeline
# import re
# # Load the model
# classifier = pipeline("text-classification", model="Mpavan45/Telugu_Sentimental_Analysis")
# # Label mapping and emojis
# labels = ["neutral", "positive", "negative"]
# emojis = {"positive": "🤗", "negative": "😔", "neutral": "😐"}
# # Background and Title Styling
# st.markdown("""
# <style>
# .stApp {
# background-image: url('https://cdn-uploads.huggingface.co/production/uploads/675fab3a2d0851e23d23cad3/_YKXYHCbjM44ubGwnAKeQ.jpeg');
# background-size: cover;
# background-position: center;
# background-repeat: no-repeat;
# background-attachment: fixed;
# }
# .radium-title {
# font-size: 40px;
# text-align: center;
# color: #fff;
# padding: 10px;
# border-radius: 10px;
# background: linear-gradient(90deg, #8E2DE2, #4A00E0);
# box-shadow: 0 0 20px #8E2DE2, 0 0 30px #4A00E0;
# }
# .radium-label {
# font-size: 28px;
# font-weight: bold;
# color: white;
# padding: 10px 20px;
# border-radius: 12px;
# background: linear-gradient(90deg, #7F00FF, #E100FF);
# display: inline-block;
# margin-top: 20px;
# }
# </style>
# """, unsafe_allow_html=True)
# # Check Telugu text validity
# def is_mostly_telugu(text):
# if not text.strip():
# return False
# telugu_pattern = r'[\u0C00-\u0C7F]'
# allowed_pattern = r'[a-zA-Z0-9\s.,!?]'
# telugu_chars = len(re.findall(telugu_pattern, text))
# allowed_chars = len(re.findall(allowed_pattern, text))
# total_chars = len(text)
# telugu_ratio = telugu_chars / total_chars if total_chars > 0 else 0
# valid_chars = telugu_chars + allowed_chars == total_chars
# return telugu_ratio >= 0.7 and valid_chars
# # Clean text
# def clean_input(text):
# cleaned_text = re.sub(r'[^a-zA-Z0-9\u0C00-\u0C7F\s?.!]', ' ', text)
# cleaned_text = re.sub(r'([?.!])(?![?.!]\s|$)', '', cleaned_text)
# return ' '.join(cleaned_text.split())
# # Title
# st.markdown('<div class="radium-title">Telugu Sentiment Analysis</div>', unsafe_allow_html=True)
# # Input
# user_input = st.text_area("Enter your Telugu text:")
# if st.button("Predict"):
# if not user_input.strip():
# st.warning("Please enter some Telugu text.")
# else:
# cleaned = clean_input(user_input)
# if not is_mostly_telugu(cleaned):
# st.error("Please enter text primarily in Telugu script.")
# else:
# result = classifier(cleaned)[0]
# label = result['label']
# try:
# index = int(label.split('_')[-1])
# sentiment = labels[index]
# except (ValueError, IndexError):
# sentiment = label.lower() if label.lower() in labels else "neutral"
# # Display sentiment with emoji using radium label style
# sentiment_display = f'{sentiment.capitalize()} {emojis.get(sentiment, "")}'
# st.markdown(f'<div class="radium-label">{sentiment_display}</div>', unsafe_allow_html=True)
import streamlit as st
from transformers import pipeline
import re
# Load the model
classifier = pipeline("text-classification", model="Mpavan45/Telugu_Sentimental_Analysis")
# Label mapping and emojis
labels = ["neutral", "positive", "negative"]
emojis = {"positive": "🤗", "negative": "😔", "neutral": "😐"}
# UI Styling
st.markdown("""
<style>
.stApp {
background-image: url('https://cdn-uploads.huggingface.co/production/uploads/675fab3a2d0851e23d23cad3/_YKXYHCbjM44ubGwnAKeQ.jpeg');
background-size: cover;
background-position: center;
background-repeat: no-repeat;
background-attachment: fixed;
font-family: 'Segoe UI', sans-serif;
}
.radium-title {
font-size: 30px;
text-align: center;
color: #fff;
padding: 10px;
border-radius: 10px;
background: linear-gradient(90deg, #8E2DE2, #4A00E0);
box-shadow: 0 0 20px #8E2DE2, 0 0 30px #4A00E0;
margin-bottom: 20px;
}
.radium-label {
font-size: 28px;
font-weight: bold;
color: white;
padding: 10px 20px;
border-radius: 12px;
background: linear-gradient(90deg, #7F00FF, #E100FF);
display: inline-block;
margin-top: 20px;
}
.radium-button > button {
font-size: 20px !important;
font-weight: bold !important;
color: white !important;
border: none !important;
padding: 12px 28px !important;
border-radius: 12px !important;
background: linear-gradient(90deg, #8E2DE2, #4A00E0) !important;
box-shadow: 0 0 10px #8E2DE2, 0 0 20px #4A00E0;
transition: all 0.3s ease-in-out;
}
.radium-button > button:hover {
box-shadow: 0 0 20px #fff, 0 0 30px #8E2DE2;
transform: scale(1.05);
}
textarea {
font-size: 20px !important;
line-height: 1.5 !important;
padding: 10px !important;
}
.example-box {
background-color: rgba(255, 255, 255, 0.1);
color: white;
padding: 10px 15px;
border-radius: 10px;
font-size: 15px;
margin-bottom: 10px;
}
</style>
""", unsafe_allow_html=True)
# Title
st.markdown('<div class="radium-title">Telugu Sentiment Analysis</div>', unsafe_allow_html=True)
# Functions
def is_mostly_telugu(text):
if not text.strip():
return False
telugu_pattern = r'[\u0C00-\u0C7F]'
allowed_pattern = r'[a-zA-Z0-9\s.,!?]'
telugu_chars = len(re.findall(telugu_pattern, text))
allowed_chars = len(re.findall(allowed_pattern, text))
total_chars = len(text)
telugu_ratio = telugu_chars / total_chars if total_chars > 0 else 0
valid_chars = telugu_chars + allowed_chars == total_chars
return telugu_ratio >= 0.7 and valid_chars
def clean_input(text):
cleaned_text = re.sub(r'[^a-zA-Z0-9\u0C00-\u0C7F\s?.!]', ' ', text)
cleaned_text = re.sub(r'([?.!])(?![?.!]\s|$)', '', cleaned_text)
return ' '.join(cleaned_text.split())
# Show examples
st.markdown("### 📝 You can only enter pure Telugu text. Try one of the examples below if you'd like:")
st.markdown('<div class="example-box">ఆమెతో మాట్లాడిన తర్వాత నా మనసు తేలికపడింది.</div>', unsafe_allow_html=True)
st.markdown('<div class="example-box">ఈ రోజు నేను చాలా నిరాశతో ఉన్నాను. ఏది కూడా సరిగ్గా జరగడం లేదు.</div>', unsafe_allow_html=True)
# Input
user_input = st.text_area(" ", height=180, key="input_box")
# Buttons
col1, col2 = st.columns(2)
with col1:
if st.markdown('<div class="radium-button">', unsafe_allow_html=True):
if st.button("🔮 Predict"):
if not user_input.strip():
st.warning("Please enter some Telugu text.")
else:
cleaned = clean_input(user_input)
if not is_mostly_telugu(cleaned):
st.error("Please enter text primarily in Telugu script.")
else:
result = classifier(cleaned)[0]
label = result['label']
try:
index = int(label.split('_')[-1])
sentiment = labels[index]
except (ValueError, IndexError):
sentiment = label.lower() if label.lower() in labels else "neutral"
sentiment_display = f'{sentiment.capitalize()} {emojis.get(sentiment, "")}'
st.markdown(f'<div class="radium-label">{sentiment_display}</div>', unsafe_allow_html=True)
# Clear button with session state reset
with col2:
if st.markdown('<div class="radium-button">', unsafe_allow_html=True):
if st.button("🧹 Clear"):
st.session_state.input_box = ""