Spaces:
Build error
Build error
add app
Browse files- streamlit.py +68 -0
streamlit.py
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
## Import Libraries, and read the key-token
|
| 2 |
+
import openai
|
| 3 |
+
import os
|
| 4 |
+
import requests
|
| 5 |
+
import streamlit as st
|
| 6 |
+
|
| 7 |
+
## Load the Key token
|
| 8 |
+
# key_token = os.getenv('OpenAI_KEY_TOKEN')
|
| 9 |
+
openai.api_key = os.getenv("OPENAI_KEY")
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
## paramas
|
| 13 |
+
|
| 14 |
+
def fit_text(text:str):
|
| 15 |
+
## prompts
|
| 16 |
+
system_prompt = 'You are a smart input form for Dall-E model'
|
| 17 |
+
user_prompt = f'''It receives the text from the user {text}, translates it into English,
|
| 18 |
+
and reorganizes the text to make the Dall-E model understand it and use it easily to create a good image.
|
| 19 |
+
'''
|
| 20 |
+
## messages
|
| 21 |
+
messages = [
|
| 22 |
+
{'role': 'system', 'content': system_prompt},
|
| 23 |
+
{'role': 'user', 'content': user_prompt}
|
| 24 |
+
]
|
| 25 |
+
|
| 26 |
+
## Using ChatGPT for ChatCompletion
|
| 27 |
+
response = openai.ChatCompletion.create(
|
| 28 |
+
model='gpt-3.5-turbo',
|
| 29 |
+
messages=messages,
|
| 30 |
+
temperature=0.7,
|
| 31 |
+
max_tokens=1000,
|
| 32 |
+
)
|
| 33 |
+
|
| 34 |
+
response = response['choices'][0]['message']['content']
|
| 35 |
+
|
| 36 |
+
return response
|
| 37 |
+
|
| 38 |
+
def generate_image(description:str):
|
| 39 |
+
response = openai.Image.create(
|
| 40 |
+
prompt=description, ## The description for the image
|
| 41 |
+
n=1,
|
| 42 |
+
size='1024x1024'
|
| 43 |
+
)
|
| 44 |
+
|
| 45 |
+
image_url = response['data'][0]['url']
|
| 46 |
+
return image_url
|
| 47 |
+
|
| 48 |
+
st.set_page_config(page_title="Image Generation")
|
| 49 |
+
|
| 50 |
+
st.header("Image Generation Application 🤖💬")
|
| 51 |
+
inputs = st.text_input("Enter a description of the image you want to create : ", key="input")
|
| 52 |
+
|
| 53 |
+
if st.button("Generate Image"):
|
| 54 |
+
if len(inputs) > 3:
|
| 55 |
+
user_description = fit_text(inputs)
|
| 56 |
+
image_url = generate_image(user_description)
|
| 57 |
+
st.image(image_url, caption="Generated Image", use_column_width=True)
|
| 58 |
+
|
| 59 |
+
st.download_button(
|
| 60 |
+
label="Download image as *.png",
|
| 61 |
+
data=image_url,
|
| 62 |
+
file_name='image.png',
|
| 63 |
+
mime='image',
|
| 64 |
+
)
|
| 65 |
+
|
| 66 |
+
|
| 67 |
+
else:
|
| 68 |
+
st.warning("Please provide a description with more than 3 characters.")
|