GreenRaptor commited on
Commit
163fcbf
·
1 Parent(s): 2f4f46f

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +57 -0
app.py ADDED
@@ -0,0 +1,57 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 = ""
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
+ message = [{"role": "user", "content": prompt}]
19
+
20
+ response = openai.ChatCompletion.create(
21
+ model="gpt-4",
22
+ messages=message,
23
+ temperature=0.9,
24
+ max_tokens=150,
25
+ top_p=1,
26
+ frequency_penalty=0,
27
+ presence_penalty=0.6,
28
+ stop=[" Human:", " AI:"]
29
+ )
30
+
31
+ return response["choices"][0]["message"]["content"]
32
+
33
+
34
+
35
+ def chatgpt_clone(input, history):
36
+ history = history or []
37
+ s = list(sum(history, ()))
38
+ s.append(input)
39
+ inp = ' '.join(s)
40
+ output = openai_create(inp)
41
+ history.append((input, output))
42
+ return history, history
43
+
44
+
45
+ block = gr.Blocks()
46
+
47
+
48
+ with block:
49
+ gr.Markdown("""<h1><center>Build Yo'own ChatGPT with OpenAI API & Gradio</center></h1>
50
+ """)
51
+ chatbot = gr.Chatbot()
52
+ message = gr.Textbox(placeholder=prompt)
53
+ state = gr.State()
54
+ submit = gr.Button("SEND")
55
+ submit.click(chatgpt_clone, inputs=[message, state], outputs=[chatbot, state])
56
+
57
+ block.launch(debug = True)