Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,73 +0,0 @@
|
|
1 |
-
import os
|
2 |
-
import openai
|
3 |
-
import gradio as gr
|
4 |
-
|
5 |
-
openai.api_key = "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"
|
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)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|