NeonSamurai commited on
Commit
8103fc7
·
verified ·
1 Parent(s): 782e6ad

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +127 -0
app.py CHANGED
@@ -0,0 +1,127 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from transformers import pipeline
3
+ import re
4
+
5
+ st.set_page_config(page_title="Hindi Sentiment Analysis", layout="centered")
6
+
7
+ # Custom CSS for styling
8
+ st.markdown("""
9
+ <style>
10
+ @import url('https://fonts.googleapis.com/css2?family=Poppins&display=swap');
11
+
12
+ html, body, [class*="css"] {
13
+ font-family: 'Poppins', sans-serif;
14
+ text-align: center;
15
+ }
16
+
17
+ .centered-title {
18
+ font-size: 50px;
19
+ font-weight: bold;
20
+ margin-bottom: 10px;
21
+ }
22
+
23
+ .sentiment-result {
24
+ font-size: 26px;
25
+ font-weight: 600;
26
+ margin-top: 20px;
27
+ }
28
+
29
+ .confidence {
30
+ font-size: 20px;
31
+ margin-top: 10px;
32
+ }
33
+
34
+ .original-text {
35
+ font-style: italic;
36
+ margin-top: 10px;
37
+ }
38
+
39
+ textarea {
40
+ text-align: left !important;
41
+ }
42
+ </style>
43
+ """, unsafe_allow_html=True)
44
+
45
+ # Load model
46
+ pipe = pipeline("text-classification", model="NeonSamurai/hindi_bert_model")
47
+
48
+ names = ["neutral", "positive", "negative"]
49
+ emojis = {"positive": "🤗", "negative": "😔", "neutral": "😐"}
50
+
51
+ # Utility functions
52
+ def is_mostly_hindi(text):
53
+ if not text.strip():
54
+ return False
55
+ devanagari_pattern = r'[\u0900-\u097F]'
56
+ allowed_pattern = r'[a-zA-Z0-9\s.,!?]'
57
+ devanagari_chars = len(re.findall(devanagari_pattern, text))
58
+ allowed_chars = len(re.findall(allowed_pattern, text))
59
+ total_chars = len(text)
60
+ hindi_proportion = devanagari_chars / total_chars if total_chars > 0 else 0
61
+ valid_chars = devanagari_chars + allowed_chars == total_chars
62
+ return hindi_proportion >= 0.7 and valid_chars
63
+
64
+ def clean_input(text):
65
+ cleaned_text = re.sub(r'[^a-zA-Z0-9\u0900-\u097F\s?.!]', ' ', text)
66
+ cleaned_text = re.sub(r'([?.!])(?![?.!]\s|$)', '', cleaned_text)
67
+ cleaned_text = ' '.join(cleaned_text.split())
68
+ return cleaned_text
69
+
70
+ # Title
71
+ st.markdown("<div class='centered-title'>📊 Hindi Sentiment Analysis</div>", unsafe_allow_html=True)
72
+
73
+ # Instructions
74
+ with st.expander("ℹ️ Instructions"):
75
+ st.markdown("""
76
+ - Please enter a sentence or paragraph in **Hindi (Devanagari script)**.
77
+ - Example: *यह फिल्म बहुत अच्छी थी और अभिनय शानदार था।*
78
+ - The app will classify the text as **positive**, **negative**, or **neutral**.
79
+ """)
80
+
81
+ # Text input
82
+ user_input = st.text_area("✍️ Enter Hindi text below:", height=150, placeholder="Type your Hindi text here...")
83
+
84
+ # Predict button
85
+ if st.button("🔍 Predict Sentiment"):
86
+ if not user_input.strip():
87
+ st.warning("⚠️ Please enter some text.")
88
+ else:
89
+ cleaned_input = clean_input(user_input)
90
+ if not is_mostly_hindi(cleaned_input):
91
+ st.error("❌ Input should be primarily in Hindi (Devanagari script).")
92
+ else:
93
+ result = pipe(cleaned_input)[0]
94
+ sentiment_index = int(result['label'].split("_")[1])
95
+ sentiment = names[sentiment_index]
96
+ emoji = emojis[sentiment]
97
+
98
+ st.markdown(f"<div class='sentiment-result'>✅ Sentiment: <b>{sentiment.capitalize()} {emoji}</b></div>", unsafe_allow_html=True)
99
+ st.markdown(f"<div class='confidence'>Confidence Score: <code>{result['score']:.2f}</code></div>", unsafe_allow_html=True)
100
+ st.markdown(f"<div class='original-text'>“{user_input}”</div>", unsafe_allow_html=True)
101
+
102
+ # Background-Image
103
+ st.markdown(
104
+ """
105
+ <style>
106
+ .stApp {
107
+ background-image: url("https://cdn-uploads.huggingface.co/production/uploads/673f5e166c2774fcc8a82f0b/OIaLFHTZGXeTvsizCCNsK.png");
108
+ background-size: cover;
109
+ background-position: center;
110
+ height: 100vh;
111
+ }
112
+
113
+ /* Semi-transparent overlay */
114
+ .stApp::before {
115
+ content: "";
116
+ position: absolute;
117
+ top: 0;
118
+ left: 0;
119
+ width: 100%;
120
+ height: 100%;
121
+ background: rgba(0, 0, 0, 0.4); /* 40% transparency */
122
+ z-index: -1;
123
+ }
124
+ </style>
125
+ """,
126
+ unsafe_allow_html=True
127
+ )