Update app.py
Browse files
app.py
CHANGED
@@ -68,6 +68,43 @@ with st.sidebar:
|
|
68 |
# Define functions
|
69 |
#------------------------------------------------------------------------
|
70 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
71 |
def query(payload):
|
72 |
response = requests.post(API_URL, headers=headers, json=payload)
|
73 |
if response.status_code != 200:
|
@@ -84,16 +121,42 @@ def generate_image(prompt):
|
|
84 |
def main():
|
85 |
st.title("Stxtement | Image Generation")
|
86 |
|
87 |
-
|
88 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
89 |
if st.button("Generate Image"):
|
90 |
if prompt:
|
91 |
image = generate_image(prompt)
|
92 |
if image:
|
|
|
|
|
93 |
st.image(image, caption="Generated Image")
|
94 |
else:
|
95 |
st.warning("Please enter a prompt.")
|
96 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
97 |
#------------------------------------------------------------------------
|
98 |
# Main Guard
|
99 |
#------------------------------------------------------------------------
|
|
|
68 |
# Define functions
|
69 |
#------------------------------------------------------------------------
|
70 |
|
71 |
+
# SIMPLE CODE
|
72 |
+
# def query(payload):
|
73 |
+
# response = requests.post(API_URL, headers=headers, json=payload)
|
74 |
+
# if response.status_code != 200:
|
75 |
+
# st.error(f"Error: {response.status_code} - {response.text}")
|
76 |
+
# return None
|
77 |
+
# return response.content
|
78 |
+
|
79 |
+
# def generate_image(prompt):
|
80 |
+
# image_bytes = query({"inputs": prompt})
|
81 |
+
# if image_bytes:
|
82 |
+
# return Image.open(io.BytesIO(image_bytes))
|
83 |
+
# return None
|
84 |
+
|
85 |
+
# def main():
|
86 |
+
# st.title("Stxtement | Image Generation")
|
87 |
+
|
88 |
+
# prompt = st.text_input("Enter a prompt for image generation:")
|
89 |
+
|
90 |
+
# if st.button("Generate Image"):
|
91 |
+
# if prompt:
|
92 |
+
# image = generate_image(prompt)
|
93 |
+
# if image:
|
94 |
+
# st.image(image, caption="Generated Image")
|
95 |
+
# else:
|
96 |
+
# st.warning("Please enter a prompt.")
|
97 |
+
|
98 |
+
# COMPREHENSIVE CODE
|
99 |
+
import streamlit as st
|
100 |
+
import requests
|
101 |
+
from PIL import Image
|
102 |
+
import io
|
103 |
+
|
104 |
+
#------------------------------------------------------------------------
|
105 |
+
# Define functions
|
106 |
+
#------------------------------------------------------------------------
|
107 |
+
|
108 |
def query(payload):
|
109 |
response = requests.post(API_URL, headers=headers, json=payload)
|
110 |
if response.status_code != 200:
|
|
|
121 |
def main():
|
122 |
st.title("Stxtement | Image Generation")
|
123 |
|
124 |
+
# Initialize session state variables for prompt and image
|
125 |
+
if "image" not in st.session_state:
|
126 |
+
st.session_state["image"] = None
|
127 |
+
if "prompt" not in st.session_state:
|
128 |
+
st.session_state["prompt"] = ""
|
129 |
+
|
130 |
+
# Input field for the prompt
|
131 |
+
prompt = st.text_input("Enter a prompt for image generation:", value=st.session_state["prompt"])
|
132 |
+
|
133 |
if st.button("Generate Image"):
|
134 |
if prompt:
|
135 |
image = generate_image(prompt)
|
136 |
if image:
|
137 |
+
st.session_state["image"] = image # Store generated image in session state
|
138 |
+
st.session_state["prompt"] = prompt # Store the prompt in session state
|
139 |
st.image(image, caption="Generated Image")
|
140 |
else:
|
141 |
st.warning("Please enter a prompt.")
|
142 |
|
143 |
+
# Show download button if an image is generated
|
144 |
+
if st.session_state["image"]:
|
145 |
+
image_bytes = io.BytesIO()
|
146 |
+
st.session_state["image"].save(image_bytes, format='PNG')
|
147 |
+
st.download_button(
|
148 |
+
label="Download Image",
|
149 |
+
data=image_bytes.getvalue(),
|
150 |
+
file_name="generated_image.png",
|
151 |
+
mime="image/png"
|
152 |
+
)
|
153 |
+
|
154 |
+
# Add reset button to clear the prompt and image
|
155 |
+
if st.button("Reset"):
|
156 |
+
st.session_state["image"] = None
|
157 |
+
st.session_state["prompt"] = ""
|
158 |
+
st.experimental_rerun() # Reset the app by rerunning it
|
159 |
+
|
160 |
#------------------------------------------------------------------------
|
161 |
# Main Guard
|
162 |
#------------------------------------------------------------------------
|