zArabi commited on
Commit
9b69fbc
1 Parent(s): c5ae5ff

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +65 -7
app.py CHANGED
@@ -1,15 +1,73 @@
1
  import gradio as gr
2
- from transformers import pipeline
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3
 
4
  pipeline = pipeline(task="image-classification", model="julien-c/hotdog-not-hotdog")
5
 
6
- def predict(image):
7
- predictions = pipeline(image)
8
- return {p["label"]: p["score"] for p in predictions}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
 
10
  gr.Interface(
11
  predict,
12
- inputs=gr.inputs.Image(label="Upload hot dog candidate", type="filepath"),
13
- outputs=gr.outputs.Label(num_top_classes=2),
14
- title="Hot Dog? Or Not?",
15
  ).launch()
 
1
  import gradio as gr
2
+ from transformers import BertModel, BertConfig
3
+ import torch.nn as nn
4
+ import torch.nn.functional as F
5
+ import huggingface_hub
6
+ from huggingface_hub import hf_hub_download
7
+
8
+ huggingface_hub.Repository = 'zArabi/Persian-Sentiment-Analysis'
9
+
10
+ class SentimentModel(nn.Module):
11
+ def __init__(self, config):
12
+ super(SentimentModel, self).__init__()
13
+ self.bert = BertModel.from_pretrained(modelName, return_dict=False)
14
+ self.dropout = nn.Dropout(0.3)
15
+ self.classifier = nn.Linear(config.hidden_size, config.num_labels)
16
+
17
+ def forward(self, input_ids, attention_mask):
18
+ _, pooled_output = self.bert(
19
+ input_ids=input_ids,
20
+ attention_mask=attention_mask)
21
+ pooled_output = self.dropout(pooled_output)
22
+ logits = self.classifier(pooled_output)
23
+ return logits
24
+
25
+ modelName = 'HooshvareLab/bert-fa-base-uncased'
26
+ class_names = ['negative', 'neutral', 'positive']
27
+ label2id = {label: i for i, label in enumerate(class_names)}
28
+ id2label = {v: k for k, v in label2id.items()}
29
+
30
+ config = BertConfig.from_pretrained(
31
+ modelName,
32
+ num_labels=len(class_names),
33
+ id2label=id2label,
34
+ label2id=label2id)
35
+
36
+ downloadedModelFile = hf_hub_download(repo_id="zArabi/Persian-Sentiment-Analysis", filename="persianModel")
37
+ loaded_model = torch.load(downloadedModelFile)
38
+
39
+ max_len=512
40
 
41
  pipeline = pipeline(task="image-classification", model="julien-c/hotdog-not-hotdog")
42
 
43
+ def predict(text):
44
+ text = cleaning(text)
45
+ encoding = tokenizer.encode_plus(
46
+ sample_text,
47
+ max_length=max_len,
48
+ truncation=True,
49
+ padding="max_length",
50
+ add_special_tokens=True, # Add '[CLS]' and '[SEP]'
51
+ return_token_type_ids=True,
52
+ return_attention_mask=True,
53
+ return_tensors='pt', # Return PyTorch tensors
54
+ )
55
+ input_ids = encoding["input_ids"].to(device)
56
+ attention_mask = encoding["attention_mask"].to(device)
57
+ outputs = loaded_model (input_ids, attention_mask)
58
+ probs = F.softmax(outputs,dim=1)
59
+ values, indices = torch.max(probs, dim=1)
60
+ data = {
61
+ 'comments': sample_text,
62
+ 'preds': indices.cpu().numpy()[0],
63
+ 'label': class_names[indices.cpu().numpy()[0]],
64
+ 'probablities': {class_names[i] : round(probs[0][i].item(),3) for i in range(len(probs[0]))}
65
+ }
66
+ return data
67
 
68
  gr.Interface(
69
  predict,
70
+ inputs=gr.Textbox(label="Explore your sentence!",lines=2, placeholder="Type Here..."),
71
+ outputs=gr.outputs.Label(num_top_classes=3),
72
+ title="What are feeling?!",
73
  ).launch()