File size: 4,549 Bytes
25e184d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import openai
import requests
import urllib.parse
import json
import gradio as gr
from PIL import Image, ImageDraw, ImageFont
from io import BytesIO
import os

openai.api_key = os.environ["OPEN_AI_KEY"]



png_bytes_array = []
images = []

system_prompt = """Transcript of a dialog, where the User interacts with an Assistant named Stablediffy. Stablediffy knows much about prompt engineering for stable diffusion (an open-source image generation software). The User asks Stablediffy about prompts for stable diffusion Image Generation.

Possible keywords for stable diffusion: "cinematic, colorful background, concept art, dramatic lighting, high detail, highly detailed, hyper realistic, intricate, intricate sharp details, octane render, smooth, studio lighting, trending on artstation, landscape, scenery, cityscape, underwater, salt flat, tundra, jungle, desert mountain, ocean, beach, lake, waterfall, ripples, swirl, waves, avenue, horizon, pasture, plateau, garden, fields, floating island, forest, cloud forest, grasslands, flower field, flower ocean, volcano, cliff, snowy mountain
city, cityscape, street, downtown"

-- Transcript --

USER: suggest a prompt for a young girl from Swiss sitting by the window with headphones on
ASSISTANT: gorgeous young Swiss girl sitting by window with headphones on, wearing white bra with translucent shirt over, soft lips, beach blonde hair, octane render, unreal engine, photograph, realistic skin texture, photorealistic, hyper realism, highly detailed, 85mm portrait photography, award winning, hard rim lighting photography

USER: suggest a prompt for an mysterious city
ASSISTANT: Mysterious city, cityscape, urban, downtown, street, noir style, cinematic lightning, dramatic lightning, intricate, sharp details, octane render, unreal engine, highly detailed, night scene, dark lighting, gritty atmosphere

USER: suggest a prompt for a high quality render of a car in 1950
ASSISTANT: Car in 1950, highly detailed, classic car, 1950's, highly detailed, dramatic lightning, cinematic lightning, unreal engine

USER:"""

def get_modified_text_response(user_query):
    try:
        response = openai.ChatCompletion.create(
            model="gpt-3.5-turbo",
            messages=[{"role": "system", "content": system_prompt},
                    {"role": "user", "content": "suggest a prompt for" +  user_query}]
        )
        res = response["choices"][0]["message"]["content"].replace('\'', '')
        print(res)
        return res
    except:
        return "no gpt response"

def text_to_image(text, selected_value):
    global png_bytes_array
    images = []
    if(selected_value == "New"):
        png_bytes_array = []
        image_urls = generate_image(text)
        for img in image_urls:
            response = requests.get(img['url'])
            image = Image.open(BytesIO(response.content))
            images.append(image)
            bytesIO = BytesIO()
            image.save(bytesIO, format="PNG")
            png_bytes = bytesIO.getvalue()
            png_bytes_array.append(png_bytes)
        return images
    
    else:
        index = int(selected_value[-1])
        image_urls = variation_image(png_bytes_array[index])
        for img in image_urls:
            response = requests.get(img['url'])
            image = Image.open(BytesIO(response.content))
            images.append(image)
        return images
    
def variation_image(image):
    response = openai.Image.create_variation(
    image = image,
    n=4,
    size="1024x1024"
        )
    #print(response)
    #image_url = response['data'][0]['url']
    return response['data']
def generate_image(prompt):
    better_prompt = get_modified_text_response(prompt)
    response = openai.Image.create(
    prompt=better_prompt,
    n=4,
    size="1024x1024"
        )

    return response['data']
def main():
    radio_buttons = gr.inputs.Radio(["New", "Var0","Var1", "Var2", "Var3"], label="Select a variation option")
    out = gr.Gallery(
            label="Generated images", show_label=False, elem_id="gallery"
        ).style(columns=[2], rows=[2], object_fit="contain", height="auto")
    iface = gr.Interface(fn=text_to_image, inputs=["text",radio_buttons], 
                         #outputs=gr.Gallery([gr.Image(type="pil").style(height=300,width=300),gr.Image(type="pil").style(height=300,width=300),gr.Image(type="pil").style(height=300,width=300),gr.Image(type="pil").style(height=300,width=300)]))
                         outputs = out)
    iface.launch()  

if __name__ == "__main__":
    main()