Spaces:
Sleeping
Sleeping
Upload 2 files
Browse files- app.py +84 -0
- templates/index.html +116 -0
app.py
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from flask import Flask, render_template, request
|
| 2 |
+
import os
|
| 3 |
+
import google.generativeai as genai
|
| 4 |
+
import markdown # To convert Markdown to HTML
|
| 5 |
+
from bs4 import BeautifulSoup # For post-processing HTML
|
| 6 |
+
|
| 7 |
+
app = Flask(__name__)
|
| 8 |
+
|
| 9 |
+
# Set and configure the Google API key
|
| 10 |
+
os.environ["GOOGLE_API_KEY"] = 'AIzaSyCh5ePMjn6WDWhMMZwA7A0JI9HaZj2FmhA'
|
| 11 |
+
genai.configure(api_key=os.getenv('GOOGLE_API_KEY'))
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
# Function to calculate BMI
|
| 15 |
+
def calculate_bmi(weight, height):
|
| 16 |
+
return weight / (height * height)
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
# Function to classify BMI into categories
|
| 20 |
+
def classify_bmi(bmi):
|
| 21 |
+
if bmi < 18.5:
|
| 22 |
+
return "Underweight"
|
| 23 |
+
elif 18.5 <= bmi < 25:
|
| 24 |
+
return "Normal weight"
|
| 25 |
+
elif 25 <= bmi < 30:
|
| 26 |
+
return "Overweight"
|
| 27 |
+
else:
|
| 28 |
+
return "Obese"
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
# Function to pre-process the HTML output: add CSS classes to table elements
|
| 32 |
+
def preprocess_html_table(html):
|
| 33 |
+
soup = BeautifulSoup(html, "html.parser")
|
| 34 |
+
for table in soup.find_all("table"):
|
| 35 |
+
# You can add any classes you need; here, we add Bootstrap styling classes as an example.
|
| 36 |
+
table['class'] = table.get('class', []) + ["table", "table-striped", "table-bordered"]
|
| 37 |
+
return str(soup)
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
@app.route("/", methods=["GET", "POST"])
|
| 41 |
+
def index():
|
| 42 |
+
recommendation_html = None
|
| 43 |
+
if request.method == "POST":
|
| 44 |
+
try:
|
| 45 |
+
age = int(request.form.get("age"))
|
| 46 |
+
height = float(request.form.get("height"))
|
| 47 |
+
weight = float(request.form.get("weight"))
|
| 48 |
+
except (TypeError, ValueError):
|
| 49 |
+
recommendation_html = "<p>Invalid input. Please try again.</p>"
|
| 50 |
+
return render_template("index.html", recommendation=recommendation_html)
|
| 51 |
+
|
| 52 |
+
target = request.form.get("target")
|
| 53 |
+
level = request.form.get("level")
|
| 54 |
+
|
| 55 |
+
# Calculate BMI and classify the category
|
| 56 |
+
bmi = calculate_bmi(weight, height)
|
| 57 |
+
bmi_category = classify_bmi(bmi)
|
| 58 |
+
|
| 59 |
+
# Create a prompt for the Gemini model
|
| 60 |
+
prompt = (
|
| 61 |
+
f"User details: Age: {age}, Height: {height}m, Weight: {weight}kg, "
|
| 62 |
+
f"BMI: {bmi:.2f}, Category: {bmi_category}, Target: {target}, Level: {level}. "
|
| 63 |
+
"Based on these details, generate a Markdown-formatted output that includes two sections: \n\n"
|
| 64 |
+
"1. **Exercise Suggester Table:** Create a table with columns such as Exercise, Reps, Sets, and Duration (or any other relevant details). \n\n"
|
| 65 |
+
"2. **1-Week Gym Workout Planner:** Provide a comprehensive day-by-day workout planner for one week, formatted clearly in Markdown. \n\n"
|
| 66 |
+
"Ensure the Markdown output is properly formatted and easy to read."
|
| 67 |
+
)
|
| 68 |
+
|
| 69 |
+
model = genai.GenerativeModel('gemini-1.5-flash')
|
| 70 |
+
response = model.generate_content(prompt)
|
| 71 |
+
|
| 72 |
+
# Convert the Markdown output to HTML with table support
|
| 73 |
+
recommendation_html = markdown.markdown(
|
| 74 |
+
response.text, extensions=["tables", "fenced_code"]
|
| 75 |
+
)
|
| 76 |
+
|
| 77 |
+
# Pre-process the HTML to adjust the table formatting (e.g., add CSS classes)
|
| 78 |
+
recommendation_html = preprocess_html_table(recommendation_html)
|
| 79 |
+
|
| 80 |
+
return render_template("index.html", recommendation=recommendation_html)
|
| 81 |
+
|
| 82 |
+
|
| 83 |
+
if __name__ == "__main__":
|
| 84 |
+
app.run(debug=True, port=4529)
|
templates/index.html
ADDED
|
@@ -0,0 +1,116 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<!DOCTYPE html>
|
| 2 |
+
<html lang="en">
|
| 3 |
+
<head>
|
| 4 |
+
<meta charset="UTF-8">
|
| 5 |
+
<title>Gym Exercise and Routine Recommendation System</title>
|
| 6 |
+
<link rel="stylesheet" href="{{ url_for('static', filename='style.css') }}">
|
| 7 |
+
<style>
|
| 8 |
+
/* Basic CSS styling for the application */
|
| 9 |
+
body {
|
| 10 |
+
font-family: Arial, sans-serif;
|
| 11 |
+
background-color: #f5f5f5;
|
| 12 |
+
margin: 0;
|
| 13 |
+
padding: 0;
|
| 14 |
+
}
|
| 15 |
+
.container {
|
| 16 |
+
width: 50%;
|
| 17 |
+
margin: 50px auto;
|
| 18 |
+
background-color: #fff;
|
| 19 |
+
padding: 20px 30px;
|
| 20 |
+
border-radius: 8px;
|
| 21 |
+
box-shadow: 0 2px 10px rgba(0,0,0,0.1);
|
| 22 |
+
}
|
| 23 |
+
h1 {
|
| 24 |
+
text-align: center;
|
| 25 |
+
color: #333;
|
| 26 |
+
}
|
| 27 |
+
form {
|
| 28 |
+
display: flex;
|
| 29 |
+
flex-direction: column;
|
| 30 |
+
}
|
| 31 |
+
label {
|
| 32 |
+
margin-top: 10px;
|
| 33 |
+
color: #555;
|
| 34 |
+
}
|
| 35 |
+
input[type="number"], select {
|
| 36 |
+
padding: 8px;
|
| 37 |
+
margin-top: 5px;
|
| 38 |
+
border: 1px solid #ddd;
|
| 39 |
+
border-radius: 4px;
|
| 40 |
+
}
|
| 41 |
+
button {
|
| 42 |
+
margin-top: 20px;
|
| 43 |
+
padding: 10px;
|
| 44 |
+
background-color: #4285F4;
|
| 45 |
+
color: #fff;
|
| 46 |
+
border: none;
|
| 47 |
+
border-radius: 4px;
|
| 48 |
+
cursor: pointer;
|
| 49 |
+
font-size: 16px;
|
| 50 |
+
}
|
| 51 |
+
button:hover {
|
| 52 |
+
background-color: #357ae8;
|
| 53 |
+
}
|
| 54 |
+
.result {
|
| 55 |
+
margin-top: 30px;
|
| 56 |
+
padding: 15px;
|
| 57 |
+
background-color: #e8f0fe;
|
| 58 |
+
border-radius: 4px;
|
| 59 |
+
}
|
| 60 |
+
/* Table styling */
|
| 61 |
+
table {
|
| 62 |
+
width: 100%;
|
| 63 |
+
border-collapse: collapse;
|
| 64 |
+
margin-top: 20px;
|
| 65 |
+
}
|
| 66 |
+
table, th, td {
|
| 67 |
+
border: 1px solid #ddd;
|
| 68 |
+
}
|
| 69 |
+
th, td {
|
| 70 |
+
padding: 8px;
|
| 71 |
+
text-align: center;
|
| 72 |
+
}
|
| 73 |
+
th {
|
| 74 |
+
background-color: #4285F4;
|
| 75 |
+
color: #fff;
|
| 76 |
+
}
|
| 77 |
+
</style>
|
| 78 |
+
</head>
|
| 79 |
+
<body>
|
| 80 |
+
<div class="container">
|
| 81 |
+
<h1>Gym Exercise and Routine Recommendation System</h1>
|
| 82 |
+
<form method="POST">
|
| 83 |
+
<label for="age">Enter your age:</label>
|
| 84 |
+
<input type="number" name="age" id="age" min="1" max="120" value="30" required>
|
| 85 |
+
|
| 86 |
+
<label for="height">Enter your height (in meters):</label>
|
| 87 |
+
<input type="number" step="0.01" name="height" id="height" min="0.1" max="3.0" value="1.7" required>
|
| 88 |
+
|
| 89 |
+
<label for="weight">Enter your weight (in kg):</label>
|
| 90 |
+
<input type="number" name="weight" id="weight" min="1" max="500" value="70" required>
|
| 91 |
+
|
| 92 |
+
<label for="target">Select your target:</label>
|
| 93 |
+
<select name="target" id="target">
|
| 94 |
+
<option value="Weight loss">Weight loss</option>
|
| 95 |
+
<option value="Weight gain">Weight gain</option>
|
| 96 |
+
</select>
|
| 97 |
+
|
| 98 |
+
<label for="level">Select your level:</label>
|
| 99 |
+
<select name="level" id="level">
|
| 100 |
+
<option value="Beginner">Beginner</option>
|
| 101 |
+
<option value="Intermediate">Intermediate</option>
|
| 102 |
+
<option value="Advanced">Advanced</option>
|
| 103 |
+
</select>
|
| 104 |
+
|
| 105 |
+
<button type="submit">Generate Recommendations</button>
|
| 106 |
+
</form>
|
| 107 |
+
{% if recommendation %}
|
| 108 |
+
<div class="result">
|
| 109 |
+
<h2>Exercise Recommendations</h2>
|
| 110 |
+
<!-- The recommendation will be rendered as formatted HTML -->
|
| 111 |
+
{{ recommendation | safe }}
|
| 112 |
+
</div>
|
| 113 |
+
{% endif %}
|
| 114 |
+
</div>
|
| 115 |
+
</body>
|
| 116 |
+
</html>
|