From 73c78b126c09e80c88c9fe0efccbb5dcaf7f4e10 Mon Sep 17 00:00:00 2001 From: Efraim Feinstein Date: Wed, 18 Dec 2024 14:06:35 -0800 Subject: [PATCH] change port and update model --- .../player-service-model/Dockerfile | 2 +- .../player-service-model/README.md | 35 +++++++++++-------- .../a4a_model/features_db.csv | 2 +- .../player-service-model/a4a_model/player.csv | 2 +- .../player-service-model/a4a_model/server.py | 17 +++++---- .../a4a_model/train.ipynb | 18 +++++----- 6 files changed, 44 insertions(+), 32 deletions(-) diff --git a/player-service-backend/player-service-model/Dockerfile b/player-service-backend/player-service-model/Dockerfile index b595142c..cc899acd 100644 --- a/player-service-backend/player-service-model/Dockerfile +++ b/player-service-backend/player-service-model/Dockerfile @@ -14,5 +14,5 @@ WORKDIR /usr/local/a4a_model RUN poetry config virtualenvs.create false RUN poetry install WORKDIR /usr/local/a4a_model/a4a_model -EXPOSE 5000 +EXPOSE 8657 ENTRYPOINT ["python", "server.py"] \ No newline at end of file diff --git a/player-service-backend/player-service-model/README.md b/player-service-backend/player-service-model/README.md index 3de5c8f6..f30a4ccb 100644 --- a/player-service-backend/player-service-model/README.md +++ b/player-service-backend/player-service-model/README.md @@ -1,30 +1,37 @@ # Player Service Model -This is a thin model wrapper container based on `Player.csv` data. +This is a thin model wrapper container based on `Player.csv` data. -To build and run: +To build and run using docker: ```shell docker build -t a4a_model . -docker run -d -p 5000:5000 a4a_model +docker run -d -p 8657:8657 a4a_model ``` +OR, alternatively, using podman: +```shell +podman build -t a4a_model . +podman run --rm -p 8657:8657 -it localhost/a4a_model:latest +``` + -This will expose port 5000. +This will expose the model on port 8657. -To send an inference request to the AI model using a seed from the database: +To send an inference request to the AI model using player names: ```shell -$ curl -H "Content-type: application/json" -d '{"seed_id":"abbotji01","team_size":10}' http://127.0.0.1:5000/team/generate -{"seed_id":"abbotji01","team_size":10,"member_ids":["abbotji01","combspa01","maurero01","cummijo01","flemida01","macdobo01","eddych01","morriha02","mcgrifr01","blossgr01"]} +$ curl -H "Content-type: application/json" -d '{"seed_id":"abbotji01","team_size":10}' http://127.0.0.1:8657/team/generate +{"seed_id":"abbotji01","prediction_id":"38f5f02f-b1be-4282-8d0e-865b3995d50a","team_size":10,"member_ids":["abbotji01","combspa01","maurero01","cummijo01","flemida01","macdobo01","eddych01","morriha02","mcgrifr01","blossgr01"]} ``` -To send an inference request to the AI model using a set of features: +To send an inference request to the AI model using features: ```shell -$ curl -H "Content-type: application/json" -d '{"features":{"birth_year":1970, "height":70, "weight":120, "bats":"R", "throws":"L"},"team_size":10}' http://127.0.0.1:5000/team/generate +To send an inference request to the AI model using a set of features: + ```shell +$ curl -H "Content-type: application/json" -d '{"features":{"birth_year":1970, "height":70, "weight":120, "bats":"R", "throws":"L"},"team_size":10}' http://127.0.0.1:8657/team/generate {"seed_id":null,"prediction_id":"ddedf511-2e68-4ab5-87c5-c77b8d15eb23","team_size":10,"member_ids":["roblevi01","deverra01","goharlu01","albieoz01","barrefr02","urenari01","uriasju01","verdual01","mejiafr01","sierrma01"]} ``` -Note that the features are optional, and features that are not provided will be assumed to be the mean values in the training dataset. The unit of weight is pounds. The unit of height is inches. Batting and throwing may be right handed (`R`), left handed (`L`) or no preference (`N`). -To send feedback about the recommendations for a prior seed: +To send feedback about the recommendation of a prior seed: ```shell -$ curl -H "Content-type: application/json" -d '{"seed_id":"abbotji01","member_id":"maurero01","feedback":-1}' http://127.0.0.1:5000/team/feedback -{"seed_id":"abbotji01","member_id":"maurero01","accepted":true} -``` +$ curl -H "Content-type: application/json" -d '{"seed_id":"abbotji01","member_id":"maurero01","feedback":-1,"prediction_id":"38f5f02f-b1be-4282-8d0e-865b3995d50a"}' http://127.0.0.1:8657/team/feedback +{"seed_id":"abbotji01","member_id":"maurero01","accepted":true,"prediction_id":"38f5f02f-b1be-4282-8d0e-865b3995d50a"} +``` \ No newline at end of file diff --git a/player-service-backend/player-service-model/a4a_model/features_db.csv b/player-service-backend/player-service-model/a4a_model/features_db.csv index b224fbb8..50a56f6a 100644 --- a/player-service-backend/player-service-model/a4a_model/features_db.csv +++ b/player-service-backend/player-service-model/a4a_model/features_db.csv @@ -1,4 +1,4 @@ -playerId,birthYear,birthMonth,birthDay,birthCountry,birthState,birthCity,deathYear,deathMonth,deathDay,deathCountry,deathState,deathCity,nameFirst,nameLast,nameGiven,weight,height,bats,throws,debut,finalGame,retroID,bbrefID,birthFraction,birthZ,weightZ,heightZ,batsN,throwsN +playerID,birthYear,birthMonth,birthDay,birthCountry,birthState,birthCity,deathYear,deathMonth,deathDay,deathCountry,deathState,deathCity,nameFirst,nameLast,nameGiven,weight,height,bats,throws,debut,finalGame,retroID,bbrefID,birthFraction,birthZ,weightZ,heightZ,batsN,throwsN aardsda01,1981.0,12.0,27.0,USA,CO,Denver,,,,,,,David,Aardsma,David Allan,215.0,75.0,R,R,2004-04-06,2015-08-23,aardd001,aardsda01,1.1765707013622462,1.1765707013622462,1.3135157142331944,1.0397594675507622,1.0,1.0 aaronha01,1934.0,2.0,5.0,USA,AL,Mobile,,,,,,,Hank,Aaron,Henry Louis,180.0,72.0,R,R,1954-04-13,1976-10-03,aaroh101,aaronha01,-0.004123827274949573,-0.004123827274949573,-0.3040341982765978,-0.11141752422902394,1.0,1.0 aaronto01,1939.0,8.0,5.0,USA,AL,Mobile,1984.0,8.0,16.0,USA,GA,Atlanta,Tommie,Aaron,Tommie Lee,190.0,75.0,R,R,1962-04-10,1971-09-26,aarot101,aaronto01,0.12958557370318188,0.12958557370318188,0.15812291958334285,1.0397594675507622,1.0,1.0 diff --git a/player-service-backend/player-service-model/a4a_model/player.csv b/player-service-backend/player-service-model/a4a_model/player.csv index d4c8c225..8bc766b3 100644 --- a/player-service-backend/player-service-model/a4a_model/player.csv +++ b/player-service-backend/player-service-model/a4a_model/player.csv @@ -1,4 +1,4 @@ -playerId,birthYear,birthMonth,birthDay,birthCountry,birthState,birthCity,deathYear,deathMonth,deathDay,deathCountry,deathState,deathCity,nameFirst,nameLast,nameGiven,weight,height,bats,throws,debut,finalGame,retroID,bbrefID +playerID,birthYear,birthMonth,birthDay,birthCountry,birthState,birthCity,deathYear,deathMonth,deathDay,deathCountry,deathState,deathCity,nameFirst,nameLast,nameGiven,weight,height,bats,throws,debut,finalGame,retroID,bbrefID aardsda01,1981,12,27,USA,CO,Denver,,,,,,,David,Aardsma,David Allan,215,75,R,R,2004-04-06,2015-08-23,aardd001,aardsda01 aaronha01,1934,2,5,USA,AL,Mobile,,,,,,,Hank,Aaron,Henry Louis,180,72,R,R,1954-04-13,1976-10-03,aaroh101,aaronha01 aaronto01,1939,8,5,USA,AL,Mobile,1984,8,16,USA,GA,Atlanta,Tommie,Aaron,Tommie Lee,190,75,R,R,1962-04-10,1971-09-26,aarot101,aaronto01 diff --git a/player-service-backend/player-service-model/a4a_model/server.py b/player-service-backend/player-service-model/a4a_model/server.py index 8b1f7940..4792627e 100644 --- a/player-service-backend/player-service-model/a4a_model/server.py +++ b/player-service-backend/player-service-model/a4a_model/server.py @@ -13,7 +13,7 @@ nn_model = joblib.load("team_model.joblib") player_db = pd.read_csv("features_db.csv") -all_players = set(player_db["playerId"]) +all_players = set(player_db["playerID"]) features = ["birthZ", "heightZ", "weightZ", "batsN", "throwsN"] @dataclasses.dataclass @@ -64,6 +64,8 @@ class TeamFeedbackInput(BaseModel): seed_id: str member_id: str feedback: Literal[-1, 1] # expect -1 or 1 + prediction_id: str + class TeamFeedbackOutput(BaseModel): seed_id: str @@ -87,7 +89,7 @@ def generate_team(body: TeamGenerateInput) -> TeamGenerateOutput: seed = body.seed_id if body.seed_id: try: - seed_features = player_db[player_db.playerId == seed][features] + seed_features = player_db[player_db.playerID == seed][features] except: raise TeamException(f"The seed {body.seed_id} has no associated player") elif body.features: @@ -106,7 +108,7 @@ def generate_team(body: TeamGenerateInput) -> TeamGenerateOutput: # run exclusions print(f"{seed=} {seed_features=} has {exclude_db.get(seed)=}") member_ids = [m - for m in list(player_db.take(member_indices[0])["playerId"]) + for m in list(player_db.take(member_indices[0])["playerID"]) if m not in exclude_db.get(seed, set())] print(f"{member_ids=}") @@ -123,6 +125,7 @@ def generate_team(body: TeamGenerateInput) -> TeamGenerateOutput: def team_feedback(body: TeamFeedbackInput) -> TeamFeedbackOutput: seed_id = body.seed_id member_id = body.member_id + prediction_id = body.prediction_id accepted = True if seed_id not in all_players: accepted = False @@ -135,7 +138,8 @@ def team_feedback(body: TeamFeedbackInput) -> TeamFeedbackOutput: return TeamFeedbackOutput( seed_id=seed_id, member_id=member_id, - accepted=accepted + accepted=accepted, + prediction_id=prediction_id or str(uuid.uuid4()), ) @@ -154,7 +158,7 @@ class LLMFeedbackOutput(BaseModel): user_prompt: str @app.route('/llm/generate', methods=['POST']) -@validate +@validate() def generate_description(body: LLMInput) -> LLMOutput: data = request.json # Implement logic to generate a description based on the provided data @@ -163,6 +167,7 @@ def generate_description(body: LLMInput) -> LLMOutput: @app.route('/llm/feedback', methods=['POST']) +@validate() def description_feedback(body: LLMFeedbackInput) -> LLMFeedbackOutput: data = request.json # Implement logic to process feedback for a description @@ -171,4 +176,4 @@ def description_feedback(body: LLMFeedbackInput) -> LLMFeedbackOutput: if __name__ == '__main__': - app.run(host="0.0.0.0", debug=True) + app.run(host="0.0.0.0", port=8657, debug=True) diff --git a/player-service-backend/player-service-model/a4a_model/train.ipynb b/player-service-backend/player-service-model/a4a_model/train.ipynb index 99546947..4428aade 100644 --- a/player-service-backend/player-service-model/a4a_model/train.ipynb +++ b/player-service-backend/player-service-model/a4a_model/train.ipynb @@ -41,7 +41,7 @@ { "data": { "text/plain": [ - " playerId birthYear birthMonth birthDay birthCountry birthState \\\n", + " playerID birthYear birthMonth birthDay birthCountry birthState \\\n", "0 aardsda01 1981.0 12.0 27.0 USA CO \n", "1 aaronha01 1934.0 2.0 5.0 USA AL \n", "2 aaronto01 1939.0 8.0 5.0 USA AL \n", @@ -83,7 +83,7 @@ " \n", " \n", " \n", - " playerId\n", + " playerID\n", " birthYear\n", " birthMonth\n", " birthDay\n", @@ -759,7 +759,7 @@ "cell_type": "code", "source": [ "def get_nearest_neighbors(id: str, n=25):\n", - " seed = df[df[\"playerId\"] == id][features]\n", + " seed = df[df[\"playerID\"] == id][features]\n", " neighbor_indices = nn_model.kneighbors(seed, n, return_distance=False)\n", " return df.take(neighbor_indices[0])[\"id\"]" ], @@ -781,7 +781,7 @@ { "data": { "text/plain": [ - " playerId birthYear birthMonth birthDay birthCountry birthState \\\n", + " playerID birthYear birthMonth birthDay birthCountry birthState \\\n", "0 aardsda01 1981.0 12.0 27.0 USA CO \n", "1 aaronha01 1934.0 2.0 5.0 USA AL \n", "2 aaronto01 1939.0 8.0 5.0 USA AL \n", @@ -830,7 +830,7 @@ " \n", " \n", " \n", - " playerId\n", + " playerID\n", " birthYear\n", " birthMonth\n", " birthDay\n", @@ -996,7 +996,7 @@ }, "cell_type": "code", "source": [ - "seed = df[df[\"playerId\"] == \"aaronha01\"][features]\n", + "seed = df[df[\"playerID\"] == \"aaronha01\"][features]\n", "neighbor_indices = nn_model.kneighbors(seed, 25, return_distance=False)\n" ], "id": "6d35d7781406b91f", @@ -1017,7 +1017,7 @@ { "data": { "text/plain": [ - " playerId birthYear birthMonth birthDay birthCountry birthState \\\n", + " playerID birthYear birthMonth birthDay birthCountry birthState \\\n", "1 aaronha01 1934.0 2.0 5.0 USA AL \n", "11649 mendomi01 1933.0 11.0 16.0 Cuba NaN \n", "4059 davieje01 1933.0 2.0 10.0 USA MI \n", @@ -1146,7 +1146,7 @@ " \n", " \n", " \n", - " playerId\n", + " playerID\n", " birthYear\n", " birthMonth\n", " birthDay\n", @@ -1832,7 +1832,7 @@ } }, "cell_type": "code", - "source": "\"aardsda01\" in list(df[\"playerId\"])", + "source": "\"aardsda01\" in list(df[\"playerID\"])", "id": "db2f862e8f782268", "outputs": [ {