import streamlit as st from transformers import pipeline import re st.set_page_config(page_title="Hindi Sentiment Analysis", layout="centered") # Custom CSS for styling st.markdown(""" """, unsafe_allow_html=True) # Load model pipe = pipeline("text-classification", model="NeonSamurai/hindi_sentiment_bert_finetuned") names = ["neutral", "positive", "negative"] emojis = {"positive": "🤗", "negative": "😔", "neutral": "😐"} # Utility functions def is_mostly_hindi(text): if not text.strip(): return False devanagari_pattern = r'[\u0900-\u097F]' allowed_pattern = r'[a-zA-Z0-9\s.,!?]' devanagari_chars = len(re.findall(devanagari_pattern, text)) allowed_chars = len(re.findall(allowed_pattern, text)) total_chars = len(text) hindi_proportion = devanagari_chars / total_chars if total_chars > 0 else 0 valid_chars = devanagari_chars + allowed_chars == total_chars return hindi_proportion >= 0.7 and valid_chars def clean_input(text): cleaned_text = re.sub(r'[^a-zA-Z0-9\u0900-\u097F\s?.!]', ' ', text) cleaned_text = re.sub(r'([?.!])(?![?.!]\s|$)', '', cleaned_text) cleaned_text = ' '.join(cleaned_text.split()) return cleaned_text # Title st.markdown("
📊 Hindi Sentiment Analysis
", unsafe_allow_html=True) # Instructions with st.expander("ℹ️ Instructions"): st.markdown(""" - Please enter a sentence or paragraph in **Hindi (Devanagari script)**. - Example: *यह फिल्म बहुत अच्छी थी और अभिनय शानदार था।* - The app will classify the text as **positive**, **negative**, or **neutral**. """) # Text input user_input = st.text_area("✍️ Enter Hindi text below:", height=150, placeholder="Type your Hindi text here...") # Predict button if st.button("🔍 Predict Sentiment"): if not user_input.strip(): st.warning("⚠️ Please enter some text.") else: cleaned_input = clean_input(user_input) if not is_mostly_hindi(cleaned_input): st.error("❌ Input should be primarily in Hindi (Devanagari script).") else: result = int(pipe(cleaned_input)[0]['label'].split("_")[1]) sentiment = names[result] emoji = emojis[sentiment] st.markdown(f"
✅ Sentiment: {sentiment.capitalize()} {emoji}
", unsafe_allow_html=True) st.markdown(f"
Confidence Score: {result['score']:.2f}
", unsafe_allow_html=True) st.markdown(f"
“{user_input}”
", unsafe_allow_html=True) # Background-Image st.markdown( """ """, unsafe_allow_html=True )