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