Spaces:
Sleeping
Sleeping
File size: 6,164 Bytes
1f22e94 8ba15ea 36a20b5 a2ce106 36a20b5 8ba15ea 36a20b5 8ba15ea 36a20b5 8ba15ea 36a20b5 8ba15ea 36a20b5 8ba15ea 36a20b5 8ba15ea 36a20b5 8ba15ea 36a20b5 8ba15ea 36a20b5 8ba15ea 36a20b5 8ba15ea 36a20b5 8ba15ea 36a20b5 8ba15ea 36a20b5 8ba15ea |
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 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 |
import csv
import json
import os
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
import uvicorn
from pydantic import BaseModel
import pandas as pd
from tmp.utils import update_products_csv
from search.matching_judge import compare_matching_with_manual
'''compare_matching_with_manual("C:\\Projects (Mediterra)\\!TechLead\\WineMatching\\Data (New5)\\products.csv",
"C:\\Projects (Mediterra)\\!TechLead\\WineMatching\\Data (New4)\\ws-items-for-test.csv",
"C:\\Projects (Mediterra)\\!TechLead\\WineMatching\m1-50-250325-133739.csv",
"C:\\Projects (Mediterra)\\!TechLead\\WineMatching\\Data (New4)\\matching-20250318.csv")'''
processor=Processor(LONG_TYPES_LIST,
SHORT_TYPES_LIST,
SOUR,
WINE_TYPES,
GBS,
COLORS_FOR_TRIM,
GRAPES,
OTHER_WORDS,
SOUR_MERGE_DICT,
TYPES_WINES_DICT,
COLOR_MERGE_DICT)
searcher=Searcher()
class item_by_id(BaseModel):
result_file: str
id: str
class match_request(BaseModel):
items: str
threshold: int
items_first: int
def get_data_dir():
return "/home/user/app/_data/"
#return "_data"
def get_products_dir():
return os.path.join(get_data_dir(), "products")
def get_items_dir():
return os.path.join(get_data_dir(), "items")
def get_results_dir():
return os.path.join(get_data_dir(), "results")
app = FastAPI()
@app.get("/api/get_result_csv")
async def get_result_csv():
results = []
for file in os.listdir(get_results_dir()):
if file.endswith(".csv"):
results.append(file)
results_json = json.dumps(results)
return results_json
@app.post("/api/upload_result_csv")
async def upload_result_csv(file: UploadFile = File(...)):
try:
contents = file.file.read()
with open(os.path.join(get_results_dir(), file.filename), 'wb') as f:
f.write(contents)
except Exception:
raise HTTPException(status_code=500, detail='Something went wrong')
finally:
file.file.close()
return {"message": f"Successfully uploaded {file.filename}"}
@app.post("/api/upload_products_csv")
async def upload_products_csv(file: UploadFile, overwrite_existing: int):
try:
datadir = get_products_dir()
if not os.path.exists(datadir):
os.makedirs(datadir)
tempfile = os.path.join(datadir, "products.csv_upload")
contents = file.file.read()
with open(tempfile, 'wb') as f:
f.write(contents)
fullfn = os.path.join(datadir, "products.csv")
update_products_csv(tempfile, fullfn, overwrite_existing)
os.remove(tempfile)
except Exception:
raise HTTPException(status_code=500, detail='Something went wrong')
finally:
file.file.close()
return {"message": f"Successfully uploaded {file.filename}"}
#@app.post("/api/upload_items_csv")
def upload_items_csv(file: UploadFile):
try:
itemsdir = get_items_dir()
if not os.path.exists(itemsdir):
os.makedirs(itemsdir)
contents = file.file.read()
fullfn = os.path.join(itemsdir, file.filename)
with open(fullfn, 'wb') as f:
f.write(contents)
except Exception:
raise HTTPException(status_code=500, detail='Something went wrong')
finally:
file.file.close()
#return {"message": f"Successfully uploaded {file.filename}"}
return fullfn
@app.get("/api/get_items_csv")
async def get_items_csv():
itemsdir = get_items_dir()
results = []
for file in os.listdir(itemsdir):
if file.endswith(".csv"):
results.append(file)
results_json = json.dumps(results)
return results_json
@app.post("/api/match")
async def match(items_file: UploadFile, threshold: int, items_first: int):
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"}
items_fn = upload_items_csv(items_file)
#if len(r.items) == 0:
# return {"Status": "Error", "ErrorDesc": "Items file not specified"}
if not threshold:
threshold = 50
#items_fn = os.path.join(get_items_dir(), r.items)
#if not os.path.isfile(items_fn):
# return {"Status": "Error", "ErrorDesc": "Items file not found"}
row_items = pd.read_csv(items_fn, sep='\t')
os.remove(items_fn)
row_products = pd.read_csv(prods_file, sep='\t', on_bad_lines='skip')
df, items, products = processor.process(row_products, row_items, items_first, threshold)
results_dir = get_results_dir()
if not os.path.exists(results_dir):
os.makedirs(results_dir)
output_csv = "m1-" + str(threshold) + "-" + datetime.datetime.now().strftime('%y%m%d-%H%M%S') + ".csv"
df.to_csv(os.path.join(results_dir, output_csv), sep='\t', index=False)
return {"Status": "Success", "result_file" : output_csv}
@app.get("/api/get_matched_by_id")
async def get_matched_by_id(item: item_by_id):
fullfn = os.path.join(get_results_dir(), item.result_file)
if not os.path.isfile(fullfn):
return {"Status": "Error", "ErrorDesc": "Specified result CSV file not found"}
(df, is_alternative) = searcher.search(fullfn, int(item.id))
if df.empty:
return {"Status": "Success", "IsAlternative": False, "Data": ""}
return {"Status": "Success", "IsAlternative": is_alternative, "Data": df.to_json(orient='records')}
if __name__ == "__main__":
uvicorn.run(
app,
host="0.0.0.0",
port=8000,
log_level="debug"
) |