import json import datetime from processor.processor import Processor from constants.constants import * from search.search_by_id import Searcher from fastapi import FastAPI, File, UploadFile, HTTPException from fastapi.responses import FileResponse from preprocess.utils.common.utils import get_delimiter import uvicorn from pydantic import BaseModel from tmp.utils import update_products_csv from preprocess.utils.products.products import * import threading from processor.matching_score import compare_matching_with_correct from preprocess.utils.common.utils import find_full_word from rapidfuzz import fuzz, process processor=Processor(LONG_TYPES_LIST, SHORT_TYPES_LIST, SOUR, WINE_TYPES, GBS, GRAPES, OTHER_WORDS, #SOUR_MERGE_DICT, TYPES_WINES_DICT, COLOR_MERGE_DICT, COUNTRY_LIST) class match_request(BaseModel): items: str threshold: int items_first: int app = FastAPI() @app.post("/api/upload_products_csv") async def upload_products_csv(file: UploadFile, overwrite_existing: int, wait_for_finish: int = False): try: datadir = get_products_dir() tempfile = os.path.join(datadir, "products.csv_upload") with open(tempfile, 'wb') as f: f.write(file.file.read()) fullfn = os.path.join(datadir, "products.csv") update_products_csv(tempfile, fullfn, overwrite_existing) os.remove(tempfile) products_data = create_products_data(fullfn) if wait_for_finish: processor.process_products_full(products_data) else: thread = threading.Thread(target=processor.process_products_full, args=[products_data]) thread.start() except Exception as ex: raise HTTPException(status_code=500, detail='Error occurred: ' + str(ex)) finally: file.file.close() return {"Status": "Success", "Message": f"Successfully uploaded {file.filename}"} @app.post("/api/match") async def match(items_file: UploadFile, threshold: int, items_first: int, order_invariant_names_matching: int = False, thread_count: int = 8): #prods_file = os.path.join(get_products_dir(), "products.csv") #if not os.path.isfile(prods_file): #return {"Status": "Error", "ErrorDesc": "File 'Products.csv' not found"} try: items_fn = upload_client_file(items_file) if not threshold: threshold = 50 df, items, products = processor.process_new(items_fn, items_first, threshold, order_invariant_names_matching, thread_count) results_dir = get_results_dir() output_csv = "m1-" + str(threshold) + "-" + datetime.now().strftime('%y%m%d-%H%M%S') + ".csv" df.to_csv(os.path.join(results_dir, output_csv), sep='\t', index=False) except Exception as ex: raise HTTPException(status_code=500, detail='Error occurred: ' + str(ex)) return FileResponse(path=os.path.join(results_dir, output_csv), filename=output_csv, media_type='text/csv') def upload_client_file(file: UploadFile): try: tempdir = get_temp_dir() contents = file.file.read() fullfn = os.path.join(tempdir, file.filename) with open(fullfn, 'wb') as f: f.write(contents) except Exception as ex: raise HTTPException(status_code=500, detail='Error uploading file : ' + str(ex)) finally: file.file.close() return fullfn @app.post("/api/match_score") async def match_score(items_file: UploadFile, auto_matching_file: UploadFile, correct_matching_file: UploadFile): try: items_fn = upload_client_file(items_file) matching_fn = upload_client_file(auto_matching_file) correct_matching_fn = upload_client_file(correct_matching_file) basepath = get_temp_dir() results = compare_matching_with_correct(os.path.join(get_products_dir(), "products.csv"), items_fn, matching_fn, correct_matching_fn, processor, os.path.join(basepath, "mscore.csv"), os.path.join(basepath, "mscore_counts.json")) except Exception as ex: raise HTTPException(status_code=500, detail='Error occurred: ' + str(ex)) return json.dumps(results) @app.post("/api/verify_correct_matching") async def verify_correct_matching(correct_matching_file: UploadFile, items_file: UploadFile): try: correct_fn = upload_client_file(correct_matching_file) items_fn = upload_client_file(items_file) basepath = get_temp_dir() output_csv_filename = "mscore_to_correct.csv" output_csv_path = os.path.join(basepath, output_csv_filename) result_list = processor.verify_correct_matching(correct_fn, items_fn) results_df = pd.DataFrame(result_list) results_df.to_csv(output_csv_path) except Exception as ex: raise HTTPException(status_code=500, detail='Error occurred: ' + str(ex)) return FileResponse(path=output_csv_path, filename=output_csv_filename, media_type='text/csv') if __name__ == "__main__": uvicorn.run( app, host="0.0.0.0", port=8000, log_level="debug" )