Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import openai
|
3 |
+
import gradio as gr
|
4 |
+
|
5 |
+
#if you have OpenAI API key as an environment variable, enable the below
|
6 |
+
#openai.api_key = os.getenv("OPENAI_API_KEY")
|
7 |
+
|
8 |
+
#if you have OpenAI API key as a string, enable the below
|
9 |
+
openai.api_key = "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"
|
10 |
+
|
11 |
+
start_sequence = "\nAI:"
|
12 |
+
restart_sequence = "\nHuman: "
|
13 |
+
|
14 |
+
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: "
|
15 |
+
|
16 |
+
def openai_create(prompt):
|
17 |
+
|
18 |
+
response = openai.Completion.create(
|
19 |
+
model="text-davinci-003",
|
20 |
+
prompt=prompt,
|
21 |
+
temperature=0.9,
|
22 |
+
max_tokens=150,
|
23 |
+
top_p=1,
|
24 |
+
frequency_penalty=0,
|
25 |
+
presence_penalty=0.6,
|
26 |
+
stop=[" Human:", " AI:"]
|
27 |
+
)
|
28 |
+
|
29 |
+
return response.choices[0].text
|
30 |
+
|
31 |
+
|
32 |
+
|
33 |
+
def chatgpt_clone(input, history):
|
34 |
+
history = history or []
|
35 |
+
s = list(sum(history, ()))
|
36 |
+
s.append(input)
|
37 |
+
inp = ' '.join(s)
|
38 |
+
output = openai_create(inp)
|
39 |
+
history.append((input, output))
|
40 |
+
return history, history
|
41 |
+
|
42 |
+
|
43 |
+
block = gr.Blocks()
|
44 |
+
|
45 |
+
|
46 |
+
with block:
|
47 |
+
gr.Markdown("""<h1><center>Build Yo'own ChatGPT with OpenAI API & Gradio</center></h1>
|
48 |
+
""")
|
49 |
+
chatbot = gr.Chatbot()
|
50 |
+
message = gr.Textbox(placeholder=prompt)
|
51 |
+
state = gr.State()
|
52 |
+
submit = gr.Button("SEND")
|
53 |
+
submit.click(chatgpt_clone, inputs=[message, state], outputs=[chatbot, state])
|
54 |
+
|
55 |
+
block.launch(debug = True)
|