matteopilotto commited on
Commit
358b024
·
1 Parent(s): 39bcce7

Add application file

Browse files
Files changed (2) hide show
  1. app.py +143 -0
  2. requirements.txt +2 -0
app.py ADDED
@@ -0,0 +1,143 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import random
3
+ import time
4
+ from langchain.schema.messages import HumanMessage, SystemMessage
5
+ import pinecone
6
+ from langchain.vectorstores import Pinecone
7
+ from langchain.embeddings.openai import OpenAIEmbeddings
8
+ from langchain.chat_models import ChatOpenAI
9
+ import gradio as gr
10
+
11
+ def retrieve_knowledge(query, k=10, randomize=True):
12
+ knowledge = [d.page_content.strip() for d in vectorstore.similarity_search(query, k=k)]
13
+
14
+ if randomize:
15
+ knowledge = random.sample(knowledge, k)
16
+
17
+ knowledge = "\n\n\n".join(knowledge)
18
+
19
+ return knowledge
20
+
21
+
22
+ def generate_workout(query, knowledge):
23
+ messages = [
24
+ SystemMessage(content=system_prompt.format(workout_context=knowledge)),
25
+ HumanMessage(content=query)
26
+ ]
27
+
28
+ response = llm.invoke(messages).content.strip()
29
+
30
+ return response
31
+
32
+
33
+ def run(gender, muscle_group, equipment, level, duration, k=5, randomize=True):
34
+ query = f"{duration}-minute {muscle_group} workout for {gender} {level} level {equipment}"
35
+ knowledge = retrieve_knowledge(query, k, randomize)
36
+ response = generate_workout(query, knowledge)
37
+
38
+ for i in range(len(response)):
39
+ time.sleep(0.001)
40
+ yield response[:i+1]
41
+
42
+
43
+ # embedding model
44
+ embedding_model_name = "text-embedding-ada-002"
45
+
46
+ embedding_model = OpenAIEmbeddings(
47
+ model=embedding_model_name,
48
+ openai_api_key=os.getenv("OPENAI_API_KEY")
49
+ )
50
+
51
+
52
+ # llm
53
+ llm = ChatOpenAI(
54
+ openai_api_key=os.getenv("OPENAI_API_KEY"),
55
+ model_name="gpt-4-1106-preview",
56
+ # model_name="gpt-3.5-turbo-1106",
57
+ temperature=0.0
58
+ )
59
+
60
+
61
+ # vectorstore
62
+ index_name = "workouts"
63
+
64
+ pinecone.init(
65
+ api_key=os.getenv("PINECONE_API_KEY"),
66
+ environment=os.getenv("PINECONE_ENVIRONMENT")
67
+ )
68
+
69
+ text_field = "text"
70
+ index = pinecone.Index(index_name)
71
+ vectorstore = Pinecone(index, embedding_model, text_field)
72
+
73
+
74
+ # prompt
75
+ system_prompt = """
76
+ You're the world's best personal trainer.
77
+ You always provide your clients with all the information needed to become fitter, stronger and healthier through physical training.
78
+ You use your science science know and expertise, nutrition advice, and other relevant factors to create workout routines suitable to your clients.
79
+ If clients tell you they do not have access to gym equipments, you never fail to recommend exercises that do not require any tool or equipment.
80
+
81
+ For each exercise you always provide the reps, sets and rest intervals in seconds appropriate for each exercise and the client's fitness level.
82
+ You start each workout program with about 5 minutes of warm-up exercises to make the body ready for more strenuous activities and make it easier to exercise.
83
+ You end each workout routine with 5 about minutes of cool-down exercises to ease the body, lower the chance of injury, promote blood flow, and reduce stress to the heart and the muscles.
84
+ The warm-up and cool-down exercises are always different and they are always appropriate for the muscle group the person wants to train.
85
+ You never recommend exercises in the main workout routine in the warm-up or cool-down sections.
86
+ Remember, when clients tell you they do not have access to gym equipments, all the exercises you recommend, including the warm-up and cool-down exercises, can be performed without any tool.
87
+ You always limit yourself to respond with the list of exercises. You never add any additional comment.
88
+
89
+ Design the workout based on the following information:
90
+ {workout_context}
91
+
92
+
93
+ Output format:
94
+ ## 🤸 Warp-up:
95
+ - <exercise name> (<duration> minutes)
96
+ ...
97
+ - <exercise name> (<duration> minutes)
98
+
99
+ ## 🏋️‍♀️ Workout
100
+ - <exercise name> (<reps> reps, <sets> sets, <rest interval> seconds rest)
101
+ ...
102
+ - <exercise name> (<reps> reps, <sets> sets, <rest interval> seconds rest)
103
+
104
+ ## 🧘 Cool-down:
105
+ - <exercise name> (<duration> minutes)
106
+ ...
107
+ - <exercise name> (<duration> minutes)
108
+ """.strip()
109
+
110
+ css = """
111
+ #gen-button {
112
+ background-color: #cc6600;
113
+ color: white;
114
+ font-size: 24px !important;
115
+ }
116
+ """.strip()
117
+
118
+ with gr.Blocks(theme=gr.themes.Monochrome(radius_size=gr.themes.sizes.radius_sm), css=css) as demo:
119
+ with gr.Row():
120
+ gr.Markdown("# Workout Generator")
121
+
122
+ with gr.Row():
123
+ with gr.Column(scale=1):
124
+ with gr.Row():
125
+ gender = gr.Radio(["Male", "Female"], label="Gender", elem_id="#my-button")
126
+ with gr.Row():
127
+ level = gr.Radio(["Beginner", "Intermediate", "Advanced"], label="Level")
128
+ with gr.Row():
129
+ muscle_group = gr.Radio(["Shoulders", "Chest", "Back", "Abs", "Arms", "Legs"], label="Muscle Group")
130
+ with gr.Row():
131
+ equipment = gr.Radio(["Gym Equipment", "Dumbbells Only", "No Equipment"], label="Equipment")
132
+ with gr.Row():
133
+ duration = gr.Slider(20, 60, step=5, label="Duration (minutes)")
134
+ with gr.Row():
135
+ # clear_button = gr.ClearButton(value="Clear Inputs")
136
+ generate_button = gr.Button("Generate Workout", variant="primary", elem_id="gen-button")
137
+ with gr.Column(scale=1, min_width=800, elem_id="#gen-output"):
138
+ generation = gr.Markdown(value="")
139
+
140
+ generate_button.click(run, inputs=[gender, level, muscle_group, equipment, duration], outputs=generation)
141
+ # clear_button.click(fn=lambda: [None, None, None, None, None], outputs=[gender, level, muscle_group, equipment, duration])
142
+
143
+ demo.launch(share=True)
requirements.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ langchain
2
+ pinecone