import openai import gradio as gr from gradio.components import Textbox, Image, Text # Set up OpenAI API credentials openai.api_key = "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" model_path = "models/dreamlike-art/dreamlike-photoreal-2.0" model_prefix = "nsfw, photoreal style" model = gr.Interface.load(model_path) base_prompt = "give only one answer, limit is 36 words" # Define the OpenAI prompt completion function def generate_text(prompt): response = openai.Completion.create( model="text-davinci-003", prompt=f"{prompt}\n\nQ: {base_prompt}\nA:", #prompt=prompt, max_tokens=64, n=1, stop=None, temperature=0.5, ) return response.choices[0].text.strip() # Define the image generation function def generate_image(prompt): # Generate text based on prompt text = generate_text(prompt) # Generate image based on text image = model(text) return text, image # Define the Gradio interface prompt_input = Textbox("Enter a prompt", lines=3) text_output = Text("Output will appear here", lines=10) image_output = Image() iface = gr.Interface( generate_image, inputs=prompt_input, outputs=[text_output, image_output], title="Image Generator", description="Generates an image based on a prompt using OpenAI's GPT-3 and DALL-E models." ) # Update the output boxes with the generated text and image def update_output(text, image_url): # Get the current text in the output box current_text = text_output.value # Add the new text to the current text new_text = current_text + "\n\n" + text # Update the text output box with the new text text_output.update(new_text) iface.interface_result_hooks = [update_output] # Launch the interface iface.launch()