Spaces:
Sleeping
Sleeping
Download src/streamlit_app.py from Mehreen44/Roman_Urdu_app: direct link, hf CLI and curl.
- Browser
- Download file 2.02 kB
-
https://huggingface.co/spaces/Mehreen44/Roman_Urdu_app/resolve/main/src/streamlit_app.py
- Command line
-
hf download hf://spaces/Mehreen44/Roman_Urdu_app/src/streamlit_app.py
-
curl -L -o streamlit_app.py https://huggingface.co/spaces/Mehreen44/Roman_Urdu_app/resolve/main/src/streamlit_app.py
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" | |
| 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%}" | |
| ) |