Spaces:
Sleeping
Sleeping
Commit ·
02e14ff
1
Parent(s): 715efdf
app created
Browse files
app.py
ADDED
|
@@ -0,0 +1,122 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import base64
|
| 2 |
+
import time
|
| 3 |
+
|
| 4 |
+
import requests
|
| 5 |
+
import streamlit as st
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
def encode_image(uploaded_file):
|
| 9 |
+
bytes_data = uploaded_file.getvalue()
|
| 10 |
+
encoded = base64.b64encode(bytes_data).decode()
|
| 11 |
+
return f"data:image/jpeg;base64,{encoded}"
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
class Output:
|
| 15 |
+
def __init__(self, obj: dict):
|
| 16 |
+
self.data = obj["data"]
|
| 17 |
+
self.output_image = False
|
| 18 |
+
if self.is_image():
|
| 19 |
+
self.get_result()
|
| 20 |
+
|
| 21 |
+
def is_image(self):
|
| 22 |
+
return "images" in self.data
|
| 23 |
+
|
| 24 |
+
def get_result(self):
|
| 25 |
+
self.images = [ImageOutput(**img) for img in self.data["images"]]
|
| 26 |
+
for img in self.images:
|
| 27 |
+
if img.is_output():
|
| 28 |
+
self.output_image = True
|
| 29 |
+
self.output_url = img.url
|
| 30 |
+
break
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
class ImageOutput:
|
| 34 |
+
def __init__(self, **kwargs):
|
| 35 |
+
for key, value in kwargs.items():
|
| 36 |
+
setattr(self, key, value)
|
| 37 |
+
|
| 38 |
+
def is_output(self):
|
| 39 |
+
return self.type == "output"
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
def main():
|
| 43 |
+
st.title("Image Generator")
|
| 44 |
+
|
| 45 |
+
## add sidebar with a text field for the token
|
| 46 |
+
st.sidebar.title("Settings")
|
| 47 |
+
st.sidebar.subheader("Enter your API token")
|
| 48 |
+
token = st.sidebar.text_input("Token")
|
| 49 |
+
|
| 50 |
+
# File uploader
|
| 51 |
+
uploaded_file = st.file_uploader(
|
| 52 |
+
"Choose an image", type=["png", "jpg", "jpeg"]
|
| 53 |
+
)
|
| 54 |
+
|
| 55 |
+
if uploaded_file:
|
| 56 |
+
st.image(uploaded_file, caption="Uploaded Image")
|
| 57 |
+
|
| 58 |
+
# Text input
|
| 59 |
+
input_text = st.text_input("Enter text")
|
| 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 = {
|
| 66 |
+
"Authorization": f"Bearer {token}",
|
| 67 |
+
"Content-Type": "application/json",
|
| 68 |
+
}
|
| 69 |
+
|
| 70 |
+
data = {
|
| 71 |
+
"execution_mode": "async",
|
| 72 |
+
"inputs": {"input_image": encoded_image, "input_text": 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(
|
| 80 |
+
"https://api.comfydeploy.com/api/run",
|
| 81 |
+
headers=headers,
|
| 82 |
+
json=data,
|
| 83 |
+
)
|
| 84 |
+
response.raise_for_status()
|
| 85 |
+
run_id = response.json()["run_id"]
|
| 86 |
+
st.write(f"Run ID: {run_id}")
|
| 87 |
+
counter = 0
|
| 88 |
+
retrieving = True
|
| 89 |
+
while retrieving:
|
| 90 |
+
time.sleep(2)
|
| 91 |
+
get_response = requests.get(
|
| 92 |
+
f"https://www.comfydeploy.com/api/run?run_id={run_id}",
|
| 93 |
+
headers=headers,
|
| 94 |
+
)
|
| 95 |
+
get_response.raise_for_status()
|
| 96 |
+
result = get_response.json()
|
| 97 |
+
|
| 98 |
+
if result["status"] == "success":
|
| 99 |
+
st.success("Image generated successfully!")
|
| 100 |
+
retrieving = False
|
| 101 |
+
break
|
| 102 |
+
elif result["status"] == "running":
|
| 103 |
+
continue
|
| 104 |
+
elif result["status"] == "failed":
|
| 105 |
+
st.error("Image generation failed")
|
| 106 |
+
break
|
| 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
|
| 113 |
+
if output.output_image
|
| 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 |
+
|
| 120 |
+
|
| 121 |
+
if __name__ == "__main__":
|
| 122 |
+
main()
|