Skip to content

Commit

Permalink
Add on device summary sample (#1263)
Browse files Browse the repository at this point in the history
* Add on device summary sample

* small tweaks and clean up

* more clean up

* Add link to blog post

* show correct error when ai is not supported in browser

* addressed comments

* fix path
  • Loading branch information
sebastianbenz authored Sep 13, 2024
1 parent 0a92c15 commit 18a4801
Show file tree
Hide file tree
Showing 15 changed files with 1,749 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
dist
16 changes: 16 additions & 0 deletions functional-samples/ai.gemini-on-device-summarization/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# On-device Summarization with Gemini Nano

This sample demonstrates how to use the experimental Summarization API in Chrome. To learn more about the API and how to sign-up for the preview, head over to [Built-in AI on developer.chrome.com](https://developer.chrome.com/docs/ai/built-in).

## Overview

The extension summarizes the content of the currently open tab. It uses Mozilla's [readability](https://github.com/mozilla/readability) library to extract the content of the currently active tab and displays a summary of the page generated by [Chrome's experimental summarization API](https://developer.chrome.com/blog/august2024-summarization-ai) in a side panel.

## Running this extension

1. Clone this repository
2. Run `npm install` in this folder to install all dependencies.
3. Run `npm run build` to bundle the content script .
4. Load the 'dist' directory in Chrome as an [unpacked extension](https://developer.chrome.com/docs/extensions/mv3/getstarted/development-basics/#load-unpacked).
5. Click the extension icon to open the summary side panel.
6. Open any web page, the page's content summary will automatically be displayed in the side panel.
22 changes: 22 additions & 0 deletions functional-samples/ai.gemini-on-device-summarization/background.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
chrome.sidePanel
.setPanelBehavior({ openPanelOnActionClick: true })
.catch((error) => console.error(error));

chrome.tabs.onActivated.addListener((activeInfo) => {
showSummary(activeInfo.tabId);
});
chrome.tabs.onUpdated.addListener(async (tabId) => {
showSummary(tabId);
});

async function showSummary(tabId) {
const tab = await chrome.tabs.get(tabId);
if (!tab.url.startsWith('http')) {
return;
}
const injection = await chrome.scripting.executeScript({
target: { tabId },
files: ['scripts/extract-content.js']
});
chrome.storage.session.set({ pageContent: injection[0].result });
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
23 changes: 23 additions & 0 deletions functional-samples/ai.gemini-on-device-summarization/manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"name": "Summarizer",
"version": "0.1",
"manifest_version": 3,
"description": "Get a short summary of every webpage.",
"background": {
"service_worker": "background.js"
},
"permissions": ["tabs", "scripting", "sidePanel", "storage"],
"host_permissions": ["http://*/*", "https://*/*"],
"side_panel": {
"default_path": "sidepanel/index.html"
},
"action": {
"default_icon": {
"16": "images/icon16.png",
"32": "images/icon32.png",
"48": "images/icon48.png",
"128": "images/icon128.png"
},
"default_title": "Generate a summary"
}
}
Loading

0 comments on commit 18a4801

Please sign in to comment.