Skip to content

Commit 9ab7544

Browse files
Accept database param from frontend to connect particular DB connection
1 parent 013e76a commit 9ab7544

File tree

2 files changed

+24
-25
lines changed

2 files changed

+24
-25
lines changed

backend/score.py

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,12 @@ def healthy_condition():
1111
output = {"healthy": True}
1212
return output
1313

14-
1514
def healthy():
1615
return True
1716

18-
1917
def sick():
2018
return False
2119

22-
2320
app = FastAPI()
2421

2522
app.add_middleware(
@@ -31,10 +28,9 @@ def sick():
3128
)
3229
app.add_api_route("/health", health([healthy_condition, healthy]))
3330

34-
3531
@app.post("/sources")
3632
async def create_source_knowledge_graph(
37-
uri=Form(), userName=Form(), password=Form(), file: UploadFile = File(...), model=Form()
33+
uri=Form(), database=Form(), userName=Form(), password=Form(), file: UploadFile = File(...), model=Form()
3834
):
3935
"""
4036
Calls 'create_source_node_graph' function in a new thread to create
@@ -50,13 +46,14 @@ async def create_source_knowledge_graph(
5046
'Source' Node creation in Neo4j database
5147
"""
5248
result = await asyncio.to_thread(
53-
create_source_node_graph_local_file, uri, userName, password, file, model
49+
create_source_node_graph_local_file, uri, database, userName, password, file, model
5450
)
5551
return result
5652

5753
@app.post("/url/scan")
5854
async def create_source_knowledge_graph_url(
5955
uri=Form(),
56+
database=Form(),
6057
userName=Form(),
6158
password=Form(),
6259
source_url=Form(),
@@ -67,13 +64,14 @@ async def create_source_knowledge_graph_url(
6764
model=Form(None)
6865
):
6966
return create_source_node_graph_url(
70-
uri, userName, password, source_url, max_limit, query_source, model, aws_access_key_id, aws_secret_access_key
67+
uri, database, userName, password, source_url, max_limit, query_source, model, aws_access_key_id, aws_secret_access_key
7168
)
7269

7370

7471
@app.post("/extract")
7572
async def extract_knowledge_graph_from_file(
7673
uri=Form(),
74+
database=Form(),
7775
userName=Form(),
7876
password=Form(),
7977
file: UploadFile = File(None),
@@ -103,6 +101,7 @@ async def extract_knowledge_graph_from_file(
103101
return await asyncio.to_thread(
104102
extract_graph_from_file,
105103
uri,
104+
database,
106105
userName,
107106
password,
108107
model,
@@ -115,6 +114,7 @@ async def extract_knowledge_graph_from_file(
115114
return await asyncio.to_thread(
116115
extract_graph_from_file,
117116
uri,
117+
database,
118118
userName,
119119
password,
120120
model,
@@ -129,11 +129,11 @@ async def extract_knowledge_graph_from_file(
129129

130130

131131
@app.get("/sources_list")
132-
async def get_source_list(uri=Form(),userName=Form(),password=Form()):
132+
async def get_source_list(uri=Form(),database=Form(),userName=Form(),password=Form()):
133133
"""
134134
Calls 'get_source_list_from_graph' which returns list of sources which alreday exist in databse
135135
"""
136-
result = await asyncio.to_thread(get_source_list_from_graph,uri,userName,password)
136+
result = await asyncio.to_thread(get_source_list_from_graph,uri,database,userName,password)
137137
return result
138138

139139
@app.post("/update_similarity_graph")
@@ -145,7 +145,5 @@ async def update_similarity_graph():
145145
result = await asyncio.to_thread(update_graph)
146146
return result
147147

148-
149-
150148
if __name__ == "__main__":
151-
uvicorn.run(app)
149+
uvicorn.run(app)

backend/src/main.py

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
warnings.filterwarnings("ignore")
2323

2424
load_dotenv()
25-
logging.basicConfig(format='%(asctime)s - %(message)s',level='INFO')
25+
logging.basicConfig(format='%(asctime)s - %(message)s',level='DEBUG')
2626
# from langchain.document_loaders import S3FileLoader
2727

2828
def update_exception_db(graph_obj,file_name,exp_msg):
@@ -47,12 +47,13 @@ def create_source_node(graph_obj,file_name,file_size,file_type,source,model,url=
4747
# graph_obj.query('MERGE(d:Document {'+source_node.format(file_name.split('/')[-1])+'}) '+update_node_prop.format(job_status,error_message))
4848
raise Exception(str(e))
4949

50-
def create_source_node_graph_local_file(uri, userName, password, file, model):
50+
def create_source_node_graph_local_file(uri, db_name, userName, password, file, model):
5151
"""
5252
Creates a source node in Neo4jGraph and sets properties.
5353
5454
Args:
5555
uri: URI of Graph Service to connect to
56+
db_name: database name to connect
5657
userName: Username to connect to Graph Service with ( default : None )
5758
password: Password to connect to Graph Service with ( default : None )
5859
file: File object with information about file to be added
@@ -65,7 +66,7 @@ def create_source_node_graph_local_file(uri, userName, password, file, model):
6566
file_size = file.size
6667
file_name = file.filename
6768
source = 'local file'
68-
graph = Neo4jGraph(url=uri, username=userName, password=password)
69+
graph = Neo4jGraph(url=uri,database=db_name, username=userName, password=password)
6970

7071
create_source_node(graph,file_name,file_size,file_type,source,model)
7172
return create_api_response("Success",data="Source Node created successfully",file_source=source)
@@ -129,12 +130,13 @@ def check_url_source(url):
129130
except Exception as e:
130131
raise e
131132

132-
def create_source_node_graph_url(uri, userName, password, source_url, max_limit, wiki_query,model, aws_access_key_id=None,aws_secret_access_key=None):
133+
def create_source_node_graph_url(uri, db_name, userName, password, source_url, max_limit, wiki_query,model, aws_access_key_id=None,aws_secret_access_key=None):
133134
"""
134135
Creates a source node in Neo4jGraph and sets properties.
135136
136137
Args:
137138
uri: URI of Graph Service to connect to
139+
db_name: db_name is database name to connect to graph db
138140
userName: Username to connect to Graph Service with ( default : None )
139141
password: Password to connect to Graph Service with ( default : None )
140142
s3_url: s3 url for the bucket to fetch pdf files from
@@ -144,7 +146,7 @@ def create_source_node_graph_url(uri, userName, password, source_url, max_limit,
144146
Success or Failed message of node creation
145147
"""
146148
try:
147-
graph = Neo4jGraph(url=uri, username=userName, password=password)
149+
graph = Neo4jGraph(url=uri, database=db_name, username=userName, password=password)
148150
source_type = check_url_source(source_url)
149151
print(f"source type URL:{source_type}")
150152
if source_type == "s3 bucket":
@@ -239,12 +241,13 @@ def wiki_loader(wiki_query,max_sources,max_wiki_pages=2):
239241

240242

241243

242-
def extract_graph_from_file(uri, userName, password, model, file=None,source_url=None,aws_access_key_id=None,aws_secret_access_key=None,wiki_query=None,max_sources=None,max_wiki_pages=2):
244+
def extract_graph_from_file(uri, db_name, userName, password, model, file=None,source_url=None,aws_access_key_id=None,aws_secret_access_key=None,wiki_query=None,max_sources=None,max_wiki_pages=2):
243245
"""
244246
Extracts a Neo4jGraph from a PDF file based on the model.
245247
246248
Args:
247249
uri: URI of the graph to extract
250+
db_name : db_name is database name to connect graph db
248251
userName: Username to use for graph creation ( if None will use username from config file )
249252
password: Password to use for graph creation ( if None will use password from config file )
250253
file: File object containing the PDF file to be used
@@ -257,7 +260,7 @@ def extract_graph_from_file(uri, userName, password, model, file=None,source_url
257260
# logging.info(f"extract_graph_from_file called for file:{file.filename}")
258261
try:
259262
start_time = datetime.now()
260-
graph = Neo4jGraph(url=uri, username=userName, password=password)
263+
graph = Neo4jGraph(url=uri, database=db_name, username=userName, password=password)
261264
source_node = "fileName: '{}'"
262265
if file!=None:
263266
file_name, file_key, pages = get_documents_from_file(file)
@@ -343,12 +346,9 @@ def extract_graph_from_file(uri, userName, password, model, file=None,source_url
343346
return create_api_response("Success",data=output)
344347

345348
except Exception as e:
346-
# job_status = "Failed"
347349
error_message = str(e)
348350
logging.info(f"file name in exception: {file_name}")
349-
# update_node_prop = 'SET d.status = "{}", d.errorMessage = "{}"'
350351
update_exception_db(graph,file_name,error_message)
351-
# graph.query('MERGE(d:Document {'+source_node.format(file_name)+'}) '+update_node_prop.format(job_status,error_message))
352352
logging.exception(f'Exception Stack trace: {error_message}')
353353
return create_api_response(job_status,error=error_message)
354354

@@ -393,10 +393,11 @@ def get_documents_from_youtube(url):
393393
print("Youtube pages = ",pages)
394394
return file_name, file_key, pages
395395

396-
def get_source_list_from_graph(uri,userName,password):
396+
def get_source_list_from_graph(uri,db_name,userName,password):
397397
"""
398398
Args:
399399
uri: URI of the graph to extract
400+
db_name: db_name is database name to connect to graph db
400401
userName: Username to use for graph creation ( if None will use username from config file )
401402
password: Password to use for graph creation ( if None will use password from config file )
402403
file: File object containing the PDF file to be used
@@ -407,15 +408,15 @@ def get_source_list_from_graph(uri,userName,password):
407408
"""
408409
logging.info("Get existing files list from graph")
409410
try:
410-
graph = Neo4jGraph(url=uri, username=userName, password=password)
411+
graph = Neo4jGraph(url=uri, database=db_name, username=userName, password=password)
411412
query = "MATCH(d:Document) RETURN d ORDER BY d.updatedAt DESC"
412413
result = graph.query(query)
413414
list_of_json_objects = [entry['d'] for entry in result]
414415
return create_api_response("Success",data=list_of_json_objects)
415416
except Exception as e:
416417
job_status = "Failed"
417418
error_message = str(e)
418-
logging.exception('Exception')
419+
logging.exception(f'Exception:{error_message}')
419420
return create_api_response(job_status,error=error_message)
420421

421422
def update_graph(graph):

0 commit comments

Comments
 (0)