Gowthamvemula's picture
Update app.py
a0fda38 verified
import streamlit as st
from transformers import pipeline
import re
# Set page configuration
st.set_page_config(page_title="Telugu Sentiment Analysis", page_icon="📝", layout="centered")
# Initialize the pipeline
pipe = pipeline("text-classification", model="Gowthamvemula/Teugu_Sentimental_fine-tuning")
# Sentiment mapping and emojis
label_map = {'LABEL_0': 'Neutral', 'LABEL_1': 'Positive', 'LABEL_2': 'Negative'}
emojis = {'Positive': '😊', 'Negative': '😞', 'Neutral': '😐'}
# --- CSS Styling ---
st.markdown("""
<style>
body, .stApp {
background-image: url("https://cdn-uploads.huggingface.co/production/uploads/67445925102349e867c92342/i_3xTHfWB672yRzdPDZ6L.jpeg");
transition: background 0.6s ease;
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
}
body.default-bg, .stApp.default-bg {
background: radial-gradient(circle at top left, #e0f7fa, #fffde7);
}
body.positive-bg, .stApp.positive-bg {
background: radial-gradient(circle at top left, #d4fc79, #96e6a1);
}
body.negative-bg, .stApp.negative-bg {
background: radial-gradient(circle at top right, #ff9a9e, #fad0c4);
}
body.neutral-bg, .stApp.neutral-bg {
background: radial-gradient(circle at bottom left, #a18cd1, #fbc2eb);
}
h1 {
text-align: center;
font-size: 5rem;
margin-top: 25px;
background: linear-gradient(60deg, #FFFFCC, #FFFFCC);
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
animation: fadeInSlide 1.5s ease-out;
letter-spacing: 1px;
}
@keyframes fadeInSlide {
0% { opacity: 0; transform: translateY(-30px); }
100% { opacity: 1; transform: translateY(0); }
}
.container-box {
background: rgba(255, 255, 255, 0.7);
border-radius: 30px;
padding: 35px;
box-shadow: 0 12px 40px rgba(0,0,0,0.12);
backdrop-filter: blur(16px);
max-width: 750px;
margin: auto;
margin-top: 25px;
animation: popIn 1s ease-out;
}
@keyframes popIn {
0% { opacity: 0; transform: scale(0.93); }
100% { opacity: 1; transform: scale(1); }
}
textarea {
font-size: 16px !important;
padding: 15px !important;
background: rgba(30, 30, 30, 0.9);
border-radius: 12px !important;
border: 2px solid #6c5ce7 !important;
color: #f0f0f0 !important;
transition: all 0.3s ease-in-out;
}
textarea:focus {
border-color: #00b894 !important;
transform: scale(1.02);
box-shadow: 0 0 12px rgba(0, 184, 148, 0.4);
}
.stButton > button {
background: linear-gradient(45deg, #6a89cc, #38ada9);
color: white;
padding: 0.7em 1.7em;
font-size: 1.15rem;
border-radius: 35px;
border: none;
transition: transform 0.3s ease, box-shadow 0.3s ease;
margin-top: 18px;
display: block;
margin-left: auto;
margin-right: auto;
}
.stButton > button:hover {
transform: scale(1.06);
box-shadow: 0 8px 20px rgba(0, 0, 0, 0.2);
background: linear-gradient(45deg, #38ada9, #6a89cc);
}
.output-box {
margin-top: 30px;
text-align: center;
padding: 25px;
border-radius: 20px;
font-size: 1.9rem;
font-weight: bold;
animation: fadeInUp 0.8s ease-out;
backdrop-filter: blur(8px);
}
.output-box.Positive {
background: linear-gradient(145deg, #d4fc79, #96e6a1);
color: #222;
}
.output-box.Negative {
background: linear-gradient(145deg, #ff9a9e, #fad0c4);
color: #2c2c2c;
}
.output-box.Neutral {
background: linear-gradient(145deg, #a18cd1, #fbc2eb);
color: #2f3542;
}
@keyframes fadeInUp {
0% { opacity: 0; transform: translateY(30px); }
100% { opacity: 1; transform: translateY(0); }
}
.score-info {
text-align: center;
font-size: 2.1rem;
margin-top: 12px;
color: #CCF2FF;
animation: fadeIn 1s ease-out;
}
.instructions {
text-align: center;
font-size: 4.1rem;
margin-bottom: 20px;
color: #FFFFCC;
animation: fadeIn 1.2s ease-out;
}
@keyframes fadeIn {
0% { opacity: 0; }
100% { opacity: 1; }
}
.block-container {
padding-top: 2rem;
}
@media (max-width: 640px) {
h1 { font-size: 2.2rem; }
.container-box { padding: 25px; max-width: 95%; }
.output-box { font-size: 1.5rem; }
.stButton > button { font-size: 1rem; }
}
</style>
""", unsafe_allow_html=True)
# --- Header ---
st.markdown("<h1>Telugu Sentiment Analysis</h1>", unsafe_allow_html=True)
# --- Main Container ---
with st.container():
st.markdown("<div class='container-box'>", unsafe_allow_html=True)
st.markdown("""
<p class='instructions'>
Enter a Telugu sentence below and click 'Predict' to analyze its sentiment.<br>
Example: ఈ సినిమా చాలా బాగుంది!
</p>
</div>
""", unsafe_allow_html=True)
user_input = st.text_area("Your Telugu Text", height=200, placeholder="ఉదాహరణ: ఈ సినిమా చాలా బాగుంది!")
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())
def is_mostly_telugu(text):
if not text.strip():
return False
telugu_chars = len(re.findall(r'[\u0C00-\u0C7F]', text))
allowed_chars = len(re.findall(r'[a-zA-Z0-9\s.,!?]', text))
total_chars = len(text)
return (telugu_chars / total_chars >= 0.7) and (telugu_chars + allowed_chars == total_chars)
st.markdown("<script>document.body.className = 'default-bg'; document.querySelector('.stApp').className = 'stApp default-bg';</script>", unsafe_allow_html=True)
if st.button("Predict"):
if not user_input.strip():
st.warning("⚠️ Please enter some text.")
else:
cleaned = clean_input(user_input)
if not is_mostly_telugu(cleaned):
st.error("❌ Text should be primarily in Telugu script.")
else:
result = pipe(cleaned)[0]
idx = int(result['label'].split('_')[1])
if idx == 0:
sentiment = "Neutral"
elif idx == 1:
sentiment = "Positive"
else:
sentiment = "Negative"
emoji = emojis[sentiment]
st.markdown(f"<script>document.body.className = '{sentiment.lower()}-bg'; document.querySelector('.stApp').className = 'stApp {sentiment.lower()}-bg';</script>", unsafe_allow_html=True)
st.markdown(f"<div class='output-box {sentiment}'>{sentiment} {emoji}</div>", unsafe_allow_html=True)
st.markdown(f"<div class='score-info'>Confidence: {result['score']:.2f} | Text: {user_input}</div>", unsafe_allow_html=True)
st.markdown("</div>", unsafe_allow_html=True)
# # import streamlit as st
# # from transformers import pipeline
# # import re
# # # Set page configuration
# # st.set_page_config(page_title="Telugu Sentiment Analysis", page_icon="📝", layout="centered")
# # # Initialize the pipeline
# # pipe = pipeline("text-classification", model="Gowthamvemula/Teugu_Sentimental_fine-tuning")
# # # Sentiment mapping and emojis
# # label_map = {'LABEL_0': 'Neutral', 'LABEL_1': 'Positive', 'LABEL_2': 'Negative'}
# # emojis = {'Positive': '😊', 'Negative': '😞', 'Neutral': '😐'}
# # # --- CSS Styling ---
# # st.markdown("""
# # <style>
# # body, .stApp {
# # transition: background 0.5s ease;
# # }
# # /* Default background */
# # body.default-bg, .stApp.default-bg {
# # background: linear-gradient(135deg, #d9e4f5, #f5e0dc);
# # }
# # /* Positive background */
# # body.positive-bg, .stApp.positive-bg {
# # background: linear-gradient(135deg, #b8e994, #78e08f);
# # }
# # /* Negative background */
# # body.negative-bg, .stApp.negative-bg {
# # background: linear-gradient(135deg, #ff7979, #eb4d4b);
# # }
# # /* Neutral background */
# # body.neutral-bg, .stApp.neutral-bg {
# # background: linear-gradient(135deg, #7ed6df, #4834d4);
# # }
# # h1 {
# # text-align: center;
# # font-size: 3rem;
# # margin-top: 25px;
# # background: linear-gradient(45deg, #ff4757, #1e90ff);
# # -webkit-background-clip: text;
# # -webkit-text-fill-color: transparent;
# # animation: fadeInSlide 1.5s ease-out;
# # }
# # @keyframes fadeInSlide {
# # 0% { opacity: 0; transform: translateY(-20px); }
# # 100% { opacity: 1; transform: translateY(0); }
# # }
# # .container-box {
# # background: rgba(255, 255, 255, 0.85);
# # border-radius: 25px;
# # padding: 30px;
# # box-shadow: 0 10px 30px rgba(0,0,0,0.15);
# # backdrop-filter: blur(12px);
# # margin: auto;
# # max-width: 700px;
# # margin-top: 20px;
# # animation: popIn 1s ease-out;
# # }
# # @keyframes popIn {
# # 0% { opacity: 0; transform: scale(0.95); }
# # 50% { transform: scale(1.02); }
# # 100% { opacity: 1; transform: scale(1); }
# # }
# # textarea {
# # font-size: 16px !important;
# # padding: 15px !important;
# # background: rgba(40, 40, 40, 0.95);
# # border-radius: 12px !important;
# # border: 2px solid #aaa !important;
# # color: #f0f0f0 !important;
# # transition: border-color 0.3s ease, transform 0.3s ease;
# # }
# # textarea:focus {
# # border-color: #1e90ff !important;
# # transform: scale(1.01);
# # box-shadow: 0 0 10px rgba(30, 144, 255, 0.3);
# # }
# # .stButton > button {
# # background: linear-gradient(45deg, #0984e3, #00cec9);
# # color: white;
# # padding: 0.6em 1.5em;
# # font-size: 1.1rem;
# # border-radius: 30px;
# # border: none;
# # transition: transform 0.3s ease, box-shadow 0.3s ease;
# # margin-top: 15px;
# # display: block;
# # margin-left: auto;
# # margin-right: auto;
# # }
# # .stButton > button:hover {
# # transform: translateY(-3px) scale(1.05);
# # box-shadow: 0 5px 15px rgba(0, 0, 0, 0.2);
# # background: linear-gradient(45deg, #00cec9, #0984e3);
# # }
# # .output-box {
# # margin-top: 30px;
# # text-align: center;
# # padding: 25px;
# # border-radius: 15px;
# # font-size: 1.8rem;
# # font-weight: bold;
# # backdrop-filter: blur(6px);
# # animation: fadeInUp 0.8s ease-out;
# # transition: background 0.3s ease;
# # }
# # /* Sentiment-specific output box styles */
# # .output-box.Positive {
# # background: linear-gradient(45deg, #78e08f, #b8e994);
# # color: #1a1a1a;
# # }
# # .output-box.Negative {
# # background: linear-gradient(45deg, #eb4d4b, #ff7979);
# # color: #ffffff;
# # }
# # .output-box.Neutral {
# # background: linear-gradient(45deg, #7ed6df, #4834d4);
# # color: #ffffff;
# # }
# # @keyframes fadeInUp {
# # 0% { opacity: 0; transform: translateY(20px); }
# # 100% { opacity: 1; transform: translateY(0); }
# # }
# # .score-info {
# # text-align: center;
# # font-size: 1rem;
# # margin-top: 10px;
# # color: #333;
# # animation: fadeIn 1s ease-out;
# # }
# # .instructions {
# # text-align: center;
# # font-size: 1.1rem;
# # margin-bottom: 20px;
# # color: #2d3436;
# # animation: fadeIn 1.2s ease-out;
# # }
# # @keyframes fadeIn {
# # 0% { opacity: 0; }
# # 100% { opacity: 1; }
# # }
# # /* Remove default top padding */
# # .block-container {
# # padding-top: 2rem;
# # }
# # /* Responsive design */
# # @media (max-width: 600px) {
# # h1 { font-size: 2.2rem; }
# # .container-box { padding: 20px; max-width: 90%; }
# # .output-box { font-size: 1.5rem; }
# # .stButton > button { font-size: 1rem; }
# # }
# # </style>
# # """, unsafe_allow_html=True)
# # # --- Header ---
# # st.markdown("<h1>Telugu Sentiment Analysis</h1>", unsafe_allow_html=True)
# # # --- Main Container ---
# # with st.container():
# # st.markdown("<div class='container-box'>", unsafe_allow_html=True)
# # st.markdown("""
# # <p class='instructions'>
# # Enter a Telugu sentence below and click 'Predict' to analyze its sentiment.<br>
# # Example: ఈ సినిమా చాలా బాగుంది!
# # </p>
# # """, unsafe_allow_html=True)
# # user_input = st.text_area("Your Telugu Text", height=150, placeholder="ఉదాహరణ: ఈ సినిమా చాలా బాగుంది!")
# # 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())
# # def is_mostly_telugu(text):
# # if not text.strip():
# # return False
# # telugu_chars = len(re.findall(r'[\u0C00-\u0C7F]', text))
# # allowed_chars = len(re.findall(r'[a-zA-Z0-9\s.,!?]', text))
# # total_chars = len(text)
# # return (telugu_chars / total_chars >= 0.7) and (telugu_chars + allowed_chars == total_chars)
# # # Set default background
# # st.markdown("<script>document.body.className = 'default-bg'; document.querySelector('.stApp').className = 'stApp default-bg';</script>", unsafe_allow_html=True)
# # if st.button("Predict"):
# # if not user_input.strip():
# # st.warning("⚠️ Please enter some text.")
# # else:
# # cleaned = clean_input(user_input)
# # if not is_mostly_telugu(cleaned):
# # st.error("❌ Text should be primarily in Telugu script.")
# # else:
# # result = pipe(cleaned)[0]
# # idx = int(result['label'].split('_')[1])
# # if idx == 0:
# # sentiment = "Neutral"
# # elif idx == 1:
# # sentiment = "Positive"
# # else:
# # sentiment = "Negative"
# # emoji = emojis[sentiment]
# # # Update background based on sentiment
# # st.markdown(f"<script>document.body.className = '{sentiment.lower()}-bg'; document.querySelector('.stApp').className = 'stApp {sentiment.lower()}-bg';</script>", unsafe_allow_html=True)
# # st.markdown(f"<div class='output-box {sentiment}'>{sentiment} {emoji}</div>", unsafe_allow_html=True)
# # st.markdown(f"<div class='score-info'>Confidence: {result['score']:.2f} | Text: {user_input}</div>", unsafe_allow_html=True)
# # st.markdown("</div>", unsafe_allow_html=True)