Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,75 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import pandas as pd
|
2 |
+
import gradio as gr
|
3 |
+
import matplotlib.pyplot as plt
|
4 |
+
import io
|
5 |
+
import base64
|
6 |
+
from sklearn.feature_extraction.text import TfidfVectorizer
|
7 |
+
from sklearn.metrics.pairwise import linear_kernel
|
8 |
+
|
9 |
+
# Load your dataset
|
10 |
+
data = pd.read_csv('Blinkit Cart Prediction.csv')
|
11 |
+
|
12 |
+
# Create a TF-IDF vectorizer
|
13 |
+
tfidf_vectorizer = TfidfVectorizer(max_features=1000) # Adjust max_features as needed
|
14 |
+
tfidf_matrix = tfidf_vectorizer.fit_transform(data['Description'])
|
15 |
+
|
16 |
+
# Function to recommend products
|
17 |
+
def recommend_products(user_choice, num_recommendations=10):
|
18 |
+
# Transform the user choice using the same TF-IDF vectorizer
|
19 |
+
user_choice_vector = tfidf_vectorizer.transform([user_choice])
|
20 |
+
|
21 |
+
# Compute the cosine similarity between the user choice and all products
|
22 |
+
cosine_similarities = linear_kernel(user_choice_vector, tfidf_matrix)
|
23 |
+
|
24 |
+
# Get the indices of the most similar products
|
25 |
+
similar_indices = cosine_similarities.argsort()[0][-num_recommendations - 1:-1][::-1]
|
26 |
+
|
27 |
+
# Get the recommended product IDs and names
|
28 |
+
recommended_products = data.iloc[similar_indices][['ProductID', 'ProductName']]
|
29 |
+
|
30 |
+
return recommended_products
|
31 |
+
|
32 |
+
# Define the input and output components
|
33 |
+
input_component = gr.inputs.Textbox(label="Enter Your Choice")
|
34 |
+
output_component = gr.outputs.HTML(label="Recommended Products")
|
35 |
+
|
36 |
+
# Create the Gradio interface
|
37 |
+
def recommend_interface(user_choice):
|
38 |
+
recommended_products = recommend_products(user_choice)
|
39 |
+
|
40 |
+
# Create a bar graph of recommended products
|
41 |
+
plt.figure(figsize=(10, 6))
|
42 |
+
plt.bar(recommended_products['ProductName'], range(len(recommended_products)), color='skyblue')
|
43 |
+
plt.xticks(rotation=45, ha="right")
|
44 |
+
plt.xlabel("Recommended Products")
|
45 |
+
plt.ylabel("Ranking")
|
46 |
+
plt.title("Top Recommended Products")
|
47 |
+
|
48 |
+
# Encode the image as base64
|
49 |
+
buffer = io.BytesIO()
|
50 |
+
plt.savefig(buffer, format="png")
|
51 |
+
graph_base64 = base64.b64encode(buffer.getvalue()).decode()
|
52 |
+
plt.close()
|
53 |
+
|
54 |
+
# Create an HTML string with an embedded image
|
55 |
+
graph_html = f'<img src="data:image/png;base64,{graph_base64}" />'
|
56 |
+
|
57 |
+
# Create an HTML string for the table of recommended products
|
58 |
+
table_html = recommended_products.to_html(index=False)
|
59 |
+
|
60 |
+
# Concatenate the HTML strings for both components
|
61 |
+
result_html = f"<h2>Recommended Products:</h2>{table_html}<br>{graph_html}"
|
62 |
+
|
63 |
+
return result_html
|
64 |
+
|
65 |
+
# Launch the Gradio app
|
66 |
+
interface = gr.Interface(
|
67 |
+
fn=recommend_interface, inputs=input_component, outputs=output_component,
|
68 |
+
live = True,
|
69 |
+
description = " Press flag if any erroneous output comes ",
|
70 |
+
theme=gr.themes.Soft(),
|
71 |
+
title = "Blinkit Cart Prediction",
|
72 |
+
examples = [['necklace'],
|
73 |
+
['DSLR camera '],['tea '], ['Smart TV '] , ['protein bars'] , ['sunglasses '] ],
|
74 |
+
)
|
75 |
+
interface.launch(inline=False)
|