From a0289f23d966438ef987ce7430fe98efc9c04000 Mon Sep 17 00:00:00 2001 From: TimAdams84 Date: Fri, 22 Mar 2024 11:54:39 +0100 Subject: [PATCH] Feat: Add endpoints to create concepts with mapping and update visualization --- index/api/routes.py | 23 +++++++++++++++++++++++ index/db/model.py | 2 +- 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/index/api/routes.py b/index/api/routes.py index fcf5325..ed9ffa2 100644 --- a/index/api/routes.py +++ b/index/api/routes.py @@ -75,6 +75,13 @@ def serve_visualization(): return db_plot_html +@app.patch("/visualization", tags=["visualization"]) +def update_visualization(): + global db_plot_html + db_plot_html = get_html_plot_for_current_database_state(repository) + return {"message": "DB visualization plot has been updated successfully"} + + @app.put("/terminologies/{id}", tags=["terminologies"]) async def create_or_update_terminology(id: str, name: str): try: @@ -99,6 +106,22 @@ async def create_or_update_concept(id: str, terminology_id: str, name: str): raise HTTPException(status_code=400, detail=f"Failed to create or update concept: {str(e)}") +@app.put("/concepts/{id}/mappings", tags=["concepts", "mappings"]) +async def create_concept_and_attach_mapping(id: str, terminology_id: str, concept_name: str, text: str): + try: + terminology = repository.session.query(Terminology).filter(Terminology.id == terminology_id).first() + if not terminology: + raise HTTPException(status_code=404, detail=f"Terminology with id {terminology_id} not found") + concept = Concept(terminology=terminology, name=concept_name, id=id) + repository.store(concept) + embedding = embedding_model.get_embedding(text) + mapping = Mapping(concept=concept, text=text, embedding=embedding) + repository.store(mapping) + return {"message": f"Concept {id} created or updated successfully"} + except Exception as e: + raise HTTPException(status_code=400, detail=f"Failed to create or update concept: {str(e)}") + + @app.put("/mappings/", tags=["mappings"]) async def create_or_update_mapping(concept_id: str, text: str): try: diff --git a/index/db/model.py b/index/db/model.py index 7ef989d..c495703 100644 --- a/index/db/model.py +++ b/index/db/model.py @@ -47,7 +47,7 @@ def __init__(self, concept: Concept, text: str, embedding: list) -> object: self.embedding_json = json.dumps(embedding) # Store embedding as JSON def __str__(self): - return f"{self.concept_id} : {self.concept.name} | {self.text}" + return f"{self.concept.terminology.name} > {self.concept_id} : {self.concept.name} | {self.text}" @property def embedding(self):