poulta commited on
Commit
f1c1b75
·
1 Parent(s): 038d27b

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +67 -0
app.py ADDED
@@ -0,0 +1,67 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+
3
+ import openai
4
+ import gradio as gr
5
+
6
+ openai.api_key = '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'
7
+ #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
8
+
9
+ def clean_textbox(*args):
10
+ n = len(args)
11
+ return [""] * n
12
+
13
+
14
+ class ChatGPT:
15
+ def __init__(self):
16
+ self.messages = [{'role': 'system', 'content': "You are now a very useful maid assistant! If you have a question you can't answer, please reply with As a classy girl, I can't answer this question"}]
17
+
18
+ def reset(self, *args):
19
+ self.messages = [{'role': 'system', 'content': "You are now a very useful maid assistant! If you have a question you can't answer, please reply with As a classy girl, I can't answer this question"}]
20
+ return clean_textbox(*args)
21
+
22
+ def chat(self, prompt):
23
+ self.messages.append({"role": "user", "content": prompt})
24
+ completion = openai.ChatCompletion.create(
25
+ model="gpt-3.5-turbo",
26
+ messages=self.messages,
27
+ temperature=0.7
28
+ )
29
+ res_msg = completion.choices[0].message["content"].strip()
30
+ self.messages.append({"role": "assistant", "content": res_msg})
31
+ return res_msg
32
+
33
+
34
+ if __name__ == '__main__':
35
+ my_chatgpt = ChatGPT()
36
+ with gr.Blocks(title="ChatGPT") as demo:
37
+ gr.Markdown('''
38
+ # ChatGPT
39
+ ''')
40
+ with gr.Row():
41
+ with gr.Column(scale=9):
42
+ prompt = gr.Text(label='ChatGPT_Prompt', show_label=False, lines=3,
43
+ placeholder='ChatGPT Prompt')
44
+ res = gr.Text(label='ChatGPT_result', show_label=False, lines=3,
45
+ placeholder='chatgpt results')
46
+
47
+ with gr.Column(scale=1):
48
+ btn_gen = gr.Button(value="send", variant='primary')
49
+ btn_clear = gr.Button(value="restart chat")
50
+
51
+ gr.Examples([
52
+ ["How to Become a My channel Grow more Pyresearch youtube channel"],
53
+ ["Suppose there is a pond with an infinite amount of water in it. There are currently 2 empty jugs with volumes of 5 liters and 6 liters respectively. The problem is how to get 3 liters of water from the pond with only these 2 jugs."],
54
+ ["Please help me with C++Write quick sort code."]],
55
+ inputs=[prompt],
56
+ outputs=[res],
57
+ fn=my_chatgpt.chat,
58
+ cache_examples=False)
59
+
60
+ btn_gen.click(fn=my_chatgpt.chat, inputs=prompt,
61
+ outputs=res)
62
+ btn_clear.click(fn=my_chatgpt.reset,
63
+ inputs=[prompt, res],
64
+ outputs=[prompt, res])
65
+
66
+ demo.queue()
67
+ demo.launch()