Spaces:
Runtime error
Runtime error
Download app.py from MSGod/gram_convertor: direct link, hf CLI and curl.
- Browser
- Download file 1.61 kB
-
https://huggingface.co/spaces/MSGod/gram_convertor/resolve/main/app.py
- Command line
-
hf download hf://spaces/MSGod/gram_convertor/app.py
-
curl -L -o app.py https://huggingface.co/spaces/MSGod/gram_convertor/resolve/main/app.py
1.61 kB
| import gradio as gr | |
| import pandas as pd | |
| from datasets import load_dataset | |
| # Load dataset from Hugging Face Hub | |
| dataset = load_dataset("MS-God/ingredient") # Replace with your actual dataset repo | |
| # Convert to Pandas DataFrame | |
| df = dataset["train"].to_pandas() | |
| def convert_to_grams(ingredient, amount, unit): | |
| # Normalize ingredient names | |
| df["Ingredient"] = df["Ingredient"].str.strip().str.lower() | |
| ingredient = ingredient.strip().lower() | |
| # Find matching ingredient | |
| row = df[df["Ingredient"] == ingredient] | |
| if row.empty: | |
| return "Ingredient not found. Check spelling or dataset." | |
| # Get weight per unit and handle missing values | |
| try: | |
| weight_per_unit = float(row.iloc[0]["Grams"]) | |
| except ValueError: | |
| return "Invalid weight data in dataset." | |
| try: | |
| amount = float(amount) | |
| except ValueError: | |
| return "Invalid amount input. Please enter a numeric value." | |
| # Convert units (assuming 'cup', 'tbsp', 'tsp') | |
| unit_conversion = {"cup": 1, "tbsp": 1/16, "tsp": 1/48} | |
| unit = unit.strip().lower() | |
| if unit not in unit_conversion: | |
| return "Invalid unit. Please use cup, tbsp, or tsp." | |
| converted_weight = amount * unit_conversion[unit] * weight_per_unit | |
| return f"{converted_weight:.2f} grams" | |
| # Create Gradio interface | |
| demo = gr.Interface( | |
| fn=convert_to_grams, | |
| inputs=["text", "text", "text"], | |
| outputs="text", | |
| title="Ingredient to Grams Converter", | |
| description="Enter an ingredient, amount, and unit (e.g., '1 cup'), and get the weight in grams." | |
| ) | |
| demo.launch() | |