File size: 4,716 Bytes
78598a2
0a14fb8
29196f6
10301de
bc07ac8
 
29196f6
7a9eec0
 
29196f6
e107e3a
fd39aa3
2352d40
 
 
7e255e6
2352d40
 
 
 
fd39aa3
2352d40
fd39aa3
981c36b
71c3abf
981c36b
 
 
 
88e008c
 
71c3abf
981c36b
fd39aa3
7ae290e
88e008c
981c36b
 
88e008c
 
 
7ae290e
88e008c
981c36b
fd39aa3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
71c3abf
fd39aa3
 
981c36b
 
 
fd39aa3
 
 
 
29196f6
 
 
 
 
 
 
 
 
 
 
a893999
7a9eec0
 
 
 
234dd82
fd39aa3
 
de65d4b
fd39aa3
a893999
88e008c
fd39aa3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a8d725d
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
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("""
    <style>
    .stApp {
        background-image: url('https://cdn-uploads.huggingface.co/production/uploads/675fab3a2d0851e23d23cad3/_YKXYHCbjM44ubGwnAKeQ.jpeg');
        background-size: cover;
        background-position: center;
        background-repeat: no-repeat;
        background-attachment: fixed;
        font-family: 'Segoe UI', sans-serif;
    }

    .radium-title {
        font-size: 30px;
        text-align: center;
        color: #fff;
        padding: 10px;
        border-radius: 10px;
        background: linear-gradient(90deg, #8E2DE2, #4A00E0);
        box-shadow: 0 0 20px #8E2DE2, 0 0 30px #4A00E0;
        margin-bottom: 20px;
    }

    .radium-label {
        font-size: 28px;
        font-weight: bold;
        color: white;
        padding: 10px 20px;
        border-radius: 12px;
        background: linear-gradient(90deg, #7F00FF, #E100FF);
        display: inline-block;
        margin-top: 20px;
    }

    .radium-button > button {
        font-size: 20px !important;
        font-weight: bold !important;
        color: white !important;
        border: none !important;
        padding: 12px 28px !important;
        border-radius: 12px !important;
        background: linear-gradient(90deg, #8E2DE2, #4A00E0) !important;
        box-shadow: 0 0 10px #8E2DE2, 0 0 20px #4A00E0;
        transition: all 0.3s ease-in-out;
    }

    .radium-button > button:hover {
        box-shadow: 0 0 20px #fff, 0 0 30px #8E2DE2;
        transform: scale(1.05);
    }

    textarea {
        font-size: 20px !important;
        line-height: 1.5 !important;
        padding: 10px !important;
    }

    .example-box {
        background-color: rgba(255, 255, 255, 0.1);
        color: white;
        padding: 10px 15px;
        border-radius: 10px;
        font-size: 15px;
        margin-bottom: 10px;
    }
    </style>
""", unsafe_allow_html=True)

# Title
st.markdown('<div class="radium-title">Telugu Sentiment Analysis</div>', 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('<div class="example-box">ఆమెతో మాట్లాడిన తర్వాత నా మనసు తేలికపడింది.</div>', unsafe_allow_html=True)
st.markdown('<div class="example-box">ఈ రోజు నేను చాలా నిరాశతో ఉన్నాను. ఏది కూడా సరిగ్గా జరగడం లేదు.</div>', 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('<div class="radium-button">', 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'<div class="radium-label">{sentiment_display}</div>', unsafe_allow_html=True)