Commit
·
3200dde
1
Parent(s):
629aaed
Nutritional values
Browse files
app.py
CHANGED
|
@@ -5,6 +5,9 @@
|
|
| 5 |
from transformers import pipeline
|
| 6 |
import os
|
| 7 |
import openai
|
|
|
|
|
|
|
|
|
|
| 8 |
openai.organization = "org-5Z0c3Uk1VG7t3TsczN6M4FCi"
|
| 9 |
#openai.api_key = os.getenv("OPENAI_API_KEY")
|
| 10 |
openai.api_key_path ="./key.txt"
|
|
@@ -35,4 +38,49 @@ def classifyImage(image):
|
|
| 35 |
result = pipe(image)
|
| 36 |
return result[0]['label']
|
| 37 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
|
|
|
|
|
|
|
|
|
| 5 |
from transformers import pipeline
|
| 6 |
import os
|
| 7 |
import openai
|
| 8 |
+
import requests
|
| 9 |
+
import json
|
| 10 |
+
|
| 11 |
openai.organization = "org-5Z0c3Uk1VG7t3TsczN6M4FCi"
|
| 12 |
#openai.api_key = os.getenv("OPENAI_API_KEY")
|
| 13 |
openai.api_key_path ="./key.txt"
|
|
|
|
| 38 |
result = pipe(image)
|
| 39 |
return result[0]['label']
|
| 40 |
|
| 41 |
+
def analyze_nutrition(ingredients):
|
| 42 |
+
# Edamam API endpoint for nutrition analysis
|
| 43 |
+
endpoint = "https://api.edamam.com/api/nutrition-data"
|
| 44 |
+
# Edamam API application ID and key
|
| 45 |
+
app_id = "26722303"
|
| 46 |
+
app_key = "44f19a04e17d83e91706e4047804e690"
|
| 47 |
+
|
| 48 |
+
for ingredient in ingredients:
|
| 49 |
+
# Parameters for the API request
|
| 50 |
+
params = {
|
| 51 |
+
"app_id": app_id,
|
| 52 |
+
"app_key": app_key,
|
| 53 |
+
"ingr": ingredient
|
| 54 |
+
}
|
| 55 |
+
print(ingredient)
|
| 56 |
+
|
| 57 |
+
try:
|
| 58 |
+
# Send a GET request to the API
|
| 59 |
+
response = requests.get(endpoint, params=params)
|
| 60 |
+
|
| 61 |
+
# Check if the request was successful
|
| 62 |
+
if response.status_code == 200:
|
| 63 |
+
# Parse the JSON response
|
| 64 |
+
data = response.json()
|
| 65 |
+
|
| 66 |
+
# Print the nutritional information for the ingredient
|
| 67 |
+
print("Nutritional Information for", ingredient)
|
| 68 |
+
print("Calories:", data['calories'])
|
| 69 |
+
print("Calories from Protein:", data['totalNutrientsKCal']['PROCNT_KCAL']['quantity'])
|
| 70 |
+
print("Calories from Fat:", data['totalNutrientsKCal']['FAT_KCAL']['quantity'])
|
| 71 |
+
print("Calories from Carbohydrates:", data['totalNutrientsKCal']['CHOCDF_KCAL']['quantity'])
|
| 72 |
+
print("Grams in Protein:", data['totalNutrients']['PROCNT']['quantity'])
|
| 73 |
+
print("Grams in Carbohydrates:", data['totalNutrients']['CHOCDF']['quantity'])
|
| 74 |
+
print() # Add a newline for separation
|
| 75 |
+
|
| 76 |
+
else:
|
| 77 |
+
print("Error for", ingredient, ":", response.status_code)
|
| 78 |
+
|
| 79 |
+
except requests.exceptions.RequestException as e:
|
| 80 |
+
print("Error for", ingredient, ":", e)
|
| 81 |
+
|
| 82 |
+
# Example ingredients list
|
| 83 |
+
ingredients = ["Orange per 100 grams", "Apple per 100 grams", "Banana per 100 grams"]
|
| 84 |
|
| 85 |
+
# Analyze nutrition for all ingredients
|
| 86 |
+
analyze_nutrition(ingredients)
|