Spaces:
Build error
Build error
File size: 5,451 Bytes
3cc4e3f 82d614d 3cc4e3f 606ca5f cb92a0f c5b2790 606ca5f 3cc4e3f 95c9287 3cc4e3f d659798 c5b2790 3cc4e3f 606ca5f 3cc4e3f 606ca5f 3cc4e3f 606ca5f 3cc4e3f d659798 3cc4e3f eedd5dc 606ca5f 3cc4e3f 82d614d cb92a0f 3cc4e3f 82d614d 3cc4e3f d4bade4 3cc4e3f 82d614d 606ca5f 82d614d 606ca5f 3cc4e3f 82d614d 3cc4e3f cb92a0f eedd5dc cb92a0f 3cc4e3f cb92a0f 3cc4e3f c5b2790 d4bade4 3cc4e3f | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 | 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"
) |