How to AI-Powered Firebase Extension for Text Summarization

Firebase

Sep 10, 2023

Integrating text summarization into mobile or web applications has never been easier, thanks to powerful solutions like PaLM API. With PaLM API, developers can harness advanced natural language processing capabilities to incorporate text summarization into their applications seamlessly.

Firebase Extensions Summarize Text with PaLM API further simplifies the integration process, offering an easy approach to enhance your applications with concise and practical text summaries.

Prerequisites

PaLM API

The PaLM interface and MakerSuite streamline the process of leveraging Google’s advanced language models to develop cutting-edge AI solutions. PaLM API enables users to perform various text-understanding tasks, including sentiment analysis, entity recognition, and document understanding. Leveraging the power of Firebase Extensions, the integration of text summarization into your applications becomes seamless with the PaLM API.

Summarize Text with PaLM API Firebase Extension

The Summarize Text with PaLM API Firebase extension utilizes the robust capabilities of Google Cloud’s PaLM API for text understanding and summarization. This extension is designed to process and distill large bodies of text, providing concise and meaningful summaries.

When developers enable this extension, it triggers a Cloud Function whenever text data is submitted. The function, powered by PaLM API, performs advanced natural language processing to generate a summary of the input text. The summary includes key insights, ensuring that the most relevant information is retained.

For each generated summary, the extension captures:

  • The condensed summary itself, which is crafted for easy comprehension.
  • Additional metadata that may include details on the source text and the summarization process.

During setup, developers configure the extension to work with Firestore to store and manage the generated summaries.

Extension Installation

You can install the extension either through the Firebase web console or through its command line interface.

Option 1: Using the Firebase CLI

To install the extension through the Firebase CLI:

firebase ext:install googlecloud/firestore-palm-summarize-text --project=projectId_or_alias

Option 2: Using the Firebase Console

Visit the extension homepage and click “Install in Firebase Console”.

Install in Firebase Console

Configure the Extension

While configuring the extension, you can specify

Configuration Parameters

  • Cloud Function Location: This is where you want to deploy the functions created for this extension. This parameter cannot be reconfigured after installation. For help selecting a location, refer to the location selection guide.

  • Palm API Provider: You have two options for accessing the PaLM API. If you choose Vertex AI, the service will be enabled automatically without additional steps. However, if you opt for Generative Language, you can either provide an API key obtained through MakerSuite or your GCP console or use Application Default Credentials if Generative Language AI is enabled in your Google Cloud project.

  • Collection Name: Configuring the collection name involves specifying the designated storage location for recorded data in Firestore.

  • Text field: This refers to the specific area or section within a document that contains the text to be summarized. In practical terms, this could be a chapter, a paragraph, or any defined segment within a document.

  • Response Field: This refers to the specific location within the message document where the generated response should be placed.

  • Target Summary Length (Optional): Specify the desired length of the summary by indicating the number of sentences you would like it to comprise. It’s the minimum length.

  • Content Filter Threshold (Optional): This is an optional parameter. Define the threshold for identifying harmful content when using the Generative Language PaLM API. This specification allows you to set the level of sensitivity in detecting and blocking harmful content generated by the PaLM provider. It’s important to note that this feature is exclusive to the Generative Language PaLM API and allows you to tailor content moderation according to your specific standards and requirements.

How does it work?

Behind the scenes, the “Summarize Text” extension utilizes Cloud Functions that listen to the specified Cloud Firestore collection. When you enter a long text that you want to summarize in a designated field within any document in that collection, the extension performs the following steps:

  1. Listens for changes: The Cloud Function deployed from the Firebase Extension monitors the specified collection for any changes or new documents.
  2. Retrieves the text: When a new document is added, or an existing document is modified, the extension retrieves the text from the specified field within that document.
  3. Generates a summary: Using the Generative Language API, the extension processes the retrieved text and generates a summary based on the specified target length.
  4. Saves the summary: The generated summary is then saved to the Firestore document in the designated response field.

How does Summarizing Text Extensions work

Summarizing Text

You can use any frontend technology to build an example to see how this extension works. What you need to do is:

  • Create a page that enables the user to enter text
  • Store the entered text in Cloud Firestore at the specified collection with the specified Text field from above
  • Display the summarized text from the response field

Here are the steps you should use in your frontend application using the Firebase SDK.

  1. Initialize Firebase: In Javascript, Firebase must be initialized for the project. Read Firebase docs to learn how you can add Firebase to your app.

    const firebaseConfig = {
      // Your Firebase Config Here
    };
    
    firebase.initializeApp(firebaseConfig);
    
  2. Enter the long text to summarize it: Once you’ve entered the long text, the extension’s Cloud Function and PaLM API work collaboratively to process, analyze, and generate a summary that encapsulates the key insights within the specified target length. Here is an example of an Input field in HTML and Javascript.

    <textarea
      id="summary-input"
      rows="5"
      placeholder="Type your summary text here"
    ></textarea>
    
    <script>
      // If you want to capture and handle the input, you can use JavaScript.
      const inputElement = document.getElementById('summary-input');
      inputElement.addEventListener('input', (event) => {
        const inputValue = event.target.value;
      });
    </script>
    
  3. Store the data in the Cloud Firestore: Once the user presses the save button, you can store the data by adding the text to the field that you have selected in the extension. Here is an example in Javascript.

    // Assuming you have Firebase initialized
    const db = firebase.firestore();
    const textDocuments = db.collection('text_documents'); // Corresponds to your collection in the parameters
    
    function addTextToFirebase(inputText) {
      if (inputText && inputText.trim() !== '') {
        return textDocuments.add({
          text: inputText, // 'text' is your field in the parameters
        });
      } else {
        console.log('Error: Text is empty');
      }
    }
    
  4. Listen to Firestore & Display Summarize Text: By listening to a document using `onSnapshot``, you can detect real-time data. Furthermore, Upon entering a long text, the Firebase and PaLM API engage in a seamless integration process. The app efficiently retrieves the processed data and seamlessly showcases the generated summary. This streamlined workflow ensures a user-friendly experience, allowing users to input extensive text and receive a concise and meaningful summary effortlessly.

<div id="summaryDisplay">
  <!-- Summary will be displayed here -->
</div>
const db = firebase.firestore();
const textDocuments = db.collection('text_documents');

function listenToDocumentUpdates(docID) {
  return textDocuments.doc(docID).onSnapshot(
    (snapshot) => {
      displaySummary(snapshot);
    },
    (error) => {
      console.error('Error fetching document:', error);
    }
  );
}

// Function to display the summary
function displaySummary(snapshot) {
  if (snapshot.exists) {
    const summaryText = snapshot.data().summary || '';
    document.getElementById('summaryDisplay').innerText = summaryText;
  } else {
    console.log('No such document!');
  }
}

Overview of Status object in Firestore document

Status Object for Document

The status object details the processing state of a text summarization request. It includes timestamps for when the processing started when it was last updated, and when it was completed. Additionally, it holds information about the current state of the request and any error messages if the process encounters issues.

  • state: Represents the current state of the process. Possible values are ‘PROCESSING‘, ‘COMPLETED‘, and ‘ERRORED‘.
  • updateTime: Timestamp indicating when the status was last updated.
  • startTime: Timestamp marking the start of the processing.
  • completeTime: Timestamp marking the completion of the process.
  • error: Holds an error message, if any.

Result

Once the process is completed, the app displays a summary of that text for the inserted long text.

Upon completing the process, the application presents a summarized version of the initially entered long text. This summarization output is seamlessly displayed in the app, giving users a succinct and easily digestible representation of the original content.

Final app result using Firebase Summarize Text Extension

Conclusion

Incorporating the “Summarize Text” Firebase Extension into your web or mobile applications offers several advantages, including improved user experience, enhanced content discoverability, and streamlined feedback analysis. By leveraging AI-powered text summarization capabilities, you can give users concise summaries of lengthy content, allowing them to grasp key points and make informed decisions quickly.

The implementation process involves setting up Firebase, installing the extension, configuring parameters, and designing the app’s UI to accommodate the summarization feature. With the “Summarize Text” Firebase Extension in web or mobile, you can optimize content presentation and create a more user-friendly experience within your applications.