moazx commited on
Commit
ce18cd2
1 Parent(s): 4d9f2db

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -41
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
- # Path to the zip file
8
- zip_file_path = "./trained_model.zip"
9
-
10
- # Directory to extract the contents of the zip file
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
- #device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
23
 
24
  # Move the model to the device
25
- #model.to(device)
26
 
27
 
28
 
29
  # Define function for sentiment analysis
30
  def predict_sentiment(review):
31
- # # Step 1: Tokenization
32
- # encoded_text = tokenizer(
33
- # review, padding=True, truncation=True, max_length=256, return_tensors="pt"
34
- # )
35
-
36
- # # Move input tensors to the appropriate device
37
- # input_ids = encoded_text["input_ids"].to(device)
38
- # attention_mask = encoded_text["attention_mask"].to(device)
39
-
40
- # # Step 2: Inference
41
- # with torch.no_grad():
42
- # outputs = model(input_ids, attention_mask=attention_mask)
43
-
44
- # # Step 3: Prediction with probabilities
45
- # probs = torch.softmax(outputs.logits, dim=-1)
46
- # probs = (
47
- # probs.squeeze().cpu().numpy()
48
- # ) # Convert to numpy array and remove the batch dimension
49
-
50
- # # Map predicted class index to label
51
- # label_map = {0: 'سلبي', 1: 'إيجابي'}
52
-
53
- # output_dict = {label_map[i]: float(probs[i]) for i in range(len(probs))}
54
- return 1
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