Update app.py
Browse files
app.py
CHANGED
|
@@ -1,57 +1,46 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
| 3 |
import torch
|
| 4 |
-
import zipfile
|
| 5 |
-
import os
|
| 6 |
|
| 7 |
-
#
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
extracted_dir = "./trained_model"
|
| 12 |
-
|
| 13 |
-
# Extract the contents of the zip file
|
| 14 |
-
with zipfile.ZipFile(zip_file_path, 'r') as zip_ref:
|
| 15 |
-
zip_ref.extractall(extracted_dir)
|
| 16 |
-
|
| 17 |
-
# Load the saved model and tokenizer
|
| 18 |
-
#tokenizer = AutoTokenizer.from_pretrained(extracted_dir)
|
| 19 |
-
#model = AutoModelForSequenceClassification.from_pretrained(extracted_dir)
|
| 20 |
|
| 21 |
# Define the device to run inference on (GPU if available, otherwise CPU)
|
| 22 |
-
|
| 23 |
|
| 24 |
# Move the model to the device
|
| 25 |
-
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
# Define function for sentiment analysis
|
| 30 |
def predict_sentiment(review):
|
| 31 |
-
#
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
#
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
#
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
#
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
#
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
return
|
| 55 |
|
| 56 |
|
| 57 |
# Create Gradio interface
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
| 3 |
import torch
|
|
|
|
|
|
|
| 4 |
|
| 5 |
+
# Load the model from the Hugging Face Model Hub
|
| 6 |
+
model_name = "moazx/AraBERT-Restaurant-Sentiment"
|
| 7 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 8 |
+
model = AutoModelForSequenceClassification.from_pretrained(model_name)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
# Define the device to run inference on (GPU if available, otherwise CPU)
|
| 11 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 12 |
|
| 13 |
# Move the model to the device
|
| 14 |
+
model.to(device)
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
# Define function for sentiment analysis
|
| 19 |
def predict_sentiment(review):
|
| 20 |
+
# Step 1: Tokenization
|
| 21 |
+
encoded_text = tokenizer(
|
| 22 |
+
review, padding=True, truncation=True, max_length=256, return_tensors="pt"
|
| 23 |
+
)
|
| 24 |
+
|
| 25 |
+
# Move input tensors to the appropriate device
|
| 26 |
+
input_ids = encoded_text["input_ids"].to(device)
|
| 27 |
+
attention_mask = encoded_text["attention_mask"].to(device)
|
| 28 |
+
|
| 29 |
+
# Step 2: Inference
|
| 30 |
+
with torch.no_grad():
|
| 31 |
+
outputs = model(input_ids, attention_mask=attention_mask)
|
| 32 |
+
|
| 33 |
+
# Step 3: Prediction with probabilities
|
| 34 |
+
probs = torch.softmax(outputs.logits, dim=-1)
|
| 35 |
+
probs = (
|
| 36 |
+
probs.squeeze().cpu().numpy()
|
| 37 |
+
) # Convert to numpy array and remove the batch dimension
|
| 38 |
+
|
| 39 |
+
# Map predicted class index to label
|
| 40 |
+
label_map = {0: 'سلبي', 1: 'إيجابي'}
|
| 41 |
+
|
| 42 |
+
output_dict = {label_map[i]: float(probs[i]) for i in range(len(probs))}
|
| 43 |
+
return output_dict
|
| 44 |
|
| 45 |
|
| 46 |
# Create Gradio interface
|