Spaces:
Sleeping
Sleeping
Commit ·
ed69556
1
Parent(s): 31f880d
Initial commit
Browse files- .gitignore +3 -0
- app.py +29 -0
- model.py +67 -0
- requirements.txt +4 -0
- streamlit_utlis.py +20 -0
.gitignore
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
config.yaml
|
| 2 |
+
.streamlit/*
|
| 3 |
+
__pycache__/*
|
app.py
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from model import generate_image, generate_image_prompt, process_image_prompt
|
| 2 |
+
import streamlit as st
|
| 3 |
+
from streamlit_utlis import check_password
|
| 4 |
+
|
| 5 |
+
if not check_password():
|
| 6 |
+
st.stop()
|
| 7 |
+
|
| 8 |
+
st.title('Image Generator🐶')
|
| 9 |
+
description = st.text_input(r'Describe the image you want. I will give you a few prompt to choose 🐻❄️')
|
| 10 |
+
prompt = ''
|
| 11 |
+
|
| 12 |
+
if st.button('GENERATE PROMPT 🐯'):
|
| 13 |
+
st.success('Generating prompts for you, please wait... 🦁')
|
| 14 |
+
response = generate_image_prompt(description)
|
| 15 |
+
result = process_image_prompt(response)
|
| 16 |
+
|
| 17 |
+
st.subheader("Here are some prompts for you to use🐧")
|
| 18 |
+
for i in range(len(result)):
|
| 19 |
+
st.text(result[i].replace("-", str(i+1)))
|
| 20 |
+
|
| 21 |
+
st.divider()
|
| 22 |
+
|
| 23 |
+
prompt = st.text_input(r"Get a prompt from above or enter your own 🦝")
|
| 24 |
+
if st.button('GENERATE IMAGE 🐥'):
|
| 25 |
+
st.success('Generating image for you, please wait... 🐨')
|
| 26 |
+
result = generate_image(prompt)
|
| 27 |
+
|
| 28 |
+
st.subheader("Here is your image... 🦭")
|
| 29 |
+
st.image(result, caption=prompt)
|
model.py
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from openai import AzureOpenAI
|
| 2 |
+
import json
|
| 3 |
+
from io import BytesIO
|
| 4 |
+
import requests
|
| 5 |
+
import re
|
| 6 |
+
import streamlit as st
|
| 7 |
+
|
| 8 |
+
def get_client():
|
| 9 |
+
client = AzureOpenAI(
|
| 10 |
+
api_version="2024-05-01-preview",
|
| 11 |
+
azure_endpoint=st.secrets['endpoint'],
|
| 12 |
+
api_key=st.secrets['key'],
|
| 13 |
+
)
|
| 14 |
+
return client
|
| 15 |
+
|
| 16 |
+
def generate_image(prompt):
|
| 17 |
+
client = get_client()
|
| 18 |
+
result = client.images.generate(
|
| 19 |
+
model="Dalle3",
|
| 20 |
+
prompt=prompt,
|
| 21 |
+
n=1
|
| 22 |
+
)
|
| 23 |
+
|
| 24 |
+
image_url = json.loads(result.model_dump_json())['data'][0]['url']
|
| 25 |
+
result = requests.get(image_url)
|
| 26 |
+
return BytesIO(result.content)
|
| 27 |
+
|
| 28 |
+
def generate_image_prompt(prompt):
|
| 29 |
+
payload = {
|
| 30 |
+
"messages": [
|
| 31 |
+
{
|
| 32 |
+
"role": "system",
|
| 33 |
+
"content": [
|
| 34 |
+
{
|
| 35 |
+
"type": "text",
|
| 36 |
+
"text": "You give a few examples of english prompts that help generate image base on user's input. Return prompts in bullet point"
|
| 37 |
+
}
|
| 38 |
+
]
|
| 39 |
+
},
|
| 40 |
+
{
|
| 41 |
+
"role": "user",
|
| 42 |
+
"content": [
|
| 43 |
+
{
|
| 44 |
+
"type": "text",
|
| 45 |
+
"text": prompt
|
| 46 |
+
}
|
| 47 |
+
]
|
| 48 |
+
}
|
| 49 |
+
],
|
| 50 |
+
"temperature": 0.9,
|
| 51 |
+
"top_p": 0.95,
|
| 52 |
+
"max_tokens": 800
|
| 53 |
+
}
|
| 54 |
+
|
| 55 |
+
response = requests.post(st.secrets['completionendpoint'], headers={"Content-Type": "application/json", "api-key": st.secrets['key']}, json=payload)
|
| 56 |
+
response.raise_for_status() # Will raise an HTTPError if the HTTP request returned an unsuccessful status code
|
| 57 |
+
return response.json()['choices'][0]['message']['content']
|
| 58 |
+
|
| 59 |
+
def process_image_prompt(response):
|
| 60 |
+
response = response.split('\n')
|
| 61 |
+
response = [re.sub(r"(?<!\\)['\"](.*?)(?<!\\)['\"]", r"\1", response[i]) for i in range(len(response))]
|
| 62 |
+
return response
|
| 63 |
+
|
| 64 |
+
if __name__ == "__main__":
|
| 65 |
+
response = generate_image_prompt('halong bay, vietnam')
|
| 66 |
+
response = process_image_prompt(response)
|
| 67 |
+
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
openai==1.43.1
|
| 2 |
+
pillow==10.4.0
|
| 3 |
+
requests==2.32.3
|
| 4 |
+
streamlit==1.38.0
|
streamlit_utlis.py
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import hmac
|
| 2 |
+
import streamlit as st
|
| 3 |
+
|
| 4 |
+
def check_password():
|
| 5 |
+
def password_entered():
|
| 6 |
+
if hmac.compare_digest(st.session_state["password"], st.secrets["adminpassword"]):
|
| 7 |
+
st.session_state["password_correct"] = True
|
| 8 |
+
del st.session_state["password"] # Don't store the password.
|
| 9 |
+
else:
|
| 10 |
+
st.session_state["password_correct"] = False
|
| 11 |
+
|
| 12 |
+
if st.session_state.get("password_correct", False):
|
| 13 |
+
return True
|
| 14 |
+
|
| 15 |
+
st.text_input(
|
| 16 |
+
"Enter Password 🚀", type="password", on_change=password_entered, key="password"
|
| 17 |
+
)
|
| 18 |
+
if "password_correct" in st.session_state:
|
| 19 |
+
st.error("Password incorrect 😕")
|
| 20 |
+
return False
|