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()