ruminasval commited on
Commit
7739f9e
·
verified ·
1 Parent(s): 1c0dcfb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -11
app.py CHANGED
@@ -119,7 +119,7 @@ def predict(image):
119
  frame_recommendations = recommend_glasses_tree(pred_label)
120
 
121
  description = face_shape_descriptions.get(pred_label, "tidak dikenali")
122
- frame_images = []
123
 
124
  # Load images for all recommended frames
125
  for frame in frame_recommendations:
@@ -127,21 +127,21 @@ def predict(image):
127
  if frame_image_path and os.path.exists(frame_image_path):
128
  try:
129
  frame_image = Image.open(frame_image_path)
130
- frame_images.append(frame_image)
131
  except Exception as e:
132
  print(f"Error loading image for {frame}: {e}")
133
 
134
  # Build explanation text
135
  if frame_recommendations:
136
- recommended_frames = ', '.join(frame_recommendations)
137
  explanation = (f"Bentuk wajah kamu adalah {pred_label} ({pred_prob:.2f}%). "
138
  f"Kamu memiliki bentuk wajah {description} "
139
- f"Rekomendasi bentuk kacamata yang sesuai dengan wajah kamu adalah: {recommended_frames}.")
140
  else:
141
  explanation = (f"Bentuk wajah kamu adalah {pred_label} ({pred_prob:.2f}%). "
142
  f"Tidak ada rekomendasi frame untuk bentuk wajah ini.")
143
 
144
- return pred_label, explanation, frame_images
145
 
146
  except ValueError as ve:
147
  return "Error", str(ve), []
@@ -156,17 +156,15 @@ with gr.Blocks(theme=gr.themes.Soft()) as iface:
156
  with gr.Row():
157
  with gr.Column():
158
  image_input = gr.Image(type="pil")
159
- with gr.Row():
160
- upload_button = gr.UploadButton("Unggah Gambar", file_types=["image"])
161
- clear_button = gr.Button("Ganti")
162
  with gr.Column():
163
  detected_shape = gr.Textbox(label="Bentuk Wajah Terdeteksi")
164
  explanation_output = gr.Textbox(label="Penjelasan")
165
- recommendation_gallery = gr.Gallery(label="Rekomendasi Kacamata", columns=3)
166
 
167
- upload_button.upload(predict, inputs=upload_button, outputs=[detected_shape, explanation_output, recommendation_gallery])
168
  clear_button.click(lambda: (None, "", []), inputs=None, outputs=[image_input, detected_shape, recommendation_gallery])
169
- image_input.upload(predict, inputs=image_input, outputs=[detected_shape, explanation_output, recommendation_gallery])
170
 
171
  if __name__ == "__main__":
172
  iface.launch()
 
119
  frame_recommendations = recommend_glasses_tree(pred_label)
120
 
121
  description = face_shape_descriptions.get(pred_label, "tidak dikenali")
122
+ gallery_items = []
123
 
124
  # Load images for all recommended frames
125
  for frame in frame_recommendations:
 
127
  if frame_image_path and os.path.exists(frame_image_path):
128
  try:
129
  frame_image = Image.open(frame_image_path)
130
+ gallery_items.append((frame_image, frame)) # Tambahkan nama frame
131
  except Exception as e:
132
  print(f"Error loading image for {frame}: {e}")
133
 
134
  # Build explanation text
135
  if frame_recommendations:
136
+ recommended_frames_text = ', '.join(frame_recommendations)
137
  explanation = (f"Bentuk wajah kamu adalah {pred_label} ({pred_prob:.2f}%). "
138
  f"Kamu memiliki bentuk wajah {description} "
139
+ f"Rekomendasi bentuk kacamata yang sesuai dengan wajah kamu adalah: {recommended_frames_text}.")
140
  else:
141
  explanation = (f"Bentuk wajah kamu adalah {pred_label} ({pred_prob:.2f}%). "
142
  f"Tidak ada rekomendasi frame untuk bentuk wajah ini.")
143
 
144
+ return pred_label, explanation, gallery_items
145
 
146
  except ValueError as ve:
147
  return "Error", str(ve), []
 
156
  with gr.Row():
157
  with gr.Column():
158
  image_input = gr.Image(type="pil")
159
+ upload_button = gr.Button("Unggah Gambar")
160
+ clear_button = gr.Button("Ganti")
 
161
  with gr.Column():
162
  detected_shape = gr.Textbox(label="Bentuk Wajah Terdeteksi")
163
  explanation_output = gr.Textbox(label="Penjelasan")
164
+ recommendation_gallery = gr.Gallery(label="Rekomendasi Kacamata", columns=3, show_label=False)
165
 
166
+ upload_button.click(predict, inputs=image_input, outputs=[detected_shape, explanation_output, recommendation_gallery])
167
  clear_button.click(lambda: (None, "", []), inputs=None, outputs=[image_input, detected_shape, recommendation_gallery])
 
168
 
169
  if __name__ == "__main__":
170
  iface.launch()