dindizz's picture
Update app.py
53b2eef verified
import math
import gradio as gr
import matplotlib.pyplot as plt
import numpy as np
# Function to calculate BRI
def calculate_bri(height, waist):
# Convert height and waist to meters
height_m = height / 100
waist_m = waist / 100
# Formula to calculate BRI
bri = 364.2 - 365.5 * math.sqrt(1 - (waist_m / height_m)**2)
return round(bri, 2)
# Function to generate a body shape graph
def plot_body_shape(bri):
fig, ax = plt.subplots(figsize=(5, 5))
# Create a circular shape for the reference healthy range (green)
circle = plt.Circle((0.5, 0.5), 0.25, color='green', alpha=0.5, label="Healthy Range")
ax.add_artist(circle)
# Scale the user's body shape based on BRI (black)
user_circle = plt.Circle((0.5, 0.5), min(bri / 20, 0.5), color='black', alpha=0.7, label="Your Body Shape")
ax.add_artist(user_circle)
# Set plot parameters
ax.set_xlim(0, 1)
ax.set_ylim(0, 1)
ax.set_aspect('equal', 'box')
plt.legend()
plt.title("Body Shape with BRI Reference")
# Save and return the plot
plot_path = "bri_plot.png"
plt.savefig(plot_path)
plt.close(fig) # Close the figure to free up memory
return plot_path
# Function to provide a summary
def bri_summary(bri):
if bri < 6:
category = "Your body is relatively narrow, within a healthy range."
elif 6 <= bri < 10:
category = "Your body shape is slightly round, suggesting moderate fat accumulation."
elif 10 <= bri < 15:
category = "Your body is more round, and you may be at risk for cardiovascular issues."
else:
category = "Your body is significantly round, indicating a higher risk for health problems."
return f"BRI is a health metric that can assess your body shape and provide insights into potential health risks. Your BRI is {bri}, meaning {category}"
# Gradio interface function
def bri_calculator(height, waist):
bri_value = calculate_bri(height, waist)
summary = bri_summary(bri_value)
plot_path = plot_body_shape(bri_value)
return summary, plot_path
# Creating Gradio Interface
interface = gr.Interface(
fn=bri_calculator,
inputs=[
gr.Number(label="Height (in cm)"),
gr.Number(label="Waist Circumference (in cm)")
],
outputs=[
"text",
"image"
],
title="BRI Calculator",
description=(
"The Body Roundness Index (BRI) is an innovative health metric that provides insight "
"into body shape based on height, waist, and hip circumference. "
"BRI ranges between 1 to 20, where 1 represents a narrow body and 20 indicates a more round shape. "
"This tool can give a better estimate of body fat distribution and potential health risks, such as cardiovascular disease."
"\n\nThe graph will display your body shape (in black) alongside a reference healthy body shape (in green). "
"The BRI is correlated with bioelectrical impedance measurements and can predict risks like coronary heart disease."
)
)
# Launch the interface
interface.launch()