import gradio as gr from transformers import BertModel, BertConfig import torch.nn as nn import torch.nn.functional as F import huggingface_hub from huggingface_hub import hf_hub_download huggingface_hub.Repository = 'zArabi/Persian-Sentiment-Analysis' class SentimentModel(nn.Module): def __init__(self, config): super(SentimentModel, self).__init__() self.bert = BertModel.from_pretrained(modelName, return_dict=False) self.dropout = nn.Dropout(0.3) self.classifier = nn.Linear(config.hidden_size, config.num_labels) def forward(self, input_ids, attention_mask): _, pooled_output = self.bert( input_ids=input_ids, attention_mask=attention_mask) pooled_output = self.dropout(pooled_output) logits = self.classifier(pooled_output) return logits 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) downloadedModelFile = hf_hub_download(repo_id="zArabi/Persian-Sentiment-Analysis", filename="persianModel") loaded_model = torch.load(downloadedModelFile) max_len=512 pipeline = pipeline(task="image-classification", model="julien-c/hotdog-not-hotdog") def predict(text): text = cleaning(text) encoding = tokenizer.encode_plus( sample_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': sample_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()