File size: 2,022 Bytes
a5b759b
 
 
 
93a070f
 
a5b759b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
93a070f
a5b759b
 
93a070f
 
a5b759b
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
import pandas as pd
import numpy as np
from sentence_transformers import SentenceTransformer
from sklearn.metrics.pairwise import cosine_similarity
import gradio as gr

# Sample book data
book_metadata = pd.DataFrame([
    {"book_id": "1", "title": "Harry Potter", "description": "A fantasy novel about a young wizard", "page_count": 309},
    {"book_id": "2", "title": "The Hobbit", "description": "A short fantasy adventure with dragons and magic", "page_count": 193},
    {"book_id": "3", "title": "The Great Gatsby", "description": "A classic novel about wealth and dreams", "page_count": 180}
])

amazon_reviews = pd.DataFrame([
    {"book_id": "1", "review": "Amazing story of magic and friendship."},
    {"book_id": "2", "review": "A timeless tale filled with dragons and courage."},
    {"book_id": "3", "review": "A beautiful depiction of the roaring twenties."}
])

combined = pd.merge(book_metadata, amazon_reviews, on="book_id")
combined["full_text"] = combined["description"] + " " + combined["review"]

model = SentenceTransformer("all-MiniLM-L6-v2")
combined["embedding"] = list(model.encode(combined["full_text"].tolist()))

def recommend_books(query, max_pages):
    user_vec = model.encode([query])
    book_embeddings = np.vstack(combined["embedding"].values)
    scores = cosine_similarity(user_vec, book_embeddings)[0]
    combined["score"] = scores

    filtered = combined[combined["page_count"] <= max_pages]
    top_books = filtered.sort_values("score", ascending=False).head(3)

    return "\\n\\n".join([
        f\"{row['title']} ({row['page_count']} pages)\\n{row['description']}\\nScore: {round(row['score'], 4)}\"
        for _, row in top_books.iterrows()
    ])

demo = gr.Interface(
    fn=recommend_books,
    inputs=[
        gr.Textbox(label=\"Your Query\", placeholder=\"e.g., I want a short fantasy book about wizards\"),
        gr.Slider(minimum=50, maximum=1000, label=\"Max Page Count\", step=50)
    ],
    outputs=\"text\",
    title=\"📚 Book Recommender\"
)

demo.launch()