File size: 3,632 Bytes
53be45e 7a69293 3200dde 6f1daf1 7a69293 53be45e dfb7e1f 6f1daf1 dfb7e1f 6f1daf1 53be45e dfb7e1f 53be45e 3200dde 8ada8ab 3200dde 8ada8ab 3200dde 8ada8ab 3200dde 8ada8ab 3200dde 8ada8ab 3200dde 8ada8ab e9a5d73 8ada8ab |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# This is the main logic file that contains hugging face model interaction
# This model is for detecting food in the image.
# Use a pipeline as a high-level helper
from transformers import pipeline
import os
import openai
import requests
import json
from openai import OpenAI
openai.organization = "org-5Z0c3Uk1VG7t3TsczN6M4FCi"
#openai.api_key = os.getenv("OPENAI_API_KEY")
openai.api_key_path ="./key.txt"
def askGPT(prompt="what can I make with potato?"):
client = OpenAI(
base_url='http://localhost:11434/v1/',
# required but ignored
api_key='ollama',
)
chat_completion = client.chat.completions.create(
messages=[
{
'role': 'user',
'content': prompt,
}
],
model='llama3',
)
result = chat_completion.choices[0].message.content
#print(chat_completion.choices[0].message.content)
return result
'''def askGPT(prompt="what can I make with potato?"):
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[
{
"role": "system",
"content":prompt
},
{
"role": "user",
"content": ""
} ],
temperature=1,
max_tokens=256,
top_p=1,
frequency_penalty=0,
presence_penalty=0
)
result = response["choices"][0]["message"]["content"]
return result'''
def classifyImage(image):
pipe = pipeline("image-classification", model="microsoft/resnet-50")
result = pipe(image)
return result[0]['label']
def analyze_nutrition(ingredients):
# Edamam API endpoint for nutrition analysis
endpoint = "https://api.edamam.com/api/nutrition-data"
# Edamam API application ID and key
app_id = "26722303"
app_key = "44f19a04e17d83e91706e4047804e690"
processed_ingredients = set()
food_dict= {}
for ingredient in ingredients:
if ingredient in processed_ingredients:
continue
# Parameters for the API request
params = {
"app_id": app_id,
"app_key": app_key,
"ingr": ingredient
}
try:
# Send a GET request to the API
response = requests.get(endpoint, params=params)
# Check if the request was successful
if response.status_code == 200:
# Parse the JSON response
data = response.json()
food_dict[ingredient] = {
'Calories': str(data['calories']) + "kcal",
'Calories from Protein': str(data['totalNutrientsKCal']['PROCNT_KCAL']['quantity']) + "kcal",
'Calories from Fat': str(data['totalNutrientsKCal']['FAT_KCAL']['quantity']) + "kcal",
'Calories from Carbohydrates': str(data['totalNutrientsKCal']['CHOCDF_KCAL']['quantity']) + "kcal",
'Grams in Protein': str(data['totalNutrients']['PROCNT']['quantity']) + "g",
'Grams in Carbohydrates': str(data['totalNutrients']['CHOCDF']['quantity']) +"g"
}
processed_ingredients.add(ingredient)
else:
print("Error for", ingredient, ":", response.status_code)
except requests.exceptions.RequestException as e:
print("Error for", ingredient, ":", e)
return food_dict
# Example ingredients list
# ingredients = ["Orange per 100 grams", "Apple per 100 grams", "Banana per 100 grams"]
# # # Analyze nutrition for all ingredients
# print(analyze_nutrition(ingredients))
|