-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.py
119 lines (100 loc) · 4.07 KB
/
functions.py
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
import openai
import re
import json
import os
openai_api_key = os.getenv('OPENAI_API_KEY')
def generate_prompt(input_info):
exercises = []
# Get the user's goals, intensity level, gender, weight, height, age, experience level, and equipment availability.
days_per_week = input_info["days_per_week"]
goals = input_info["goal"]
intensity = input_info["intensity"]
gender = input_info["gender"]
weight = input_info["weight"]
height = input_info["height_ft"] + str("'") + input_info["height_in"] + str('"')
age = input_info["age"]
experience = input_info["experience"]
equipment = input_info["equipment"]
duration = input_info["duration_hr"] + str(" hours and ") + input_info["duration_min"] + str(" minutes")
exercise_mn = (int(input_info["duration_hr"])*60 + int(input_info["duration_min"]))//10
exercise_mx = (int(input_info["duration_hr"])*60 + int(input_info["duration_min"]))//7
# create the prompt template
prompt_template = """
Design a {days_per_week}-day workout for a {gender}, \
{age}-year-old, {weight} lbs, {height} tall individual. \
Intensity: {intensity}. Goal: {goals}. Experience: {experience}. \
Equipment: {equipment}. Duration: {duration}. \
Must be {days_per_week} days.
The output should be in the following format for easier parsing:
Day 1
Muscle Group: Chest and Triceps
- Warm-up: 5-10 minutes of light cardio
- Bench press: 3 sets of 8-10 reps
- Dumbbell flyes: 3 sets of 12 reps
- Incline dumbbell press: 3 sets of 10 reps
- Push-ups: 3 sets to failure
- Tricep pushdown: 3 sets of 10-12 reps
- Overhead tricep extension: 3 sets of 10 reps
- Close-grip bench press: 3 sets of 8-10 reps
- Cool down: 5-10 minutes of stretching
(Continue for other days...)
"""
# fill in the template with the actual values
prompt = prompt_template.format(
gender=gender,
age=age,
weight=weight,
height=height,
days_per_week=days_per_week,
intensity=intensity,
goals=goals,
experience=experience,
equipment=equipment,
duration=duration,
exercise_mn=exercise_mn,
exercise_mx=exercise_mx
)
print(prompt)
return prompt
def generate_response(prompt):
openai.api_key = openai_api_key
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[
{"role": "system", "content": "You are a helpful fitness trainer."},
{"role": "user", "content": prompt}
],
max_tokens=2000,
temperature=0
)
out = response.choices[0].message.content.strip()
return out
def parse_response(response):
parsed_data = {'Day': [], 'Muscle Group': [], 'Exercises': []}
day_splits = re.split(r'Day \d+', response)[1:]
for index, day_data in enumerate(day_splits, start=1):
# Append Day
parsed_data['Day'].append(f'Day {index}')
# Append Muscle Group
muscle_group_match = re.search(r'Muscle Group: ([\w\s\and]+)', day_data)
if muscle_group_match:
parsed_data['Muscle Group'].append(muscle_group_match.group(1))
# Append Exercises
exercises_match = re.findall(r'- ([\w\s]+): (\d+ sets of [\d\-]+ reps)', day_data)
if exercises_match:
bullet_points = [f"- {exercise}: {sets_reps}" for exercise, sets_reps in exercises_match]
parsed_data['Exercises'].append('\n'.join(bullet_points))
return parsed_data
def generate_clarify_response(prompt):
openai.api_key = openai_api_key
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[
{"role": "system", "content": "You are a helpful fitness trainer. Explain how to do the exercise step by step using numbered buellet points."},
{"role": "user", "content": prompt}
],
max_tokens=300,
temperature=0.3
)
out = response.choices[0].message.content.strip()
return out