-
Notifications
You must be signed in to change notification settings - Fork 1
/
functions.py
162 lines (126 loc) · 4.75 KB
/
functions.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
158
159
160
161
162
import openai
# Importing constants for prompt
import tags
from samples.voicedictation.colon import colon_eg as colon
from samples.voicedictation.deleteLastWord import delete_eg as deleteLastWord
from samples.voicedictation.parenthesis import parenthesis_eg as parenthesis
from samples.voicedictation.period import period_eg as period
from samples.voicedictation.send import send_eg as send
from samples.voicedictation.space import space_eg as space
from samples.grammar.capital import capital_eg as capital
from samples.grammar.repetetion import repetetion_eg as repetetion
def grammerCorrection(content):
'''
Returns a string with standard American english Grammer
Parameters:
content (str): content (sentence, paragraph) by the user
Returns:
response.choices[0].text (str): text response from the API without grammatical errors
'''
text = f"{tags.grammar_fix}{capital}{repetetion}{tags.promptStart}{content}{tags.promptEnd}"
response = openai.Completion.create(
engine="davinci",
prompt=text,
temperature=0.3,
max_tokens=400,
top_p=1,
frequency_penalty=0,
presence_penalty=0,
stop=["###"]
)
return response.choices[0].text
def modal_one(content):
'''
Returns a summary using the curie engine from completion endpoint
Parameters:
content (str): content (sentence, paragraph) by the user
Returns:
response.choices[0].text (str): text response from the API
'''
text = f"{content} {tags.tldr}"
response = openai.Completion.create(
engine="curie",
prompt=text,
temperature=0.3,
max_tokens=140,
top_p=1,
frequency_penalty=0,
presence_penalty=0,
stop=["\n"]
)
return response.choices[0].text
def modal_two(content):
'''
Returns a summary using the davinci engine from completion endpoint
Parameters:
content (str): content (sentence, paragraph) by the user
Returns:
response.choices[0].text (str): text response from the API
'''
text = f"{content} {tags.tldr}"
response = openai.Completion.create(
engine="davinci",
prompt=text,
temperature=0.3,
max_tokens=140,
top_p=1,
frequency_penalty=0,
presence_penalty=0,
stop=["\n"]
)
return response.choices[0].text
def modal_three(content):
'''
Returns a summary using the davinci engine from completion endpoint
Parameters:
content (str): content (sentence, paragraph) by the user
Returns:
response.choices[0].text (str): text response from the API
'''
text = f"{content} {tags.inotherwords}"
response = openai.Completion.create(
engine="davinci",
prompt=text,
temperature=0,
max_tokens=140,
top_p=1,
frequency_penalty=0,
presence_penalty=0,
stop=["."]
)
return response.choices[0].text
def summarizer(query):
'''
Returns the same string as the user's query but removes voice dictation error like "delete last word, period, colon, parenthesis, send, space"
Parameters:
query (str): Query (sentence, paragraph) by the user
Returns:
Res (str); Response without voice dictation errors (space, period, delete last word)
'''
text = f"{tags.verbatim_fix}{space}{period}{deleteLastWord}{colon}{send}{parenthesis}{tags.promptStart}{query}{tags.promptEnd}"
response = openai.Completion.create(
engine="davinci",
prompt=text,
temperature=0.3,
max_tokens=400,
top_p=1,
frequency_penalty=0,
presence_penalty=0,
stop=["###"]
)
responseText = response.choices[0].text
final = grammerCorrection(responseText)
return final
def summarizer2(query):
'''
Returns three different summaries of the query by the user
Parameters:
query (str): Query (sentence, paragraph) by the user
Returns:
[ summary1 , summary2 , summary3 ] : list of different summary of the query by the user
'''
text = grammerCorrection(query)
r1 = modal_one(text)
r2 = modal_two(text)
r3 = modal_three(text)
return [r1,r2,r3,text]