Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -2,54 +2,72 @@ import os
|
|
2 |
import openai
|
3 |
import gradio as gr
|
4 |
|
5 |
-
|
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 = "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"
|
10 |
|
11 |
start_sequence = "\nAI:"
|
12 |
restart_sequence = "\nHuman: "
|
13 |
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
prompt=prompt,
|
21 |
-
temperature=0.9,
|
22 |
-
max_tokens=150,
|
23 |
-
top_p=1,
|
24 |
-
frequency_penalty=0,
|
25 |
-
presence_penalty=0.6,
|
26 |
-
stop=[" Human:", " AI:"]
|
27 |
-
)
|
28 |
|
29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
30 |
|
|
|
31 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
32 |
|
33 |
-
def
|
34 |
history = history or []
|
35 |
-
|
36 |
-
s.append(input)
|
37 |
-
inp = ' '.join(s)
|
38 |
-
output = openai_create(inp)
|
39 |
history.append((input, output))
|
40 |
-
return history, history
|
41 |
|
|
|
|
|
|
|
|
|
|
|
42 |
|
43 |
block = gr.Blocks()
|
44 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
45 |
|
46 |
-
|
47 |
-
gr.Markdown("""<h1><center>Build Yo'own ChatGPT with OpenAI API & Gradio</center></h1>
|
48 |
-
""")
|
49 |
-
chatbot = gr.Chatbot()
|
50 |
-
message = gr.Textbox(placeholder=prompt)
|
51 |
-
state = gr.State()
|
52 |
-
submit = gr.Button("SEND")
|
53 |
-
submit.click(chatgpt_clone, inputs=[message, state], outputs=[chatbot, state])
|
54 |
-
|
55 |
-
block.launch(debug = True)
|
|
|
2 |
import openai
|
3 |
import gradio as gr
|
4 |
|
5 |
+
openai.api_key = "sk-xxxxxxxxxxxxxxxxxxxxxxxxxxx"
|
|
|
|
|
|
|
|
|
6 |
|
7 |
start_sequence = "\nAI:"
|
8 |
restart_sequence = "\nHuman: "
|
9 |
|
10 |
+
prompt_txt = "输入你的问题"
|
11 |
+
prompt_img = "输入你的图像提示词"
|
12 |
+
|
13 |
+
messages = [
|
14 |
+
{"role": "system", "content": "you are a very useful assistant"},
|
15 |
+
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
|
17 |
+
def openai_create(input):
|
18 |
+
user_message = {"role": "user", "content": input}
|
19 |
+
messages.append(user_message)
|
20 |
+
completion = openai.ChatCompletion.create(
|
21 |
+
model="gpt-3.5-turbo-16k",
|
22 |
+
messages=messages,
|
23 |
+
)
|
24 |
+
completion = completion.choices[0].message.content
|
25 |
+
messages.append({"role": "assistant", "content": completion})
|
26 |
|
27 |
+
return completion
|
28 |
|
29 |
+
def chatgpt_image(input, image_size, history,image_input):
|
30 |
+
completion = openai.Image.create(
|
31 |
+
prompt=input,
|
32 |
+
n=1,
|
33 |
+
size=image_size
|
34 |
+
)
|
35 |
+
return completion.data[0]['url'], history, ""
|
36 |
|
37 |
+
def chatgpt_chat(input, history,input_text):
|
38 |
history = history or []
|
39 |
+
output = openai_create(input)
|
|
|
|
|
|
|
40 |
history.append((input, output))
|
41 |
+
return history, history, ""
|
42 |
|
43 |
+
def clear(input,output):
|
44 |
+
history = []
|
45 |
+
input = ''
|
46 |
+
output = ''
|
47 |
+
return "","",""
|
48 |
|
49 |
block = gr.Blocks()
|
50 |
|
51 |
+
with gr.Blocks() as block:
|
52 |
+
gr.Markdown("""<h1><center>ChatGPT Bot</center></h1>""")
|
53 |
+
|
54 |
+
with gr.Tab("聊天"):
|
55 |
+
chatbot = gr.Chatbot()
|
56 |
+
text_input = gr.Textbox(placeholder=prompt_txt)
|
57 |
+
state = gr.State()
|
58 |
+
with gr.Row():
|
59 |
+
text_button = gr.Button("发送")
|
60 |
+
clear_button = gr.Button("清空")
|
61 |
+
|
62 |
+
with gr.Tab("图像"):
|
63 |
+
image_output = gr.Image(width=512, height=512)
|
64 |
+
image_input = gr.Textbox(placeholder=prompt_img)
|
65 |
+
image_size = gr.Radio(["256x256", "512x512", "1024x1024"],value="256x256",label="图片尺寸", info="选择生成的图片尺寸")
|
66 |
+
state = gr.State()
|
67 |
+
image_button = gr.Button("生成")
|
68 |
+
|
69 |
+
text_button.click(chatgpt_chat, inputs=[text_input, state], outputs=[chatbot, state, text_input])
|
70 |
+
clear_button.click(clear, inputs=[text_input,state], outputs=[chatbot,state,text_input])
|
71 |
+
image_button.click(chatgpt_image, inputs=[image_input, image_size, state], outputs=[image_output, state, image_input])
|
72 |
|
73 |
+
block.launch(debug=True, server_name="0.0.0.0", server_port=8000, share=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|