SLSLK commited on
Commit
a4d80d9
Β·
verified Β·
1 Parent(s): f0f8078

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -35
app.py CHANGED
@@ -54,27 +54,31 @@ def save_image_from_url(url):
54
  return None
55
 
56
 
57
- def upload_to_imgbb(file):
 
 
 
 
 
 
 
58
  try:
59
- api_key = os.getenv("IMGBB_API_KEY")
60
- if not api_key:
61
- st.error("❌ Missing IMGBB_API_KEY")
62
- return None
 
 
63
 
64
- res = requests.post(
65
- "https://api.imgbb.com/1/upload",
66
- data={"key": api_key},
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"❌ Upload Exception: {e}")
78
  return None
79
 
80
 
@@ -96,26 +100,19 @@ def generate_image(prompt):
96
 
97
  def edit_image(input_image, prompt):
98
  try:
99
- if hasattr(input_image, "getvalue"):
100
- image_url = upload_to_imgbb(input_image)
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=image_url,
114
  prompt=prompt,
115
  api_name="/edit_image_interface"
116
  )
117
 
118
- return extract_url(result[0])
 
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
- if hasattr(input_image, "getvalue"):
128
- tmp = tempfile.NamedTemporaryFile(delete=False, suffix=".png")
129
- tmp.write(input_image.getvalue())
130
- tmp.close()
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: