|
|
|
|
|
import gradio as gr |
|
|
import os |
|
|
import openai |
|
|
|
|
|
openai.api_key = os.getenv("OPENAI_API_KEY") |
|
|
|
|
|
def paint(prompt): |
|
|
try: |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
response = openai.Image.create( |
|
|
prompt=prompt, |
|
|
n=1, |
|
|
size="1024x1024" |
|
|
) |
|
|
except Exception as e: |
|
|
print(e) |
|
|
return e |
|
|
|
|
|
return response["data"][0]["url"] |
|
|
|
|
|
print("你是一位画家,需要根据用户的描述绘画,并提供相应的下载地址") |
|
|
|
|
|
def answer(question, history=[]): |
|
|
history.append(question) |
|
|
response = paint(question) |
|
|
history.append(response) |
|
|
responses = [(u,b) for u,b in zip(history[::2], history[1::2])] |
|
|
return responses, history |
|
|
|
|
|
with gr.Blocks(css="#chatbot{height:300px} .overflow-y-auto{height:500px}") as demo: |
|
|
chatbot = gr.Chatbot(elem_id="chatbot") |
|
|
state = gr.State([]) |
|
|
|
|
|
with gr.Row(): |
|
|
txt = gr.Textbox(show_label=False, placeholder="Enter text and press enter").style(container=False) |
|
|
|
|
|
txt.submit(answer, [txt, state], [chatbot, state]) |
|
|
|
|
|
demo.launch() |
|
|
|