File size: 3,741 Bytes
c2e327f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3545a80
c2e327f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
from sentence_transformers import SentenceTransformer, util

# from transformers import MBartForConditionalGeneration, MBart50TokenizerFast
import time
import os
import json
import pandas as pd
import numpy as np
import category_encoders as ce
import string
import pickle
import tqdm.autonotebook
from fastapi import FastAPI, Request, UploadFile, File
from joblib import dump, load
from pydantic import BaseModel
import sys
from database_build import index_corpus
from predict_different_aas import ask_database
from predict_one_aas import query_specific_aas
from typing import Any, Dict, AnyStr, List, Union
import chromadb
from chromadb.config import Settings
from typing import Union

app = FastAPI(title="Interface Semantic Matching")

JSONObject = Dict[AnyStr, Any]
JSONArray = List[Any]
JSONStructure = Union[JSONArray, JSONObject]


class submodelElement(BaseModel):
    datatype: str 
    definition: str
    name: str
    semantic_id: str
    unit: str 
    return_matches: int
    aas_id: str
    number_aas_returned: int

@app.on_event("startup")
def load_hf_model():
    global model
    # Altes Modell
    # model = SentenceTransformer('mboth/distil-eng-quora-sentence')

    # Fine Tuned Modell
    model = SentenceTransformer("gart-labor/eng-distilBERT-se-eclass")

    # global model_translate
    # model_translate = MBartForConditionalGeneration.from_pretrained("facebook/mbart-large-50-many-to-many-mmt")
    # global tokenizer_translate
    # tokenizer_translate = MBart50TokenizerFast.from_pretrained("facebook/mbart-large-50-many-to-many-mmt")

    with open("app/metadata.pickle", "rb") as handle:
        global metalabel
        metalabel = pickle.load(handle)
    global client_chroma
    client_chroma = chromadb.Client(
        Settings(
            chroma_api_impl="rest",
            # chroma_server_host muss angepasst werden nach jedem Neustart AWS
            chroma_server_host="3.67.86.197",
            chroma_server_http_port=8000,
        )
    )


@app.post("/PostAssetAdministrationShellEmbeddings")
async def index_aas(aas: UploadFile = File(...)):
    data = json.load(aas.file)
    print(type(data))
    # aas = new_file
    #aas, submodels, conceptDescriptions, assets, aas_df, collection, aas_name= index_corpus(data, model, metalabel, client_chroma)
    collection = index_corpus(data, model, metalabel, client_chroma)
    ready = 'AAS ready'
    return ready


@app.post("/GetSubmodelElementsFromDifferentAASBySemanticIdAndSemanticInformation")
def predict_different_aas(name: str, definition: str, number_aas_returned: Union[int, None] = 1, semantic_id: Union[str, None] = "NaN", unit: Union[str, None] = "NaN", datatype: Union[str, None] = "NaN"):
    collections = client_chroma.list_collections()
    query = {
        "Name": name,
        "Definition": definition,
        "Unit": unit,
        "Datatype": datatype,
        "SemanticId": semantic_id,
        "NumberAASReturned": number_aas_returned
    }
    results = ask_database(query, metalabel, model, collections, client_chroma)

    return results

@app.post("/GetSubmodelElementsFromSpecificAASBySemanticIdAndSemanticInformation")
def predict_specific_aas(name: str, definition: str, aas_id: str, return_matches:  Union[int, None] = 2,  semantic_id: Union[str, None] = "NaN", unit: Union[str, None] = "NaN", datatype: Union[str, None] = "NaN"):
    collections = client_chroma.list_collections()
    query = {
        "Name": name,
        "Definition": definition,
        "Unit": unit,
        "Datatype": datatype,
        "SemanticId": semantic_id,
        "ReturnMatches": return_matches,
        "AASId": aas_id,
    }
    result = query_specific_aas(query, metalabel, model, collections, client_chroma)

    return result