import pandas as pd from sklearn.feature_extraction.text import TfidfVectorizer from sklearn.metrics.pairwise import cosine_similarity import gradio as gr class BookRecommender: def __init__(self): self.df = None self.similarity_matrix = None def load_data(self, file_obj): try: if file_obj.name.endswith('.csv'): df = pd.read_csv(file_obj) elif file_obj.name.endswith(('.xls', '.xlsx')): df = pd.read_excel(file_obj) else: raise ValueError("Unsupported file format. Please provide a CSV or Excel file.") return df except Exception as e: return str(e) def preprocess_data(self, df): df['summary'] = df['summary'].fillna('') df['title'] = df['title'].fillna('') df = df.drop_duplicates(subset=['title', 'summary']) return df def create_tfidf_matrix(self, df): tfidf = TfidfVectorizer(stop_words='english') tfidf_matrix = tfidf.fit_transform(df['summary']) return tfidf_matrix def calculate_similarity(self, tfidf_matrix): return cosine_similarity(tfidf_matrix) def recommend_books(self, book_title): if self.df is None or self.similarity_matrix is None: return ["Please upload and process a file first."] try: book_index = self.df[self.df['title'] == book_title].index[0] except IndexError: return ["Book title not found."] similar_books_indices = self.similarity_matrix[book_index].argsort()[::-1][1:6] return self.df['title'].iloc[similar_books_indices].tolist() def create_interface(self): def process_file(file_obj): if file_obj is None: return "Please upload a file first.", None self.df = self.load_data(file_obj) self.df = self.preprocess_data(self.df) tfidf_matrix = self.create_tfidf_matrix(self.df) self.similarity_matrix = self.calculate_similarity(tfidf_matrix) return "File uploaded and processed successfully!", gr.update(interactive=True) def recommend_interface(book_title): recommendations = self.recommend_books(book_title) return recommendations with gr.Blocks() as iface: file_input = gr.File(label="Upload CSV or Excel file") process_button = gr.Button("Process File") status_text = gr.Textbox(label="Status", interactive=False) text_input = gr.Textbox(lines=1, placeholder="Enter book title", interactive=False) output_list = gr.Textbox(label="Recommended Books", interactive=False) process_button.click(process_file, inputs=file_input, outputs=[status_text, text_input]) text_input.submit(recommend_interface, inputs=text_input, outputs=output_list) return iface recommender = BookRecommender() interface = recommender.create_interface() interface.launch()