Update app.py
Browse files
app.py
CHANGED
|
@@ -54,27 +54,31 @@ def save_image_from_url(url):
|
|
| 54 |
return None
|
| 55 |
|
| 56 |
|
| 57 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
try:
|
| 59 |
-
|
| 60 |
-
if
|
| 61 |
-
|
| 62 |
-
|
|
|
|
|
|
|
| 63 |
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
files={"image": file.getvalue()}
|
| 68 |
-
)
|
| 69 |
|
| 70 |
-
if res.status_code == 200:
|
| 71 |
-
return res.json()["data"]["url"]
|
| 72 |
else:
|
| 73 |
-
st.error(f"β Upload Error HTTP {res.status_code}")
|
| 74 |
return None
|
| 75 |
|
| 76 |
except Exception as e:
|
| 77 |
-
st.error(f"β
|
| 78 |
return None
|
| 79 |
|
| 80 |
|
|
@@ -96,26 +100,19 @@ def generate_image(prompt):
|
|
| 96 |
|
| 97 |
def edit_image(input_image, prompt):
|
| 98 |
try:
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
elif isinstance(input_image, str) and not input_image.startswith("http"):
|
| 103 |
-
with open(input_image, "rb") as f:
|
| 104 |
-
image_url = upload_to_imgbb(f)
|
| 105 |
-
|
| 106 |
-
else:
|
| 107 |
-
image_url = input_image
|
| 108 |
-
|
| 109 |
-
if not image_url:
|
| 110 |
return None
|
| 111 |
|
| 112 |
result = client_main.predict(
|
| 113 |
-
input_image=
|
| 114 |
prompt=prompt,
|
| 115 |
api_name="/edit_image_interface"
|
| 116 |
)
|
| 117 |
|
| 118 |
-
|
|
|
|
| 119 |
|
| 120 |
except Exception as e:
|
| 121 |
st.error(f"β Edit Error: {e}")
|
|
@@ -124,13 +121,10 @@ def edit_image(input_image, prompt):
|
|
| 124 |
|
| 125 |
def process_image(input_image):
|
| 126 |
try:
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
image_input = handle_file(tmp.name)
|
| 132 |
-
else:
|
| 133 |
-
image_input = handle_file(input_image)
|
| 134 |
|
| 135 |
result = client_process.predict(
|
| 136 |
image=image_input,
|
|
@@ -160,8 +154,8 @@ def run_async(func, *args):
|
|
| 160 |
|
| 161 |
def show_progress(job):
|
| 162 |
progress_bar = st.progress(0)
|
| 163 |
-
|
| 164 |
percent = 0
|
|
|
|
| 165 |
while not job["done"]:
|
| 166 |
percent += 5
|
| 167 |
if percent > 95:
|
|
|
|
| 54 |
return None
|
| 55 |
|
| 56 |
|
| 57 |
+
def prepare_image_for_api(input_image):
|
| 58 |
+
"""
|
| 59 |
+
π₯ UNIVERSAL FIX:
|
| 60 |
+
- Upload β temp file
|
| 61 |
+
- Path β direkt
|
| 62 |
+
- URL β direkt
|
| 63 |
+
β immer handle_file(...)
|
| 64 |
+
"""
|
| 65 |
try:
|
| 66 |
+
# π₯ Upload (Streamlit)
|
| 67 |
+
if hasattr(input_image, "getvalue"):
|
| 68 |
+
tmp = tempfile.NamedTemporaryFile(delete=False, suffix=".png")
|
| 69 |
+
tmp.write(input_image.getvalue())
|
| 70 |
+
tmp.close()
|
| 71 |
+
return handle_file(tmp.name)
|
| 72 |
|
| 73 |
+
# π Lokaler Pfad oder URL
|
| 74 |
+
elif isinstance(input_image, str):
|
| 75 |
+
return handle_file(input_image)
|
|
|
|
|
|
|
| 76 |
|
|
|
|
|
|
|
| 77 |
else:
|
|
|
|
| 78 |
return None
|
| 79 |
|
| 80 |
except Exception as e:
|
| 81 |
+
st.error(f"β Prepare Error: {e}")
|
| 82 |
return None
|
| 83 |
|
| 84 |
|
|
|
|
| 100 |
|
| 101 |
def edit_image(input_image, prompt):
|
| 102 |
try:
|
| 103 |
+
image_input = prepare_image_for_api(input_image)
|
| 104 |
+
if not image_input:
|
| 105 |
+
st.error("β Invalid image input")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 106 |
return None
|
| 107 |
|
| 108 |
result = client_main.predict(
|
| 109 |
+
input_image=image_input,
|
| 110 |
prompt=prompt,
|
| 111 |
api_name="/edit_image_interface"
|
| 112 |
)
|
| 113 |
|
| 114 |
+
html = result[0]
|
| 115 |
+
return extract_url(html)
|
| 116 |
|
| 117 |
except Exception as e:
|
| 118 |
st.error(f"β Edit Error: {e}")
|
|
|
|
| 121 |
|
| 122 |
def process_image(input_image):
|
| 123 |
try:
|
| 124 |
+
image_input = prepare_image_for_api(input_image)
|
| 125 |
+
if not image_input:
|
| 126 |
+
st.error("β Invalid image input")
|
| 127 |
+
return None
|
|
|
|
|
|
|
|
|
|
| 128 |
|
| 129 |
result = client_process.predict(
|
| 130 |
image=image_input,
|
|
|
|
| 154 |
|
| 155 |
def show_progress(job):
|
| 156 |
progress_bar = st.progress(0)
|
|
|
|
| 157 |
percent = 0
|
| 158 |
+
|
| 159 |
while not job["done"]:
|
| 160 |
percent += 5
|
| 161 |
if percent > 95:
|