Skip to content

Instantly share code, notes, and snippets.

@osanseviero
Created August 24, 2021 12:39
Show Gist options
  • Save osanseviero/d112274a8f5e5568529befefa466e0cd to your computer and use it in GitHub Desktop.
Save osanseviero/d112274a8f5e5568529befefa466e0cd to your computer and use it in GitHub Desktop.
Question generation chrome extension
""" Chrome Manifest file: https://developer.chrome.com/docs/extensions/mv3/getstarted/
{
"manifest_version": 3,
"name": "Hugging Face Question Generator",
"version": "1.0",
"description": "Generate questions based on selected text",
"permissions": [
"contextMenus",
"storage",
"tabs",
"activeTab",
"scripting"
],
"background": {
"service_worker": "generate_question.js"
},
"icons":{
"128": "icon.png"
}
}
"""
generateQuestion = function(info, tab){
const path = "https://api-inference.huggingface.co/models/voidful/bart-eqg-question-generator";
const payload = JSON.stringify({
"inputs": info.selectionText
});
const options = {
"wait_for_model": true,
"use_gpu": false,
"method" : "POST",
"contentType" : "application/json",
"payload" : payload,
"headers" = {"Authorization": "Bearer YOUR_TOKEN"},
};
(async () => {
const rawResponse = await fetch(path, {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
},
options: {
use_gpu: false,
wait_for_model: true,
},
body: payload
});
const content = await rawResponse.json();
console.log(content[0]["generated_text"]);
})();
};
chrome.contextMenus.create({
title: "Generate Question with HF",
id: "Generate Question with HF 4",
contexts:["selection"], // ContextType
})
chrome.contextMenus.onClicked.addListener(generateQuestion);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment