SLSLK commited on
Commit
3503531
·
verified ·
1 Parent(s): ab1fc99

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +68 -45
app.py CHANGED
@@ -5,6 +5,14 @@ import threading
5
  import time
6
  import tempfile
7
  import requests
 
 
 
 
 
 
 
 
8
 
9
  # ---------------------------
10
  # Clients
@@ -25,30 +33,50 @@ if "page" not in st.session_state:
25
  st.session_state.page = "🖼️ Generieren"
26
 
27
  # ---------------------------
28
- # Helper
29
  # ---------------------------
30
  def extract_url(html):
31
  match = re.search(r"src='([^']+)'", html)
32
  return match.group(1) if match else None
33
 
34
 
35
- def url_to_file(url):
36
  try:
37
  r = requests.get(url)
38
  if r.status_code != 200:
39
  st.error(f"❌ Download Error: HTTP {r.status_code}")
40
  return None
41
 
42
- tmp = tempfile.NamedTemporaryFile(delete=False, suffix=".png")
43
- tmp.write(r.content)
44
- tmp.close()
45
- return tmp.name
 
 
 
46
 
47
  except Exception as e:
48
- st.error(f"❌ URL Download Exception: {e}")
49
  return None
50
 
51
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
52
  # ---------------------------
53
  # API FUNCTIONS
54
  # ---------------------------
@@ -60,7 +88,6 @@ def generate_image(prompt):
60
  api_name="/text_to_image_interface"
61
  )
62
  return extract_url(result[0])
63
-
64
  except Exception as e:
65
  st.error(f"❌ Generate Error: {e}")
66
  return None
@@ -68,13 +95,9 @@ def generate_image(prompt):
68
 
69
  def edit_image(input_image, prompt):
70
  try:
71
- if isinstance(input_image, str) and input_image.startswith("http"):
72
- file_path = url_to_file(input_image)
73
- if not file_path:
74
- return None
75
- image_input = handle_file(file_path)
76
- else:
77
- image_input = handle_file(input_image)
78
 
79
  result = client_main.predict(
80
  input_image=image_input,
@@ -91,13 +114,9 @@ def edit_image(input_image, prompt):
91
 
92
  def process_image(input_image):
93
  try:
94
- if isinstance(input_image, str) and input_image.startswith("http"):
95
- file_path = url_to_file(input_image)
96
- if not file_path:
97
- return None
98
- image_input = handle_file(file_path)
99
- else:
100
- image_input = handle_file(input_image)
101
 
102
  result = client_process.predict(
103
  image=image_input,
@@ -165,12 +184,14 @@ if page == "🖼️ Generieren":
165
  progress.progress(100)
166
  status.text("Fertig ✅")
167
 
168
- img_url = job["result"]
169
 
170
- if img_url:
171
- st.image(img_url)
 
 
172
  st.session_state.gallery.append({
173
- "url": img_url,
174
  "prompt": prompt
175
  })
176
 
@@ -181,10 +202,10 @@ elif page == "✨ Bearbeiten":
181
  st.subheader("✨ Bild bearbeiten")
182
 
183
  if st.session_state.selected_image:
184
- st.image(st.session_state.selected_image, caption="Aus Galerie")
185
  input_image = st.session_state.selected_image
186
  else:
187
- uploaded = st.file_uploader("Bild hochladen", type=["png", "jpg"])
188
  input_image = uploaded
189
 
190
  edit_prompt = st.text_area("Bearbeitung")
@@ -206,26 +227,28 @@ elif page == "✨ Bearbeiten":
206
  progress.progress(100)
207
  status.text("Fertig ✅")
208
 
209
- result = job["result"]
 
 
 
 
210
 
211
- if result:
212
- st.image(result)
213
  st.session_state.gallery.append({
214
- "url": result,
215
  "prompt": edit_prompt
216
  })
217
 
218
  # ---------------------------
219
- # ANALYSE (PROCESS)
220
  # ---------------------------
221
  elif page == "🧪 Analyse":
222
  st.subheader("🧪 Bild Analyse")
223
 
224
  if st.session_state.selected_image:
225
- st.image(st.session_state.selected_image, caption="Aus Galerie")
226
  input_image = st.session_state.selected_image
227
  else:
228
- uploaded = st.file_uploader("Bild auswählen", type=["png", "jpg"])
229
  input_image = uploaded
230
 
231
  if st.button("Analysieren"):
@@ -248,10 +271,12 @@ elif page == "🧪 Analyse":
248
  result = job["result"]
249
 
250
  if isinstance(result, str) and result.startswith("http"):
251
- st.image(result)
 
 
252
  st.session_state.gallery.append({
253
- "url": result,
254
- "prompt": "Analyse Ergebnis"
255
  })
256
  else:
257
  st.write(result)
@@ -268,25 +293,23 @@ elif page == "📚 Galerie":
268
  cols = st.columns(3)
269
 
270
  for i, item in enumerate(st.session_state.gallery):
271
- url = item["url"]
272
- prompt_text = item["prompt"]
273
 
274
  with cols[i % 3]:
275
- st.image(url, caption=prompt_text)
276
 
277
  col1, col2 = st.columns(2)
278
 
279
- # 🔥 EDIT BUTTON
280
  with col1:
281
  if st.button("✏️ Edit", key=f"edit_{i}"):
282
- st.session_state.selected_image = url
283
  st.session_state.page = "✨ Bearbeiten"
284
  st.rerun()
285
 
286
- # 🔥 ANALYSE BUTTON
287
  with col2:
288
  if st.button("🧪 Analyse", key=f"proc_{i}"):
289
- st.session_state.selected_image = url
290
  st.session_state.page = "🧪 Analyse"
291
  st.rerun()
292
 
 
5
  import time
6
  import tempfile
7
  import requests
8
+ import os
9
+ import uuid
10
+
11
+ # ---------------------------
12
+ # Setup Storage (persistent)
13
+ # ---------------------------
14
+ IMAGE_DIR = "data/images"
15
+ os.makedirs(IMAGE_DIR, exist_ok=True)
16
 
17
  # ---------------------------
18
  # Clients
 
33
  st.session_state.page = "🖼️ Generieren"
34
 
35
  # ---------------------------
36
+ # Helpers
37
  # ---------------------------
38
  def extract_url(html):
39
  match = re.search(r"src='([^']+)'", html)
40
  return match.group(1) if match else None
41
 
42
 
43
+ def save_image_from_url(url):
44
  try:
45
  r = requests.get(url)
46
  if r.status_code != 200:
47
  st.error(f"❌ Download Error: HTTP {r.status_code}")
48
  return None
49
 
50
+ filename = f"{uuid.uuid4()}.png"
51
+ path = os.path.join(IMAGE_DIR, filename)
52
+
53
+ with open(path, "wb") as f:
54
+ f.write(r.content)
55
+
56
+ return path
57
 
58
  except Exception as e:
59
+ st.error(f"❌ Save Error: {e}")
60
  return None
61
 
62
 
63
+ def prepare_image(input_image):
64
+ # ❌ block gradio tmp
65
+ if isinstance(input_image, str) and "/tmp/gradio" in input_image:
66
+ st.error("❌ Invalid temp path from gradio")
67
+ return None
68
+
69
+ # 🌐 URL → speichern
70
+ if isinstance(input_image, str) and input_image.startswith("http"):
71
+ local_path = save_image_from_url(input_image)
72
+ if not local_path:
73
+ return None
74
+ return handle_file(local_path)
75
+
76
+ # 📁 local file
77
+ return handle_file(input_image)
78
+
79
+
80
  # ---------------------------
81
  # API FUNCTIONS
82
  # ---------------------------
 
88
  api_name="/text_to_image_interface"
89
  )
90
  return extract_url(result[0])
 
91
  except Exception as e:
92
  st.error(f"❌ Generate Error: {e}")
93
  return None
 
95
 
96
  def edit_image(input_image, prompt):
97
  try:
98
+ image_input = prepare_image(input_image)
99
+ if not image_input:
100
+ return None
 
 
 
 
101
 
102
  result = client_main.predict(
103
  input_image=image_input,
 
114
 
115
  def process_image(input_image):
116
  try:
117
+ image_input = prepare_image(input_image)
118
+ if not image_input:
119
+ return None
 
 
 
 
120
 
121
  result = client_process.predict(
122
  image=image_input,
 
184
  progress.progress(100)
185
  status.text("Fertig ✅")
186
 
187
+ url = job["result"]
188
 
189
+ if url:
190
+ local_path = save_image_from_url(url)
191
+
192
+ st.image(local_path)
193
  st.session_state.gallery.append({
194
+ "path": local_path,
195
  "prompt": prompt
196
  })
197
 
 
202
  st.subheader("✨ Bild bearbeiten")
203
 
204
  if st.session_state.selected_image:
205
+ st.image(st.session_state.selected_image)
206
  input_image = st.session_state.selected_image
207
  else:
208
+ uploaded = st.file_uploader("Bild hochladen")
209
  input_image = uploaded
210
 
211
  edit_prompt = st.text_area("Bearbeitung")
 
227
  progress.progress(100)
228
  status.text("Fertig ✅")
229
 
230
+ url = job["result"]
231
+
232
+ if url:
233
+ local_path = save_image_from_url(url)
234
+ st.image(local_path)
235
 
 
 
236
  st.session_state.gallery.append({
237
+ "path": local_path,
238
  "prompt": edit_prompt
239
  })
240
 
241
  # ---------------------------
242
+ # ANALYSE
243
  # ---------------------------
244
  elif page == "🧪 Analyse":
245
  st.subheader("🧪 Bild Analyse")
246
 
247
  if st.session_state.selected_image:
248
+ st.image(st.session_state.selected_image)
249
  input_image = st.session_state.selected_image
250
  else:
251
+ uploaded = st.file_uploader("Bild auswählen")
252
  input_image = uploaded
253
 
254
  if st.button("Analysieren"):
 
271
  result = job["result"]
272
 
273
  if isinstance(result, str) and result.startswith("http"):
274
+ local_path = save_image_from_url(result)
275
+ st.image(local_path)
276
+
277
  st.session_state.gallery.append({
278
+ "path": local_path,
279
+ "prompt": "Analyse"
280
  })
281
  else:
282
  st.write(result)
 
293
  cols = st.columns(3)
294
 
295
  for i, item in enumerate(st.session_state.gallery):
296
+ path = item["path"]
297
+ prompt = item["prompt"]
298
 
299
  with cols[i % 3]:
300
+ st.image(path, caption=prompt)
301
 
302
  col1, col2 = st.columns(2)
303
 
 
304
  with col1:
305
  if st.button("✏️ Edit", key=f"edit_{i}"):
306
+ st.session_state.selected_image = path
307
  st.session_state.page = "✨ Bearbeiten"
308
  st.rerun()
309
 
 
310
  with col2:
311
  if st.button("🧪 Analyse", key=f"proc_{i}"):
312
+ st.session_state.selected_image = path
313
  st.session_state.page = "🧪 Analyse"
314
  st.rerun()
315