Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
import re
|
| 4 |
+
|
| 5 |
+
# Initialize the pipeline
|
| 6 |
+
pipe = pipeline("text-classification", model="trohith89/Hindi_Sentiment_3_class")
|
| 7 |
+
names = ["neutral", "positive", "negative"]
|
| 8 |
+
emojis = {"positive": "🤗", "negative": "😔", "neutral": "😐"}
|
| 9 |
+
|
| 10 |
+
# Function to check if text is mostly Hindi with some English allowed
|
| 11 |
+
def is_mostly_hindi(text):
|
| 12 |
+
if not text.strip():
|
| 13 |
+
return False
|
| 14 |
+
# Devanagari script (Hindi) range: \u0900-\u097F
|
| 15 |
+
devanagari_pattern = r'[\u0900-\u097F]'
|
| 16 |
+
# English letters, numbers, and common punctuation
|
| 17 |
+
allowed_pattern = r'[a-zA-Z0-9\s.,!?]'
|
| 18 |
+
# Find all Devanagari and allowed characters
|
| 19 |
+
devanagari_chars = len(re.findall(devanagari_pattern, text))
|
| 20 |
+
allowed_chars = len(re.findall(allowed_pattern, text))
|
| 21 |
+
total_chars = len(text)
|
| 22 |
+
# Calculate proportion of Devanagari characters
|
| 23 |
+
hindi_proportion = devanagari_chars / total_chars if total_chars > 0 else 0
|
| 24 |
+
# Check if at least 70% of characters are Hindi and all characters are either Hindi or allowed
|
| 25 |
+
valid_chars = devanagari_chars + allowed_chars == total_chars
|
| 26 |
+
return hindi_proportion >= 0.7 and valid_chars
|
| 27 |
+
|
| 28 |
+
# Streamlit app
|
| 29 |
+
st.set_page_config(page_title="Hindi Sentiment Analysis", page_icon="😊")
|
| 30 |
+
|
| 31 |
+
# Bright headline
|
| 32 |
+
st.markdown("<h1 style='text-align: center; color: #FF4B4B; font-size: 48px;'>हिंदी भावना विश्लेषण</h1>", unsafe_allow_html=True)
|
| 33 |
+
|
| 34 |
+
# Instructions
|
| 35 |
+
st.markdown("""
|
| 36 |
+
<div style='text-align: center; font-size: 20px; margin-bottom: 20px;'>
|
| 37 |
+
कृपया नीचे दिए गए टेक्स्ट क्षेत्र में हिंदी में एक वाक्य या पैराग्राफ दर्ज करें। <br>
|
| 38 |
+
आप कुछ अंग्रेजी शब्दों का उपयोग कर सकते हैं, लेकिन टेक्स्ट मुख्य रूप से हिंदी में होना चाहिए। <br>
|
| 39 |
+
उदाहरण: "यह movie बहुत अच्छी थी और acting शानदार थी।" <br>
|
| 40 |
+
'Predict' बटन पर क्लिक करें और हम आपके टेक्स्ट की भावना (सकारात्मक, नकारात्मक, या तटस्थ) का विश्लेषण करेंगे!
|
| 41 |
+
</div>
|
| 42 |
+
""", unsafe_allow_html=True)
|
| 43 |
+
|
| 44 |
+
# Text input area
|
| 45 |
+
user_input = st.text_area("", placeholder="यहाँ हिंदी में अपना टेक्स्ट दर्ज करें...", height=150)
|
| 46 |
+
|
| 47 |
+
# Predict button
|
| 48 |
+
if st.button("Predict"):
|
| 49 |
+
if not user_input.strip():
|
| 50 |
+
st.warning("⚠️ कृपया टेक्स्ट दर्ज करें। खाली इनपुट मान्य नहीं है।")
|
| 51 |
+
elif not is_mostly_hindi(user_input):
|
| 52 |
+
st.error("❌ कृपया मुख्य रूप से हिंदी में टेक्स्ट दर्ज करें (देवनागरी लिपि)। कुछ अंग्रेजी शब्द ठीक हैं, लेकिन अन्य भाषाएँ या अक्षर समर्थित नहीं हैं।")
|
| 53 |
+
else:
|
| 54 |
+
# Predict sentiment
|
| 55 |
+
result = pipe(user_input)[0]
|
| 56 |
+
sentiment_index = int(result['label'].split("_")[1])
|
| 57 |
+
sentiment = names[sentiment_index]
|
| 58 |
+
emoji = emojis[sentiment]
|
| 59 |
+
|
| 60 |
+
# Display result
|
| 61 |
+
st.success(f"**भावना**: {sentiment.capitalize()} {emoji}")
|
| 62 |
+
st.write(f"**आपका टेक्स्ट**: {user_input}")
|
| 63 |
+
st.write(f"**विश्वास स्कोर**: {result['score']:.2f}")
|