1- import os
21from contextlib import asynccontextmanager
32from typing import Annotated
43
4+ import confidence
55from asmtransformers .models .embedder import ASMEmbedder
66from fastapi import FastAPI , Request
77from fastapi .params import Body
88
9- from citatio .db import SQLiteDatabase
9+ from citatio .db import PostgreSQLDatabase
1010from citatio .models import ControlFlowGraph
1111
1212
1515
1616@asynccontextmanager
1717async def lifespan (app : FastAPI ):
18- model = os .environ .get ('CITATIO_MODEL' , DEFAULT_MODEL )
19- database = os .environ .get ('CITATIO_SQLITE_DATABASE' , ':memory:' )
18+ config = confidence .load_name ('citatio' )
19+ if not config .database :
20+ raise ValueError (f'missing database configuration, available: { config } ' )
2021
21- if database == ':memory:' :
22- # TODO: log warning that we're running in in-memory database
23- pass
22+ app .state .model = ASMEmbedder .from_pretrained (config .model or DEFAULT_MODEL )
2423
25- app .state .model = ASMEmbedder .from_pretrained (model )
26-
27- with SQLiteDatabase .from_name (database ) as database :
24+ async with await PostgreSQLDatabase .connect (** config .database ) as database :
2825 app .state .database = database
29-
3026 yield
3127
3228
3329app = FastAPI (lifespan = lifespan )
3430
3531
3632@app .post ('/api/v1/add' )
37- async def add_function ( # NB: function body isn't actually async, forcing it to run blocking on the event loop
33+ async def add_function (
3834 request : Request ,
3935 name : Annotated [str , Body ()],
4036 cfg : Annotated [ControlFlowGraph , Body ()],
@@ -43,15 +39,15 @@ async def add_function( # NB: function body isn't actually async, forcing it to
4339 binary_sha256 : Annotated [str , Body ()] = None ,
4440):
4541 embedding = request .app .state .model .encode (str (cfg ), architecture = architecture )
46- request .app .state .database .add_function (name , cfg , embedding , binary_name , binary_sha256 )
42+ await request .app .state .database .add_function (name , cfg , embedding , binary_name , binary_sha256 )
4743
4844
4945@app .post ('/api/v1/search' )
50- async def search_function ( # NB: function body isn't actually async, forcing it to run blocking on the event loop
46+ async def search_function (
5147 request : Request ,
5248 cfg : Annotated [ControlFlowGraph , Body ()],
5349 architecture : str = 'arm64' ,
5450 top_n : Annotated [int , Body ()] = 25 ,
5551):
5652 embedding = request .app .state .model .encode (str (cfg ), architecture = architecture )
57- return request .app .state .database .search_function (embedding , top_n )
53+ return await request .app .state .database .search_function (embedding , top_n )
0 commit comments