Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -6,3 +6,88 @@ from sentence_transformers.util import semantic_search
|
|
6 |
import pandas as pd
|
7 |
import requests
|
8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
import pandas as pd
|
7 |
import requests
|
8 |
|
9 |
+
#Import corpus embeddings
|
10 |
+
corpus_ger = pd.read_json('JoBeer/corpus_embed_ger_model5.json')
|
11 |
+
corpus_eng = pd.read_json('JoBeer/corpus_embed_eng_mboth.json')
|
12 |
+
corpus_fr = pd.read_json('JoBeer/corpus_embed_fr_Sahajtomar.json')
|
13 |
+
|
14 |
+
#Import models
|
15 |
+
model_ger = SentenceTransformer('JoBeer/model5')
|
16 |
+
model_eng = SentenceTransformer('mboth/distil-eng-quora-sentence')
|
17 |
+
model_fr = SentenceTransformer('Sahajtomar/french_semantic')
|
18 |
+
|
19 |
+
#Definition of search function
|
20 |
+
def predict(name, description, language, classCode='nofilter', top_k=10):
|
21 |
+
#language detection
|
22 |
+
if language == 'german':
|
23 |
+
model = model_ger
|
24 |
+
corpus = corpus_ger
|
25 |
+
if language == 'english':
|
26 |
+
model = model_eng
|
27 |
+
corpus = corpus_eng
|
28 |
+
if language == 'french':
|
29 |
+
model = model_fr
|
30 |
+
corpus = corpus_fr
|
31 |
+
|
32 |
+
text = name + '; ' + description #Verkettung name und description
|
33 |
+
query_embedding = model.encode(text, convert_to_tensor=True) #Erzeugung Query Embedding
|
34 |
+
|
35 |
+
#Filterung ECLASS Corpus
|
36 |
+
if classCode == 'nofilter':
|
37 |
+
corpus_filtered = corpus
|
38 |
+
|
39 |
+
else:
|
40 |
+
url = f'https://bcon2-api.azurewebsites.net/api/eclass?codedname={classCode}'
|
41 |
+
response = requests.get(url) #http request um auf Filter API zuzugreifen
|
42 |
+
lines = response.text.split('\n')
|
43 |
+
properties_filtered_list = [line[-21:-1] for line in lines]
|
44 |
+
corpus_filtered = corpus[corpus['irdi'].isin(properties_filtered_list)] #Der ECLASS Corpus wird mit Hilfe der Liste gefiltert
|
45 |
+
corpus_filtered.reset_index(drop=True,inplace=True) #Index wird zurückgesetzt
|
46 |
+
|
47 |
+
#Umwandlung corpus Embeddings in Tensor
|
48 |
+
corpus_embeddings = torch.Tensor(corpus_filtered["Embeddings"])
|
49 |
+
|
50 |
+
#Einspeisung Modell
|
51 |
+
output = sentence_transformers.util.semantic_search(query_embedding, corpus_embeddings, top_k = top_k)
|
52 |
+
|
53 |
+
#Auslesen der Modellausgabe
|
54 |
+
preferedNames = []
|
55 |
+
definitions = []
|
56 |
+
irdis = []
|
57 |
+
scores = []
|
58 |
+
for i in range(0,top_k):
|
59 |
+
preferedNames.append(corpus_filtered.iloc[output[0][i].get('corpus_id'),1])
|
60 |
+
definitions.append(corpus_filtered.iloc[output[0][i].get('corpus_id'),2])
|
61 |
+
irdis.append(corpus_filtered.iloc[output[0][i].get('corpus_id'),0])
|
62 |
+
scores.append(output[0][i].get('score'))
|
63 |
+
|
64 |
+
predictions = pd.DataFrame({'preferedName' : preferedNames, 'definition' : definitions, 'irdi' : irdis,'score' : scores,})
|
65 |
+
return predictions
|
66 |
+
|
67 |
+
#gradio user interface
|
68 |
+
with gr.Blocks() as demo:
|
69 |
+
with gr.Row():
|
70 |
+
gr.Markdown("""# ECLASS-Search-Demo
|
71 |
+
This is a semantic search application that maps unknown product properties to the ECLASS standard. It is created by ECLASS e.V. in collaboration with the GART-labortory of the cologne university of applied science.""") #This demo provides a semantic search application for ECLASS features.
|
72 |
+
|
73 |
+
with gr.Row():
|
74 |
+
#inputs
|
75 |
+
name_tx = gr.Textbox(label="Name:",placeholder="Name of the Property", lines=1)
|
76 |
+
description_tx = gr.Textbox(label="Description:", placeholder="Description of the Property", lines=1)
|
77 |
+
|
78 |
+
with gr.Row():
|
79 |
+
#inputs
|
80 |
+
classcode_tx = gr.Textbox(value='nofilter', label="Filter with ECLASS ClassCode", placeholder="type nofilter", lines=1)
|
81 |
+
top_k_nu = gr.Number(value=10, label="Number of Matches")
|
82 |
+
language_drop = gr.Dropdown(["german", "english","french"], value='german', label="Select language")
|
83 |
+
|
84 |
+
#button
|
85 |
+
search = gr.Button("search")
|
86 |
+
|
87 |
+
#output
|
88 |
+
prediction_df = gr.Dataframe(headers = ['preferedName', 'definition', 'irdi', 'score']) #value = predictions,
|
89 |
+
|
90 |
+
#Hinterlegt Search-Function für button "search"
|
91 |
+
search.click(fn=predict, inputs=[name_tx,description_tx,language_drop,classcode_tx,top_k_nu], outputs=prediction_df)
|
92 |
+
|
93 |
+
demo.launch(debug=True)
|