Spaces:
Runtime error
Runtime error
Upload 3 files
Browse files- app.py +120 -0
- best.pt +3 -0
- requirements.txt +7 -0
app.py
ADDED
|
@@ -0,0 +1,120 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
import cv2
|
| 3 |
+
import numpy as np
|
| 4 |
+
from PIL import Image
|
| 5 |
+
import google.generativeai as genai
|
| 6 |
+
import gradio as gr
|
| 7 |
+
from ultralytics import YOLO
|
| 8 |
+
import os
|
| 9 |
+
|
| 10 |
+
# Force PyTorch to use CPU
|
| 11 |
+
device = torch.device("cpu")
|
| 12 |
+
|
| 13 |
+
# Configure Google AI API
|
| 14 |
+
# genai.configure(api_key='AIzaSyATgL92t9qBCe4eqFX1cSfyfkzKMooQM48')
|
| 15 |
+
# model = genai.GenerativeModel('gemini-2.0-flash')
|
| 16 |
+
|
| 17 |
+
genai.configure(api_key='AIzaSyATgL92t9qBCe4eqFX1cSfyfkzKMooQM48')
|
| 18 |
+
|
| 19 |
+
generation_config = {
|
| 20 |
+
"temperature": 1,
|
| 21 |
+
"top_p": 0.95,
|
| 22 |
+
"top_k": 40,
|
| 23 |
+
"max_output_tokens": 8192,
|
| 24 |
+
"response_mime_type": "text/plain",
|
| 25 |
+
}
|
| 26 |
+
|
| 27 |
+
model = genai.GenerativeModel(
|
| 28 |
+
model_name="gemini-1.5-flash",
|
| 29 |
+
generation_config=generation_config,
|
| 30 |
+
)
|
| 31 |
+
|
| 32 |
+
# Test Gemini API connection
|
| 33 |
+
try:
|
| 34 |
+
test_response = model.generate_content("Say Hello!")
|
| 35 |
+
print("Test Response from Gemini:", test_response.text)
|
| 36 |
+
except Exception as e:
|
| 37 |
+
print("Error: Gemini API connection failed! Check API key.")
|
| 38 |
+
print(e)
|
| 39 |
+
exit()
|
| 40 |
+
|
| 41 |
+
class FoodDetectionSystem:
|
| 42 |
+
def __init__(self, model_path):
|
| 43 |
+
self.model = YOLO('best.pt') # Load trained YOLOv8 model
|
| 44 |
+
self.model.to(device) # Ensure model runs on CPU
|
| 45 |
+
|
| 46 |
+
def detect_food(self, image_path):
|
| 47 |
+
"""Detect food items in the image"""
|
| 48 |
+
results = self.model(image_path) # Run inference on image
|
| 49 |
+
detected_items = []
|
| 50 |
+
|
| 51 |
+
for result in results:
|
| 52 |
+
boxes = result.boxes.cpu() # Ensure bounding boxes are on CPU
|
| 53 |
+
for box in boxes:
|
| 54 |
+
class_id = int(box.cls[0])
|
| 55 |
+
conf = float(box.conf[0])
|
| 56 |
+
if conf > 0.5: # Confidence threshold
|
| 57 |
+
detected_items.append(result.names[class_id])
|
| 58 |
+
|
| 59 |
+
return list(set(detected_items)) # Remove duplicates
|
| 60 |
+
|
| 61 |
+
def generate_recipe(ingredients, calorie_requirement):
|
| 62 |
+
"""Generate recipe using Gemini AI"""
|
| 63 |
+
prompt = f"""
|
| 64 |
+
Create a healthy recipe using some or all of these ingredients: {', '.join(ingredients)}.
|
| 65 |
+
The recipe should be approximately {calorie_requirement} calories suggest 2 recipies.
|
| 66 |
+
Please provide:
|
| 67 |
+
Recipi Number
|
| 68 |
+
1. Recipe name
|
| 69 |
+
2. Ingredients list with quantities
|
| 70 |
+
3. Step-by-step instructions
|
| 71 |
+
4. Approximate calorie count per serving
|
| 72 |
+
"""
|
| 73 |
+
|
| 74 |
+
response = model.generate_content(prompt)
|
| 75 |
+
return response.text
|
| 76 |
+
|
| 77 |
+
def process_image_and_generate_recipe(image, calorie_requirement):
|
| 78 |
+
"""Main function to process image and generate recipe"""
|
| 79 |
+
try:
|
| 80 |
+
# Save uploaded image temporarily
|
| 81 |
+
temp_path = "temp_upload.jpg"
|
| 82 |
+
image.save(temp_path)
|
| 83 |
+
|
| 84 |
+
print("Image saved at:", temp_path)
|
| 85 |
+
print("food detection start")
|
| 86 |
+
# Initialize and use food detection system
|
| 87 |
+
detector = FoodDetectionSystem('path_to_your_trained_model.pt')
|
| 88 |
+
detected_foods = detector.detect_food(temp_path)
|
| 89 |
+
print(detected_foods)
|
| 90 |
+
if not detected_foods:
|
| 91 |
+
return "No food items detected in the image. Please try another image."
|
| 92 |
+
|
| 93 |
+
# Generate recipe
|
| 94 |
+
recipe = generate_recipe(detected_foods, calorie_requirement)
|
| 95 |
+
|
| 96 |
+
# Clean up
|
| 97 |
+
os.remove(temp_path)
|
| 98 |
+
|
| 99 |
+
return f"Detected Foods: {', '.join(detected_foods)}\n\nGenerated Recipe:\n{recipe}"
|
| 100 |
+
|
| 101 |
+
except Exception as e:
|
| 102 |
+
return f"An error occurred: {str(e)}"
|
| 103 |
+
|
| 104 |
+
# Create Gradio interface
|
| 105 |
+
def create_interface():
|
| 106 |
+
iface = gr.Interface(
|
| 107 |
+
fn=process_image_and_generate_recipe,
|
| 108 |
+
inputs=[
|
| 109 |
+
gr.Image(type="pil", label="Upload Food Image"),
|
| 110 |
+
gr.Number(label="Desired Calorie Count", value=500)
|
| 111 |
+
],
|
| 112 |
+
outputs=gr.Textbox(label="Results"),
|
| 113 |
+
title="Food Detection and Recipe Generator",
|
| 114 |
+
description="Upload a food image to detect ingredients and generate a recipe based on your calorie requirements."
|
| 115 |
+
)
|
| 116 |
+
return iface
|
| 117 |
+
|
| 118 |
+
if __name__ == "__main__":
|
| 119 |
+
iface = create_interface()
|
| 120 |
+
iface.launch()
|
best.pt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:41e869f27c180a813c57ad97b93864c801a321df9a5b47434e4d64f59ece46b4
|
| 3 |
+
size 6318051
|
requirements.txt
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
torch
|
| 2 |
+
opencv-python
|
| 3 |
+
numpy
|
| 4 |
+
Pillow
|
| 5 |
+
google-generativeai
|
| 6 |
+
gradio
|
| 7 |
+
ultralytics
|