Spaces:
Sleeping
Sleeping
yazanateer
commited on
Commit
•
396af30
1
Parent(s):
cd8b7d1
Update app.py
Browse files
app.py
CHANGED
@@ -1,9 +1,10 @@
|
|
1 |
import pandas as pd
|
|
|
2 |
from sklearn.feature_extraction.text import TfidfVectorizer
|
3 |
from sklearn.metrics.pairwise import linear_kernel
|
4 |
-
|
5 |
|
6 |
-
#
|
7 |
data = {
|
8 |
'product_id': [1, 2, 3, 4, 5],
|
9 |
'name': ['Laptop', 'Smartphone', 'Headphones', 'Smartwatch', 'Tablet'],
|
@@ -16,39 +17,77 @@ data = {
|
|
16 |
]
|
17 |
}
|
18 |
|
|
|
|
|
|
|
19 |
|
|
|
20 |
products_df = pd.DataFrame(data)
|
21 |
|
22 |
-
# Vectorizing the product descriptions
|
23 |
tfidf = TfidfVectorizer(stop_words='english')
|
24 |
tfidf_matrix = tfidf.fit_transform(products_df['description'])
|
25 |
|
26 |
# Compute the cosine similarity matrix
|
27 |
cosine_sim = linear_kernel(tfidf_matrix, tfidf_matrix)
|
28 |
|
29 |
-
# Function to get product recommendations
|
30 |
def get_recommendations(product_name, cosine_sim=cosine_sim):
|
31 |
idx = products_df[products_df['name'] == product_name].index[0]
|
32 |
sim_scores = list(enumerate(cosine_sim[idx]))
|
33 |
sim_scores = sorted(sim_scores, key=lambda x: x[1], reverse=True)
|
34 |
-
sim_scores = sim_scores[1:4]
|
35 |
product_indices = [i[0] for i in sim_scores]
|
36 |
-
return products_df
|
37 |
-
|
38 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
39 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
40 |
|
41 |
-
#
|
42 |
def recommend_products(product_name):
|
43 |
-
|
44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
45 |
|
|
|
46 |
iface = gr.Interface(
|
47 |
fn=recommend_products,
|
48 |
inputs=gr.Dropdown(choices=products_df['name'].tolist(), label="Select a product"),
|
49 |
-
outputs=gr.Textbox(label="Recommended Products"),
|
50 |
title="AI-driven E-commerce Recommendation Engine",
|
51 |
-
description="Select a product to get recommendations
|
52 |
)
|
53 |
|
54 |
# Launch the Gradio interface
|
|
|
1 |
import pandas as pd
|
2 |
+
import gradio as gr
|
3 |
from sklearn.feature_extraction.text import TfidfVectorizer
|
4 |
from sklearn.metrics.pairwise import linear_kernel
|
5 |
+
from groq import Groq
|
6 |
|
7 |
+
# Sample data for products
|
8 |
data = {
|
9 |
'product_id': [1, 2, 3, 4, 5],
|
10 |
'name': ['Laptop', 'Smartphone', 'Headphones', 'Smartwatch', 'Tablet'],
|
|
|
17 |
]
|
18 |
}
|
19 |
|
20 |
+
# Initialize Groq client
|
21 |
+
api_key = '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'
|
22 |
+
client = Groq(api_key=api_key)
|
23 |
|
24 |
+
# Create DataFrame
|
25 |
products_df = pd.DataFrame(data)
|
26 |
|
27 |
+
# Vectorizing the product descriptions
|
28 |
tfidf = TfidfVectorizer(stop_words='english')
|
29 |
tfidf_matrix = tfidf.fit_transform(products_df['description'])
|
30 |
|
31 |
# Compute the cosine similarity matrix
|
32 |
cosine_sim = linear_kernel(tfidf_matrix, tfidf_matrix)
|
33 |
|
34 |
+
# Function to get product recommendations
|
35 |
def get_recommendations(product_name, cosine_sim=cosine_sim):
|
36 |
idx = products_df[products_df['name'] == product_name].index[0]
|
37 |
sim_scores = list(enumerate(cosine_sim[idx]))
|
38 |
sim_scores = sorted(sim_scores, key=lambda x: x[1], reverse=True)
|
39 |
+
sim_scores = sim_scores[1:4] # Get top 3 recommendations
|
40 |
product_indices = [i[0] for i in sim_scores]
|
41 |
+
return products_df.iloc[product_indices]
|
|
|
42 |
|
43 |
+
# Function to fetch descriptions from Groq based on product names
|
44 |
+
def fetch_descriptions(product_names):
|
45 |
+
descriptions = []
|
46 |
+
try:
|
47 |
+
for product_name in product_names:
|
48 |
+
chat_completion = client.chat.completions.create(
|
49 |
+
messages=[
|
50 |
+
{
|
51 |
+
"role": "user",
|
52 |
+
"content": f"Generate description for {product_name}",
|
53 |
+
}
|
54 |
+
],
|
55 |
+
model="llama3-8b-8192",
|
56 |
+
)
|
57 |
+
description = chat_completion.choices[0].message.content.strip()
|
58 |
+
descriptions.append((product_name, description))
|
59 |
+
except Exception as e:
|
60 |
+
descriptions.append((product_name, f"Description fetch failed: {str(e)}"))
|
61 |
+
return descriptions
|
62 |
|
63 |
+
# Function to format description in organized manner
|
64 |
+
def format_description(product_name, description):
|
65 |
+
formatted_description = (
|
66 |
+
f"**{product_name}:**\n\n"
|
67 |
+
f"{description}\n\n"
|
68 |
+
)
|
69 |
+
return formatted_description
|
70 |
|
71 |
+
# Function to recommend products and fetch descriptions
|
72 |
def recommend_products(product_name):
|
73 |
+
recommended_products_df = get_recommendations(product_name)
|
74 |
+
recommended_product_names = recommended_products_df['name'].tolist()
|
75 |
+
recommended_product_descriptions = fetch_descriptions(recommended_product_names)
|
76 |
+
|
77 |
+
# Combine product names and descriptions for output
|
78 |
+
recommendations_with_descriptions = [
|
79 |
+
format_description(name, desc) for name, desc in recommended_product_descriptions
|
80 |
+
]
|
81 |
+
return '\n'.join(recommendations_with_descriptions)
|
82 |
+
|
83 |
|
84 |
+
# Define Gradio interface
|
85 |
iface = gr.Interface(
|
86 |
fn=recommend_products,
|
87 |
inputs=gr.Dropdown(choices=products_df['name'].tolist(), label="Select a product"),
|
88 |
+
outputs=gr.Textbox(label="Recommended Products with Descriptions", type="text"),
|
89 |
title="AI-driven E-commerce Recommendation Engine",
|
90 |
+
description="Select a product to get recommendations along with their descriptions."
|
91 |
)
|
92 |
|
93 |
# Launch the Gradio interface
|