Skip to content

Commit

Permalink
nutritional information python file complete
Browse files Browse the repository at this point in the history
  • Loading branch information
LordFarquaadtheCreator committed Oct 22, 2023
1 parent 52592be commit 6bad38d
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 27 deletions.
49 changes: 22 additions & 27 deletions ml/nutritional.py
Original file line number Diff line number Diff line change
@@ -1,38 +1,33 @@
import requests
import csv
import json
import openai
import csv

# Replace with your GPT-4 API endpoint and API key
GPT4_API_ENDPOINT = "https://api.openai.com/v1/chat/completions"
# Set your OpenAI API key
GPT4_API_KEY = json.load(open("ml/openAIKey.json"))["token"]
openai.api_key = GPT4_API_KEY

# Food item for which you want nutritional information
food_item = "banana"
# Define the conversation
conversation = [
{"role": "user", "content": "Provide nutritional information for banana in json file format. Do not include any information that is not appropiate for the json file format"},
]

# Create a prompt for GPT-4
prompt = f"Provide nutritional information for {food_item}"

# Request data from GPT-4
response = requests.post(
GPT4_API_ENDPOINT,
model="gpt-4",
headers={"Authorization": f"Bearer {GPT4_API_KEY}"},
json={"prompt": prompt, "max_tokens": 100},
# Call the model
response = openai.ChatCompletion.create(
model="gpt-4", # Use the appropriate model
messages=conversation,
)
print("Response Status Code:", response.status_code)
print("Response Content:", response.text)
# if response.status_code == 200:
# # Extract nutritional information from the GPT-4 response
# nutritional_info = response.json()["choices"][0]["text"]

# # Save nutritional information to a CSV file
# with open("nutrition_info.csv", "w", newline="") as csv_file:
# writer = csv.writer(csv_file)
# writer.writerow(["Food Item", "Nutritional Information"])
# writer.writerow([food_item, nutritional_info])
# Extract the nutritional information from the response
nutritional_info = response["choices"][0]["message"]["content"]
json_object = json.loads(nutritional_info)

# Define the output JSON file name
output_file = "nutritional_info.json"

# print(f"Nutritional information for {food_item} saved to nutrition_info.csv")
# Write the nutritional information to a JSON file
with open(output_file, "w") as file:
json.dump(json_object, file, indent=4)

# else:
# print(response.status_code)
# print("Failed to retrieve nutritional information from GPT-4")
print(f"Nutritional information saved to {output_file}")
21 changes: 21 additions & 0 deletions nutritional_info.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"Banana": {
"Serving_Size": "1 medium (118g)",
"Calories": 105,
"Total_Fat": "0.3g",
"Sodium": "1mg",
"Potassium": "422mg",
"Total_Carbohydrate": "27g",
"Dietary_Fiber": "3.1g",
"Sugars": "14g",
"Protein": "1.3g",
"Vitamin_A": "1%",
"Vitamin_C": "17%",
"Calcium": "1%",
"Iron": "2%",
"Vitamin_D": "0%",
"Vitamin_B6": "20%",
"Vitamin_B12": "0%",
"Magnesium": "8%"
}
}

0 comments on commit 6bad38d

Please sign in to comment.