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()