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 = ["negative","neutral", "positive"] emojis = {"positive": "🤗", "negative": "😔", "neutral": "😐"} # UI Styling st.markdown(""" """, unsafe_allow_html=True) # Title st.markdown('
Telugu Sentiment Analysis
', 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('
ఆమెతో మాట్లాడిన తర్వాత నా మనసు తేలికపడింది.
', unsafe_allow_html=True) st.markdown('
ఈ రోజు నేను చాలా నిరాశతో ఉన్నాను. ఏది కూడా సరిగ్గా జరగడం లేదు.
', 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('
', 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'
{sentiment_display}
', unsafe_allow_html=True)