Spaces:
Build error
Build error
mnavas
commited on
Commit
·
584eeb2
1
Parent(s):
3155660
app
Browse files- app.py +30 -4
- code-desc.csv +0 -0
- cpv.csv +0 -0
app.py
CHANGED
@@ -1,7 +1,33 @@
|
|
1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
2 |
|
3 |
-
|
4 |
-
|
5 |
|
6 |
-
|
7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
import pandas as pd
|
3 |
+
import torch
|
4 |
+
import numpy as np
|
5 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
6 |
|
7 |
+
tokenizer = AutoTokenizer.from_pretrained("mnavas/roberta-finetuned-CPV_Spanish")
|
8 |
+
model = AutoModelForSequenceClassification.from_pretrained("mnavas/roberta-finetuned-CPV_Spanish")
|
9 |
|
10 |
+
cpv = pd.read_csv("cpv.csv")
|
11 |
+
df = pd.read_csv("code-desc.csv")
|
12 |
+
|
13 |
+
labels = df.columns[1:]
|
14 |
+
cpv = cpv.columns[1:]
|
15 |
+
|
16 |
+
id2label = {idx:label for idx, label in enumerate(labels)}
|
17 |
+
label2id = {label:idx for idx, label in enumerate(labels)}
|
18 |
+
|
19 |
+
def askcpv(description):
|
20 |
+
|
21 |
+
encoding = tokenizer(description, return_tensors="pt")
|
22 |
+
encoding = {k: v.to(model.device) for k,v in encoding.items()}
|
23 |
+
outputs = model(**encoding)
|
24 |
+
sigmoid = torch.nn.Sigmoid()
|
25 |
+
probs = sigmoid(logits.squeeze().cpu())
|
26 |
+
probabilites = torch.nn.functional.softmax(out[0], dim=0)
|
27 |
+
values, indices = torch.topk(probabilites, k=10)
|
28 |
+
# turn predicted id's into actual label names
|
29 |
+
# predicted_labels = [id2label[idx] for idx, label in enumerate(predictions) if label == 1.0]
|
30 |
+
# return predicted_labels
|
31 |
+
return {cpv[i]: v.item() for i, v in zip(indices, values)}
|
32 |
+
|
33 |
+
gr.Interface(fn=askcpv, inputs="textbox", outputs="label").launch()
|
code-desc.csv
ADDED
Binary file (13.6 kB). View file
|
|
cpv.csv
ADDED
Binary file (9.91 kB). View file
|
|