Spaces:
Runtime error
Runtime error
File size: 2,503 Bytes
48d28d5 6a00324 745b80e 9e0ab0f 48d28d5 98b274f 9e0ab0f 745b80e 96664a2 2ebfb5d 745b80e 562e111 745b80e c05a673 10520fd c1157cb ff8b6b5 9ca2928 3dcb268 d85a37a 41c8dfe dc2b889 |
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 |
import gradio as gr
import sentence_transformers
from sentence_transformers import SentenceTransformer
import torch
from sentence_transformers.util import semantic_search
import pandas as pd
model = SentenceTransformer('JoBeer/all-mpnet-base-v2-eclass')
corpus = pd.read_json('corpus.jsonl', lines = True, encoding = 'utf-8')
def predict(name, description):
text = 'Description: '+ description + '; Name: ' + name
query_embedding = model.encode(text, convert_to_tensor=True)
corpus_embeddings = torch.Tensor(corpus["embeddings"])
output = sentence_transformers.util.semantic_search(query_embedding, corpus_embeddings, top_k = 5)
preferedName1 = corpus.iloc[output[0][0].get('corpus_id'),2]
definition1 = corpus.iloc[output[0][0].get('corpus_id'),1]
IRDI1 = corpus.iloc[output[0][0].get('corpus_id'),4]
score1 = output[0][0].get('score')
preferedName2 = corpus.iloc[output[0][1].get('corpus_id'),2]
definition2 = corpus.iloc[output[0][1].get('corpus_id'),1]
IRDI2 = corpus.iloc[output[0][1].get('corpus_id'),4]
score2 = output[0][1].get('score')
df = [[preferedName1, definition1, IRDI1, score1], [preferedName2, definition2, IRDI1, score2]]
return pd.Dataframe(df)
interface = gr.Interface(fn = predict,
inputs = [gr.Textbox(label="Name:", placeholder="z.B. GTIN", lines=1), gr.Textbox(label="Description:", placeholder="z.B. Globel Trade Item Number", lines=1)],
#outputs = [gr.Textbox(label = 'preferedName'),gr.Textbox(label = 'definition'), gr.Textbox(label = 'IDRI'),gr.Textbox(label = 'score')],
outputs = [gr.Dataframe(row_count = (2, "fixed"), col_count=(4, "fixed"), label="Predictions", headers=['preferedName', 'definition', 'IRDI', 'score'])],
examples = [['GTIN', 'Globel Trade Item Number'], ['Global Trade Item Number', 'the identification number from the GS1 system with which the trading units can be uniquely identified worldwide'],
['Device type', 'describing a set of common specific characteristics in products or goods'], ['Item type','the type of product, an item can be assigned to'],
['Nominal power','power being consumed by or dissipated within an electric component as a variable'], ['Power consumption', 'power that is typically taken from the auxiliary power supply when the device is operating normally']], theme = 'huggingface',
title = 'ECLASS-Property-Search')
interface.launch() |