Update app.py file
Browse filesUpdate app.py file to remove the local_files_only=True parameter from from_pretrained() calls.
app.py
CHANGED
|
@@ -1,55 +1,56 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
import torch
|
| 3 |
-
from transformers import AutoModelForSequenceClassification, AutoTokenizer
|
| 4 |
-
|
| 5 |
-
# Defining the model path relative to the repository root.
|
| 6 |
-
model_path = "abdullah123456/NLP_Project"
|
| 7 |
-
|
| 8 |
-
# Loading the model and tokenizer from the local directory.
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
# Defining a simple text cleaning function
|
| 14 |
-
def clean_text(text):
|
| 15 |
-
return " ".join(text.split())
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
["
|
| 49 |
-
["
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
|
|
|
| 55 |
iface.launch()
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
from transformers import AutoModelForSequenceClassification, AutoTokenizer
|
| 4 |
+
|
| 5 |
+
# Defining the model path relative to the repository root.
|
| 6 |
+
model_path = "abdullah123456/NLP_Project"
|
| 7 |
+
|
| 8 |
+
# Loading the model and tokenizer from the local directory.
|
| 9 |
+
model = AutoModelForSequenceClassification.from_pretrained(model_path)
|
| 10 |
+
tokenizer = AutoTokenizer.from_pretrained(model_path)
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
# Defining a simple text cleaning function
|
| 14 |
+
def clean_text(text):
|
| 15 |
+
return " ".join(text.split())
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
# Defining the prediction function that the web interface will use.
|
| 19 |
+
def predict_sentiment(tweet: str) -> str:
|
| 20 |
+
|
| 21 |
+
# Cleaning the tweet
|
| 22 |
+
tweet_clean = clean_text(tweet)
|
| 23 |
+
|
| 24 |
+
# Tokenizing the tweet.
|
| 25 |
+
inputs = tokenizer(tweet_clean, return_tensors="pt", truncation=True, padding="max_length", max_length=128)
|
| 26 |
+
|
| 27 |
+
# Moving the input tensors to the same device as the model.
|
| 28 |
+
inputs = {k: v.to(model.device) for k, v in inputs.items()}
|
| 29 |
+
|
| 30 |
+
with torch.no_grad():
|
| 31 |
+
outputs = model(**inputs)
|
| 32 |
+
# Getting the predicted class index.
|
| 33 |
+
predicted_class = torch.argmax(outputs.logits, dim=1).item()
|
| 34 |
+
|
| 35 |
+
# Defining label mapping.
|
| 36 |
+
label_mapping = {0: "negative", 1: "neutral", 2: "positive"}
|
| 37 |
+
return label_mapping.get(predicted_class, "unknown")
|
| 38 |
+
|
| 39 |
+
# Creating the Gradio Interface.
|
| 40 |
+
iface = gr.Interface(
|
| 41 |
+
fn=predict_sentiment,
|
| 42 |
+
inputs=gr.Textbox(lines=4, placeholder="Enter an Urdu tweet here...", label="Urdu Tweet"),
|
| 43 |
+
outputs=gr.Textbox(label="Predicted Sentiment"),
|
| 44 |
+
title="Urdu Tweet Sentiment Analysis",
|
| 45 |
+
description="This app uses a fine-tuned transformer model to predict the sentiment of Urdu tweets. "
|
| 46 |
+
"Enter your tweet in the textbox below and click 'Submit' to see the prediction.",
|
| 47 |
+
examples=[
|
| 48 |
+
["السلام علیکم! آج کا دن بہت خوبصورت ہے۔"],
|
| 49 |
+
["میں بہت غمگین ہوں، دل بہت دکھ رہا ہے۔"],
|
| 50 |
+
["آپ کا کام بہت اچھا ہے!"]
|
| 51 |
+
]
|
| 52 |
+
)
|
| 53 |
+
|
| 54 |
+
# Launching the interface.
|
| 55 |
+
if __name__ == "__main__":
|
| 56 |
iface.launch()
|