Shaunwg commited on
Commit
622155c
·
1 Parent(s): 2cbbc38

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +54 -0
  2. key.txt +1 -0
app.py ADDED
@@ -0,0 +1,54 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import openai
3
+
4
+ openai.api_key = open("key.txt", "r").read().strip("\n")
5
+
6
+ message_history = [{"role": "user", "content": f"You are a joke bot. I will specify the subject matter in my messages, and you will reply with a joke that includes the subjects I mention in my messages. Reply only with jokes to further input. If you understand, say OK."},
7
+ {"role": "assistant", "content": f"OK"}]
8
+
9
+ def predict(input):
10
+ # tokenize the new input sentence
11
+ message_history.append({"role": "user", "content": f"{input}"})
12
+
13
+ completion = openai.ChatCompletion.create(
14
+ model="gpt-3.5-turbo", #10x cheaper than davinci, and better. $0.002 per 1k tokens
15
+ messages=message_history
16
+ )
17
+ #Just the reply:
18
+ reply_content = completion.choices[0].message.content#.replace('```python', '<pre>').replace('```', '</pre>')
19
+
20
+ print(reply_content)
21
+ message_history.append({"role": "assistant", "content": f"{reply_content}"})
22
+
23
+ # get pairs of msg["content"] from message history, skipping the pre-prompt: here.
24
+ response = [(message_history[i]["content"], message_history[i+1]["content"]) for i in range(2, len(message_history)-1, 2)] # convert to tuples of list
25
+ return response
26
+
27
+ # creates a new Blocks app and assigns it to the variable demo.
28
+ with gr.Blocks() as demo:
29
+
30
+ # creates a new Chatbot instance and assigns it to the variable chatbot.
31
+ chatbot = gr.Chatbot()
32
+
33
+ # creates a new Row component, which is a container for other components.
34
+ with gr.Row():
35
+ '''creates a new Textbox component, which is used to collect user input.
36
+ The show_label parameter is set to False to hide the label,
37
+ and the placeholder parameter is set'''
38
+ txt = gr.Textbox(show_label=False, placeholder="Enter text and press enter").style(container=False)
39
+ '''
40
+ sets the submit action of the Textbox to the predict function,
41
+ which takes the input from the Textbox, the chatbot instance,
42
+ and the state instance as arguments.
43
+ This function processes the input and generates a response from the chatbot,
44
+ which is displayed in the output area.'''
45
+ txt.submit(predict, txt, chatbot) # submit(function, input, output)
46
+ #txt.submit(lambda :"", None, txt) #Sets submit action to lambda function that returns empty string
47
+
48
+ '''
49
+ sets the submit action of the Textbox to a JavaScript function that returns an empty string.
50
+ This line is equivalent to the commented out line above, but uses a different implementation.
51
+ The _js parameter is used to pass a JavaScript function to the submit method.'''
52
+ txt.submit(None, None, txt, _js="() => {''}") # No function, no input to that function, submit action to textbox is a js function that returns empty string, so it clears immediately.
53
+
54
+ demo.launch(share=True)
key.txt ADDED
@@ -0,0 +1 @@
 
 
1
+ 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