zachriek commited on
Commit
0bd4e99
1 Parent(s): 2ae8b0f
Files changed (1) hide show
  1. app.py +53 -0
app.py ADDED
@@ -0,0 +1,53 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import openai
3
+ import gradio as gr
4
+ from dotenv import load_dotenv
5
+
6
+ load_dotenv()
7
+
8
+ openai.api_key = os.getenv("API_KEY")
9
+
10
+ start_sequence = "\nAI: "
11
+ restart_sequence = "\nHuman: "
12
+
13
+ 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: "
14
+
15
+
16
+ def openai_create(prompt):
17
+ response = openai.Completion.create(
18
+ model="text-davinci-003",
19
+ prompt=prompt,
20
+ temperature=0.9,
21
+ max_tokens=150,
22
+ top_p=1,
23
+ frequency_penalty=0,
24
+ presence_penalty=0,
25
+ stop=[" Human:", " AI:"]
26
+ )
27
+ return response.choices[0].text
28
+
29
+
30
+ def chatgpt_clone(input, history):
31
+ history = history or []
32
+ s = list(sum(history, ()))
33
+ s.append(input)
34
+ inp = ' '.join(s)
35
+ output = openai_create(inp)
36
+ history.append((input, output))
37
+ return history, history
38
+
39
+
40
+ block = gr.Blocks()
41
+
42
+
43
+ with block:
44
+ gr.Markdown("""<h1><center>Build Your Own ChatGPT with OpenAI API & Gradio</center></h1>
45
+ """)
46
+ chatbot = gr.Chatbot().style(color_map=("#CE6400", "#0b0f19"))
47
+ message = gr.Textbox(placeholder=prompt)
48
+ state = gr.State()
49
+ submit = gr.Button("SEND", variant="primary")
50
+ submit.click(chatgpt_clone, inputs=[
51
+ message, state], outputs=[chatbot, state])
52
+
53
+ block.launch()