Building a Chat Summarizer with Cohere and Gradio

Modern development workflows frequently involve parsing large volumes of text. Whether it's a lengthy email thread, a dense article, or a long chat log, the ability to quickly extract the core message is valuable. Text summarization, a key task in natural language processing (NLP), addresses this need directly. This guide demonstrates how to construct a practical chat summarizer using the Cohere API for the underlying language model and Gradio for a simple web interface.

A screenshot of the Notion interface showing the option of using AI intelligence to write text, pages, and so on.
Notion offers a feature that uses AI technology to convert a longer block of text into a shorter version that summarizes the main points.

The barrier to entry for such NLP tasks is now remarkably low. Instead of building and training complex models from scratch, developers can leverage readily available APIs. The process described here requires only a handful of code steps, making it an accessible project for both seasoned developers and those new to the field.

Core Concepts and API Setup

Cohere is a cloud-based NLP platform that provides pre-trained models for a variety of tasks, including text classification, entity extraction, and, notably, conversation summarization. Its API analyzes the text of a conversation to identify key sentences and contextual information, such as speaker and sentiment, producing a concise summary of the main discussion points.

To begin, you must sign up for an API key on the Cohere website. With the key, install the official Python package using pip:


pip install cohere

Next, initialize the Cohere client, passing your unique API key for authentication:

import cohere

# initialize Cohere client
co = cohere.Client("YOUR_API_KEY")

The conversation you want to summarize is then provided as the input text for the model:

conversation = """
Senior Dev: Hey, have you seen the latest pull request for the authentication module?
Junior Dev: No, not yet. What’s in it?
Senior Dev: They’ve added support for JWT tokens, so we can use that instead of session cookies for authentication.
Junior Dev: Oh, that’s great. I’ve been wanting to switch to JWT for a while now.
Senior Dev: Yeah, it’s definitely more secure and scalable. I’ve reviewed the code and it looks good, so go ahead and merge it if you’re comfortable with it.
Junior Dev: Will do, thanks for the heads-up!
"""

With the input ready, generating a summary is a single API call. The co.summarize() method accepts parameters for the model, as well as the desired summary length and extractiveness:

response = co.summarize(conversation, model = 'summarize-xlarge', length = 'short', extractiveness = 'high', temperature = 0.5,)summary = response.summary

The resulting summary can be displayed or processed further using standard methods like print():

print(summary)

Creating a Web Interface with Gradio

While the API calls work perfectly in a script, a graphical interface makes the tool usable for a wider audience. Gradio is a Python library designed for prototyping machine learning models with minimal UI code. After installation, it allows you to wrap the summarization logic in an interactive web app.

First, import the necessary libraries, including both cohere and gradio:

import gradio as gr
import cohere

If Gradio isn't installed, you can add it using pip:

!pip install gradio

Re-initialize the Cohere client within your application script:

co = cohere.Client("YOUR API KEY")

The core of the application is a function that takes the raw conversation text and calls the API to produce the summary:

def chat_summarizer(conversation):
    # generate summary using Cohere API
response = co.summarize(conversation, model = 'summarize-xlarge', length = 'short', extractiveness = 'high', temperature = 0.5)
summary = response.summary

return summary

This function, chat_summarizer, encapsulates the API call with its chosen parameters. Finally, you define the interface components for input and output, and tie it to this function using gr.Interface:

chat_input = gr.inputs.Textbox(lines = 10, label = "Conversation")
chat_output = gr.outputs.Textbox(label = "Summary")

chat_interface = gr.Interface(
  fn = chat_summarizer,
  inputs = chat_input,
  outputs = chat_output,
  title = "Chat Summarizer",
  description = "This app generates a summary of a chat conversation using Cohere API."
)

The gr.inputs.textbox and gr.outputs.textbox define the user-facing input field and the area for the generated summary. Launching the app is done with a single method call:

chat_interface.launch()

This command opens a local webpage where a user can paste a conversation and generate its summary with the click of a button. A working demo of this implementation is available to explore.

The Value of Automated Summaries

In a world where communication is predominantly digital, automated chat summarization is a powerful tool for efficiency. It saves time by removing the need to manually comb through long conversations to find the essential points, which can be crucial for decision-making and preventing miscommunication. The applications extend beyond chat, proving useful for condensing email chains or distilling key points from a long document.

Advanced AI and NLP have made text summarization both accurate and efficient, turning a previously complex engineering task into a simple API integration. Given its low overhead and high potential for productivity gains, incorporating a summarization feature into your projects is a worthwhile consideration.