first commit
Browse files- code.gs +75 -0
- demo.gif +0 -0
- index.html +26 -7
code.gs
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
const API_URL = "https://api-inference.huggingface.co/models/mistralai/Mistral-7B-Instruct-v0.3";
|
| 2 |
+
|
| 3 |
+
// Function to display a menu in Google Sheets
|
| 4 |
+
function onOpen() {
|
| 5 |
+
const ui = SpreadsheetApp.getUi();
|
| 6 |
+
ui.createMenu('Hugging Sheets')
|
| 7 |
+
.addItem('Enter your HF API Key', 'showApiKeyPrompt')
|
| 8 |
+
.addToUi();
|
| 9 |
+
}
|
| 10 |
+
|
| 11 |
+
// Function to prompt the user for their Hugging Face API key
|
| 12 |
+
function showApiKeyPrompt() {
|
| 13 |
+
const ui = SpreadsheetApp.getUi();
|
| 14 |
+
const response = ui.prompt('Enter your Hugging Face API Key:');
|
| 15 |
+
if (response.getSelectedButton() == ui.Button.OK) {
|
| 16 |
+
const apiKey = response.getResponseText();
|
| 17 |
+
PropertiesService.getScriptProperties().setProperty('HF_API_KEY', apiKey);
|
| 18 |
+
ui.alert('API Key saved successfully!');
|
| 19 |
+
}
|
| 20 |
+
}
|
| 21 |
+
|
| 22 |
+
// Function to call the Hugging Face API
|
| 23 |
+
function queryHuggingFace(prompt, model) {
|
| 24 |
+
const apiKey = PropertiesService.getScriptProperties().getProperty('HF_API_KEY');
|
| 25 |
+
if (!apiKey) {
|
| 26 |
+
throw new Error('Please enter your Hugging Face API key using the menu.');
|
| 27 |
+
}
|
| 28 |
+
|
| 29 |
+
const url = `https://api-inference.huggingface.co/models/${model}`;
|
| 30 |
+
const headers = {
|
| 31 |
+
"Authorization": `Bearer ${apiKey}`,
|
| 32 |
+
"Content-Type": "application/json"
|
| 33 |
+
};
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
const formattedPrompt = `<s> [INST] You are a helpful and honest assistant. Please, respond concisely and truthfully. [/INST] ${prompt} </s>`;
|
| 37 |
+
const payload = {
|
| 38 |
+
"inputs": formattedPrompt
|
| 39 |
+
};
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
const options = {
|
| 43 |
+
"method": "post",
|
| 44 |
+
"headers": headers,
|
| 45 |
+
"payload": JSON.stringify(payload)
|
| 46 |
+
};
|
| 47 |
+
|
| 48 |
+
const response = UrlFetchApp.fetch(url, options);
|
| 49 |
+
const json = JSON.parse(response.getContentText());
|
| 50 |
+
|
| 51 |
+
return json;
|
| 52 |
+
}
|
| 53 |
+
|
| 54 |
+
// Function to create the custom formula "=HF(prompt, model)"
|
| 55 |
+
function HF(prompt, model) {
|
| 56 |
+
try {
|
| 57 |
+
const response = queryHuggingFace(prompt, model);
|
| 58 |
+
const fullResponse = response[0].generated_text; // Adjust based on the actual response structure
|
| 59 |
+
// Extract the part of the response after the prompt
|
| 60 |
+
const generatedOutput = fullResponse.split(`</s>`).pop().trim();
|
| 61 |
+
return generatedOutput;
|
| 62 |
+
} catch (error) {
|
| 63 |
+
return `Error: ${error.message}`;
|
| 64 |
+
}
|
| 65 |
+
}
|
| 66 |
+
|
| 67 |
+
// Add the formula to Google Sheets
|
| 68 |
+
function onInstall(e) {
|
| 69 |
+
onOpen(e);
|
| 70 |
+
const formula = SpreadsheetApp.newUserDefinedFunctionBuilder()
|
| 71 |
+
.setName('HF')
|
| 72 |
+
.setFunction('HF')
|
| 73 |
+
.build();
|
| 74 |
+
SpreadsheetApp.installUserDefinedFunction(formula);
|
| 75 |
+
}
|
demo.gif
ADDED
|
index.html
CHANGED
|
@@ -3,17 +3,36 @@
|
|
| 3 |
<head>
|
| 4 |
<meta charset="utf-8" />
|
| 5 |
<meta name="viewport" content="width=device-width" />
|
| 6 |
-
<title>
|
| 7 |
<link rel="stylesheet" href="style.css" />
|
| 8 |
</head>
|
| 9 |
<body>
|
| 10 |
<div class="card">
|
| 11 |
-
<h1>
|
| 12 |
-
<p>
|
| 13 |
-
<p>
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
</
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
</div>
|
| 18 |
</body>
|
| 19 |
</html>
|
|
|
|
|
|
| 3 |
<head>
|
| 4 |
<meta charset="utf-8" />
|
| 5 |
<meta name="viewport" content="width=device-width" />
|
| 6 |
+
<title>Hugging Face Sheets add-on</title>
|
| 7 |
<link rel="stylesheet" href="style.css" />
|
| 8 |
</head>
|
| 9 |
<body>
|
| 10 |
<div class="card">
|
| 11 |
+
<h1>Hugging Face Sheets add-on</h1>
|
| 12 |
+
<p>Hugging Face in your spreadsheet?</p>
|
| 13 |
+
<p>Because spreadsheets can be incredibly useful for journalists, this project connects Hugging Face Inference API with Google Sheets. Handy for prompting, extraction, classification, translation, etc.</p>
|
| 14 |
+
<img url="demo.gif" width="100%">
|
| 15 |
+
|
| 16 |
+
<h2>Steps</h2>
|
| 17 |
+
<ol>
|
| 18 |
+
<li>You will need to create a script in Google Sheets (Extensions > Apps Script).</li>
|
| 19 |
+
<li>Replace the existing code with <a href="code.gs" target="_blank">this script</a> and save it.</li>
|
| 20 |
+
<li>Back in the Sheet, you should see a new tab called "Hugging Sheets".</li>
|
| 21 |
+
<li>Click on it and add your API key.</li>
|
| 22 |
+
<li>Voilà!</li>
|
| 23 |
+
</ol>
|
| 24 |
+
<p><strong>Note:</strong> To generate your API key, click on the <a href="https://huggingface.co/settings/tokens">Access Token tab in your Hugging Face settings</a>.</p>
|
| 25 |
+
<p>Tested with llama 8b and 70b, and mixtral, e.g.:</p>
|
| 26 |
+
<ul>
|
| 27 |
+
<li><code>=HF(cell or prompt, "meta-llama/Meta-Llama-3-70B-Instruct")</code></li>
|
| 28 |
+
<li><code>=HF(cell or prompt, "meta-llama/Meta-Llama-3-8B-Instruct")</code></li>
|
| 29 |
+
<li><code>=HF(cell or prompt, "mistralai/Mixtral-8x7B-Instruct-v0.3")</code></li>
|
| 30 |
+
</ul>
|
| 31 |
+
|
| 32 |
+
<p>This <a href="https://huggingface.co/blog/inference-pro#supported-models">blog post</a> gives a good overview of the supported models.
|
| 33 |
+
|
| 34 |
+
|
| 35 |
</div>
|
| 36 |
</body>
|
| 37 |
</html>
|
| 38 |
+
|