Skip to content

Commit

Permalink
Feat: Extend API and add Unit Test
Browse files Browse the repository at this point in the history
  • Loading branch information
tiadams committed Feb 22, 2024
1 parent f067d8e commit 48abe71
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 285 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ pip install -r requirements.txt
Run the Backend API on port 5000:

```bash
uvicorn main:app --reload --port 5000
uvicorn index.api.routes:app --reload --port 5000
```

### Run the Backend via Docker
Expand Down
49 changes: 47 additions & 2 deletions index/api/routes.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import json
import logging
from typing import Dict

from fastapi import FastAPI
from fastapi import FastAPI, HTTPException
from starlette.middleware.cors import CORSMiddleware
from starlette.responses import RedirectResponse

from index.db.model import Terminology, Concept, Mapping
from index.repository.sqllite import SQLLiteRepository
from index.embedding import MPNetAdapter

Expand Down Expand Up @@ -48,9 +51,51 @@ def get_current_version():
return app.version


@app.put("/terminologies/{id}", tags=["terminologies"])
async def create_or_update_terminology(id: str, name: str):
try:
terminology = Terminology(name=name, id=id)
repository.store(terminology)
return {"message": f"Terminology {id} created or updated successfully"}
except Exception as e:
raise HTTPException(status_code=400, detail=f"Failed to create or update terminology: {str(e)}")


@app.put("/concepts/{id}", tags=["concepts"])
async def create_or_update_concept(id: str, terminology_id: str, name: 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=name, id=id)
repository.store(concept)
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/{id}", tags=["mappings"])
async def create_or_update_mapping(id: str, concept_id: str, text: str):
try:
concept = repository.session.query(Concept).filter(Concept.id == concept_id).first()
if not concept:
raise HTTPException(status_code=404, detail=f"Concept with id {concept_id} not found")
embedding = embedding_model.get_embedding(text)
# Convert embedding from numpy array to list
embedding_list = embedding.tolist()
print(embedding_list)
mapping = Mapping(concept=concept, text=text, embedding=json.dumps(embedding_list))
repository.store(mapping)
return {"message": f"Mapping {id} created or updated successfully"}
except Exception as e:
raise HTTPException(status_code=400, detail=f"Failed to create or update mapping: {str(e)}")


@app.post("/mappings", tags=["mappings"])
async def get_closest_mappings_for_text(text: str):
embedding = embedding_model.get_embedding(text)
embedding = embedding_model.get_embedding(text).tolist()
print(embedding)
closest_mappings, similarities = repository.get_closest_mappings(embedding)
response_data = []
for mapping, similarity in zip(closest_mappings, similarities):
Expand Down
3 changes: 3 additions & 0 deletions index/db/model.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import json

import numpy as np
from sqlalchemy import Column, ForeignKey, Integer, String, Text
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import relationship
Expand Down Expand Up @@ -41,6 +42,8 @@ class Mapping(Base):
def __init__(self, concept: Concept, text: str, embedding: list):
self.concept = concept
self.text = text
if isinstance(embedding, np.ndarray):
embedding = embedding.tolist()
self.embedding_json = json.dumps(embedding) # Store embedding as JSON

@property
Expand Down
281 changes: 0 additions & 281 deletions index/main.py

This file was deleted.

2 changes: 1 addition & 1 deletion index/repository/sqllite.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def get_closest_mappings(self, embedding, limit=5):
)

distances_and_mappings = distances_and_mappings_query.all()

print(distances_and_mappings)
# Sort results based on distances
sorted_results = sorted(distances_and_mappings, key=lambda x: x[1])

Expand Down
Loading

0 comments on commit 48abe71

Please sign in to comment.