Menu iconMenu iconNatural Language Processing with Python Updated Edition
Natural Language Processing with Python Updated Edition

Chapter 12: Project: News Aggregator

12.5 Evaluating and Deploying the Aggregator

In this section, we will focus on evaluating the performance of the news aggregator chatbot and deploying it to a suitable platform. Evaluation helps ensure that the chatbot meets user expectations and performs well under various conditions. Deployment makes the chatbot accessible to users, allowing them to benefit from its functionalities.

12.5.1 Evaluating the Aggregator

Evaluation involves measuring the chatbot's performance in terms of accuracy, responsiveness, and user satisfaction. We will use different metrics and methods to evaluate these aspects.

1. Accuracy Metrics

Accuracy metrics assess how well the chatbot categorizes and summarizes news articles. Common metrics include precision, recall, and F1-score for categorization accuracy and user feedback for summarization quality.

Example: Evaluating Categorization Accuracy

We can use the sklearn library to calculate precision, recall, and F1-score for the categorization model.

evaluate.py:

from sklearn.metrics import classification_report
from nlp_engine import categorize_article

# Load test articles and their true categories
with open('data/test_articles.json', 'r') as file:
    test_articles = json.load(file)

# Prepare data for evaluation
true_categories = [article['category'] for article in test_articles]
predicted_categories = [categorize_article(article['content']) for article in test_articles]

# Flatten lists for multi-label classification
true_categories_flat = [item for sublist in true_categories for item in sublist]
predicted_categories_flat = [item for sublist in predicted_categories for item in sublist]

# Print classification report
print(classification_report(true_categories_flat, predicted_categories_flat))

In this script, we load test articles and their true categories, predict the categories using the trained model, and print a classification report that includes precision, recall, and F1-score.

2. User Feedback for Summarization

User feedback is essential for assessing the quality of the summaries generated by the chatbot. We can collect feedback from users through surveys or direct ratings.

Example: Collecting User Feedback

We can modify our Flask application to include a feedback form where users can rate the quality of the summaries.

app.py (continued):

feedback_data = []

@app.route('/feedback', methods=['POST'])
def feedback():
    user_feedback = request.json
    feedback_data.append(user_feedback)
    return jsonify({'message': 'Thank you for your feedback!'})

# HTML template for feedback form
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>News Aggregator Chatbot</title>
    <link rel="stylesheet" href="<https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css>">
</head>
<body>
    <div class="container">
        <h1 class="mt-5">News Aggregator Chatbot</h1>
        <form id="news-form" class="mt-4">
            <div class="form-group">
                <label for="category">Select News Category</label>
                <select class="form-control" id="category" name="category">
                    <option value="general">General</option>
                    <option value="business">Business</option>
                    <option value="technology">Technology</option>
                    <option value="sports">Sports</option>
                    <option value="entertainment">Entertainment</option>
                </select>
            </div>
            <button type="submit" class="btn btn-primary">Get News</button>
        </form>
        <div id="news-articles" class="mt-4"></div>
    </div>

    <script src="<https://code.jquery.com/jquery-3.5.1.min.js>"></script>
    <script>
        $(document).ready(function() {
            $('#news-form').on('submit', function(event) {
                event.preventDefault();
                const category = $('#category').val();
                $.post('/get_news', { category: category }, function(data) {
                    let articlesHtml = '<div class="list-group">';
                    data.forEach(article => {
                        articlesHtml += `
                            <div class="list-group-item">
                                <h5 class="mb-1">${article.title}</h5>
                                <p class="mb-1">${article.description}</p>
                                <small>${article.source} - ${article.publishedAt}</small>
                                <button class="btn btn-secondary btn-sm mt-2" onclick="categorizeArticle('${article.content}')">Categorize</button>
                                <div class="category mt-2" id="category-${article.title}"></div>
                                <button class="btn btn-secondary btn-sm mt-2" onclick="summarizeArticle('${article.content}')">Summarize</button>
                                <div class="summary mt-2" id="summary-${article.title}"></div>
                                <form id="feedback-form-${article.title}" class="mt-2">
                                    <label for="rating">Rate this summary:</label>
                                    <select class="form-control" id="rating-${article.title}">
                                        <option value="1">1 - Poor</option>
                                        <option value="2">2 - Fair</option>
                                        <option value="3">3 - Good</option>
                                        <option value="4">4 - Very Good</option>
                                        <option value="5">5 - Excellent</option>
                                    </select>
                                    <button type="submit" class="btn btn-primary btn-sm mt-2">Submit Feedback</button>
                                </form>
                            </div>
                        `;
                    });
                    articlesHtml += '</div>';
                    $('#news-articles').html(articlesHtml);
                });
            });

            function summarizeArticle(article) {
                const summaryType = 'extractive';  // or 'abstractive'
                $.post('/summarize_article', { article: article, summary_type: summaryType }, function(data) {
                    $('#summary-' + article.title).html('<p>' + data.summary + '</p>');
                });
            }

            function categorizeArticle(article) {
                $.post('/categorize_article', { article: article }, function(data) {
                    $('#category-' + article.title).html('<p>' + data.category.join(', ') + '</p>');
                });
            }

            $('body').on('submit', 'form[id^="feedback-form-"]', function(event) {
                event.preventDefault();
                const articleTitle = $(this).attr('id').replace('feedback-form-', '');
                const rating = $(`#rating-${articleTitle}`).val();
                const feedback = {
                    article: articleTitle,
                    rating: rating
                };
                $.post('/feedback', feedback, function(response) {
                    alert(response.message);
                });
            });
        });
    </script>
</body>
</html>

In this updated template, we add a feedback form for each article, allowing users to rate the quality of the summaries. The feedback is sent to the /feedback endpoint and stored for further analysis.

3. Response Time

Response time is critical for user experience. We need to ensure that the chatbot responds promptly to user queries.

Example: Measuring Response Time

We can measure the response time for different queries using Python's time module.

evaluate.py (continued):

import time

# Function to measure response time
def measure_response_time(endpoint, data):
    start_time = time.time()
    response = requests.post(endpoint, json=data)
    end_time = time.time()
    response_time = end_time - start_time
    return response_time

# Measure response time for summarization
article = "Sample news article content..."
data = {"article": article, "summary_type": "extractive"}
response_time = measure_response_time("<http://localhost:5000/summarize_article>", data)
print(f"Summarization Response Time: {response_time} seconds")

By measuring response time, we can ensure that the chatbot meets the desired performance criteria.

12.5.2 Deploying the Aggregator

Once the chatbot is evaluated and performs satisfactorily, the next step is deployment. We will deploy the chatbot to a suitable platform where users can interact with it.

1. Web Application Deployment

We can deploy the chatbot as a web application using a cloud platform such as Heroku, AWS, or Google Cloud.

Example: Deploying on Heroku

To deploy the chatbot on Heroku, follow these steps:

  1. Install Heroku CLI: Download and install the Heroku CLI from Heroku.
  2. Log In to Heroku: Open a terminal and log in to your Heroku account.
    heroku login
  3. Create a Heroku App: Create a new Heroku app.
    heroku create your-app-name
  4. Prepare the Project for Deployment: Create a Procfile and requirements.txt in the project directory.
    Procfile:requirements.txt:
    web: python app.py
    Flask
    requests
    nltk
    gensim
    transformers
  5. Push the Project to Heroku: Initialize a Git repository, add the project files, and push to Heroku.
    git init
    git add .
    git commit -m "Initial commit"
    git push heroku master
  6. Open the Heroku App: Open the deployed app in your browser.
    heroku open

By following these steps, the chatbot will be deployed on Heroku and accessible via a public URL.

2. Integrating with Messaging Apps

We can also integrate the chatbot with messaging apps like Facebook Messenger, Slack, or WhatsApp. Each platform provides APIs for chatbot integration.

Example: Integrating with Facebook Messenger

To integrate the chatbot with Facebook Messenger, follow these steps:

  1. Create a Facebook Page: Create a new Facebook Page for your chatbot.
  2. Set Up Facebook Developer Account: Register as a developer on the Facebook Developer portal.
  3. Create a Facebook App: Create a new app and add the Messenger product.
  4. Generate Access Token: Generate a Page Access Token for your app.
  5. Set Up Webhook: Set up a webhook to receive messages from users.

Webhook Setup Example:

from flask import Flask, request
import requests

app = Flask(__name__)

PAGE_ACCESS_TOKEN = 'your_facebook_page_access_token'

def send_message(recipient_id, message_text):
    url = f"<https://graph.facebook.com/v11.0/me/messages?access_token={PAGE_ACCESS_TOKEN}>"
    headers = {"Content-Type": "application/json"}
    payload = {
        "recipient": {"id": recipient_id},
        "message": {"text": message_text}
    }
    requests.post(url, headers=headers, json=payload)

@app.route('/webhook', methods=['GET'])
def webhook_verification():
    mode = request.args.get('hub.mode')
    token = request.args.get('hub.verify_token')
    challenge = request.args.get('hub.challenge')
    if mode == 'subscribe' and token == 'your_verify_token':
        return challenge
    return 'Verification failed', 403

@app.route('/webhook', methods=['POST'])
def webhook():
    data = request.json
    if data['object'] == 'page':
        for entry in data['entry']:
            for messaging_event in entry['messaging']:
                sender_id = messaging_event['sender']['id']
                if 'message' in messaging_event:
                    message_text = messaging_event['message']['text']
                    response_text = summarize_text(message_text)  # Modify to call appropriate summarization function
                    send_message(sender_id, response_text)
    return 'OK', 200

if __name__ == '__main__':
    app.run(debug=True)

In this script, we define a webhook endpoint to receive messages from Facebook Messenger and respond using the send_message function.

In this section, we covered the evaluation and deployment of our news aggregator chatbot. We discussed various metrics and methods to evaluate the chatbot's performance, including accuracy metrics, user feedback, and response time. We also provided examples of deploying the chatbot as a web application using Heroku and integrating it with Facebook Messenger.

By following these steps, you can ensure that your chatbot performs well in real-world scenarios and is accessible to users on different platforms. The deployment process makes the chatbot available to users, allowing them to interact with it and benefit from its functionalities.

12.5 Evaluating and Deploying the Aggregator

In this section, we will focus on evaluating the performance of the news aggregator chatbot and deploying it to a suitable platform. Evaluation helps ensure that the chatbot meets user expectations and performs well under various conditions. Deployment makes the chatbot accessible to users, allowing them to benefit from its functionalities.

12.5.1 Evaluating the Aggregator

Evaluation involves measuring the chatbot's performance in terms of accuracy, responsiveness, and user satisfaction. We will use different metrics and methods to evaluate these aspects.

1. Accuracy Metrics

Accuracy metrics assess how well the chatbot categorizes and summarizes news articles. Common metrics include precision, recall, and F1-score for categorization accuracy and user feedback for summarization quality.

Example: Evaluating Categorization Accuracy

We can use the sklearn library to calculate precision, recall, and F1-score for the categorization model.

evaluate.py:

from sklearn.metrics import classification_report
from nlp_engine import categorize_article

# Load test articles and their true categories
with open('data/test_articles.json', 'r') as file:
    test_articles = json.load(file)

# Prepare data for evaluation
true_categories = [article['category'] for article in test_articles]
predicted_categories = [categorize_article(article['content']) for article in test_articles]

# Flatten lists for multi-label classification
true_categories_flat = [item for sublist in true_categories for item in sublist]
predicted_categories_flat = [item for sublist in predicted_categories for item in sublist]

# Print classification report
print(classification_report(true_categories_flat, predicted_categories_flat))

In this script, we load test articles and their true categories, predict the categories using the trained model, and print a classification report that includes precision, recall, and F1-score.

2. User Feedback for Summarization

User feedback is essential for assessing the quality of the summaries generated by the chatbot. We can collect feedback from users through surveys or direct ratings.

Example: Collecting User Feedback

We can modify our Flask application to include a feedback form where users can rate the quality of the summaries.

app.py (continued):

feedback_data = []

@app.route('/feedback', methods=['POST'])
def feedback():
    user_feedback = request.json
    feedback_data.append(user_feedback)
    return jsonify({'message': 'Thank you for your feedback!'})

# HTML template for feedback form
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>News Aggregator Chatbot</title>
    <link rel="stylesheet" href="<https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css>">
</head>
<body>
    <div class="container">
        <h1 class="mt-5">News Aggregator Chatbot</h1>
        <form id="news-form" class="mt-4">
            <div class="form-group">
                <label for="category">Select News Category</label>
                <select class="form-control" id="category" name="category">
                    <option value="general">General</option>
                    <option value="business">Business</option>
                    <option value="technology">Technology</option>
                    <option value="sports">Sports</option>
                    <option value="entertainment">Entertainment</option>
                </select>
            </div>
            <button type="submit" class="btn btn-primary">Get News</button>
        </form>
        <div id="news-articles" class="mt-4"></div>
    </div>

    <script src="<https://code.jquery.com/jquery-3.5.1.min.js>"></script>
    <script>
        $(document).ready(function() {
            $('#news-form').on('submit', function(event) {
                event.preventDefault();
                const category = $('#category').val();
                $.post('/get_news', { category: category }, function(data) {
                    let articlesHtml = '<div class="list-group">';
                    data.forEach(article => {
                        articlesHtml += `
                            <div class="list-group-item">
                                <h5 class="mb-1">${article.title}</h5>
                                <p class="mb-1">${article.description}</p>
                                <small>${article.source} - ${article.publishedAt}</small>
                                <button class="btn btn-secondary btn-sm mt-2" onclick="categorizeArticle('${article.content}')">Categorize</button>
                                <div class="category mt-2" id="category-${article.title}"></div>
                                <button class="btn btn-secondary btn-sm mt-2" onclick="summarizeArticle('${article.content}')">Summarize</button>
                                <div class="summary mt-2" id="summary-${article.title}"></div>
                                <form id="feedback-form-${article.title}" class="mt-2">
                                    <label for="rating">Rate this summary:</label>
                                    <select class="form-control" id="rating-${article.title}">
                                        <option value="1">1 - Poor</option>
                                        <option value="2">2 - Fair</option>
                                        <option value="3">3 - Good</option>
                                        <option value="4">4 - Very Good</option>
                                        <option value="5">5 - Excellent</option>
                                    </select>
                                    <button type="submit" class="btn btn-primary btn-sm mt-2">Submit Feedback</button>
                                </form>
                            </div>
                        `;
                    });
                    articlesHtml += '</div>';
                    $('#news-articles').html(articlesHtml);
                });
            });

            function summarizeArticle(article) {
                const summaryType = 'extractive';  // or 'abstractive'
                $.post('/summarize_article', { article: article, summary_type: summaryType }, function(data) {
                    $('#summary-' + article.title).html('<p>' + data.summary + '</p>');
                });
            }

            function categorizeArticle(article) {
                $.post('/categorize_article', { article: article }, function(data) {
                    $('#category-' + article.title).html('<p>' + data.category.join(', ') + '</p>');
                });
            }

            $('body').on('submit', 'form[id^="feedback-form-"]', function(event) {
                event.preventDefault();
                const articleTitle = $(this).attr('id').replace('feedback-form-', '');
                const rating = $(`#rating-${articleTitle}`).val();
                const feedback = {
                    article: articleTitle,
                    rating: rating
                };
                $.post('/feedback', feedback, function(response) {
                    alert(response.message);
                });
            });
        });
    </script>
</body>
</html>

In this updated template, we add a feedback form for each article, allowing users to rate the quality of the summaries. The feedback is sent to the /feedback endpoint and stored for further analysis.

3. Response Time

Response time is critical for user experience. We need to ensure that the chatbot responds promptly to user queries.

Example: Measuring Response Time

We can measure the response time for different queries using Python's time module.

evaluate.py (continued):

import time

# Function to measure response time
def measure_response_time(endpoint, data):
    start_time = time.time()
    response = requests.post(endpoint, json=data)
    end_time = time.time()
    response_time = end_time - start_time
    return response_time

# Measure response time for summarization
article = "Sample news article content..."
data = {"article": article, "summary_type": "extractive"}
response_time = measure_response_time("<http://localhost:5000/summarize_article>", data)
print(f"Summarization Response Time: {response_time} seconds")

By measuring response time, we can ensure that the chatbot meets the desired performance criteria.

12.5.2 Deploying the Aggregator

Once the chatbot is evaluated and performs satisfactorily, the next step is deployment. We will deploy the chatbot to a suitable platform where users can interact with it.

1. Web Application Deployment

We can deploy the chatbot as a web application using a cloud platform such as Heroku, AWS, or Google Cloud.

Example: Deploying on Heroku

To deploy the chatbot on Heroku, follow these steps:

  1. Install Heroku CLI: Download and install the Heroku CLI from Heroku.
  2. Log In to Heroku: Open a terminal and log in to your Heroku account.
    heroku login
  3. Create a Heroku App: Create a new Heroku app.
    heroku create your-app-name
  4. Prepare the Project for Deployment: Create a Procfile and requirements.txt in the project directory.
    Procfile:requirements.txt:
    web: python app.py
    Flask
    requests
    nltk
    gensim
    transformers
  5. Push the Project to Heroku: Initialize a Git repository, add the project files, and push to Heroku.
    git init
    git add .
    git commit -m "Initial commit"
    git push heroku master
  6. Open the Heroku App: Open the deployed app in your browser.
    heroku open

By following these steps, the chatbot will be deployed on Heroku and accessible via a public URL.

2. Integrating with Messaging Apps

We can also integrate the chatbot with messaging apps like Facebook Messenger, Slack, or WhatsApp. Each platform provides APIs for chatbot integration.

Example: Integrating with Facebook Messenger

To integrate the chatbot with Facebook Messenger, follow these steps:

  1. Create a Facebook Page: Create a new Facebook Page for your chatbot.
  2. Set Up Facebook Developer Account: Register as a developer on the Facebook Developer portal.
  3. Create a Facebook App: Create a new app and add the Messenger product.
  4. Generate Access Token: Generate a Page Access Token for your app.
  5. Set Up Webhook: Set up a webhook to receive messages from users.

Webhook Setup Example:

from flask import Flask, request
import requests

app = Flask(__name__)

PAGE_ACCESS_TOKEN = 'your_facebook_page_access_token'

def send_message(recipient_id, message_text):
    url = f"<https://graph.facebook.com/v11.0/me/messages?access_token={PAGE_ACCESS_TOKEN}>"
    headers = {"Content-Type": "application/json"}
    payload = {
        "recipient": {"id": recipient_id},
        "message": {"text": message_text}
    }
    requests.post(url, headers=headers, json=payload)

@app.route('/webhook', methods=['GET'])
def webhook_verification():
    mode = request.args.get('hub.mode')
    token = request.args.get('hub.verify_token')
    challenge = request.args.get('hub.challenge')
    if mode == 'subscribe' and token == 'your_verify_token':
        return challenge
    return 'Verification failed', 403

@app.route('/webhook', methods=['POST'])
def webhook():
    data = request.json
    if data['object'] == 'page':
        for entry in data['entry']:
            for messaging_event in entry['messaging']:
                sender_id = messaging_event['sender']['id']
                if 'message' in messaging_event:
                    message_text = messaging_event['message']['text']
                    response_text = summarize_text(message_text)  # Modify to call appropriate summarization function
                    send_message(sender_id, response_text)
    return 'OK', 200

if __name__ == '__main__':
    app.run(debug=True)

In this script, we define a webhook endpoint to receive messages from Facebook Messenger and respond using the send_message function.

In this section, we covered the evaluation and deployment of our news aggregator chatbot. We discussed various metrics and methods to evaluate the chatbot's performance, including accuracy metrics, user feedback, and response time. We also provided examples of deploying the chatbot as a web application using Heroku and integrating it with Facebook Messenger.

By following these steps, you can ensure that your chatbot performs well in real-world scenarios and is accessible to users on different platforms. The deployment process makes the chatbot available to users, allowing them to interact with it and benefit from its functionalities.

12.5 Evaluating and Deploying the Aggregator

In this section, we will focus on evaluating the performance of the news aggregator chatbot and deploying it to a suitable platform. Evaluation helps ensure that the chatbot meets user expectations and performs well under various conditions. Deployment makes the chatbot accessible to users, allowing them to benefit from its functionalities.

12.5.1 Evaluating the Aggregator

Evaluation involves measuring the chatbot's performance in terms of accuracy, responsiveness, and user satisfaction. We will use different metrics and methods to evaluate these aspects.

1. Accuracy Metrics

Accuracy metrics assess how well the chatbot categorizes and summarizes news articles. Common metrics include precision, recall, and F1-score for categorization accuracy and user feedback for summarization quality.

Example: Evaluating Categorization Accuracy

We can use the sklearn library to calculate precision, recall, and F1-score for the categorization model.

evaluate.py:

from sklearn.metrics import classification_report
from nlp_engine import categorize_article

# Load test articles and their true categories
with open('data/test_articles.json', 'r') as file:
    test_articles = json.load(file)

# Prepare data for evaluation
true_categories = [article['category'] for article in test_articles]
predicted_categories = [categorize_article(article['content']) for article in test_articles]

# Flatten lists for multi-label classification
true_categories_flat = [item for sublist in true_categories for item in sublist]
predicted_categories_flat = [item for sublist in predicted_categories for item in sublist]

# Print classification report
print(classification_report(true_categories_flat, predicted_categories_flat))

In this script, we load test articles and their true categories, predict the categories using the trained model, and print a classification report that includes precision, recall, and F1-score.

2. User Feedback for Summarization

User feedback is essential for assessing the quality of the summaries generated by the chatbot. We can collect feedback from users through surveys or direct ratings.

Example: Collecting User Feedback

We can modify our Flask application to include a feedback form where users can rate the quality of the summaries.

app.py (continued):

feedback_data = []

@app.route('/feedback', methods=['POST'])
def feedback():
    user_feedback = request.json
    feedback_data.append(user_feedback)
    return jsonify({'message': 'Thank you for your feedback!'})

# HTML template for feedback form
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>News Aggregator Chatbot</title>
    <link rel="stylesheet" href="<https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css>">
</head>
<body>
    <div class="container">
        <h1 class="mt-5">News Aggregator Chatbot</h1>
        <form id="news-form" class="mt-4">
            <div class="form-group">
                <label for="category">Select News Category</label>
                <select class="form-control" id="category" name="category">
                    <option value="general">General</option>
                    <option value="business">Business</option>
                    <option value="technology">Technology</option>
                    <option value="sports">Sports</option>
                    <option value="entertainment">Entertainment</option>
                </select>
            </div>
            <button type="submit" class="btn btn-primary">Get News</button>
        </form>
        <div id="news-articles" class="mt-4"></div>
    </div>

    <script src="<https://code.jquery.com/jquery-3.5.1.min.js>"></script>
    <script>
        $(document).ready(function() {
            $('#news-form').on('submit', function(event) {
                event.preventDefault();
                const category = $('#category').val();
                $.post('/get_news', { category: category }, function(data) {
                    let articlesHtml = '<div class="list-group">';
                    data.forEach(article => {
                        articlesHtml += `
                            <div class="list-group-item">
                                <h5 class="mb-1">${article.title}</h5>
                                <p class="mb-1">${article.description}</p>
                                <small>${article.source} - ${article.publishedAt}</small>
                                <button class="btn btn-secondary btn-sm mt-2" onclick="categorizeArticle('${article.content}')">Categorize</button>
                                <div class="category mt-2" id="category-${article.title}"></div>
                                <button class="btn btn-secondary btn-sm mt-2" onclick="summarizeArticle('${article.content}')">Summarize</button>
                                <div class="summary mt-2" id="summary-${article.title}"></div>
                                <form id="feedback-form-${article.title}" class="mt-2">
                                    <label for="rating">Rate this summary:</label>
                                    <select class="form-control" id="rating-${article.title}">
                                        <option value="1">1 - Poor</option>
                                        <option value="2">2 - Fair</option>
                                        <option value="3">3 - Good</option>
                                        <option value="4">4 - Very Good</option>
                                        <option value="5">5 - Excellent</option>
                                    </select>
                                    <button type="submit" class="btn btn-primary btn-sm mt-2">Submit Feedback</button>
                                </form>
                            </div>
                        `;
                    });
                    articlesHtml += '</div>';
                    $('#news-articles').html(articlesHtml);
                });
            });

            function summarizeArticle(article) {
                const summaryType = 'extractive';  // or 'abstractive'
                $.post('/summarize_article', { article: article, summary_type: summaryType }, function(data) {
                    $('#summary-' + article.title).html('<p>' + data.summary + '</p>');
                });
            }

            function categorizeArticle(article) {
                $.post('/categorize_article', { article: article }, function(data) {
                    $('#category-' + article.title).html('<p>' + data.category.join(', ') + '</p>');
                });
            }

            $('body').on('submit', 'form[id^="feedback-form-"]', function(event) {
                event.preventDefault();
                const articleTitle = $(this).attr('id').replace('feedback-form-', '');
                const rating = $(`#rating-${articleTitle}`).val();
                const feedback = {
                    article: articleTitle,
                    rating: rating
                };
                $.post('/feedback', feedback, function(response) {
                    alert(response.message);
                });
            });
        });
    </script>
</body>
</html>

In this updated template, we add a feedback form for each article, allowing users to rate the quality of the summaries. The feedback is sent to the /feedback endpoint and stored for further analysis.

3. Response Time

Response time is critical for user experience. We need to ensure that the chatbot responds promptly to user queries.

Example: Measuring Response Time

We can measure the response time for different queries using Python's time module.

evaluate.py (continued):

import time

# Function to measure response time
def measure_response_time(endpoint, data):
    start_time = time.time()
    response = requests.post(endpoint, json=data)
    end_time = time.time()
    response_time = end_time - start_time
    return response_time

# Measure response time for summarization
article = "Sample news article content..."
data = {"article": article, "summary_type": "extractive"}
response_time = measure_response_time("<http://localhost:5000/summarize_article>", data)
print(f"Summarization Response Time: {response_time} seconds")

By measuring response time, we can ensure that the chatbot meets the desired performance criteria.

12.5.2 Deploying the Aggregator

Once the chatbot is evaluated and performs satisfactorily, the next step is deployment. We will deploy the chatbot to a suitable platform where users can interact with it.

1. Web Application Deployment

We can deploy the chatbot as a web application using a cloud platform such as Heroku, AWS, or Google Cloud.

Example: Deploying on Heroku

To deploy the chatbot on Heroku, follow these steps:

  1. Install Heroku CLI: Download and install the Heroku CLI from Heroku.
  2. Log In to Heroku: Open a terminal and log in to your Heroku account.
    heroku login
  3. Create a Heroku App: Create a new Heroku app.
    heroku create your-app-name
  4. Prepare the Project for Deployment: Create a Procfile and requirements.txt in the project directory.
    Procfile:requirements.txt:
    web: python app.py
    Flask
    requests
    nltk
    gensim
    transformers
  5. Push the Project to Heroku: Initialize a Git repository, add the project files, and push to Heroku.
    git init
    git add .
    git commit -m "Initial commit"
    git push heroku master
  6. Open the Heroku App: Open the deployed app in your browser.
    heroku open

By following these steps, the chatbot will be deployed on Heroku and accessible via a public URL.

2. Integrating with Messaging Apps

We can also integrate the chatbot with messaging apps like Facebook Messenger, Slack, or WhatsApp. Each platform provides APIs for chatbot integration.

Example: Integrating with Facebook Messenger

To integrate the chatbot with Facebook Messenger, follow these steps:

  1. Create a Facebook Page: Create a new Facebook Page for your chatbot.
  2. Set Up Facebook Developer Account: Register as a developer on the Facebook Developer portal.
  3. Create a Facebook App: Create a new app and add the Messenger product.
  4. Generate Access Token: Generate a Page Access Token for your app.
  5. Set Up Webhook: Set up a webhook to receive messages from users.

Webhook Setup Example:

from flask import Flask, request
import requests

app = Flask(__name__)

PAGE_ACCESS_TOKEN = 'your_facebook_page_access_token'

def send_message(recipient_id, message_text):
    url = f"<https://graph.facebook.com/v11.0/me/messages?access_token={PAGE_ACCESS_TOKEN}>"
    headers = {"Content-Type": "application/json"}
    payload = {
        "recipient": {"id": recipient_id},
        "message": {"text": message_text}
    }
    requests.post(url, headers=headers, json=payload)

@app.route('/webhook', methods=['GET'])
def webhook_verification():
    mode = request.args.get('hub.mode')
    token = request.args.get('hub.verify_token')
    challenge = request.args.get('hub.challenge')
    if mode == 'subscribe' and token == 'your_verify_token':
        return challenge
    return 'Verification failed', 403

@app.route('/webhook', methods=['POST'])
def webhook():
    data = request.json
    if data['object'] == 'page':
        for entry in data['entry']:
            for messaging_event in entry['messaging']:
                sender_id = messaging_event['sender']['id']
                if 'message' in messaging_event:
                    message_text = messaging_event['message']['text']
                    response_text = summarize_text(message_text)  # Modify to call appropriate summarization function
                    send_message(sender_id, response_text)
    return 'OK', 200

if __name__ == '__main__':
    app.run(debug=True)

In this script, we define a webhook endpoint to receive messages from Facebook Messenger and respond using the send_message function.

In this section, we covered the evaluation and deployment of our news aggregator chatbot. We discussed various metrics and methods to evaluate the chatbot's performance, including accuracy metrics, user feedback, and response time. We also provided examples of deploying the chatbot as a web application using Heroku and integrating it with Facebook Messenger.

By following these steps, you can ensure that your chatbot performs well in real-world scenarios and is accessible to users on different platforms. The deployment process makes the chatbot available to users, allowing them to interact with it and benefit from its functionalities.

12.5 Evaluating and Deploying the Aggregator

In this section, we will focus on evaluating the performance of the news aggregator chatbot and deploying it to a suitable platform. Evaluation helps ensure that the chatbot meets user expectations and performs well under various conditions. Deployment makes the chatbot accessible to users, allowing them to benefit from its functionalities.

12.5.1 Evaluating the Aggregator

Evaluation involves measuring the chatbot's performance in terms of accuracy, responsiveness, and user satisfaction. We will use different metrics and methods to evaluate these aspects.

1. Accuracy Metrics

Accuracy metrics assess how well the chatbot categorizes and summarizes news articles. Common metrics include precision, recall, and F1-score for categorization accuracy and user feedback for summarization quality.

Example: Evaluating Categorization Accuracy

We can use the sklearn library to calculate precision, recall, and F1-score for the categorization model.

evaluate.py:

from sklearn.metrics import classification_report
from nlp_engine import categorize_article

# Load test articles and their true categories
with open('data/test_articles.json', 'r') as file:
    test_articles = json.load(file)

# Prepare data for evaluation
true_categories = [article['category'] for article in test_articles]
predicted_categories = [categorize_article(article['content']) for article in test_articles]

# Flatten lists for multi-label classification
true_categories_flat = [item for sublist in true_categories for item in sublist]
predicted_categories_flat = [item for sublist in predicted_categories for item in sublist]

# Print classification report
print(classification_report(true_categories_flat, predicted_categories_flat))

In this script, we load test articles and their true categories, predict the categories using the trained model, and print a classification report that includes precision, recall, and F1-score.

2. User Feedback for Summarization

User feedback is essential for assessing the quality of the summaries generated by the chatbot. We can collect feedback from users through surveys or direct ratings.

Example: Collecting User Feedback

We can modify our Flask application to include a feedback form where users can rate the quality of the summaries.

app.py (continued):

feedback_data = []

@app.route('/feedback', methods=['POST'])
def feedback():
    user_feedback = request.json
    feedback_data.append(user_feedback)
    return jsonify({'message': 'Thank you for your feedback!'})

# HTML template for feedback form
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>News Aggregator Chatbot</title>
    <link rel="stylesheet" href="<https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css>">
</head>
<body>
    <div class="container">
        <h1 class="mt-5">News Aggregator Chatbot</h1>
        <form id="news-form" class="mt-4">
            <div class="form-group">
                <label for="category">Select News Category</label>
                <select class="form-control" id="category" name="category">
                    <option value="general">General</option>
                    <option value="business">Business</option>
                    <option value="technology">Technology</option>
                    <option value="sports">Sports</option>
                    <option value="entertainment">Entertainment</option>
                </select>
            </div>
            <button type="submit" class="btn btn-primary">Get News</button>
        </form>
        <div id="news-articles" class="mt-4"></div>
    </div>

    <script src="<https://code.jquery.com/jquery-3.5.1.min.js>"></script>
    <script>
        $(document).ready(function() {
            $('#news-form').on('submit', function(event) {
                event.preventDefault();
                const category = $('#category').val();
                $.post('/get_news', { category: category }, function(data) {
                    let articlesHtml = '<div class="list-group">';
                    data.forEach(article => {
                        articlesHtml += `
                            <div class="list-group-item">
                                <h5 class="mb-1">${article.title}</h5>
                                <p class="mb-1">${article.description}</p>
                                <small>${article.source} - ${article.publishedAt}</small>
                                <button class="btn btn-secondary btn-sm mt-2" onclick="categorizeArticle('${article.content}')">Categorize</button>
                                <div class="category mt-2" id="category-${article.title}"></div>
                                <button class="btn btn-secondary btn-sm mt-2" onclick="summarizeArticle('${article.content}')">Summarize</button>
                                <div class="summary mt-2" id="summary-${article.title}"></div>
                                <form id="feedback-form-${article.title}" class="mt-2">
                                    <label for="rating">Rate this summary:</label>
                                    <select class="form-control" id="rating-${article.title}">
                                        <option value="1">1 - Poor</option>
                                        <option value="2">2 - Fair</option>
                                        <option value="3">3 - Good</option>
                                        <option value="4">4 - Very Good</option>
                                        <option value="5">5 - Excellent</option>
                                    </select>
                                    <button type="submit" class="btn btn-primary btn-sm mt-2">Submit Feedback</button>
                                </form>
                            </div>
                        `;
                    });
                    articlesHtml += '</div>';
                    $('#news-articles').html(articlesHtml);
                });
            });

            function summarizeArticle(article) {
                const summaryType = 'extractive';  // or 'abstractive'
                $.post('/summarize_article', { article: article, summary_type: summaryType }, function(data) {
                    $('#summary-' + article.title).html('<p>' + data.summary + '</p>');
                });
            }

            function categorizeArticle(article) {
                $.post('/categorize_article', { article: article }, function(data) {
                    $('#category-' + article.title).html('<p>' + data.category.join(', ') + '</p>');
                });
            }

            $('body').on('submit', 'form[id^="feedback-form-"]', function(event) {
                event.preventDefault();
                const articleTitle = $(this).attr('id').replace('feedback-form-', '');
                const rating = $(`#rating-${articleTitle}`).val();
                const feedback = {
                    article: articleTitle,
                    rating: rating
                };
                $.post('/feedback', feedback, function(response) {
                    alert(response.message);
                });
            });
        });
    </script>
</body>
</html>

In this updated template, we add a feedback form for each article, allowing users to rate the quality of the summaries. The feedback is sent to the /feedback endpoint and stored for further analysis.

3. Response Time

Response time is critical for user experience. We need to ensure that the chatbot responds promptly to user queries.

Example: Measuring Response Time

We can measure the response time for different queries using Python's time module.

evaluate.py (continued):

import time

# Function to measure response time
def measure_response_time(endpoint, data):
    start_time = time.time()
    response = requests.post(endpoint, json=data)
    end_time = time.time()
    response_time = end_time - start_time
    return response_time

# Measure response time for summarization
article = "Sample news article content..."
data = {"article": article, "summary_type": "extractive"}
response_time = measure_response_time("<http://localhost:5000/summarize_article>", data)
print(f"Summarization Response Time: {response_time} seconds")

By measuring response time, we can ensure that the chatbot meets the desired performance criteria.

12.5.2 Deploying the Aggregator

Once the chatbot is evaluated and performs satisfactorily, the next step is deployment. We will deploy the chatbot to a suitable platform where users can interact with it.

1. Web Application Deployment

We can deploy the chatbot as a web application using a cloud platform such as Heroku, AWS, or Google Cloud.

Example: Deploying on Heroku

To deploy the chatbot on Heroku, follow these steps:

  1. Install Heroku CLI: Download and install the Heroku CLI from Heroku.
  2. Log In to Heroku: Open a terminal and log in to your Heroku account.
    heroku login
  3. Create a Heroku App: Create a new Heroku app.
    heroku create your-app-name
  4. Prepare the Project for Deployment: Create a Procfile and requirements.txt in the project directory.
    Procfile:requirements.txt:
    web: python app.py
    Flask
    requests
    nltk
    gensim
    transformers
  5. Push the Project to Heroku: Initialize a Git repository, add the project files, and push to Heroku.
    git init
    git add .
    git commit -m "Initial commit"
    git push heroku master
  6. Open the Heroku App: Open the deployed app in your browser.
    heroku open

By following these steps, the chatbot will be deployed on Heroku and accessible via a public URL.

2. Integrating with Messaging Apps

We can also integrate the chatbot with messaging apps like Facebook Messenger, Slack, or WhatsApp. Each platform provides APIs for chatbot integration.

Example: Integrating with Facebook Messenger

To integrate the chatbot with Facebook Messenger, follow these steps:

  1. Create a Facebook Page: Create a new Facebook Page for your chatbot.
  2. Set Up Facebook Developer Account: Register as a developer on the Facebook Developer portal.
  3. Create a Facebook App: Create a new app and add the Messenger product.
  4. Generate Access Token: Generate a Page Access Token for your app.
  5. Set Up Webhook: Set up a webhook to receive messages from users.

Webhook Setup Example:

from flask import Flask, request
import requests

app = Flask(__name__)

PAGE_ACCESS_TOKEN = 'your_facebook_page_access_token'

def send_message(recipient_id, message_text):
    url = f"<https://graph.facebook.com/v11.0/me/messages?access_token={PAGE_ACCESS_TOKEN}>"
    headers = {"Content-Type": "application/json"}
    payload = {
        "recipient": {"id": recipient_id},
        "message": {"text": message_text}
    }
    requests.post(url, headers=headers, json=payload)

@app.route('/webhook', methods=['GET'])
def webhook_verification():
    mode = request.args.get('hub.mode')
    token = request.args.get('hub.verify_token')
    challenge = request.args.get('hub.challenge')
    if mode == 'subscribe' and token == 'your_verify_token':
        return challenge
    return 'Verification failed', 403

@app.route('/webhook', methods=['POST'])
def webhook():
    data = request.json
    if data['object'] == 'page':
        for entry in data['entry']:
            for messaging_event in entry['messaging']:
                sender_id = messaging_event['sender']['id']
                if 'message' in messaging_event:
                    message_text = messaging_event['message']['text']
                    response_text = summarize_text(message_text)  # Modify to call appropriate summarization function
                    send_message(sender_id, response_text)
    return 'OK', 200

if __name__ == '__main__':
    app.run(debug=True)

In this script, we define a webhook endpoint to receive messages from Facebook Messenger and respond using the send_message function.

In this section, we covered the evaluation and deployment of our news aggregator chatbot. We discussed various metrics and methods to evaluate the chatbot's performance, including accuracy metrics, user feedback, and response time. We also provided examples of deploying the chatbot as a web application using Heroku and integrating it with Facebook Messenger.

By following these steps, you can ensure that your chatbot performs well in real-world scenarios and is accessible to users on different platforms. The deployment process makes the chatbot available to users, allowing them to interact with it and benefit from its functionalities.