File size: 2,027 Bytes
b860ef2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
## Import Libraries, and read the key-token
import openai
import os
import requests
import streamlit as st

## Load the Key token
# key_token = os.getenv('OpenAI_KEY_TOKEN')
openai.api_key = os.getenv("OPENAI_KEY")


## paramas

def fit_text(text:str):
    ## prompts
    system_prompt = 'You are a smart input form for Dall-E model'
    user_prompt = f'''It receives the text from the user {text}, translates it into English, 
    and reorganizes the text to make the Dall-E model understand it and use it easily to create a good image. 
    '''
    ## messages
    messages = [ 
        {'role': 'system', 'content': system_prompt}, 
        {'role': 'user', 'content': user_prompt}
            ]

    ## Using ChatGPT for ChatCompletion
    response = openai.ChatCompletion.create(
                model='gpt-3.5-turbo',           
                messages=messages,
                temperature=0.7,  
                max_tokens=1000,  
                    )

    response = response['choices'][0]['message']['content']  

    return response

def generate_image(description:str):
    response = openai.Image.create(
                prompt=description,   ## The description for the image
                n=1,    
                size='1024x1024'    
            )

    image_url = response['data'][0]['url'] 
    return image_url

st.set_page_config(page_title="Image Generation")

st.header("Image Generation Application 🤖💬")
inputs = st.text_input("Enter a description of the image you want to create : ", key="input")

if st.button("Generate Image"):
    if len(inputs) > 3:
        user_description = fit_text(inputs)
        image_url = generate_image(user_description)
        st.image(image_url, caption="Generated Image", use_column_width=True)

        st.download_button(
            label="Download image as *.png",
            data=image_url,
            file_name='image.png',
            mime='image',
        )


    else:
        st.warning("Please provide a description with more than 3 characters.")