File size: 3,036 Bytes
9a51b24
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
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()