dindizz commited on
Commit
5fe0777
·
verified ·
1 Parent(s): c42fe56

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +83 -0
app.py ADDED
@@ -0,0 +1,83 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import math
2
+ import gradio as gr
3
+ import matplotlib.pyplot as plt
4
+ import numpy as np
5
+
6
+ # Function to calculate BRI
7
+ def calculate_bri(height, waist):
8
+ # Convert height and waist to meters
9
+ height_m = height / 100
10
+ waist_m = waist / 100
11
+
12
+ # Formula to calculate BRI
13
+ bri = 364.2 - 365.5 * math.sqrt(1 - (waist_m / height_m)**2)
14
+ return round(bri, 2)
15
+
16
+ # Function to generate a body shape graph
17
+ def plot_body_shape(bri):
18
+ fig, ax = plt.subplots(figsize=(5, 5))
19
+
20
+ # Create a circular shape for the reference healthy range (green)
21
+ circle = plt.Circle((0.5, 0.5), 0.25, color='green', alpha=0.5, label="Healthy Range")
22
+ ax.add_artist(circle)
23
+
24
+ # Scale the user's body shape based on BRI (black)
25
+ user_circle = plt.Circle((0.5, 0.5), min(bri / 20, 0.5), color='black', alpha=0.7, label="Your Body Shape")
26
+ ax.add_artist(user_circle)
27
+
28
+ # Set plot parameters
29
+ ax.set_xlim(0, 1)
30
+ ax.set_ylim(0, 1)
31
+ ax.set_aspect('equal', 'box')
32
+ plt.legend()
33
+ plt.title("Body Shape with BRI Reference")
34
+
35
+ # Save and return the plot
36
+ plt.savefig('/mnt/data/bri_plot.png')
37
+ return '/mnt/data/bri_plot.png'
38
+
39
+ # Function to provide a summary
40
+ def bri_summary(bri):
41
+ if bri < 6:
42
+ category = "Your body is relatively narrow, within a healthy range."
43
+ elif 6 <= bri < 10:
44
+ category = "Your body shape is slightly round, suggesting moderate fat accumulation."
45
+ elif 10 <= bri < 15:
46
+ category = "Your body is more round, and you may be at risk for cardiovascular issues."
47
+ else:
48
+ category = "Your body is significantly round, indicating a higher risk for health problems."
49
+
50
+ 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}"
51
+
52
+ # Gradio interface function
53
+ def bri_calculator(height, waist):
54
+ bri_value = calculate_bri(height, waist)
55
+ summary = bri_summary(bri_value)
56
+ plot_path = plot_body_shape(bri_value)
57
+
58
+ return summary, plot_path
59
+
60
+ # Creating Gradio Interface
61
+ interface = gr.Interface(
62
+ fn=bri_calculator,
63
+ inputs=[
64
+ gr.inputs.Number(label="Height (in cm)"),
65
+ gr.inputs.Number(label="Waist Circumference (in cm)")
66
+ ],
67
+ outputs=[
68
+ "text",
69
+ "image"
70
+ ],
71
+ title="BRI Calculator",
72
+ description=(
73
+ "The Body Roundness Index (BRI) is an innovative health metric that provides insight "
74
+ "into body shape based on height, waist, and hip circumference. "
75
+ "BRI ranges between 1 to 20, where 1 represents a narrow body and 20 indicates a more round shape. "
76
+ "This tool can give a better estimate of body fat distribution and potential health risks, such as cardiovascular disease."
77
+ "\n\nThe graph will display your body shape (in black) alongside a reference healthy body shape (in green). "
78
+ "The BRI is correlated with bioelectrical impedance measurements and can predict risks like coronary heart disease."
79
+ )
80
+ )
81
+
82
+ # Launch the interface
83
+ interface.launch()