Download app.py from savitha7/test: direct link, hf CLI and curl.
- Browser
- Download file 4.26 kB
-
https://huggingface.co/spaces/savitha7/test/resolve/main/app.py
- Command line
-
hf download hf://spaces/savitha7/test/app.py
-
curl -L -o app.py https://huggingface.co/spaces/savitha7/test/resolve/main/app.py
4.26 kB
| import gradio as gr | |
| from transformers import CLIPProcessor, CLIPModel | |
| from PIL import Image | |
| # Load the model and processor | |
| model = CLIPModel.from_pretrained("openai/clip-vit-base-patch32") | |
| processor = CLIPProcessor.from_pretrained("openai/clip-vit-base-patch32") | |
| # Define the BMI classes | |
| bmi_classes = ["underweight (x < 18.5 BMI)", "normal weight (18.5 < x < 25 BMI)", "overweight (25 BMI < x < 30)", "obesity (x > 30 BMI)"] | |
| # Define the secondary models for BMI predictions and boundaries | |
| bmi_ranges = { | |
| "underweight (x < 18.5 bmi)": { | |
| "BMI < 16.0": (0, 16.0, 16.0), # Upper boundary 16.0 | |
| "16.0 ≤ BMI ≤ 16.99": (16.0, 16.99, 16.5), # Midpoint 16.5 | |
| "17.0 ≤ BMI ≤ 18.49": (17.0, 18.49, 17.75) # Midpoint 17.75 | |
| }, | |
| "normal weight (18.5 < x < 25 bmi)": { | |
| "18.5 ≤ BMI ≤ 20.4": (18.5, 20.4, 19.45), # Midpoint 19.45 | |
| "20.5 ≤ BMI ≤ 22.4": (20.5, 22.4, 21.45), # Midpoint 21.45 | |
| "22.5 ≤ BMI ≤ 24.9": (22.5, 24.9, 23.7) # Midpoint 23.7 | |
| }, | |
| "overweight (25 bmi < x < 30)": { | |
| "25.0 ≤ BMI ≤ 26.9": (25.0, 26.9, 25.95), # Midpoint 25.95 | |
| "27.0 ≤ BMI ≤ 28.9": (27.0, 28.9, 27.95), # Midpoint 27.95 | |
| "29.0 ≤ BMI ≤ 29.9": (29.0, 29.9, 29.45) # Midpoint 29.45 | |
| }, | |
| "obesity (x > 30 bmi)": { | |
| "30.0 ≤ BMI ≤ 34.9": (30.0, 34.9, 32.5), # Midpoint 32.5 | |
| "35.0 ≤ BMI ≤ 39.9": (35.0, 39.9, 37.45), # Midpoint 37.45 | |
| "BMI ≥ 40.0": (40.0, 100, 40.0) # Lower boundary 40.0 | |
| } | |
| } | |
| def predict_bmi(image, height_in_inches): | |
| # Prepare the inputs | |
| inputs = processor(text=bmi_classes, images=image, return_tensors="pt", padding=True) | |
| # Get model outputs | |
| outputs = model(**inputs) | |
| logits_per_image = outputs.logits_per_image # Image-text similarity scores | |
| probs = logits_per_image.softmax(dim=1) # Convert to probabilities | |
| # Find the class with the highest probability | |
| max_prob_index = probs.argmax().item() | |
| predicted_bmi_class = bmi_classes[max_prob_index] | |
| # Use the adjusted BMI value for the predicted class | |
| bmi_prediction = get_adjusted_bmi(predicted_bmi_class) | |
| # Calculate the weight based on the predicted BMI and user-provided height | |
| predicted_weight = calculate_weight(bmi_prediction, height_in_inches) | |
| # Create the JSON output | |
| result = { | |
| "weightCategory": f"{predicted_bmi_class} - ({get_bmi_label(bmi_prediction)})", | |
| "bmiPrediction": f"{bmi_prediction:.2f}", | |
| "height": str(height_in_inches), | |
| "predictedWeight": f"{predicted_weight:.2f} lbs" | |
| } | |
| return result | |
| def get_adjusted_bmi(weight_category): | |
| """Return the appropriate BMI value for the given weight category.""" | |
| category_ranges = bmi_ranges.get(weight_category.lower()) | |
| if category_ranges: | |
| for range_label, (low, high, mid) in category_ranges.items(): | |
| # Check if it's a "less than" range or "greater than or equal to" range | |
| if "BMI <" in range_label: | |
| return high # Use the upper boundary | |
| elif "BMI ≥" in range_label: | |
| return low # Use the lower boundary | |
| else: | |
| return mid # Use the midpoint | |
| return None | |
| def get_bmi_label(bmi_value): | |
| """Return the BMI range label corresponding to the predicted BMI.""" | |
| for category, ranges in bmi_ranges.items(): | |
| for label, (low, high, mid) in ranges.items(): | |
| if low <= bmi_value <= high: | |
| return label | |
| return "Unknown" | |
| def calculate_weight(bmi, height_in_inches): | |
| """Calculate the weight from BMI and height (in inches).""" | |
| height_in_meters = height_in_inches * 0.0254 # Convert height to meters | |
| weight_kg = bmi * (height_in_meters ** 2) # BMI formula to calculate weight | |
| weight_lbs = weight_kg * 2.20462 # Convert kg to lbs | |
| return weight_lbs | |
| # Create Gradio interface with updated components | |
| interface = gr.Interface( | |
| fn=predict_bmi, | |
| inputs=[gr.Image(type="pil"), gr.Number(label="Height in inches")], | |
| outputs="json", | |
| title="BMI Prediction", | |
| description="Upload an image and provide your height in inches to predict BMI category." | |
| ) | |
| # Launch the interface | |
| interface.launch() | |