| """ |
| Demo script for Book Popularity Predictor |
| ========================================== |
| |
| This script demonstrates the machine learning model capabilities |
| without the web interface. Perfect for showcasing the core functionality. |
| """ |
|
|
| import sys |
| import os |
| sys.path.append(os.path.join(os.path.dirname(__file__), 'src')) |
|
|
| from src.prediction_utils import BookPopularityPredictor |
| import pandas as pd |
|
|
| def run_demo(): |
| """Run demonstration of the book popularity prediction model""" |
| |
| print("๐ Book Popularity Predictor - Demo") |
| print("=" * 50) |
| |
| |
| predictor = BookPopularityPredictor(models_dir='models') |
| |
| |
| if not predictor.load_model_components(): |
| print("โ Failed to load model. Please ensure the model is trained.") |
| print("Run: python src/model_training.py") |
| return |
| |
| print("โ
Model loaded successfully!") |
| print(f"๐ Available authors: {len(predictor.get_top_authors())}") |
| |
| |
| demo_cases = [ |
| { |
| "author": "Stephen King", |
| "ratings_count": 100000, |
| "reviews_count": 12000, |
| "description": "Popular Stephen King horror novel" |
| }, |
| { |
| "author": "J.K. Rowling", |
| "ratings_count": 500000, |
| "reviews_count": 60000, |
| "description": "New Harry Potter series book" |
| }, |
| { |
| "author": "Jane Austen", |
| "ratings_count": 75000, |
| "reviews_count": 9000, |
| "description": "Classic romance novel reprint" |
| }, |
| { |
| "author": "Rick Riordan", |
| "ratings_count": 200000, |
| "reviews_count": 25000, |
| "description": "New Percy Jackson adventure" |
| } |
| ] |
| |
| print("\\n๐ฏ Demo Predictions:") |
| print("-" * 50) |
| |
| for i, case in enumerate(demo_cases, 1): |
| print(f"\\n{i}. {case['description']}") |
| print(f" Author: {case['author']}") |
| print(f" Expected Ratings: {case['ratings_count']:,}") |
| print(f" Expected Reviews: {case['reviews_count']:,}") |
| |
| |
| result = predictor.predict_book_rating( |
| case['author'], |
| case['ratings_count'], |
| case['reviews_count'] |
| ) |
| |
| if "error" in result: |
| print(f" โ Error: {result['error']}") |
| else: |
| predicted_rating = result["predicted_rating"] |
| confidence = result["confidence"] |
| |
| |
| if predicted_rating >= 4.5: |
| rating_desc = "Excellent (โญโญโญโญโญ)" |
| elif predicted_rating >= 4.0: |
| rating_desc = "Very Good (โญโญโญโญ)" |
| elif predicted_rating >= 3.5: |
| rating_desc = "Good (โญโญโญ)" |
| else: |
| rating_desc = "Average (โญโญ)" |
| |
| print(f" ๐ฏ Predicted Rating: {predicted_rating}/5.0 - {rating_desc}") |
| print(f" ๐ Confidence: {confidence}") |
| |
| |
| print("\\n" + "=" * 50) |
| print("๐ง Model Insights:") |
| print("-" * 50) |
| print("๐ Most Important Features:") |
| print(" 1. Author Identity (33.7% importance)") |
| print(" 2. Author Book Count (13.9% importance)") |
| print(" 3. Reviews Count (11.8% importance)") |
| print(" 4. Ratings Count (11.0% importance)") |
| |
| print("\\n๐ Model Performance:") |
| print(" โข Test Rยฒ Score: 0.544 (Medium accuracy)") |
| print(" โข Mean Absolute Error: ยฑ0.121 rating points") |
| print(" โข Training Data: 990+ books from Goodreads") |
| |
| print("\\n๐ฏ Prediction Guidelines:") |
| print(" โข Higher ratings count โ More reliable prediction") |
| print(" โข Popular authors โ Higher confidence") |
| print(" โข Typical rating range: 3.0 - 4.8") |
| print(" โข Average book rating: 4.1") |
| |
| |
| print("\\n" + "=" * 50) |
| print("๐ฎ Try Your Own Prediction!") |
| print("-" * 50) |
| |
| try: |
| |
| authors = predictor.get_top_authors() |
| print("\\n๐ Available Authors (Top 20):") |
| for i, author in enumerate(authors[:10], 1): |
| print(f" {i:2d}. {author}") |
| print(" ... and 10 more authors") |
| |
| |
| print("\\nEnter details for your book prediction:") |
| |
| |
| author_input = input("\\n๐ค Author name (from list above): ").strip() |
| if not author_input: |
| author_input = "Stephen King" |
| |
| |
| try: |
| ratings_input = input("๐ Expected ratings count (e.g., 50000): ").strip() |
| ratings_count = int(ratings_input) if ratings_input else 50000 |
| except ValueError: |
| ratings_count = 50000 |
| |
| |
| try: |
| reviews_input = input("๐ฌ Expected reviews count (optional): ").strip() |
| reviews_count = int(reviews_input) if reviews_input else None |
| except ValueError: |
| reviews_count = None |
| |
| |
| print("\\n๐ค Analyzing book characteristics...") |
| result = predictor.predict_book_rating(author_input, ratings_count, reviews_count) |
| |
| if "error" in result: |
| print(f"โ Error: {result['error']}") |
| else: |
| predicted_rating = result["predicted_rating"] |
| confidence = result["confidence"] |
| |
| print(f"\\n๐ฏ PREDICTION RESULT:") |
| print(f" ๐ Book by: {author_input}") |
| print(f" โญ Predicted Rating: {predicted_rating}/5.0") |
| print(f" ๐ฏ Confidence: {confidence}") |
| |
| if confidence == "High": |
| print(" โ
High confidence prediction!") |
| elif confidence == "Medium": |
| print(" โ ๏ธ Medium confidence - consider more popular author") |
| else: |
| print(" ๐ Low confidence - prediction may be less reliable") |
| |
| except KeyboardInterrupt: |
| print("\\n\\n๐ Demo ended by user.") |
| except Exception as e: |
| print(f"\\nโ Error during interactive prediction: {e}") |
| |
| print("\\n" + "=" * 50) |
| print("๐ Want to try the web interface?") |
| print(" Run: streamlit run streamlit_app.py") |
| print(" Then visit: http://localhost:8501") |
| print("\\n๐ Thanks for trying Book Popularity Predictor!") |
| print("=" * 50) |
|
|
| if __name__ == "__main__": |
| run_demo() |