import streamlit as st import pandas as pd from sklearn.feature_extraction.text import TfidfVectorizer from sklearn.cluster import KMeans from fastapi import FastAPI import joblib def show_recommendations(product): Y = vectorizer.transform([product]) prediction = model.predict(Y) return prediction def print_cluster(i): for ind in order_centroids[i, :10]: print(' %s' % terms[ind]), def get_cluster_terms(cluster_index): cluster_terms = [terms[ind] for ind in order_centroids[cluster_index, :10]] return cluster_terms model = joblib.load("./model.pkl") vectorizer = joblib.load("./vectorizer.pkl") order_centroids = model.cluster_centers_.argsort()[:, ::-1] terms = vectorizer.get_feature_names_out() st.title("Product Recommendation System") # Input for product description product_input = st.text_input("Enter a product description:", "") # Button to trigger recommendation if st.button("Get Recommendations"): if product_input: # Get cluster for the input product cluster_index = show_recommendations(product_input)[0] # Display the cluster number st.write(f"The product belongs to cluster: {cluster_index}") # Display the top terms in the cluster cluster_terms = get_cluster_terms(cluster_index) st.write("Top terms in this cluster:") st.write(", ".join(cluster_terms)) else: st.write("Please enter a product description.")