Update app.py
Browse files
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
|
101 |
-
|
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 |
-
|
115 |
-
|
116 |
-
recommendations = []
|
117 |
for i in range(len(recommended_images)):
|
118 |
-
|
119 |
-
|
120 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
121 |
iface = gr.Interface(
|
122 |
-
fn=
|
123 |
-
inputs=gr.Image(
|
124 |
-
outputs=gr.
|
|
|
|
|
125 |
title="Image Recommendation System",
|
126 |
-
description="Upload an image
|
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()
|