bgaspra commited on
Commit
0fcbd10
·
verified ·
1 Parent(s): 933bd54

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -23
app.py CHANGED
@@ -97,32 +97,43 @@ def get_recommendations(img_path, model, nbrs, image_paths, model_names, n_neigh
97
 
98
  return recommended_images, recommended_model_names, recommended_distances
99
 
100
- def display_images(image_paths, model_names, distances):
101
- plt.figure(figsize=(20, 10))
102
- for i, (img_path, model_name, distance) in enumerate(zip(image_paths, model_names, distances)):
103
- img = Image.open(img_path)
104
- plt.subplot(1, len(image_paths), i+1)
105
- plt.imshow(img)
106
- plt.title(f'{model_name}\nDistance: {distance:.2f}', fontsize=12)
107
- plt.axis('off')
108
- plt.show()
109
-
110
- def recommend_images(img):
111
- temp_img_path = 'temp_img.jpg'
112
- img.save(temp_img_path)
113
 
114
- recommended_images, recommended_model_names, recommended_distances = get_recommendations(temp_img_path, model, nbrs, image_paths, model_names)
115
-
116
- recommendations = []
117
  for i in range(len(recommended_images)):
118
- recommendations.append(f"Rekomendasi {i+1}:\nGambar: {recommended_images[i]}\nModel: {recommended_model_names[i]}\nEuclidian Distance: {recommended_distances[i]:.2f}")
119
- return recommended_images
120
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
121
  iface = gr.Interface(
122
- fn=recommend_images,
123
- inputs=gr.Image(label="Upload an image"),
124
- outputs=gr.Gallery(label="Rekomendasi"),
 
 
125
  title="Image Recommendation System",
126
- description="Upload an image and get recommendations based on similarity."
127
  )
 
 
128
  iface.launch()
 
97
 
98
  return recommended_images, recommended_model_names, recommended_distances
99
 
100
+ def get_recommendations_and_display(img_path):
101
+ recommended_images, recommended_model_names, recommended_distances = get_recommendations(img_path, model, nbrs, image_paths, model_names)
 
 
 
 
 
 
 
 
 
 
 
102
 
103
+ results = []
 
 
104
  for i in range(len(recommended_images)):
105
+ result = {
106
+ "Image": Image.open(recommended_images[i]),
107
+ "Model Name": recommended_model_names[i],
108
+ "Distance": recommended_distances[i]
109
+ }
110
+ results.append(result)
111
+ return results
112
+
113
+ # Define Gradio interface
114
+ def gradio_interface(input_image):
115
+ input_image.save("input_image.jpg") # Save the input image
116
+ recommendations = get_recommendations_and_display("input_image.jpg")
117
+ outputs = []
118
+ for i, rec in enumerate(recommendations):
119
+ outputs.append((f"Recommendation {i+1}", rec["Image"], rec["Model Name"], rec["Distance"]))
120
+ return outputs
121
+
122
+ # Gradio interface function
123
+ def gradio_app(image):
124
+ results = gradio_interface(image)
125
+ return results
126
+
127
+ # Create the Gradio app
128
  iface = gr.Interface(
129
+ fn=gradio_app,
130
+ inputs=gr.inputs.Image(type="pil"),
131
+ outputs=[gr.outputs.Image(type="pil", label=f"Recommendation {i+1} Image") for i in range(5)] +
132
+ [gr.outputs.Textbox(label=f"Recommendation {i+1} Model Name") for i in range(5)] +
133
+ [gr.outputs.Textbox(label=f"Recommendation {i+1} Distance") for i in range(5)],
134
  title="Image Recommendation System",
135
+ description="Upload an image to get recommendations based on the image"
136
  )
137
+
138
+ # Launch the Gradio app
139
  iface.launch()