|
import gradio as gr |
|
import torch |
|
from transformers import AutoModelForSequenceClassification, AutoTokenizer |
|
|
|
|
|
|
|
|
|
model_path = "abdullah123456/Sentiment_Analysis_Urdu_NLP" |
|
|
|
|
|
model = AutoModelForSequenceClassification.from_pretrained(model_path) |
|
tokenizer = AutoTokenizer.from_pretrained(model_path) |
|
|
|
|
|
|
|
def clean_text(text): |
|
return " ".join(text.split()) |
|
|
|
|
|
|
|
def predict_sentiment(tweet: str) -> str: |
|
|
|
|
|
tweet_clean = clean_text(tweet) |
|
|
|
|
|
inputs = tokenizer(tweet_clean, return_tensors="pt", truncation=True, padding="max_length", max_length=128) |
|
|
|
|
|
inputs = {k: v.to(model.device) for k, v in inputs.items()} |
|
|
|
with torch.no_grad(): |
|
outputs = model(**inputs) |
|
|
|
predicted_class = torch.argmax(outputs.logits, dim=1).item() |
|
|
|
|
|
label_mapping = {0: "negative", 1: "neutral", 2: "positive"} |
|
return label_mapping.get(predicted_class, "unknown") |
|
|
|
|
|
iface = gr.Interface( |
|
fn=predict_sentiment, |
|
inputs=gr.Textbox(lines=4, placeholder="Enter an Urdu tweet here...", label="Urdu Tweet"), |
|
outputs=gr.Textbox(label="Predicted Sentiment"), |
|
title="Urdu Tweet Sentiment Analysis - 1", |
|
description="This app uses a fine-tuned transformer model to predict the sentiment of Urdu tweets. " |
|
"Enter your tweet in the textbox below and click 'Submit' to see the prediction.", |
|
examples=[ |
|
["السلام علیکم! آج کا دن بہت خوبصورت ہے۔"], |
|
["میں بہت غمگین ہوں، دل بہت دکھ رہا ہے۔"], |
|
["آپ کا کام بہت اچھا ہے!"] |
|
] |
|
) |
|
|
|
|
|
if __name__ == "__main__": |
|
iface.launch() |