Building a Workout Planner App with Lyzr SDK
Tags: ai, opensource, lyzr, workout
Published: 2024-04-28 17:53:13

In today’s fitness landscape, achieving personal fitness goals requires more than just hitting the gym randomly. **Customized workout plans** tailored to individual goals and preferences are becoming increasingly essential. To streamline this process, we’ve developed a user-friendly Streamlit application that leverages AI to generate personalized workout plans.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dvn7qmnr8x0h0pljmgee.png)

Our **Workout Planner App** aims to revolutionize how fitness enthusiasts approach their training routines. Powered by advanced AI models and designed with user convenience in mind, this app allows users to create tailored workout plans aligned with their specific fitness objectives and time commitments.

**Why use Lyzr SDK’s?**

With **Lyzr SDKs**, crafting your own **GenAI** application is a breeze, requiring only a few lines of code to get up and running swiftly.

[Checkout the Lyzr SDK’s](https://docs.lyzr.ai/homepage)

Lets get Started!

Create a new file **app.py** and use that

```
import streamlit as st
from lyzr_automata.ai_models.openai import OpenAIModel
from lyzr_automata import Agent, Task
from lyzr_automata.pipelines.linear_sync_pipeline import LinearSyncPipeline
from PIL import Image
from lyzr_automata.tasks.task_literals import InputType, OutputType
import os
```
This Python script utilizes **Streamlit** for creating a web interface and integrates custom modules for automata-related functionality. It imports classes for defining agents, tasks, and pipelines, potentially involving image processing and machine learning tasks with OpenAI models.

This line of code sets “**OPENAI_API_KEY**” to the value stored in the Streamlit secrets dictionary under the key “apikey”. This API key is essential for authenticating and accessing the OpenAI API, allowing the application to utilize OpenAI’s models and services for natural language processing tasks. By assigning the API key to the environment variable, it ensures that subsequent requests made to the OpenAI API are properly authenticated.

```
# Set the OpenAI API key
os.environ["OPENAI_API_KEY"] = st.secrets["apikey"]
```

```
open_ai_text_completion_model = OpenAIModel(
    api_key=st.secrets["apikey"],
    parameters={
        "model": "gpt-4-turbo-preview",
        "temperature": 0.2,
        "max_tokens": 1500,
    },
)
```
This block of code initializes an instance of the **OpenAIModel** class with specific parameters. The api_key parameter is set to the value retrieved from Streamlit secrets, ensuring authentication with the OpenAI API. The parameters dictionary contains settings for the AI model, specifying “gpt-4-turbo-preview” as the model to be used, a temperature of 0.2, and a maximum token limit of 1500. These settings configure how the model generates text responses, controlling factors such as creativity and length. The initialized OpenAIModel instance will be utilized for generating text based on input prompts in subsequent parts of the code.

```
def workout_generation(input):
    generator_agent = Agent(
        role="FITNESS TRAINER expert",
        prompt_persona=f"Your task  is to DESIGN CUSTOMIZED WORKOUT SPLITS that cater to various user goals, including STRENGTH, ENDURANCE, FLEXIBILITY, BODY COMPOSITION, PERFORMANCE, FUNCTIONAL FITNESS, HEALTH AND WELLNESS, SPORT-SPECIFIC requirements, CONSISTENCY in training, and the DURATION of the workout program."
    )

    prompt = f"""
You are an Expert FITNESS TRAINER. Your task is to DESIGN CUSTOMIZED WORKOUT SPLITS that cater to various user goals, including STRENGTH, ENDURANCE, FLEXIBILITY, BODY COMPOSITION, PERFORMANCE, FUNCTIONAL FITNESS, HEALTH AND WELLNESS, SPORT-SPECIFIC requirements, CONSISTENCY in training, and the DURATION of the workout program.

---------

----------

   """
```
This function, **workout_generation**, is designed to generate customized workout splits. It begins by creating an agent with the role of a fitness trainer expert, providing a prompt persona specifying the task of designing workout splits tailored to various user goals such as strength, endurance, flexibility, and more. The function then constructs a prompt string incorporating this persona, prompting the fitness trainer to fulfill the task.

 

```
generator_agent_task = Task(
        name="workout Generation",
        model=open_ai_text_completion_model,
        agent=generator_agent,
        instructions=prompt,
        default_input=input,
        output_type=OutputType.TEXT,
        input_type=InputType.TEXT,
    ).execute()

    return generator_agent_task 
   
if st.button("Generate!"):
    solution = workout_generation(input)
    st.markdown(solution)
```
This code segment defines a task named “**workout Generation**” with specific parameters including the name, the OpenAI text completion model, an agent assigned to handle the task, instructions provided as a prompt, default input, and the expected input and output types. The task is then executed, returning the result stored in generator_agent_task. Subsequently, it checks if a button labeled "Generate!" is clicked using Streamlit's st.button function. If the button is clicked, it triggers the workout_generation function, passing the input provided by the user. The result is then displayed using st.markdown.

With just a few clicks, users can access expertly curated workout plans tailored to their unique needs and preferences. Whether you’re a beginner embarking on your fitness journey or a seasoned athlete seeking to optimize your training regime, our Workout Planner App is your ultimate companion for achieving fitness success.

**App link**: https://workoutplanner-lyzr.streamlit.app/

**Source Code**: https://github.com/isakshay007/workout_planner

**Connect with Lyzr**
To learn more about **Lyzr and its SDK’s**, visit our website or get in touch with our team:

**Website**: [Lyzr.ai](https://www.lyzr.ai/)
**Book a Demo**: [Book a Demo](https://www.lyzr.ai/book-demo/)
**Discord**: [Join our Discord community](https://discord.com/invite/nm7zSyEFA2)
**Slack**: [Join our Slack channel](https://anybodycanai.slack.com/join/shared_invite/zt-2a7fr38f7-_QDOY1W1WSlSiYNAEncLGw#/shared-invite/email)