Spaces:
Sleeping

Roman_Urdu_app / src /streamlit_app.py
Mehreen44's picture
update the file to fix some issues.
4e4242d verified
Raw History Blame Contribute Delete
2.02 kB
import streamlit as st
from transformers import (
AutoTokenizer,
AutoModelForSequenceClassification,
pipeline
)
# ---------------------------
# Page Configuration
# ---------------------------
st.set_page_config(
page_title="Roman Urdu Sentiment Analysis",
page_icon="πŸ’¬"
)
st.title("Roman Urdu Sentiment Analysis")
st.write("Enter a Roman Urdu sentence and predict its sentiment.")
# ---------------------------
# Load Model
# ---------------------------
MODEL_NAME = "Mehreen44/roman-urdu-sentiment"
@st.cache_resource
def load_model():
tokenizer = AutoTokenizer.from_pretrained(
MODEL_NAME
)
model = AutoModelForSequenceClassification.from_pretrained(
MODEL_NAME
)
classifier = pipeline(
"text-classification",
model=model,
tokenizer=tokenizer
)
return classifier
classifier = load_model()
# ---------------------------
# Label Mapping
# ---------------------------
label_map = {
"LABEL_0": "NEGATIVE",
"LABEL_1": "NEUTRAL",
"LABEL_2": "POSITIVE",
"label_0": "NEGATIVE",
"label_1": "NEUTRAL",
"label_2": "POSITIVE"
}
emoji_map = {
"NEGATIVE": "😞",
"NEUTRAL": "😐",
"POSITIVE": "😊"
}
# ---------------------------
# User Input
# ---------------------------
text = st.text_area(
"Enter Roman Urdu text:",
placeholder="e.g. bohat acha tha yeh"
)
# ---------------------------
# Prediction
# ---------------------------
if st.button("Analyze"):
if text.strip() == "":
st.warning("Please enter some text.")
else:
result = classifier(text)[0]
raw_label = result["label"]
confidence = result["score"]
sentiment = label_map.get(
raw_label,
raw_label
)
emoji = emoji_map.get(
sentiment,
""
)
st.success(
f"{emoji} Sentiment: {sentiment}"
)
st.write(
f"Confidence: {confidence:.2%}"
)