File size: 2,087 Bytes
b860ef2
 
 
 
 
 
21579d0
 
b860ef2
 
 
dff2fa0
 
 
 
 
 
 
6c86ba2
 
dff2fa0
 
b860ef2
 
 
dff2fa0
b860ef2
dff2fa0
b860ef2
 
 
 
 
dff2fa0
b860ef2
 
 
 
 
dff2fa0
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
70
71
72
## Import Libraries, and read the key-token
import openai
import os
import requests
import streamlit as st

openai.api_key = os.getenv("OPENAI_API_KEY")

## paramas

def fit_text(text:str):
    ## Optimized system prompt
    system_prompt = 'Generate an image based on the provided description.'

    user_prompt = f'''Given the input text "{text}", please generate an image that embodies the following characteristics: 
    - Clear and detailed representation
    - Relevant to the provided description
    - Incorporating key elements and context
    - Creative and visually appealing
    - Translate the text into English'''

    ## Messages
    messages = [ 
        {'role': 'system', 'content': system_prompt}, 
        {'role': 'user', 'content': user_prompt}
    ]

    ## Using ChatGPT for Chat Completion
    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.")