aaronsiim commited on
Commit
2de9182
·
1 Parent(s): d9e2860

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +63 -0
app.py ADDED
@@ -0,0 +1,63 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import openai
3
+ import gradio as gr
4
+
5
+ openai.api_key = "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"
6
+
7
+ # Says it the text is from AI or Human
8
+ start_sequence = "\nAI:"
9
+ restart_sequence = "\nHuman: "
10
+
11
+ # Input box prompt
12
+ prompt = "Hey! I'm an AI. How can I help? "
13
+
14
+ # CREATE FUNCTION FOR RESPONSE
15
+ # Function takes one argument that is "prompt"
16
+
17
+
18
+ def openai_create(prompt):
19
+
20
+ response = openai.Completion.create(
21
+ model="text-davinci-003",
22
+ prompt=prompt,
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
+ # Taking the response and returning the text
32
+ return response.choices[0].text
33
+
34
+
35
+ # FUNCTION TAKING ARGUMENTS (input, history)
36
+ # Function is required for gradio application to work (arguments wrapped inside a function)
37
+ # input = takes text
38
+ # history = stores the state in the app to keep the knowledge of the context of the memory
39
+ def chatgpt_app(input, history):
40
+ history = history or [] # Empty at first
41
+ s = list(sum(history, ()))
42
+ s.append(input) # Send the current message to the openAI + as a whole
43
+ inp = ' '.join(s)
44
+ output = openai_create(inp)
45
+ history.append((input, output)) # Response gets added as output
46
+ return history, history # History gets added one input, one output
47
+
48
+
49
+ # INTERFACE STARTs
50
+ # Display of chatGPT like Blocks
51
+ block = gr.Blocks()
52
+
53
+ with block:
54
+ gr.Markdown("""<h1><center>Text-To-Text Python Gradio App with OpenAI API</center></h1>
55
+ """)
56
+ chatbot = gr.Chatbot()
57
+ message = gr.Textbox(placeholder=prompt)
58
+ state = gr.State()
59
+ submit = gr.Button("SEND")
60
+ submit.click(chatgpt_app, inputs=[
61
+ message, state], outputs=[chatbot, state])
62
+
63
+ block.launch(debug=True)