File size: 2,076 Bytes
c5ae5ff
1635166
 
9b69fbc
 
 
 
5d1cadc
9b69fbc
 
 
 
 
 
 
 
 
 
 
 
5d1cadc
 
2c051ce
5d1cadc
9b69fbc
c5ae5ff
1635166
 
2c051ce
c5ae5ff
9b69fbc
 
 
1635166
9b69fbc
 
 
 
 
 
 
 
 
 
5d1cadc
9b69fbc
 
 
1635166
9b69fbc
 
 
 
8abdd11
c5ae5ff
 
 
9b69fbc
 
2c051ce
c5ae5ff
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
56
57
58
59
60
61
import gradio as gr
from transformers import BertModel, BertConfig, BertTokenizer
import torch
import torch.nn as nn
import torch.nn.functional as F
import huggingface_hub
from huggingface_hub import hf_hub_download
from preprocessing import *

modelName = 'HooshvareLab/bert-fa-base-uncased'
class_names = ['negative', 'neutral', 'positive']
label2id = {label: i for i, label in enumerate(class_names)}
id2label = {v: k for k, v in label2id.items()}

config = BertConfig.from_pretrained(
    modelName,
    num_labels=len(class_names),
    id2label=id2label,
    label2id=label2id)

path="HooshvareLab-bert-fa-base-uncased-3class-best-epoch-weight-decay=.001.bin"
downloadedModelFile = hf_hub_download(repo_id="zArabi/Persian-Sentiment-Analysis", filename=path)
loaded_model = torch.load(downloadedModelFile,map_location="cpu")
loaded_model.eval()


tokenizer = BertTokenizer.from_pretrained(modelName)
max_len=512
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")

def predict(text):
  text = cleaning(text)
  encoding = tokenizer.encode_plus(
    text,
    max_length=max_len,
    truncation=True,
    padding="max_length",
    add_special_tokens=True, # Add '[CLS]' and '[SEP]'
    return_token_type_ids=True,
    return_attention_mask=True,
    return_tensors='pt',  # Return PyTorch tensors
  )
  input_ids = encoding["input_ids"].to(device)
  attention_mask = encoding["attention_mask"].to(device)
  outputs = loaded_model(input_ids, attention_mask)
  probs = F.softmax(outputs,dim=1)
  values, indices = torch.max(probs, dim=1)
  data = {
    'comments': text,
    'preds': indices.cpu().numpy()[0],
    'label': class_names[indices.cpu().numpy()[0]],
    'probablities': {class_names[i] : round(probs[0][i].item(),3) for i in range(len(probs[0]))}
  }
  return {class_names[i] : round(probs[0][i].item(),3) for i in range(len(probs[0]))}

gr.Interface(
    predict,
    inputs=gr.Textbox(label="Explore your sentence!",lines=2, placeholder="Type Here..."),
    outputs=gr.outputs.Label(num_top_classes=3),
    title="How are feeling?!",
).launch()