|
import pandas as pd |
|
import gradio as gr |
|
import matplotlib.pyplot as plt |
|
import io |
|
import base64 |
|
from sklearn.feature_extraction.text import TfidfVectorizer |
|
from sklearn.metrics.pairwise import linear_kernel |
|
|
|
|
|
data = pd.read_csv('Blinkit Cart Prediction.csv') |
|
|
|
|
|
tfidf_vectorizer = TfidfVectorizer(max_features=1000) |
|
tfidf_matrix = tfidf_vectorizer.fit_transform(data['Description']) |
|
|
|
|
|
def recommend_products(user_choice, num_recommendations=10): |
|
|
|
user_choice_vector = tfidf_vectorizer.transform([user_choice]) |
|
|
|
|
|
cosine_similarities = linear_kernel(user_choice_vector, tfidf_matrix) |
|
|
|
|
|
similar_indices = cosine_similarities.argsort()[0][-num_recommendations - 1:-1][::-1] |
|
|
|
|
|
recommended_products = data.iloc[similar_indices][['ProductID', 'ProductName']] |
|
|
|
return recommended_products |
|
|
|
|
|
input_component = gr.inputs.Textbox(label="Enter Your Choice") |
|
output_component = gr.outputs.HTML(label="Recommended Products") |
|
|
|
|
|
def recommend_interface(user_choice): |
|
recommended_products = recommend_products(user_choice) |
|
|
|
|
|
plt.figure(figsize=(10, 6)) |
|
plt.bar(recommended_products['ProductName'], range(len(recommended_products)), color='skyblue') |
|
plt.xticks(rotation=45, ha="right") |
|
plt.xlabel("Recommended Products") |
|
plt.ylabel("Ranking") |
|
plt.title("Top Recommended Products") |
|
|
|
|
|
buffer = io.BytesIO() |
|
plt.savefig(buffer, format="png") |
|
graph_base64 = base64.b64encode(buffer.getvalue()).decode() |
|
plt.close() |
|
|
|
|
|
graph_html = f'<img src="data:image/png;base64,{graph_base64}" />' |
|
|
|
|
|
table_html = recommended_products.to_html(index=False) |
|
|
|
|
|
result_html = f"<h2>Recommended Products:</h2>{table_html}<br>{graph_html}" |
|
|
|
return result_html |
|
|
|
|
|
interface = gr.Interface( |
|
fn=recommend_interface, inputs=input_component, outputs=output_component, |
|
live = True, |
|
description = " Press flag if any erroneous output comes ", |
|
theme=gr.themes.Soft(), |
|
title = "Blinkit Cart Prediction", |
|
examples = [['necklace'], |
|
['DSLR camera '],['tea '], ['Smart TV '] , ['protein bars'] , ['sunglasses '] ], |
|
) |
|
interface.launch(inline=False) |