Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
|
| 4 |
+
# Load AI model (FLAN-T5 for text-based food recognition)
|
| 5 |
+
food_model = pipeline("text2text-generation", model="google/flan-t5-small")
|
| 6 |
+
|
| 7 |
+
# Function to get calorie data & substitute using AI
|
| 8 |
+
def get_food_substitute(food, portion_size):
|
| 9 |
+
# AI model processes input and provides a substitute
|
| 10 |
+
query = f"Suggest a healthier food alternative for {food} along with its calorie content"
|
| 11 |
+
response = food_model(query, max_length=50)[0]['generated_text']
|
| 12 |
+
|
| 13 |
+
# AI also provides calorie data (parsing response)
|
| 14 |
+
try:
|
| 15 |
+
original_food, original_calories, substitute, substitute_calories = response.split(", ")
|
| 16 |
+
original_calories = float(original_calories.split(" ")[0]) * portion_size / 100
|
| 17 |
+
substitute_calories = float(substitute_calories.split(" ")[0]) * portion_size / 100
|
| 18 |
+
calories_saved = original_calories - substitute_calories
|
| 19 |
+
|
| 20 |
+
# Format results
|
| 21 |
+
result = (
|
| 22 |
+
f"**Original Food:** {original_food} ({original_calories:.2f} kcal)\n"
|
| 23 |
+
f"**Suggested Substitute:** {substitute} ({substitute_calories:.2f} kcal)\n"
|
| 24 |
+
f"**Calories Saved:** {calories_saved:.2f} kcal"
|
| 25 |
+
)
|
| 26 |
+
except:
|
| 27 |
+
result = "Error: AI model could not process the input correctly."
|
| 28 |
+
|
| 29 |
+
return result
|
| 30 |
+
|
| 31 |
+
# Gradio UI
|
| 32 |
+
with gr.Blocks() as app:
|
| 33 |
+
gr.Markdown("# 🍽️ DeepCalorie - AI-Powered Food Substitute Finder")
|
| 34 |
+
gr.Markdown("Enter a food item, and get a **lower-calorie alternative** along with calories saved.")
|
| 35 |
+
|
| 36 |
+
with gr.Row():
|
| 37 |
+
food_input = gr.Textbox(label="Enter Food/Dish")
|
| 38 |
+
portion_input = gr.Number(label="Portion Size (grams)", value=100)
|
| 39 |
+
|
| 40 |
+
submit_button = gr.Button("Find Substitute")
|
| 41 |
+
output = gr.Markdown()
|
| 42 |
+
|
| 43 |
+
submit_button.click(get_food_substitute, inputs=[food_input, portion_input], outputs=output)
|
| 44 |
+
|
| 45 |
+
# Run the app
|
| 46 |
+
if __name__ == "__main__":
|
| 47 |
+
app.launch()
|