CPVClassifier / app.py
mnavas
m
e59c5eb
import gradio as gr
import pandas as pd
import torch
import numpy as np
from transformers import AutoTokenizer, AutoModelForSequenceClassification
tokenizer = AutoTokenizer.from_pretrained("model")
model = AutoModelForSequenceClassification.from_pretrained("model")
cpv = pd.read_csv("cpv.csv", sep=";")
df = pd.read_csv("code-desc.csv", sep=";")
labels = df.columns[1:]
cpv = cpv.code
id2label = {idx:label for idx, label in enumerate(labels)}
label2id = {label:idx for idx, label in enumerate(labels)}
codeDesc = dict(zip(df.code, df.desc))
def askcpv(description):
encoding = tokenizer(description, return_tensors="pt")
encoding = {k: v.to(model.device) for k,v in encoding.items()}
outputs = model(**encoding)
sigmoid = torch.nn.Sigmoid()
probs = sigmoid(outputs.logits.squeeze().cpu())
values, indices = torch.topk(probs, k=5)
# turn predicted id's into actual label names
# predicted_labels = [id2label[idx] for idx, label in enumerate(predictions) if label == 1.0]
# return predicted_labels
print("probs: ", probs.detach().numpy())
print("values: ", values.detach().numpy())
print("indices: ", indices.detach().numpy())
# print({i: v.item() for i, v in zip(indices, values)})
return {cpv[i]: v.item() for i, v in zip(indices.detach().numpy(), values.detach().numpy())}
gr.Interface(fn=askcpv, inputs="textbox", outputs="label", description = "Devuelve los dos primeros d铆gitos m谩s probables dada la descripci贸n de una licitaci贸n en espa帽ol.",
title = "Clasificador de c贸digos CPV", examples=[["Nuevo edificio de acceso en piscinas de Binaced."], ["Servicio de ambulancias y personal sanitario para el servicio de salvamento y socorrismo de las playas de C谩diz"]], css=".text-2xl{font-size:1.2rem !important; line-height: 0.5rem;} .text-sm{font-size:0.7rem !important} .mb-2{margin-bottom: 0.1rem};" ).launch()