File size: 5,824 Bytes
9ba1b4f 2b738ab 9ba1b4f 0074636 9ba1b4f 0074636 9ba1b4f 0074636 9ba1b4f 7636966 f11e840 7636966 2a92633 7636966 f3ad527 f0676f9 1c728de c6488cd 2a92633 1c728de 7392b8b d884274 f0676f9 d884274 ebfd9a5 d884274 ebfd9a5 d884274 f0676f9 d884274 2a92633 f0676f9 f3ad527 f0676f9 ebfd9a5 ea429ba 2a92633 ebfd9a5 2a92633 f11e840 2a92633 f0676f9 d884274 1c728de 203486a 1c728de e22d9ce f3ad527 1c728de 7392b8b f0676f9 1c728de f3ad527 2a92633 f0676f9 f3ad527 f0676f9 ebfd9a5 f0676f9 ebfd9a5 f0676f9 ebfd9a5 7392b8b ebfd9a5 2a92633 f0676f9 f3ad527 2a92633 f0676f9 203486a f0676f9 e22d9ce 2a92633 f3ad527 f11e840 c9d1a04 f3ad527 27eb025 2a92633 e22d9ce 1c728de 2a92633 f3ad527 2a92633 f0676f9 203486a ebfd9a5 f3ad527 2a92633 f3ad527 2a92633 f11e840 27eb025 2a92633 f0676f9 f3ad527 f0676f9 9ba1b4f f0676f9 beecf22 512c59f 2a92633 9ba1b4f f3ad527 9ba1b4f f0676f9 9ba1b4f 7636966 f11e840 beecf22 f11e840 c6488cd 9418607 203486a cfa6359 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 | import streamlit as st
from transformers import pipeline
import re
st.set_page_config(page_title="Hindi Sentiment Analysis", page_icon="😊")
pipe = pipeline("text-classification", model="trohith89/Hindi_Sentiment_3_class")
names = ["neutral", "positive", "negative"]
emojis = {"positive": "🤗", "negative": "😔", "neutral": "😐"}
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
# --- Custom CSS Styling ---
st.markdown("""
<style>
html, body, .stApp {
background-image: url("https://cdn-uploads.huggingface.co/production/uploads/67441c51a784a9d15cb12871/8cwxuMFDAzxohCfoqCxnE.jpeg");
background-size: cover;
background-position: center;
background-repeat: no-repeat;
background-attachment: fixed;
font-family: 'Segoe UI', sans-serif;
}
/* 🌈 Gradient & Animated Title */
h1 {
text-align: center;
font-size: 56px;
background: linear-gradient(to right, #ffb6c1, #ffa07a, #87cefa);
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
animation: popFadeSlide 2s ease-in-out;
margin-bottom: 30px;
letter-spacing: 1px;
}
@keyframes popFadeSlide {
0% {
opacity: 0;
transform: scale(0.9) translateY(-40px);
}
50% {
opacity: 1;
transform: scale(1.05) translateY(5px);
}
100% {
transform: scale(1) translateY(0);
}
}
/* 🧾 Instruction Box */
.instructions {
text-align: center;
font-size: 18px;
color: #fff;
background: rgba(0, 0, 0, 0.7);
padding: 20px;
border-radius: 15px;
animation: rotateIn 1.5s ease-out;
margin-bottom: 25px;
max-width: 600px;
margin-left: auto;
margin-right: auto;
}
@keyframes rotateIn {
0% { opacity: 0; transform: rotateY(-90deg); }
100% { opacity: 1; transform: rotateY(0); }
}
/* 📥 Text Area */
.stTextArea textarea {
border: none;
outline: none;
border-radius: 12px;
padding: 15px;
background: rgba(42, 42, 42, 0.85);
color: #e6e6e6;
font-size: 16px;
width: 100%;
max-width: 800px;
height: 160px !important;
box-sizing: border-box;
transition: all 0.4s ease;
}
.stTextArea textarea:focus {
transform: scale(1.02);
box-shadow: 0 0 15px rgba(255, 255, 255, 0.4);
}
/* 🔘 Button */
div.stButton > button {
background: linear-gradient(45deg, #004d00, #000000, #000066);
color: #e6e6e6;
font-size: 18px;
padding: 12px 30px;
border: none;
border-radius: 30px;
display: block;
margin: 20px auto;
transition: all 0.5s ease;
}
div.stButton > button:hover {
transform: translateY(-5px) rotate(1deg);
box-shadow: 0 8px 20px rgba(255, 255, 255, 0.4);
background: linear-gradient(45deg, #006600, #333333, #000099);
}
/* 🎯 Colored Sentiment Output Box */
.output-container {
padding: 25px 20px;
border-radius: 20px;
animation: fadeInUp 1s ease-out;
text-align: center;
width: 100%;
max-width: 650px;
margin: 30px auto 10px auto;
min-height: 100px;
font-size: 45px;
font-weight: 700;
}
@keyframes fadeInUp {
0% { opacity: 0; transform: translateY(40px); }
100% { opacity: 1; transform: translateY(0); }
}
.output-positive {
background: linear-gradient(45deg, #00ff00, #00cccc);
color: #1a1a1a;
}
.output-negative {
background: linear-gradient(45deg, #330066, #666666);
color: #e6e6e6;
}
.output-neutral {
background: linear-gradient(45deg, #66ccff, #ffffff);
color: #1a1a1a;
}
/* 📊 Output Info */
.output-info {
color: #f1f1f1;
font-size: 16px;
text-align: center;
margin-top: 10px;
}
</style>
""", unsafe_allow_html=True)
# --- App Content ---
st.markdown("<h1>Hindi Sentiment Analysis</h1>", unsafe_allow_html=True)
st.markdown("""
<div class='instructions'>
Please enter a sentence or paragraph in Hindi in the text area below. <br>
Text must be primarily in Hindi. <br>
Example: "यह फिल्म बहुत अच्छी थी और अभिनय शानदार था। <br>
Click the 'Predict' button to analyze the sentiment (positive, negative, or neutral)!
</div>
""", unsafe_allow_html=True)
user_input = st.text_area("", placeholder="Enter your text in Hindi here...")
if st.button("Predict"):
if not user_input.strip():
st.warning("⚠️ Please enter text. Empty input is not allowed.")
else:
cleaned_input = clean_input(user_input)
if not is_mostly_hindi(cleaned_input):
st.error("❌ Please enter text primarily in Hindi (Devanagari script).")
else:
result = pipe(cleaned_input)[0]
sentiment_index = int(result['label'].split("_")[1])
sentiment = names[sentiment_index]
emoji = emojis[sentiment]
output_class = f"output-{sentiment}"
st.markdown(f"<div class='output-container {output_class}'>{sentiment.capitalize()} {emoji}</div>", unsafe_allow_html=True)
st.markdown(f"<div class='output-info'><strong>Your Text:</strong> {user_input}<br><strong>Confidence Score:</strong> {result['score']:.2f}</div>", unsafe_allow_html=True) |