Update app.py
Browse files
app.py
CHANGED
|
@@ -2,46 +2,96 @@ import streamlit as st
|
|
| 2 |
import transformers
|
| 3 |
from transformers import pipeline
|
| 4 |
import re
|
|
|
|
| 5 |
|
| 6 |
-
#
|
| 7 |
st.set_page_config(page_title="Telugu Sentiment Analysis", layout="centered")
|
| 8 |
-
st.title("📊 Telugu Sentiment Analysis")
|
| 9 |
-
st.markdown("Analyze the sentiment (Positive, Negative, Neutral) of a given **Telugu** sentence using a fine-tuned BERT model.")
|
| 10 |
|
| 11 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
@st.cache_resource
|
| 13 |
def load_pipeline():
|
| 14 |
return pipeline("text-classification", model="Adityaganesh/Telugu_Sentiment_Analysis")
|
| 15 |
|
| 16 |
pipe = load_pipeline()
|
| 17 |
|
| 18 |
-
#
|
| 19 |
def preprocess_text(text):
|
| 20 |
text = text.strip()
|
| 21 |
text = re.sub(r"\s+", " ", text)
|
| 22 |
return text
|
| 23 |
|
| 24 |
-
#
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
-
|
|
|
|
| 28 |
if user_input.strip() == "":
|
| 29 |
st.warning("దయచేసి కొన్ని తెలుగు వాక్యాలు నమోదు చేయండి.")
|
|
|
|
|
|
|
| 30 |
else:
|
| 31 |
clean_text = preprocess_text(user_input)
|
| 32 |
-
with st.spinner("
|
| 33 |
result = pipe(clean_text)[0]
|
| 34 |
idx = int(result['label'].split('_')[1])
|
| 35 |
|
| 36 |
if idx == 0:
|
| 37 |
sentiment = "😐 Neutral"
|
| 38 |
-
color = "
|
| 39 |
elif idx == 1:
|
| 40 |
sentiment = "😊 Positive"
|
| 41 |
-
color = "
|
| 42 |
else:
|
| 43 |
sentiment = "😠 Negative"
|
| 44 |
-
color = "
|
| 45 |
|
| 46 |
-
st.markdown(
|
| 47 |
-
|
|
|
|
|
|
|
|
|
| 2 |
import transformers
|
| 3 |
from transformers import pipeline
|
| 4 |
import re
|
| 5 |
+
import base64
|
| 6 |
|
| 7 |
+
# Set page config
|
| 8 |
st.set_page_config(page_title="Telugu Sentiment Analysis", layout="centered")
|
|
|
|
|
|
|
| 9 |
|
| 10 |
+
# Set background image
|
| 11 |
+
def set_background(image_file):
|
| 12 |
+
with open(image_file, "rb") as file:
|
| 13 |
+
encoded = base64.b64encode(file.read()).decode()
|
| 14 |
+
st.markdown(
|
| 15 |
+
f"""
|
| 16 |
+
<style>
|
| 17 |
+
.stApp {{
|
| 18 |
+
background-image: url("data:image/jpg;base64,{encoded}");
|
| 19 |
+
background-size: cover;
|
| 20 |
+
background-position: center;
|
| 21 |
+
background-repeat: no-repeat;
|
| 22 |
+
}}
|
| 23 |
+
textarea {{
|
| 24 |
+
background-color: #f8f8f8 !important;
|
| 25 |
+
font-size: 18px !important;
|
| 26 |
+
}}
|
| 27 |
+
.custom-button {{
|
| 28 |
+
display: inline-block;
|
| 29 |
+
padding: 10px 25px;
|
| 30 |
+
font-size: 18px;
|
| 31 |
+
font-weight: bold;
|
| 32 |
+
color: white;
|
| 33 |
+
background-color: #ff4b4b;
|
| 34 |
+
border-radius: 10px;
|
| 35 |
+
text-align: center;
|
| 36 |
+
}}
|
| 37 |
+
</style>
|
| 38 |
+
""",
|
| 39 |
+
unsafe_allow_html=True
|
| 40 |
+
)
|
| 41 |
+
|
| 42 |
+
set_background("New3.jpg")
|
| 43 |
+
|
| 44 |
+
# Title and Description
|
| 45 |
+
st.markdown("<h1 style='text-align: center;'>📊 Telugu Sentiment Analysis</h1>", unsafe_allow_html=True)
|
| 46 |
+
st.markdown(
|
| 47 |
+
"<div style='text-align:center;'>"
|
| 48 |
+
"Analyze the sentiment (Positive, Negative, Neutral) of a given <strong>Telugu</strong> sentence using a fine-tuned BERT model."
|
| 49 |
+
"</div><br>", unsafe_allow_html=True
|
| 50 |
+
)
|
| 51 |
+
|
| 52 |
+
# Load pipeline
|
| 53 |
@st.cache_resource
|
| 54 |
def load_pipeline():
|
| 55 |
return pipeline("text-classification", model="Adityaganesh/Telugu_Sentiment_Analysis")
|
| 56 |
|
| 57 |
pipe = load_pipeline()
|
| 58 |
|
| 59 |
+
# Preprocess text
|
| 60 |
def preprocess_text(text):
|
| 61 |
text = text.strip()
|
| 62 |
text = re.sub(r"\s+", " ", text)
|
| 63 |
return text
|
| 64 |
|
| 65 |
+
# Telugu validation
|
| 66 |
+
def is_telugu(text):
|
| 67 |
+
return bool(re.fullmatch(r"[\u0C00-\u0C7F\s]+", text))
|
| 68 |
+
|
| 69 |
+
# Input
|
| 70 |
+
user_input = st.text_area("✍️ Enter Telugu Text:", height=200, placeholder="ఇక్కడ మీ తెలుగు వాక్యాన్ని నమోదు చేయండి...")
|
| 71 |
|
| 72 |
+
# Button
|
| 73 |
+
if st.button("🔍 Predict"):
|
| 74 |
if user_input.strip() == "":
|
| 75 |
st.warning("దయచేసి కొన్ని తెలుగు వాక్యాలు నమోదు చేయండి.")
|
| 76 |
+
elif not is_telugu(user_input):
|
| 77 |
+
st.error("దయచేసి కేవలం తెలుగు వాక్యాలు మాత్రమే నమోదు చేయండి.")
|
| 78 |
else:
|
| 79 |
clean_text = preprocess_text(user_input)
|
| 80 |
+
with st.spinner("విశ్లేషణ జరుగుతోంది..."):
|
| 81 |
result = pipe(clean_text)[0]
|
| 82 |
idx = int(result['label'].split('_')[1])
|
| 83 |
|
| 84 |
if idx == 0:
|
| 85 |
sentiment = "😐 Neutral"
|
| 86 |
+
color = "#808080"
|
| 87 |
elif idx == 1:
|
| 88 |
sentiment = "😊 Positive"
|
| 89 |
+
color = "#28a745"
|
| 90 |
else:
|
| 91 |
sentiment = "😠 Negative"
|
| 92 |
+
color = "#dc3545"
|
| 93 |
|
| 94 |
+
st.markdown(
|
| 95 |
+
f"<h3 style='color:{color}; text-align:center;'>📢 Prediction: {sentiment}</h3>",
|
| 96 |
+
unsafe_allow_html=True
|
| 97 |
+
)
|