From 6bad38df52f8356b7e54b427b5a9c4dfbe0d0b47 Mon Sep 17 00:00:00 2001 From: Fahad Faruqi Date: Sun, 22 Oct 2023 02:42:30 -0400 Subject: [PATCH] nutritional information python file complete --- ml/nutritional.py | 49 +++++++++++++++++++------------------------ nutritional_info.json | 21 +++++++++++++++++++ 2 files changed, 43 insertions(+), 27 deletions(-) create mode 100644 nutritional_info.json diff --git a/ml/nutritional.py b/ml/nutritional.py index 47aa288..2ae7a4d 100644 --- a/ml/nutritional.py +++ b/ml/nutritional.py @@ -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}") \ No newline at end of file diff --git a/nutritional_info.json b/nutritional_info.json new file mode 100644 index 0000000..edbcab2 --- /dev/null +++ b/nutritional_info.json @@ -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%" + } +} \ No newline at end of file