Generating Video Summary

Welcome back! In the previous lessons, we explored downloading videos from both Google Drive and LinkedIn using Java. Building on those skills, we're now going to delve deeper into generating video summaries — an essential skill for transforming lengthy transcriptions into concise and insightful content. This lesson takes you a step further by utilizing the capabilities of OpenAI's API to create detailed yet succinct summaries on the fly.

What You'll Learn

Today, you will:

  • Understand how to generate a summary from a transcription.
  • Explore the use of the OpenAI API to craft well-structured summaries.
  • Gain insights into designing effective prompts for better results.
  • Learn about system and user roles in OpenAI API requests and their importance.
Understanding Generating Video Summary

Summarizing transcriptions involves distilling the core messages from extensive spoken content, ensuring key points are retained while unnecessary details are filtered out. When dealing with long videos or lectures, extracting the main themes allows you to quickly grasp the essentials without listening to every word. The OpenAI API facilitates this by leveraging advanced language models capable of understanding context and summarizing long texts.

In our context, the OpenAI API will help us convert raw transcriptions into coherent summaries. To interact with the API, we need to construct a JSON payload that includes a list of messages, each with a role and content. The API expects two types of messages:

  • System message: Sets the stage by providing essential guidelines to the model about its role and the expected output format. This enables it to focus and execute tasks effectively.
  • User message: Defines the specific requirements, such as the transcription text that needs summarization, ensuring the model understands and processes the content correctly.
Examples and Step-by-Step Explanations

Let's walk through summarizing a transcription using the OpenAI API in Java. We will use Java's HTTP libraries to send a request to the API, construct the necessary JSON payload, and handle the response.

Step 1: Setting Up Dependencies

To work with JSON and HTTP requests in Java, you can use libraries such as org.json for JSON handling and HttpURLConnection for HTTP requests. Make sure to add the JSON library to your project dependencies.

Step 2: Constructing the JSON Payload

We need to create a JSON object that matches the OpenAI API's expected format. Here's how you can do it in Java:

import org.json.JSONArray;
import org.json.JSONObject;

String transcriptionText = "Your transcription text goes here.";

JSONObject systemMessage = new JSONObject();
systemMessage.put("role", "system");
systemMessage.put("content",
    "You are an expert content analyst and summarizer with these capabilities:\n" +
    "- Extracting key points while maintaining context\n" +
    "- Identifying main themes and core messages\n" +
    "- Preserving critical details while reducing length\n" +
    "- Maintaining the original tone and intent\n" +
    "- Organizing information hierarchically\n\n" +
    "Format your summaries with:\n" +
    "1. A one-sentence overview\n" +
    "2. 2-3 key takeaways\n" +
    "3. Important details or quotes (if any)"
);

JSONObject userMessage = new JSONObject();
userMessage.put("role", "user");
userMessage.put("content",
    "Create a structured summary of this transcription. " +
    "Focus on the core message and key points while maintaining " +
    "context and critical details.\n\n" +
    "Transcription:\n" + transcriptionText
);

JSONArray messages = new JSONArray();
messages.put(systemMessage);
messages.put(userMessage);

JSONObject requestBody = new JSONObject();
requestBody.put("model", "gpt-4o");
requestBody.put("messages", messages);
Step 3: Setting Up the HTTP Connection

Now we need to establish a connection to the OpenAI API endpoint and configure the necessary headers:

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;

private static final String OPENAI_API_KEY = System.getenv("OPENAI_API_KEY");

// Set up the HTTP connection
URL url = new URL("https://api.openai.com/v1/chat/completions");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setRequestProperty("Authorization", "Bearer " + OPENAI_API_KEY);
conn.setRequestProperty("Content-Type", "application/json");
conn.setDoOutput(true);

In this step, we:

  • Create a URL object pointing to the OpenAI chat completions endpoint
  • Open an HTTP connection and set the request method to POST
  • Add the Authorization header with our API key (retrieved from environment variables)
  • Set the Content-Type header to indicate we're sending JSON data
  • Enable output on the connection so we can write our request body
Step 4: Sending the Request and Reading the Response

Next, we send our JSON payload and read the API's response:

// Write the JSON payload to the request body
try (OutputStream os = conn.getOutputStream()) {
    byte[] input = requestBody.toString().getBytes("utf-8");
    os.write(input, 0, input.length);
}

// Read the response
int status = conn.getResponseCode();
InputStream is = (status < HttpURLConnection.HTTP_BAD_REQUEST) ?
    conn.getInputStream() : conn.getErrorStream();

BufferedReader in = new BufferedReader(new InputStreamReader(is, "utf-8"));
StringBuilder response = new StringBuilder();
String line;
while ((line = in.readLine()) != null) {
    response.append(line.trim());
}
in.close();

This step involves:

  • Writing our JSON request body to the connection's output stream
  • Getting the HTTP status code to determine if the request was successful
  • Reading from either the input stream (success) or error stream (failure)
  • Building the complete response string by reading line by line
Step 5: Parsing the Response

Finally, we extract the summary from the API's JSON response:

// Parse the response JSON to extract the summary
JSONObject jsonResponse = new JSONObject(response.toString());
JSONArray choices = jsonResponse.getJSONArray("choices");
if (choices.length() > 0) {
    JSONObject message = choices.getJSONObject(0).getJSONObject("message");
    return message.getString("content");
} else {
    return null;
}

Here we:

  • Parse the response string into a JSON object
  • Access the "choices" array, which contains the model's responses
  • Extract the first choice and get the "message" object
  • Return the "content" field, which contains our generated summary
Step 6: Complete Implementation

Here's how all the steps come together in a complete method:

public class MediaProcessorService {

    private static final String OPENAI_API_KEY = System.getenv("OPENAI_API_KEY");

    public String summarizeTranscription(String transcriptionText) {
        try {
            // Step 2: Construct the JSON payload
            JSONObject systemMessage = new JSONObject();
            systemMessage.put("role", "system");
            systemMessage.put("content",
                "You are an expert content analyst and summarizer with these capabilities:\n" +
                "- Extracting key points while maintaining context\n" +
                "- Identifying main themes and core messages\n" +
                "- Preserving critical details while reducing length\n" +
                "- Maintaining the original tone and intent\n" +
                "- Organizing information hierarchically\n\n" +
                "Format your summaries with:\n" +
                "1. A one-sentence overview\n" +
                "2. 2-3 key takeaways\n" +
                "3. Important details or quotes (if any)"
            );

            JSONObject userMessage = new JSONObject();
            userMessage.put("role", "user");
            userMessage.put("content",
                "Create a structured summary of this transcription. " +
                "Focus on the core message and key points while maintaining " +
                "context and critical details.\n\n" +
                "Transcription:\n" + transcriptionText
            );

            JSONArray messages = new JSONArray();
            messages.put(systemMessage);
            messages.put(userMessage);

            JSONObject requestBody = new JSONObject();
            requestBody.put("model", "gpt-4o");
            requestBody.put("messages", messages);

            // Step 3: Set up the HTTP connection
            URL url = new URL("https://api.openai.com/v1/chat/completions");
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("POST");
            conn.setRequestProperty("Authorization", "Bearer " + OPENAI_API_KEY);
            conn.setRequestProperty("Content-Type", "application/json");
            conn.setDoOutput(true);

            // Step 4: Send request and read response
            try (OutputStream os = conn.getOutputStream()) {
                byte[] input = requestBody.toString().getBytes("utf-8");
                os.write(input, 0, input.length);
            }

            int status = conn.getResponseCode();
            InputStream is = (status < HttpURLConnection.HTTP_BAD_REQUEST) ?
                conn.getInputStream() : conn.getErrorStream();

            BufferedReader in = new BufferedReader(new InputStreamReader(is, "utf-8"));
            StringBuilder response = new StringBuilder();
            String line;
            while ((line = in.readLine()) != null) {
                response.append(line.trim());
            }
            in.close();

            // Step 5: Parse the response
            JSONObject jsonResponse = new JSONObject(response.toString());
            JSONArray choices = jsonResponse.getJSONArray("choices");
            if (choices.length() > 0) {
                JSONObject message = choices.getJSONObject(0).getJSONObject("message");
                return message.getString("content");
            } else {
                return null;
            }
        } catch (Exception e) {
            System.out.println("Error generating summary: " + e.getMessage());
            return null;
        }
    }
}
Step 7: Using the Summarization Method

You can now use the summarizeTranscription method in your application to generate a summary from any transcription text:

public class Main {
    public static void main(String[] args) {
        MediaProcessorService service = new MediaProcessorService();
        String transcription = "Your transcription text goes here.";
        String summary = service.summarizeTranscription(transcription);
        System.out.println("Summary:\n" + summary);
    }
}
Explanation of the Process
  • Initialization: The API key is retrieved from the environment variable OPENAI_API_KEY to authenticate requests.
  • Prompt Construction: The system and user prompts are created as JSON objects and combined into a JSON array.
  • API Request: The request is sent to the OpenAI API endpoint using HttpURLConnection, with the JSON payload in the request body.
  • Response Handling: The response is read and parsed to extract the summary content from the returned JSON.
  • Error Handling: Any exceptions or API errors are caught and reported.
Lesson Summary

This lesson highlighted the importance of efficiently distilling information from video transcriptions using the OpenAI API. By converting lengthy transcripts into concise summaries, the process facilitates quick decision-making and enhances comprehension across various fields, such as education and business analytics. Mastering video summarization enables you to create accessible, information-rich content while eliminating unnecessary details. By transforming raw data into actionable insights, you bridge a critical gap in information synthesis. You can now apply these concepts in practical exercises, reinforcing your skills through hands-on coding tasks.

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal