Spaces:
Sleeping
Sleeping
Commit ·
8d015f7
1
Parent(s): 02e14ff
add of variables
Browse files
app.py
CHANGED
|
@@ -5,8 +5,16 @@ import requests
|
|
| 5 |
import streamlit as st
|
| 6 |
|
| 7 |
|
| 8 |
-
def encode_image(uploaded_file):
|
| 9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
encoded = base64.b64encode(bytes_data).decode()
|
| 11 |
return f"data:image/jpeg;base64,{encoded}"
|
| 12 |
|
|
@@ -48,18 +56,45 @@ def main():
|
|
| 48 |
token = st.sidebar.text_input("Token")
|
| 49 |
|
| 50 |
# File uploader
|
| 51 |
-
|
|
|
|
|
|
|
|
|
|
| 52 |
"Choose an image", type=["png", "jpg", "jpeg"]
|
| 53 |
)
|
| 54 |
-
|
|
|
|
| 55 |
if uploaded_file:
|
| 56 |
st.image(uploaded_file, caption="Uploaded Image")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
|
| 58 |
-
#
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
if st.button("Generate") and uploaded_file and input_text:
|
| 62 |
-
encoded_image = encode_image(uploaded_file)
|
| 63 |
|
| 64 |
# Initial API call
|
| 65 |
headers = {
|
|
@@ -69,11 +104,16 @@ def main():
|
|
| 69 |
|
| 70 |
data = {
|
| 71 |
"execution_mode": "async",
|
| 72 |
-
"inputs": {"input_image": encoded_image, "input_text":
|
| 73 |
"workflow_version_id": "c5249acc-2cda-4734-b4f8-7823cecbce3d",
|
| 74 |
"machine_id": "353b27c7-6bce-4472-b5eb-18f22d2373fc",
|
| 75 |
}
|
| 76 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
with st.spinner("Generating image..."):
|
| 78 |
try:
|
| 79 |
response = requests.post(
|
|
@@ -107,6 +147,7 @@ def main():
|
|
| 107 |
|
| 108 |
counter += 1
|
| 109 |
outputs = [Output(x) for x in result["outputs"]]
|
|
|
|
| 110 |
final_img = [
|
| 111 |
output.output_url
|
| 112 |
for output in outputs
|
|
@@ -114,6 +155,7 @@ def main():
|
|
| 114 |
][0]
|
| 115 |
st.image(final_img, caption="Generated Image")
|
| 116 |
|
|
|
|
| 117 |
except requests.exceptions.RequestException as e:
|
| 118 |
st.error(f"Error occurred: {str(e)}")
|
| 119 |
|
|
|
|
| 5 |
import streamlit as st
|
| 6 |
|
| 7 |
|
| 8 |
+
def encode_image(uploaded_file=None, image_url=None):
|
| 9 |
+
if uploaded_file:
|
| 10 |
+
# Handle file upload
|
| 11 |
+
bytes_data = uploaded_file.getvalue()
|
| 12 |
+
elif image_url:
|
| 13 |
+
# Handle image URL
|
| 14 |
+
response = requests.get(image_url)
|
| 15 |
+
bytes_data = response.content
|
| 16 |
+
else:
|
| 17 |
+
raise ValueError("Either uploaded_file or image_url must be provided.")
|
| 18 |
encoded = base64.b64encode(bytes_data).decode()
|
| 19 |
return f"data:image/jpeg;base64,{encoded}"
|
| 20 |
|
|
|
|
| 56 |
token = st.sidebar.text_input("Token")
|
| 57 |
|
| 58 |
# File uploader
|
| 59 |
+
col1, col2 = st.columns(2)
|
| 60 |
+
|
| 61 |
+
with col1:
|
| 62 |
+
uploaded_file = st.file_uploader(
|
| 63 |
"Choose an image", type=["png", "jpg", "jpeg"]
|
| 64 |
)
|
| 65 |
+
with col2:
|
| 66 |
+
image_url = st.text_input("Or enter an image URL", help="Remove the uploaded image if you want to use an image URL")
|
| 67 |
if uploaded_file:
|
| 68 |
st.image(uploaded_file, caption="Uploaded Image")
|
| 69 |
+
elif image_url:
|
| 70 |
+
st.image(image_url, caption="Image URL")
|
| 71 |
+
# Text inputs
|
| 72 |
+
input_category = st.text_input("Enter category (Optional)")
|
| 73 |
+
input_variant = st.text_input("Enter variant")
|
| 74 |
+
|
| 75 |
+
# Method input
|
| 76 |
+
option_labels = [
|
| 77 |
+
"Value 1: For replacement of element in foreground",
|
| 78 |
+
"Value 2: For replacement of background",
|
| 79 |
+
"Value 3: To generate a new image with the same dimensions based on the prompt",
|
| 80 |
+
"Value 4: Workflow stop",
|
| 81 |
+
]
|
| 82 |
+
|
| 83 |
+
option_values = [1, 2, 3, 4]
|
| 84 |
+
|
| 85 |
+
selected_option = st.selectbox(
|
| 86 |
+
"Select an option:",
|
| 87 |
+
index=None,
|
| 88 |
+
options=option_labels,
|
| 89 |
+
)
|
| 90 |
+
if selected_option is None:
|
| 91 |
+
input_number = None
|
| 92 |
+
else:
|
| 93 |
+
input_number = option_values[option_labels.index(selected_option)]
|
| 94 |
|
| 95 |
+
# Generation
|
| 96 |
+
if st.button("Generate") and (uploaded_file or image_url) and input_variant:
|
| 97 |
+
encoded_image = encode_image(uploaded_file=uploaded_file, image_url=image_url)
|
|
|
|
|
|
|
| 98 |
|
| 99 |
# Initial API call
|
| 100 |
headers = {
|
|
|
|
| 104 |
|
| 105 |
data = {
|
| 106 |
"execution_mode": "async",
|
| 107 |
+
"inputs": {"input_image": encoded_image, "input_text": input_variant},
|
| 108 |
"workflow_version_id": "c5249acc-2cda-4734-b4f8-7823cecbce3d",
|
| 109 |
"machine_id": "353b27c7-6bce-4472-b5eb-18f22d2373fc",
|
| 110 |
}
|
| 111 |
|
| 112 |
+
if input_category:
|
| 113 |
+
data["inputs"]["input_category"] = input_category
|
| 114 |
+
if input_number:
|
| 115 |
+
data["inputs"]["input_number"] = input_number
|
| 116 |
+
|
| 117 |
with st.spinner("Generating image..."):
|
| 118 |
try:
|
| 119 |
response = requests.post(
|
|
|
|
| 147 |
|
| 148 |
counter += 1
|
| 149 |
outputs = [Output(x) for x in result["outputs"]]
|
| 150 |
+
|
| 151 |
final_img = [
|
| 152 |
output.output_url
|
| 153 |
for output in outputs
|
|
|
|
| 155 |
][0]
|
| 156 |
st.image(final_img, caption="Generated Image")
|
| 157 |
|
| 158 |
+
|
| 159 |
except requests.exceptions.RequestException as e:
|
| 160 |
st.error(f"Error occurred: {str(e)}")
|
| 161 |
|