-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
125 lines (99 loc) · 3.26 KB
/
app.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
import atexit
import os
import openai
import requests
import random as r
from utils import get_files_recursively
class ChatGPT:
def __init__(self):
openai.api_key = os.getenv("OPENAI_API_KEY")
# Register the save_history method with atexit
#atexit.register(self.save_history)
self.model = "text-davinci-003"
self.conversation_history = ""
self.load_history()
def chat(self, prompt:str=None):
if not prompt:
prompt = input("you: ")
self.conversation_history += f"User: {prompt}\nAI:"
res = openai.Completion.create(
model=self.model,
#prompt=self.conversation_history,
prompt=prompt,
max_tokens=1000,
temperature=0)
reply = res.choices[0].text.strip()
self.conversation_history += reply + "\n"
print(f"{self.name}: {reply}")
return reply
@property
def talk(self):
while True:
prompt = input("you: ")
if prompt.lower() == "quit":
print("Goodbye!")
break
res = self.chat(prompt)
print(f"gpt:\n{res}")
def save_history(self):
from datetime import datetime
# Get current date and time
now = datetime.now()
# Format date and time as string
timestamp = now.strftime("%Y%m%d%H%M%S")
with open("history.txt", 'w') as f:
f.write(f"timestamp: {timestamp}")
f.write(self.conversation_history + "\n")
f.close()
print("history saved")
def load_history(self):
with open("history.txt", 'r') as f:
data = f.read()
self.conversation_history += data
f.close()
def clear_history(self):
yorn = input("ARE YOU SURE??? y or n: ")
if yorn.lower() == "y":
random_num = str(r.randint(1000, 9999))
confirm = input(f"Enter {random_num} to clear history: ")
if confirm == random_num:
with open("history.txt", 'w') as f:
data = f.write("")
f.close()
print("history cleared!")
def read_file(self, path:str=None):
if not path:
path = input("Enter path: ")
self.chat(files._get_file(path))
#class FileStorage:
# def __init__(self):
# pass
# def _get_file(self, path:str=None, name:str=None):
# if not path:
# path = input("Enter path: ")
# if not name:
# name = os.path.basename(path)
# dot = name.index(".")
# name = name[:dot]
# print(f"Name: {name}")
#
# with open(path, 'r') as f:
# data = f.read()
#
# self.__setattr__(name, data)
# print(f"{name} successfully uploaded")
# return data
#
openai.api_key = "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"
gpt = ChatGPT()
gpt.name = "Gpt # 1"
gpt2 = ChatGPT()
gpt2.name = "Gpt #2"
class Convo:
def __init__(self):
self.res = gpt.chat("start a conversation")
def go(self):
print(gpt2.chat(self.res))
print(gpt.chat(gpt2.chat(self.res)))
convo = Convo()
convo.go()