Chrome Extensions with Transformers
2025-03-174 turns8,160 charsgpt-4o
Summary
The user wants to create Chrome extensions using transformers.js, specifically an AI-powered image captioning tool for accessibility and metadata.
Messages
what are some chrome extensions I could make the the transformers.js library
Using the [`transformers.js`](https://huggingface.co/docs/transformers.js) library, you can build several powerful Chrome extensions that bring local AI capabilities directly to the browser. Here are some ideas:
### **1. AI-Powered Summarizer**
- **Functionality**: Summarizes articles, PDFs, or selected text using models like `facebook/bart-large-cnn`.
- **Use Case**: Quickly extract key points from long articles without leaving the page.
- **Extra Features**: Adjust summary length, use different models for abstractive vs. extractive summaries.
### **2. AI-Powered Chatbot for Any Website**
- **Functionality**: Uses a small conversational model like `mistral-7b-instruct` to generate responses based on selected text or page content.
- **Use Case**: Acts as an AI assistant for forums, customer support, or learning platforms.
### **3. Text-to-Speech with Transformers.js**
- **Functionality**: Uses Hugging Face TTS models to convert highlighted text into speech.
- **Use Case**: Helps with accessibility, language learning, or productivity (listen to articles while doing other tasks).
### **4. Sentiment & Tone Analyzer**
- **Functionality**: Uses `distilbert-base-uncased-finetuned-sst-2-english` to analyze the sentiment and tone of selected text.
- **Use Case**: Useful for social media monitoring, reviewing emails before sending, or filtering toxic content.
### **5. AI-Powered Code Assistant**
- **Functionality**: Uses `starcoder` or `codegen` to generate, complete, or refactor code in online coding platforms like GitHub or LeetCode.
- **Use Case**: Helps programmers generate code snippets directly in the browser.
### **6. Auto-Complete & Writing Assistant**
- **Functionality**: Uses `GPT-2` or `Mistral` to autocomplete sentences while writing in a text box (e.g., Gmail, Twitter, Medium).
- **Use Case**: Helps writers, bloggers, and social media users create high-quality content faster.
### **7. AI-Powered Translation & Language Learning Tool**
- **Functionality**: Uses `Helsinki-NLP/opus-mt` models for real-time translation.
- **Use Case**: Enhances language learning by providing quick translations and example usage in different languages.
### **8. AI-Powered Search Enhancer**
- **Functionality**: Enhances Google search results by summarizing multiple articles or answering queries directly on the search page.
- **Use Case**: Reduces research time by providing AI-generated insights.
### **9. AI-Powered Keyword Extractor for SEO**
- **Functionality**: Uses `bert-base-NER` to extract key phrases, entities, and important terms from a webpage.
- **Use Case**: Helps bloggers and marketers improve SEO by identifying crucial keywords.
### **10. AI-Powered Image Captioning**
- **Functionality**: Uses `vit-gpt2-image-captioning` to generate captions for images on a webpage.
- **Use Case**: Useful for accessibility, social media, or improving website metadata.
Would you like help setting up a boilerplate for one of these ideas? 🚀
gpt-4o
AI-Powered Image Captioning
• Functionality: Uses vit-gpt2-image-captioning to generate captions for images on a webpage.
• Use Case: Useful for accessibility, social media, or improving website metadata.
That’s a great idea! You can create a Chrome extension that automatically captions images on any webpage using `transformers.js` with the `vit-gpt2-image-captioning` model.
---
### **Plan for the Chrome Extension**
#### **1. Features**
- Automatically detects images on a webpage.
- Uses `vit-gpt2-image-captioning` to generate captions.
- Displays captions as overlays, tooltips, or replaces missing alt text.
- Option to copy captions to the clipboard.
#### **2. Tech Stack**
- **Manifest v3** for Chrome extension.
- **JavaScript (ES6)** for content scripts.
- **transformers.js** to run the model in the browser.
- **TensorFlow.js** or WebGPU for acceleration.
---
### **Implementation Breakdown**
#### **1. File Structure**
```
/ai-caption-extension
│── manifest.json
│── background.js
│── content.js
│── popup.html
│── popup.js
│── styles.css
│── inject.js
│── icon.png
```
#### **2. `manifest.json`** (Defines Extension Permissions)
```json
{
"manifest_version": 3,
"name": "AI Image Captioner",
"version": "1.0",
"description": "Automatically generates captions for images on web pages using AI.",
"permissions": ["activeTab", "scripting", "storage"],
"host_permissions": ["<all_urls>"],
"background": {
"service_worker": "background.js"
},
"content_scripts": [
{
"matches": ["<all_urls>"],
"js": ["content.js"]
}
],
"action": {
"default_popup": "popup.html",
"default_icon": {
"16": "icon.png",
"48": "icon.png",
"128": "icon.png"
}
}
}
```
#### **3. `content.js`** (Finds Images & Injects Captions)
```js
// Function to process images and generate captions
async function processImages() {
const images = document.querySelectorAll("img");
for (let img of images) {
if (!img.dataset.captioned) {
const caption = await generateCaption(img.src);
if (caption) {
let captionDiv = document.createElement("div");
captionDiv.textContent = caption;
captionDiv.style.position = "absolute";
captionDiv.style.background = "rgba(0,0,0,0.7)";
captionDiv.style.color = "white";
captionDiv.style.padding = "5px";
captionDiv.style.borderRadius = "5px";
captionDiv.style.fontSize = "12px";
captionDiv.style.maxWidth = "200px";
captionDiv.style.zIndex = "9999";
img.style.position = "relative";
img.parentElement.style.position = "relative";
img.parentElement.appendChild(captionDiv);
img.dataset.captioned = "true";
}
}
}
}
// Calls the AI model via transformers.js
async function generateCaption(imageUrl) {
const { pipeline } = await import("https://cdn.jsdelivr.net/npm/@xenova/transformers");
const captioner = await pipeline("image-to-text", "Xenova/vit-gpt2-image-captioning");
const caption = await captioner(imageUrl);
return caption[0]?.generated_text || "No caption available";
}
// Run when the page loads
window.addEventListener("load", processImages);
```
#### **4. `popup.html`** (User Interface for the Extension)
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>AI Captioner</title>
<script src="popup.js" defer></script>
<style>
body { font-family: Arial, sans-serif; padding: 10px; width: 200px; }
button { background: #007bff; color: white; padding: 10px; border: none; width: 100%; cursor: pointer; }
button:hover { background: #0056b3; }
</style>
</head>
<body>
<h3>AI Captioner</h3>
<button id="generateCaptions">Generate Captions</button>
</body>
</html>
```
#### **5. `popup.js`** (Triggers Image Captioning)
```js
document.getElementById("generateCaptions").addEventListener("click", async () => {
let [tab] = await chrome.tabs.query({ active: true, currentWindow: true });
chrome.scripting.executeScript({
target: { tabId: tab.id },
function: () => window.location.reload() // Reload to trigger captions
});
});
```
---
### **How It Works**
1. When the page loads, `content.js` runs and detects all images.
2. It fetches captions using `vit-gpt2-image-captioning` from `transformers.js`.
3. The generated captions are displayed as overlays on images.
4. Users can also click the popup button to trigger the script manually.
---
### **Next Steps**
✅ Try running the extension with a local test page.
✅ Optimize performance by caching captions in `chrome.storage`.
✅ Add settings to adjust caption styles and toggle AI processing.
Would you like me to generate a ZIP with the project files for you? 🚀
gpt-4o