Spaces:
Running
Running
## 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.") | |