Spaces:
Runtime error
Runtime error
Commit
·
a335d59
1
Parent(s):
d607864
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,58 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import openai
|
3 |
+
import gradio as gr
|
4 |
+
|
5 |
+
openai.api_key = os.getenv("OPENAI_API_KEY")
|
6 |
+
|
7 |
+
prompt = "This is a concept verification of applying GPT-3 model for China AMC"
|
8 |
+
|
9 |
+
def openai_create(prompt):
|
10 |
+
response = openai.Completion.create(
|
11 |
+
model="text-davinci-003",
|
12 |
+
prompt=prompt,
|
13 |
+
temperature=0,
|
14 |
+
max_tokens=200,
|
15 |
+
top_p=1.0,
|
16 |
+
frequency_penalty=0.0,
|
17 |
+
presence_penalty=0.0
|
18 |
+
)
|
19 |
+
return response.choices[0].text
|
20 |
+
|
21 |
+
messages = [{"role": "system", "content": "You are a professional stock analyst."}]
|
22 |
+
|
23 |
+
def openai_chatcreate(prompt):
|
24 |
+
messages.append({"role":"user","content":prompt})
|
25 |
+
print(messages)
|
26 |
+
response = openai.ChatCompletion.create(
|
27 |
+
model='gpt-3.5-turbo',
|
28 |
+
messages=messages
|
29 |
+
)
|
30 |
+
messages.append(response.choices[0]["message"])
|
31 |
+
return response.choices[0]["message"]["content"]
|
32 |
+
|
33 |
+
def chatgpt_chatclone(input, history):
|
34 |
+
history = history or []
|
35 |
+
s = list(sum(history,()))
|
36 |
+
s.append(input)
|
37 |
+
output = openai_chatcreate(input)
|
38 |
+
history.append((input, output))
|
39 |
+
print(history)
|
40 |
+
return history, history
|
41 |
+
|
42 |
+
def chatgpt_clear(input, history):
|
43 |
+
messages = []
|
44 |
+
return "", [], []
|
45 |
+
|
46 |
+
blocks = gr.Blocks()
|
47 |
+
|
48 |
+
with blocks:
|
49 |
+
chatbot = gr.Chatbot()
|
50 |
+
message = gr.Textbox(placeholder=prompt)
|
51 |
+
state = gr.State()
|
52 |
+
clear = gr.Button("Clear")
|
53 |
+
clear.click(fn=chatgpt_clear, inputs=[message, state], outputs=[message, chatbot, state])
|
54 |
+
submit = gr.Button("Send")
|
55 |
+
submit.click(fn=chatgpt_chatclone, inputs=[message, state], outputs=[chatbot, state])
|
56 |
+
|
57 |
+
|
58 |
+
blocks.launch(share=True, debug=False)
|