TieuDaoChanNhan commited on
Commit
74a2c74
·
1 Parent(s): 9f6af22
Files changed (1) hide show
  1. app.py +29 -27
app.py CHANGED
@@ -214,49 +214,54 @@ print("✅ Image processing functions ready!")
214
 
215
  # Main inference function
216
  def generate_emoji_fixed(input_image):
217
- """Fixed inference with correct architecture"""
218
-
 
 
 
 
 
219
  if input_image is None:
220
- return None, "⚠️ Please upload an image first!"
221
-
 
222
  try:
223
  print("🚀 Processing image with correct architecture...")
224
-
225
  # Preprocess for FaceNet (160x160)
226
  input_tensor = preprocess_image_for_facenet(input_image, target_size=160).to(device)
227
  print(f"📊 Input tensor shape: {input_tensor.shape}")
228
-
229
  # Inference
230
  with torch.no_grad():
231
  print("🧠 Extracting features with FaceNet...")
232
- features = face_extractor(input_tensor) # Should output 512D
233
  print(f"📊 Features shape: {features.shape}")
234
-
235
  if torch.isnan(features).any():
236
- return None, "❌ NaN detected in features!"
237
-
 
238
  print("🎭 Generating emoji...")
239
- emoji_tensor = generator(features) # Should work with correct architecture
240
  print(f"🎨 Generated shape: {emoji_tensor.shape}")
241
-
242
  if torch.isnan(emoji_tensor).any():
243
- return None, "❌ NaN detected in generated image!"
244
-
 
245
  # Postprocess
246
  emoji_image = postprocess_image(emoji_tensor)
247
  emoji_upscaled = upscale_image(emoji_image, scale_factor=4)
248
-
249
- status = f"✅ Success! Generated {emoji_image.size[0]}×{emoji_image.size[1]} emoji, upscaled to {emoji_upscaled.size}×{emoji_upscaled.size[1]}"
250
- print(status)
251
-
252
- return emoji_upscaled
253
-
254
  except Exception as e:
255
- error_msg = f"❌ Error: {str(e)}"
256
- print(error_msg)
257
  import traceback
258
  traceback.print_exc()
259
- return None, error_msg
260
 
261
  print("✅ Main inference function ready!")
262
 
@@ -278,10 +283,7 @@ if __name__ == "__main__":
278
  demo = gr.Interface(
279
  fn=generate_emoji_fixed,
280
  inputs=gr.Image(type="pil", label="Upload Image"),
281
- outputs=[
282
- gr.Image(type="pil", label="Generated Emoji"),
283
- gr.Textbox(label="Status") # Nếu muốn hiển thị thông báo
284
- ],
285
  title="DTN Face-to-Emoji Generator"
286
  )
287
 
 
214
 
215
  # Main inference function
216
  def generate_emoji_fixed(input_image):
217
+ """Fixed inference with consistent return type"""
218
+
219
+ # Create default error image
220
+ def create_error_image(text="Error"):
221
+ error_img = Image.new('RGB', (256, 256), color='lightgray')
222
+ return error_img
223
+
224
  if input_image is None:
225
+ print("⚠️ Please upload an image first!")
226
+ return create_error_image("No Image")
227
+
228
  try:
229
  print("🚀 Processing image with correct architecture...")
230
+
231
  # Preprocess for FaceNet (160x160)
232
  input_tensor = preprocess_image_for_facenet(input_image, target_size=160).to(device)
233
  print(f"📊 Input tensor shape: {input_tensor.shape}")
234
+
235
  # Inference
236
  with torch.no_grad():
237
  print("🧠 Extracting features with FaceNet...")
238
+ features = face_extractor(input_tensor)
239
  print(f"📊 Features shape: {features.shape}")
240
+
241
  if torch.isnan(features).any():
242
+ print("❌ NaN detected in features!")
243
+ return create_error_image("NaN Features")
244
+
245
  print("🎭 Generating emoji...")
246
+ emoji_tensor = generator(features)
247
  print(f"🎨 Generated shape: {emoji_tensor.shape}")
248
+
249
  if torch.isnan(emoji_tensor).any():
250
+ print("❌ NaN detected in generated image!")
251
+ return create_error_image("NaN Output")
252
+
253
  # Postprocess
254
  emoji_image = postprocess_image(emoji_tensor)
255
  emoji_upscaled = upscale_image(emoji_image, scale_factor=4)
256
+
257
+ print("✅ Success!")
258
+ return emoji_upscaled # ✅ Always return single image
259
+
 
 
260
  except Exception as e:
261
+ print(f"❌ Error: {str(e)}")
 
262
  import traceback
263
  traceback.print_exc()
264
+ return create_error_image("Processing Error") # ✅ Always return single image
265
 
266
  print("✅ Main inference function ready!")
267
 
 
283
  demo = gr.Interface(
284
  fn=generate_emoji_fixed,
285
  inputs=gr.Image(type="pil", label="Upload Image"),
286
+ outputs=gr.Image(type="pil", label="Generated Emoji"),
 
 
 
287
  title="DTN Face-to-Emoji Generator"
288
  )
289