From b34f303b023688a77a3df4ce725546829e400a62 Mon Sep 17 00:00:00 2001 From: Krystian Wasilewski Date: Fri, 27 Jan 2023 18:43:12 +0100 Subject: [PATCH] api adjusted for changing params --- main.py | 53 ++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 46 insertions(+), 7 deletions(-) diff --git a/main.py b/main.py index 2a6a07e..14c8044 100644 --- a/main.py +++ b/main.py @@ -22,13 +22,25 @@ app = FastAPI() data = pd.DataFrame() mlb = MultiLabelBinarizer() -def inference(first: pandas.core.series.Series, second_id: str, df=None): + +def inference(first: pandas.core.series.Series, + second_id: str, + release_year_param='similar', + runtime_param='similar', + seasons_param='similar', + genres_param='same', + emotions_param='same', + df=None): if df is not None: second = df.loc[second_id] else: second = data.loc[second_id] - FS = fuzzy_system(release_year_param='similar', runtime_param='similar', seasons_param='similar', genres_param='same', emotions_param='same') + FS = fuzzy_system(release_year_param=release_year_param, + runtime_param=runtime_param, + seasons_param=seasons_param, + genres_param=genres_param, + emotions_param=emotions_param) year_diff = int(first['release_year'] - second['release_year']) FS.set_variable('RELEASE_YEAR', year_diff) @@ -51,10 +63,24 @@ def inference(first: pandas.core.series.Series, second_id: str, df=None): return second_id, FS.inference(['RECOMMENDATION'])['RECOMMENDATION'] -def process_dataframe(df, production): +def process_dataframe(df, + production, + release_year_param, + runtime_param, + seasons_param, + genres_param, + emotions_param + ): scores = [] for index, row in df.iterrows(): - scores.append(inference(production, str(index), df)) + scores.append(inference(production, + str(index), + release_year_param, + runtime_param, + seasons_param, + genres_param, + emotions_param, + df)) return scores @@ -111,7 +137,13 @@ def rec_score(first_id: str, second_id: str): @app.get('/recs/{production_id}') -async def recs(production_id: str, count: int | None = 5): +async def recs(production_id: str, + release_year_param: str | None = 'similar', + runtime_param: str | None = 'similar', + seasons_param: str | None = 'similar', + genres_param: str | None = 'same', + emotions_param: str | None = 'same', + count: int | None = 5): try: first = data.loc[production_id] except KeyError: @@ -122,14 +154,21 @@ async def recs(production_id: str, count: int | None = 5): cpus = multiprocessing.cpu_count() df_list = np.array_split(data, cpus) pool = Pool(cpus) - results = [pool.apply_async(process_dataframe, [df, first]) for df in df_list] + results = [pool.apply_async(process_dataframe, + [df, + first, + release_year_param, + runtime_param, + seasons_param, + genres_param, + emotions_param]) for df in df_list] for r in results: r.wait() for r in results: scores += r.get() print(f'time elapsed = {time.time() - time_start}') - scores = [idx[0] for idx in sorted(scores, key=lambda x: x[1], reverse=True)[:count+1]] + scores = [idx[0] for idx in sorted(scores, key=lambda x: x[1], reverse=True)[:count + 1]] scores.remove(production_id) return { 'id': scores