Adityaganesh's picture
Update app.py
a51e2dc verified
# import streamlit as st
# from transformers import pipeline
# import re
# import base64
# # Page Configuration
# st.set_page_config(page_title="Telugu Sentiment Analysis", layout="centered", )
# # Function to Encode Image
# def get_base64(file_path):
# with open(file_path, "rb") as f:
# data = f.read()
# return base64.b64encode(data).decode()
# # Set Background from Image
# def set_background(image_path):
# img_data = get_base64(image_path)
# st.markdown(
# f"""
# <style>
# .stApp {{
# background-image: url("data:image/jpg;base64,{img_data}");
# background-size: cover;
# background-position: center;
# background-repeat: no-repeat;
# }}
# </style>
# """,
# unsafe_allow_html=True
# )
# # Set the background
# set_background("New3.jpg")
# # Title and Description
# st.title("📊 Telugu Sentiment Analysis")
# st.markdown("Analyze the sentiment (Positive, Negative, Neutral) of a given **Telugu** sentence using a fine-tuned BERT model.", #1affff)
# # Load the model pipeline
# @st.cache_resource
# def load_pipeline():
# return pipeline("text-classification", model="Adityaganesh/Telugu_Sentiment_Analysis")
# pipe = load_pipeline()
# # Text Preprocessing
# def preprocess_text(text):
# text = text.strip()
# text = re.sub(r"\s+", " ", text)
# return text
# # User Input
# user_input = st.text_area("Enter Telugu Text:", height=200, placeholder="ఇక్కడ మీ తెలుగు వాక్యాన్ని నమోదు చేయండి...")
# if st.button("🔍 Analyze Sentiment"):
# if user_input.strip() == "":
# st.warning("దయచేసి కొన్ని తెలుగు వాక్యాలు నమోదు చేయండి.")
# else:
# clean_text = preprocess_text(user_input)
# with st.spinner("Analyzing sentiment..."):
# result = pipe(clean_text)[0]
# idx = int(result['label'].split('_')[1])
# if idx == 0:
# sentiment = "😐 Neutral"
# color = "gray"
# elif idx == 1:
# sentiment = "😊 Positive"
# color = "green"
# else:
# sentiment = "😠 Negative"
# color = "red"
# st.markdown(f"### Prediction: <span style='color:{color}'>{sentiment}</span>", unsafe_allow_html=True)
import streamlit as st
from transformers import pipeline
import re
import base64
# Page Configuration
st.set_page_config(page_title="Telugu Sentiment Analysis", layout="centered")
# Function to Encode Image
def get_base64(file_path):
with open(file_path, "rb") as f:
data = f.read()
return base64.b64encode(data).decode()
# Set Background from Image
def set_background(image_path):
img_data = get_base64(image_path)
st.markdown(
f"""
<style>
.stApp {{
background-image: url("data:image/jpg;base64,{img_data}");
background-size: cover;
background-position: center;
background-repeat: no-repeat;
}}
</style>
""",
unsafe_allow_html=True
)
# Set the background
set_background("New3.jpg")
# Title and Description
st.markdown(
"<h1 style='text-align: center; color:#1affff;'> Telugu Sentiment Analysis</h1>",
unsafe_allow_html=True
)
st.markdown(
"<h4 style='text-align: center; color:#1affff;'>Analyze the sentiment (Positive, Negative, Neutral) of a given <b>Telugu</b> sentence using a fine-tuned BERT model.</h4>",
unsafe_allow_html=True
)
# Load the model pipeline
@st.cache_resource
def load_pipeline():
return pipeline("text-classification", model="Adityaganesh/Telugu_Sentiment_Analysis")
pipe = load_pipeline()
# Text Preprocessing
def preprocess_text(text):
text = text.strip()
text = re.sub(r"\s+", " ", text)
return text
# User Input
user_input = st.text_area("Enter Telugu Text:", height=200, placeholder="ఇక్కడ మీ తెలుగు వాక్యాన్ని నమోదు చేయండి...")
if st.button("🔍 Analyze Sentiment"):
if user_input.strip() == "":
st.warning("దయచేసి కొన్ని తెలుగు వాక్యాలు నమోదు చేయండి.")
else:
clean_text = preprocess_text(user_input)
with st.spinner("Analyzing sentiment..."):
result = pipe(clean_text)[0]
idx = int(result['label'].split('_')[1])
if idx == 0:
sentiment = " Neutral"
color = "gray"
elif idx == 1:
sentiment = " Positive"
color = "green"
else:
sentiment = " Negative"
color = "red"
st.markdown(
f"<h3><span style='color:#1affff'>Prediction:</span> <span style='color:{color}'>{sentiment}</span></h3>",
unsafe_allow_html=True)