wittummm commited on
Commit
a116079
1 Parent(s): c5ad6ee

Upload 4 files

Browse files
Files changed (4) hide show
  1. README.md +10 -1
  2. app.py +22 -0
  3. gpt_model.py +93 -0
  4. requirements.txt +3 -0
README.md CHANGED
@@ -1,3 +1,12 @@
1
  ---
2
- license: unknown
 
 
 
 
 
 
 
3
  ---
 
 
 
1
  ---
2
+ title: Gpt3 Chatbot
3
+ emoji: 馃敟
4
+ colorFrom: indigo
5
+ colorTo: indigo
6
+ sdk: gradio
7
+ sdk_version: 3.16.2
8
+ app_file: app.py
9
+ pinned: false
10
  ---
11
+
12
+ Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
app.py ADDED
@@ -0,0 +1,22 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import openai
2
+ import gradio as gr
3
+ import uuid
4
+ import os
5
+ from gpt_model import *
6
+
7
+ openai.api_key = os.environ.get('secret_key')
8
+
9
+ gpt = GPT_Model(append_output_prefix_to_query=True)
10
+
11
+ gpt.add_example(Example('hi','Hello, how are you?'))
12
+ gpt.add_example(Example('What is Neo Ivy Capital','The Neo Ivy Capital Fund is a quantitative hedge fund that invests in liquid, publicly traded equity securities via artificial intelligence strategies.'))
13
+ gpt.add_example(Example('Who is the CEO of Neo Ivy Capital','The CEO of Neo Ivy Capital is Renee Yao'))
14
+
15
+ def chatbot(input, history=[]):
16
+ output = gpt.submit_request(input)
17
+ history.append((input, output))
18
+ return history, history
19
+
20
+ gr.Interface(fn = chatbot,
21
+ inputs = ["text",'state'],
22
+ outputs = ["chatbot",'state']).launch()
gpt_model.py ADDED
@@ -0,0 +1,93 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import openai
2
+ import uuid
3
+
4
+ class Example:
5
+ def __init__(self, inp, out):
6
+ self.input = inp
7
+ self.output = out
8
+ self.id = uuid.uuid4().hex
9
+
10
+ def get_input(self):
11
+ return self.input
12
+
13
+ def get_output(self):
14
+ return self.output
15
+
16
+ def get_id(self):
17
+ return self.id
18
+
19
+ def as_dict(self):
20
+ return {
21
+ 'input': self.get_input(),
22
+ 'ouput': self.get_output(),
23
+ 'id': self.get_id()
24
+ }
25
+
26
+ class GPT_Model:
27
+ def __init__(self,
28
+ engine = 'text-davinci-003',
29
+ temperature = 0.5,
30
+ max_tokens = 1024,
31
+ input_prefix = 'input: ',
32
+ input_suffix = '\n',
33
+ output_prefix = 'output: ',
34
+ output_suffix = '\n\n',
35
+ append_output_prefix_to_query=False):
36
+ self.examples = {}
37
+ self.engine = engine
38
+ self.temperature = temperature
39
+ self.max_tokens = max_tokens
40
+ self.input_prefix = input_prefix
41
+ self.input_suffix = input_suffix
42
+ self.output_prefix = output_prefix
43
+ self.output_suffix = output_suffix
44
+ self.append_output_prefix_to_query = append_output_prefix_to_query
45
+
46
+ def add_example(self,ex):
47
+ assert isinstance(ex,Example), "Please create an example object"
48
+ self.examples[ex.get_id()] = ex
49
+
50
+ def delete_example(self,id):
51
+ if id in self.examples:
52
+ del self.examples[id]
53
+
54
+ def get_exmaple(self,id):
55
+ return self.examples.get(id,'Input id unavailable')
56
+
57
+ def get_all_example(self):
58
+ return {k:v.as_dict() for k,v in self.examples.items()}
59
+
60
+ def get_prime_text(self):
61
+ return "".join([self.format_example(ex) for ex in self.examples.values()])
62
+
63
+ def get_engine(self):
64
+ return self.engine
65
+
66
+ def get_temperature(self):
67
+ return self.temperature
68
+
69
+ def get_max_tokens(self):
70
+ return self.max_tokens
71
+
72
+ def craft_query(self, prompt):
73
+ q = self.get_prime_text() + self.input_prefix + prompt + self.input_suffix
74
+ if self.append_output_prefix_to_query:
75
+ q = q + self.output_prefix
76
+ return q
77
+
78
+ def submit_request(self, prompt):
79
+ completions = openai.Completion.create(
80
+ engine=self.get_engine(),
81
+ prompt=self.craft_query(prompt),
82
+ max_tokens=self.get_max_tokens(),
83
+ n=1,
84
+ temperature=self.get_temperature(),
85
+ )
86
+
87
+ message = completions.choices[0].text
88
+ return message.strip()
89
+
90
+ def format_example(self,ex):
91
+ return self.input_prefix+ex.get_input()+self.input_suffix+self.output_prefix+ex.get_output()+self.output_suffix
92
+
93
+
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ openai
2
+ gradio
3
+ uuid