Spaces:
Sleeping
Sleeping
File size: 1,953 Bytes
b2d71fe |
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 |
import gradio as gr
import torch
import torch.nn as nn
import torch.optim as optim
from transformers import AutoTokenizer, AutoModel
import torch.nn.functional as F
import timm
from huggingface_hub import PyTorchModelHubMixin
class TwoLayerNN(nn.Module, PyTorchModelHubMixin):
def __init__(self, input_dim, hidden_dim, output_dim):
super(TwoLayerNN, self).__init__()
self.fc1 = nn.Linear(input_dim, hidden_dim)
self.relu = nn.ReLU()
self.fc2 = nn.Linear(hidden_dim, output_dim)
self.sigmoid = nn.Sigmoid()
def forward(self, x):
out = self.fc1(x)
out = self.relu(out)
out = self.fc2(out)
out = self.sigmoid(out)
return out
classifier = TwoLayerNN.from_pretrained("Robzy/job-classifier", input_dim=384, hidden_dim=128, output_dim=1)
tokenizer = AutoTokenizer.from_pretrained("sentence-transformers/all-MiniLM-L6-v2")
embedding_model = AutoModel.from_pretrained("sentence-transformers/all-MiniLM-L6-v2")
def mean_pooling(model_output, attention_mask):
token_embeddings = model_output[0] #First element of model_output contains all token embeddings
input_mask_expanded = attention_mask.unsqueeze(-1).expand(token_embeddings.size()).float()
return torch.sum(token_embeddings * input_mask_expanded, 1) / torch.clamp(input_mask_expanded.sum(1), min=1e-9)
def infer(text):
encoded_input = tokenizer(text, padding=True, truncation=True, return_tensors='pt')
with torch.no_grad():
model_output = embedding_model(**encoded_input)
sentence_embeddings = mean_pooling(model_output, encoded_input['attention_mask'])
sentence_embeddings = F.normalize(sentence_embeddings, p=2, dim=1)
output = classifier(sentence_embeddings)
return output.item()
demo = gr.Interface(fn=infer, inputs="text", outputs="text")
gr.Textbox(placeholder="Enter job description here", label="Job Description")
demo.launch() |