-
Notifications
You must be signed in to change notification settings - Fork 0
/
achatbot.py
86 lines (63 loc) · 2.52 KB
/
achatbot.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
import os
import openai
import gradio as gr
# openai and gradio two libraries we require to run this application
# API key generated on website https://platform.openai.com/account/api-keys
openai.api_key = "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"
start_sequence = "\nAI:"
restart_sequence = "\nHuman: "
prompt = "The following is a conversation with an AI assistant. The assistant is helpful, creative, clever, and very friendly.\n\nHuman: Hello, who are you?\nAI: I am an AI created by OpenAI. How can I help you today?\nHuman: "
def openai_create(prompt):
response = openai.Completion.create(
model="text-davinci-003",
prompt=prompt,
temperature=0.9,
max_tokens=4000,
top_p=1,
frequency_penalty=0,
presence_penalty=0.6,
stop=[" Human:", " AI:"]
)
return response.choices[0].text
# function define
# input for gradio
# for gradio application to work anything that has to be wrapped inside the function and
# that function should have an input that function should have an output
# one or more inputs..in this here input is a text
# history stores the state of the current gradio application
def chatgpt_clone(input, history):
# if history doesn't exist then it will be empty []
history = history or []
# take history convert to list
s = list(sum(history, ()))
# append the current input to history
s.append(input)
# store in inp
inp = ' '.join(s)
# generate response by openai_create function and response get store in output
output = openai_create(inp)
# history append one input and output
history.append((input, output))
# store as history, history then get displayed
return history, history
# to display as msg box we are using blocks which is advanced method to create gradio application
# block bcoz it gives the chatGPT style interface like from top to bottom
block = gr.Blocks()
# in block
with block:
# Title of block
gr.Markdown("""<h1><center> Alisha Chatbot </center></h1>
""")
# we need chatbot
chatbot = gr.Chatbot()
# we need input textbox
# prompt for msg to keep in start
message = gr.Textbox(placeholder=prompt)
# to store the state
state = gr.State()
# button to click send
submit = gr.Button("SEND")
# take input
submit.click(chatgpt_clone, inputs=[message, state], outputs=[chatbot, state])
# this will launch our application
block.launch(debug=True)