-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathWizeline_christmas_project.py
113 lines (92 loc) · 4.6 KB
/
Wizeline_christmas_project.py
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
# Don't forget to install openai's pip
# pip install openai
import csv
import re
import openai
import os
# Ask the user for relevant information
file_name = input("Enter the name of the CSV file (including the .csv extension): ")
person_name = input("Enter the name of the person you are giving the gift to: ")
person_gender = input("Enter the gender of the person: ")
person_age = input("Enter the approximate age of the person: ")
person_data = "The person is " + person_age + " years old and their gender is " + person_gender
# Ask the user to enter the OpenAI API key
openai_api_key = input("Enter your OpenAI API key: ")
os.environ["OPENAI_API_KEY"] = openai_api_key
# Reads the csv file, limits it to a maximum of 1000 users and cleans the data for easy reading by the AI
def read_and_clean_csv(file_name, limit=1000):
followed_users = []
with open(file_name, mode='r', encoding='utf-8') as csvfile:
reader = csv.reader(csvfile)
for row in reader:
if row and len(followed_users) < limit: # Check if the row is not empty and the limit has not been reached
user = row[4] # Assuming usernames are in the fifth column (index 4)
clean_user = re.sub(r'[0-9]', '', user) # Remove numbers
clean_user = clean_user.replace('_', ' ').replace('.', ' ') # Replace '_' and '.' with spaces
followed_users.append(clean_user)
else:
break # Break the loop if the limit is reached
return followed_users
# Store the gift recommendations
gifts = read_and_clean_csv(file_name)
# Generate the Amazon search links for Mexico
def generate_amazon_link(search_query):
words = search_query.split()
amazon_url = "https://www.amazon.com.mx/s?k=" + '+'.join(words)
return amazon_url
# Store the links
amazon_links = [generate_amazon_link(gift) for gift in gifts]
# Introductory text for the gift list
print(f"Given the list of followed users you provided for {person_name}, I think they might like these gifts: ")
for gift, link in zip(gifts, amazon_links):
print(f"Gift: {gift}, Amazon Link: {link}")
def generate_explanations(followed_users, gifts, person_name):
# Create a prompt to explain the gifts
prompt = f"Explain why the following gifts are suitable for {person_name}, who follows these people and topics: {', '.join(followed_users)}.\n"
for gift in gifts:
prompt += f"- {gift}\n"
prompt += "Explanation:"
try:
response = openai.Completion.create(
model="gpt-4", # ChatGPT-4 model
prompt=prompt,
max_tokens=500 # Approximately $0.00833
)
return response.choices[0].text.strip()
except Exception as e:
print(f"Error generating explanations: {e}")
return ""
# Print the explanation
explanations = generate_explanations(followed_users, gifts, person_name)
print("The explanation for these gifts is:", explanations)
# Generate the image with Dall-E 3
def generate_dalle_image(description, openai_api_key):
openai.api_key = openai_api_key
try:
response = openai.Image.create(
model="dall-e-3", # DALL·E 3 model
prompt=description,
n=1, # Number of images to generate
size="1024x1024" # Image size
)
return response.data[0]['url'] # URL of the generated image
except Exception as e:
print(f"Error generating the image: {e}")
return ""
# Print the image URL
description = f"A Christmas scene with a decorated tree and the three suggested gifts. Which are: {gifts}"
image_url = generate_dalle_image(description, openai_api_key)
print("Generated image URL:", image_url)
# Example of cost calculation considering the length of ChatGPT responses and a DALL-E image
# Estimated rates
CHATGPT_TOKEN_PRICE = 0.00002 # Estimated price per token for ChatGPT
DALLE_IMAGE_PRICE = 0.02 # Price for an image generated by DALL·E
# Token estimation: Assuming each token is approximately 4 characters long
num_tokens_gifts = sum(len(gift) for gift in gifts) / 4
num_tokens_amazon = sum(len(link) for link in amazon_links) / 4
num_tokens_explanation = len("The explanation for these gifts is: " + explanations) / 4
# Calculate the total cost of the tokens
token_cost = (num_tokens_gifts + num_tokens_amazon + num_tokens_explanation) * CHATGPT_TOKEN_PRICE
# Calculate the total cost including a DALL·E image
total_cost = token_cost + DALLE_IMAGE_PRICE
print(f"The total cost in US dollars for this execution was: ${total_cost:.5f}")