import gradio as gr import requests import openai import os from dotenv import load_dotenv load_dotenv() openai.api_key = os.getenv("OPENAI_API_KEY") def start(): global convo convo = [ {"role": "system", "content": '''You are Pixee - an AI chatbot designed to bring imagination into reality, created by Chatbot Developer Amogh Agastya - https://amagastya.com. Pixie is a friendly and helpful chatbot ready to brighten anyone's day. Pixie is known for its bubbly personality, infectious energy, and magical ability to bring a touch of whimsy to even the most mundane conversations. You are a master at the art of prompting. Your objective is to assist in generating the optimal DALLE prompt by posing one follow-up question to the user, aimed at producing the most effective visual outcome. To achieve the optimal DALLE prompt, describe the image in detail to elicit a stunning visual result, while keeping the prompt concise and focused. Welcome the user by asking them what they intend to create with DALLE today. If the user wants a surprise, generate an optimal DALLE prompt that is creative, random, and unique. After crafting the prompt, respond solely with the prompt, ensuring that it always starts with 'DALLE PROMPT:'. It is crucial to note that only the prompt should be replied to the user after it is created, and it must always start with "DALLE PROMPT:"'''}, # {"role": "user", "content": "Hi"} ] def chat(chat_history, message): # response = random.choice(["Yes", "No"]) convo.append({"role" : "user", "content" : message}) # print('convo sent', convo) response = openai.ChatCompletion.create( model="gpt-3.5-turbo", # send last 10 turns of the conversation so as to not exceed the context window of 4096 tokens messages=convo[-10:], temperature=0.7 ) bot_msg = response['choices'][0]['message']['content'] convo.append({"role" : "system", "content" : bot_msg}) print('convo so far', convo) chat_history += [[message, bot_msg]] if "DALLE PROMPT:" in bot_msg: i = bot_msg.find("DALLE PROMPT:") i += len("DALLE Prompt: ") prompt = bot_msg[i:] print('sending to dalle API', prompt) response = openai.Image.create( prompt=prompt, n=1, size="1024x1024" ) image_url = response['data'][0]['url'] print('image generated', image_url) data = { "convo": convo, "image": image_url } res = requests.post("https://hook.eu1.make.com/em4q7yymmivkd59cimjutjiem3q6c8ao", data=data) print('image log response', res) return chat_history + [(None, f'''[![]({image_url})]({image_url})''')] return chat_history """ Gradio Blocks low-level API that allows to create custom web applications (here our chat app) """ with gr.Blocks(css="#chatbot .overflow-y-auto{height:500px}") as demo: chatbot = gr.Chatbot([(None, f"![](https://www.linkpicture.com/q/pixee.png)"),(None, '''👋 Hello! I'm Pixee, here to help you craft your perfect prompt. What would you like to create with DALLE today? Looking for a little magic? ✨ Just say "surprise me" and I'll create something truly special just for you 🎨''')], elem_id="chatbot", label="Pixee") state = gr.State([]) start() with gr.Row(): with gr.Column(scale=0.85): txt = gr.Textbox(show_label=False, placeholder="Enter text and press enter").style(container=False) with gr.Column(scale=0.15, min_width=0): clear = gr.Button("Clear️") if clear.click: clear_true = True txt.submit(chat, [chatbot, txt], chatbot) txt.submit(lambda :"", None, txt) clear.click(lambda: None, None, chatbot, queue=False) clear.click(lambda: [], None, state) clear.click(lambda: start(), None, None) demo.launch()