File size: 1,756 Bytes
ae604b6
7c6de6f
 
 
 
1810165
7c6de6f
1810165
7c6de6f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ae604b6
 
 
7c6de6f
 
 
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
import { ExerciseType } from "@common/enums/exercise-type.enum";
import { FitnessGoal } from "@common/enums/fitness-goal.enum";
import { FitnessLevel } from "@common/enums/fitness-level.enum";
import { Gender } from "@common/enums/gender.enum";
import { config } from "@configs/config";

const endpoint = '/fitness';

export interface IFitnessPredictionItem {
  bodyPart: string;
  equipment: string;
  name: string;
  repetitions: number;
  sets: number;
  target: string;
  type: string;
  weights_or_duration: number;
}

// Fitness Workout Params
export interface IFWParams {
  home_or_gym: 0 | 1; // 0 for home, 1 for gym
  level: FitnessLevel;
  goal: FitnessGoal;
  gender: Gender;
  age: number;
  feedback: boolean;
  old_weight: number;
  equipments: string[];
}

export class FitnessModel {
  public static async predictWorkout(
    params: IFWParams
  ): Promise<IFitnessPredictionItem[][]> {
    params.level = params.level.split(' ').map((s) => s.charAt(0).toUpperCase() + s.slice(1)).join(' ') as FitnessLevel;
    params.goal = params.goal.split(' ').map((s) => s.charAt(0).toUpperCase() + s.slice(1)).join(' ') as FitnessGoal;
    params.gender = params.gender.toUpperCase() as Gender;

    const response = await fetch(
      `${config.modelsServerUrl}${endpoint}`,
      {
        method: "POST",
        headers: {
          "Content-Type": "application/json",
        },
        body: JSON.stringify(params),
      }
    );

    if (!response.ok) {
      console.error(await response.text());
      throw new Error("Failed to fetch data from the server");
    }

    return response.text().then((data) => {
      data = data.replace('NaN', ExerciseType.DURATION.toString()).replace('\n', '')
      return JSON.parse(data)
    });
  }
}