-
Notifications
You must be signed in to change notification settings - Fork 2
/
GPT_API_without_embeddings.py
471 lines (438 loc) · 16.7 KB
/
GPT_API_without_embeddings.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
import streamlit as st
import os
import openai
openai.api_key = "Enter your OpenAI API Key here."
if "visibility" not in st.session_state:
st.session_state.visibility = "visible"
st.session_state.disabled = False
# Sidebar Design
with st.sidebar:
response=st.radio("Please choose an operation..",('Void',
'General Queries',
'Grammer and Spell Check',
'Summarize Text',
'Q&A',
'Language Translation',
'Language Detection',
'Detect and Translate',
'Code Explanation',
'Generate SQL Queries',
'Programming Language Conversion',
'Sentiment Analysis',
'Extract Keywords',
'Text Generator from keywords',
'Essay Outline Generator',
'Essay Generator'))
match response:
case 'Void':
st.write('You have not selected any operation yet!!!')
case 'General Queries':
st.write('You have selected general queries.')
case 'Grammer and Spell Check':
st.write('You have selected grammer and spell check.')
case 'Summarize Text':
st.write('You have selected for summarizing text.')
case 'Q&A':
st.write('You have selected for questionnaire.')
case 'Language Translation':
st.write('You have selected language translation.')
case 'Language Detection':
st.write('You have selected language detection.')
case 'Detect and Translate':
st.write('You have selected for language detection and translation.')
case 'Code Explanation':
st.write('You have selected for code explanation.')
case 'Generate SQL Queries':
st.write('You have selected for generating SQL queries.')
case 'Programming Language Conversion':
st.write('You have selected for converting a code snippet to another programming language.')
case 'Sentiment Analysis':
st.write('You have selected for sentiment analysis.')
case 'Extract Keywords':
st.write('You have selected for extracting keywords from text.')
case 'Text Generator from keywords':
st.write('You have selected for generating text from keywords')
case 'Essay Outline Generator':
st.write('You have selected for generating outline for an essay.')
case 'Essay Generator':
st.write('You have selected for generating an essay.')
def general(text):
response = openai.Completion.create(
model="text-davinci-003",
prompt=text,
temperature=0,
max_tokens=1000,
top_p=1,
frequency_penalty=0,
presence_penalty=0
)
return response['choices'][0]['text'].strip()
def grammer(text):
response = openai.Completion.create(
model="text-davinci-003",\
prompt="Correct this to standard English:"+text,
temperature=0,
max_tokens=1000,
top_p=1,
frequency_penalty=0,
presence_penalty=0
)
return response['choices'][0]['text'].strip()
def summary(text):
response = openai.Completion.create(
model="text-davinci-003",
prompt="Summarize this for a second-grade student:"+text,
temperature=0.01,
max_tokens=1000,
top_p=1,
frequency_penalty=0,
presence_penalty=0
)
return response['choices'][0]['text'].strip()
def questionnaire(question):
response = openai.Completion.create(
model="text-davinci-003",
prompt="Answer the question: "+question,
temperature=0,
max_tokens=140,
top_p=1,
frequency_penalty=0,
presence_penalty=0
)
return response['choices'][0]['text'].strip()
def translation(target, text):
response = openai.Completion.create(
model="text-davinci-003",
prompt="Translate "+text+" to "+target,
temperature=0,
max_tokens=140,
top_p=1,
frequency_penalty=0,
presence_penalty=0
)
return response['choices'][0]['text'].strip()
def identify_language(text):
response = openai.Completion.create(
model="text-davinci-003",
prompt="Detect the language of "+text,
temperature=0,
max_tokens=140,
top_p=1,
frequency_penalty=0,
presence_penalty=0
)
return response['choices'][0]['text'].strip()
def detect_translate(target, text):
result=[]
detected = identify_language(text)
result.append(detected)
trans = translation(target, text)
result.append(trans)
return result
def code_explain(code):
response = openai.Completion.create(
model="text-davinci-003",
prompt="Explain what the mentioned code is doing: "+code,
max_tokens=150,
top_p=1,
frequency_penalty=0,
presence_penalty=0,
stop=["\"\"\""]
)
return response['choices'][0]['text'].strip()
def sql_queries(query,schema=""):
response = openai.Completion.create(
model="text-davinci-003",
prompt=schema+" An SQL query to "+query,
temperature=0,
max_tokens=150,
top_p=1,
frequency_penalty=0,
presence_penalty=0,
stop=["#", ";"]
)
return response['choices'][0]['text'].strip()
def sentiment(text):
response = openai.Completion.create(
model="text-davinci-003",
prompt="Classify the sentiment of this text:"+text,
temperature=0,
max_tokens=500,
top_p=1,
frequency_penalty=0,
presence_penalty=0
)
return response['choices'][0]['text'].strip()
def keywords(text):
response = openai.Completion.create(
model="text-davinci-003",
prompt="Extract keywords from this text: "+text,
temperature=0,
max_tokens=500,
top_p=1,
frequency_penalty=0,
presence_penalty=0
)
return response['choices'][0]['text'].strip()
def text_generator(keywords, char):
response = openai.Completion.create(
model="text-davinci-003",
prompt="Generate a paragraph in "+char+" characters using keywords: "+keywords,
temperature=0,
max_tokens=500,
top_p=1,
frequency_penalty=0,
presence_penalty=0
)
return response['choices'][0]['text'].strip()
def essay_outline(topic):
response = openai.Completion.create(
model="text-davinci-003",
prompt="Create an outline for an essay about"+topic,
temperature=0,
max_tokens=3000,
top_p=1,
frequency_penalty=0,
presence_penalty=0
)
return response['choices'][0]['text'].strip()
def essay_generator(topic,outline="",limit="0"):
response = openai.Completion.create(
model="text-davinci-003",
prompt="Write an essay in "+limit+" words about "+topic+"using the outline"+outline,
temperature=0,
max_tokens=3000,
top_p=1,
frequency_penalty=0,
presence_penalty=0
)
return response['choices'][0]['text'].strip()
match response:
case 'Void':
st.header('This application is a one-stop solution for your NLP needs and more....')
case 'General Queries':
st.header('General Queries')
text = st.text_input(
"Enter your query here 👇*",
label_visibility=st.session_state.visibility,
disabled=st.session_state.disabled,
key="1")
if text:
result=general(text)
st.subheader("Output:")
st.write(result)
case 'Grammer and Spell Check':
st.header('Grammer and Spell Check')
inputtext = st.text_input(
"Enter your text here 👇*",
label_visibility=st.session_state.visibility,
disabled=st.session_state.disabled,
key="2")
if inputtext:
result=grammer(inputtext)
st.subheader("Output:")
st.write(result)
case 'Summarize Text':
st.header('Summarize Text')
article = st.text_input(
"Enter your article here 👇*",
label_visibility=st.session_state.visibility,
disabled=st.session_state.disabled,
key="3")
if article:
output=summary(article)
st.subheader("Output:")
st.write(output)
case 'Q&A':
st.header('Questionnaire')
question = st.text_input(
"Enter your question here 👇*",
label_visibility=st.session_state.visibility,
disabled=st.session_state.disabled,
key="4")
if question:
result=questionnaire(question)
st.subheader("Answer: ")
st.write(result)
case 'Language Translation':
st.header('Language Translation')
target = st.text_input(
"Enter your target language here 👇*",
label_visibility=st.session_state.visibility,
disabled=st.session_state.disabled,
key="5")
text = st.text_input(
"Enter your text here 👇*",
label_visibility=st.session_state.visibility,
disabled=st.session_state.disabled,
key="6")
if text and target:
output=translation(target, text)
st.subheader('Translated Text:')
st.write(output)
case 'Language Detection':
st.header('Language Detection')
text = st.text_input(
"Enter your text here 👇*",
label_visibility=st.session_state.visibility,
disabled=st.session_state.disabled,
key="7")
if text:
output=identify_language(text)
st.subheader("Output:")
st.write(output)
case 'Detect and Translate':
st.header('Detect and Translate')
target = st.text_input(
"Enter your target language here 👇*",
label_visibility=st.session_state.visibility,
disabled=st.session_state.disabled,
key="8")
text = st.text_input(
"Enter your text here 👇*",
label_visibility=st.session_state.visibility,
disabled=st.session_state.disabled,
key="9")
if text and target:
st.subheader('Language: ')
output=detect_translate(target, text)
st.write(output[0])
st.subheader('Translation: ')
st.write(output[1])
case 'Code Explanation':
st.header('Code Explanation')
code = st.text_input(
"Enter your code snippet here 👇*",
label_visibility=st.session_state.visibility,
disabled=st.session_state.disabled,
key="10")
if code:
result=code_explain(code)
st.subheader("Code explanation:")
st.write(result)
case 'Generate SQL Queries':
st.header('Generate SQL Queries')
query = st.text_input(
"Enter your query objective here 👇*",
label_visibility=st.session_state.visibility,
disabled=st.session_state.disabled,
key="11")
schema= st.text_input(
"Enter your schema here 👇",
label_visibility=st.session_state.visibility,
disabled=st.session_state.disabled,
key="12")
if query and schema:
output=sql_queries(query, schema)
st.subheader('Schema provided: ')
st.write(schema)
st.subheader('Query Objective: ')
st.write(query)
st.subheader('Query Generated: ')
st.write(output)
elif query:
output=sql_queries(query)
st.subheader('Query Objective: ')
st.write(query)
st.subheader('Query Generated: ')
st.write(output)
case 'Programming Language Conversion':
st.header('Convert Code Snippet from one Programming Language to another')
target = st.text_input(
"Enter your target here 👇*",
label_visibility=st.session_state.visibility,
disabled=st.session_state.disabled,
key="13")
code = st.text_input(
"Enter your code here 👇*",
label_visibility=st.session_state.visibility,
disabled=st.session_state.disabled,
key="14")
if target and code:
st.subheader('Generated Code: ')
result=translation(target, code)
st.write(result)
case 'Sentiment Analysis':
st.header('Sentiment Analysis')
text = st.text_input(
"Enter your text here 👇*",
label_visibility=st.session_state.visibility,
disabled=st.session_state.disabled,
key="15")
if text:
output=sentiment(text)
st.subheader("Sentiment of the text:")
st.write(output)
case 'Extract Keywords':
st.header('Extract Keywords from text')
text = st.text_input(
"Enter your text here 👇*",
label_visibility=st.session_state.visibility,
disabled=st.session_state.disabled,
key="16")
if text:
output=keywords(text)
st.subheader("Output:")
st.write(output)
case 'Text Generator from keywords':
st.header('Generate Text from keywords')
words = st.text_input(
"Enter your keywords here 👇*",
label_visibility=st.session_state.visibility,
disabled=st.session_state.disabled,
key="17")
limit = st.text_input(
"Enter your limit here 👇*",
label_visibility=st.session_state.visibility,
disabled=st.session_state.disabled,
key="18")
if words and limit:
output=text_generator(words, limit)
st.subheader("Generated Text:")
st.write(output)
case 'Essay Outline Generator':
st.header('Generate Outline for Essay')
topic = st.text_input(
"Enter your topic here 👇*",
label_visibility=st.session_state.visibility,
disabled=st.session_state.disabled,
key="19")
if topic:
output=essay_outline(topic)
st.subheader("Essay Outline:")
st.write(output)
case 'Essay Generator':
st.header('Generate Essay')
topic = st.text_input(
"Enter your topic here 👇*",
label_visibility=st.session_state.visibility,
disabled=st.session_state.disabled,
key="20")
outline = st.text_input(
"Enter your outline here 👇",
label_visibility=st.session_state.visibility,
disabled=st.session_state.disabled,
key="21")
limit = st.text_input(
"Enter your limit here 👇",
label_visibility=st.session_state.visibility,
disabled=st.session_state.disabled,
key="22")
if topic and outline:
if limit:
output=essay_generator(topic, outline=outline,limit=limit)
st.subheader('Generated Essay:')
st.write(output)
else:
output=essay_generator(topic,outline=outline)
st.subheader('Generated Essay:')
st.write(output)
elif topic:
if limit:
output=essay_generator(topic, limit=limit)
st.subheader('Generated Essay:')
st.write(output)
else:
output=essay_generator(topic)
st.subheader('Generated Essay:')
st.write(output)