-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
157 lines (139 loc) · 5.07 KB
/
main.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import os
import openai
import wget
from PIL import Image
import sys
import time
import os
from colorama import Fore
openai.organization = "org-XY7tvMhByFMbnaHiVRXoQjab"
openai.api_key = os.getenv("OPENAI_API_KEY")
openai.Model.list()
start_sequence = "\nAI:"
restart_sequence = "\nHuman: "
user_directory = os.path.expanduser('~')
user_path = user_directory.split('\\')
local_download_path = user_path[0]+"\\"+user_path[1]+"\\"+user_path[2]+"\\AppData\\Local\\OpenAIWallpaper"
# global errorhandling program
def errorHandling(param1, param2):
while True:
try:
valid_inp = int(
input("\nOut of this which option would you like to choose ? : "))
if valid_inp >= param1 and valid_inp <= param2:
return valid_inp
except ValueError:
print("Error! Enter an integer value!! You dumb🤬")
except KeyboardInterrupt:
print("OOPs feelin' like very strong keyboard stroke⌨️")
raise Exception("Thanks for coming!!")
# This function feeding from Dall-e API and convert text to drawing
def Dalle():
while (True):
user_query = input(
Fore.CYAN+"\nEnter any topic or query realted to the drawing you want : ")
#model
response = openai.Image.create(
prompt=user_query,
n=1,
size="1024x1024"
)
img_url = response['data'][0]['url']
wallpaper = wget.download(img_url, local_download_path)
words = wallpaper.split("/")
photo_path = local_download_path+"\\" + \
words[1]
image = Image.open(photo_path)
image.save(local_download_path+words[1], quality=45, optimize=True)
image_path1 = local_download_path+"\\" + \
words[1]
image = Image.open(image_path1)
image.show()
# This function makes the variation of photos
#^ Note: To use this function, either change the path of the Image.open(__) with image name or just shift the image on this path
def variation_img():
image = Image.open(
local_download_path+"\\photo.png")
new_img = image.resize((400, 400))
new_img.save(local_download_path+'\\photo.png')
response = openai.Image.create_variation(
image=open(local_download_path+"\\photo.png", "rb"),
n=1,
size="1024x1024"
)
img_url = response['data'][0]['url']
wallpaper = wget.download(img_url, local_download_path)
words = wallpaper.split("/")
photo_path = local_download_path+"\\"+words[1]
image = Image.open(photo_path)
image.save(local_download_path+words[1], quality=45, optimize=True)
image_path1 = local_download_path+"\\"+words[1]
image = Image.open(image_path1)
image.show()
#This function write code for almost anything only in python
def codex_ai():
while(True):
prompts = input(Fore.GREEN+"\n\nEnter your query : ")
response = openai.Completion.create(
engine = 'davinci-codex',
max_tokens=1024*2,
prompt = prompts,
temperature = 0, #risk taking ability, influencing creativity
top_p=1.0, #Influencing sampling
frequency_penalty=0.0, #Penalities for repeated tokens
presence_penalty=0.0, #Penalities for new words
stop=['#'] #when to stop generating stuff
)
result = response.choices[0].text
for char in result:
sys.stdout.write(Fore.MAGENTA+char)
sys.stdout.flush()
time.sleep(0.01)
#This function works on gpt-2 model and can interact with our queries and get us the most suitable answer of our question
def assistant_ai():
while(True):
prompt = input(Fore.RED+"\n\nAsk me anything : ")
response = openai.Completion.create(
model="text-davinci-003",
prompt=prompt,
temperature=0.9,
max_tokens=1024,
top_p=1,
frequency_penalty=0,
presence_penalty=0.6,
stop=[" Human:", " AI:"]
)
result = response.choices[0].text
for char in result:
sys.stdout.write(Fore.YELLOW+char)
sys.stdout.flush()
time.sleep(0.01)
# & Main function .....
if __name__ == '__main__':
print("How would u like to surf my new project ?\n1.Text to Image (warning: model is too stupid for now )\n2.Image Variations\n3.Codex Api (kind of mysterious tool for code)\n4.Ask for anything (This one is kind of addictive)")
valid_inp_main = 0
# another local error handling for input validation
while True:
try:
valid_inp_main = int(
input("\nOut of this which option would you like to choose ? : "))
if valid_inp_main >= 1 and valid_inp_main <= 4:
break
else:
print("Enter in range of [1,4]")
except ValueError:
print("Error! Enter an integer")
# switch cases as per response
match valid_inp_main:
# It will call dalle function
case 1:
Dalle()
# It will call whole image variation function
case 2:
variation_img()
# It will call codex function
case 3:
codex_ai()
# It will call assistant function
case 4:
assistant_ai()