sodcalculator

10 Tech How-To

How to Run GPT-2 in a Spreadsheet

Guide 10-41653 · Updated · Step by step

Running GPT-2 from a spreadsheet lets you send a prompt from a cell and place the generated text in another cell. A practical way to do it is to connect Google Sheets to a small local Python service that loads the model; you will need a computer capable of running Python and an internet connection for the initial setup.

What You’ll Need

  • A Google account and a Google Sheet you can edit.
  • A computer with Python 3 installed, plus a terminal or command prompt. Keep the computer running while you use the sheet.
  • Internet access to install the Python packages and download GPT-2 the first time.
  • Basic familiarity with copying code and running commands. The method below is for personal or local use; it does not publish your service to the internet.

Step-by-Step

  1. Create a working folder. Make a folder named gpt2-sheet somewhere easy to find. Open a terminal in that folder. On Windows, you can open PowerShell and use cd followed by the folder path; on macOS or Linux, use Terminal.
  2. Set up a Python environment. Run python -m venv .venv. Activate it with .venv\Scripts\Activate.ps1 in PowerShell, or source .venv/bin/activate on macOS or Linux. If the python command is not found, install Python from its official distribution and reopen the terminal. Some systems use python3 in place of python.
  3. Install the libraries. With the environment active, run python -m pip install flask transformers torch. PyTorch installation can vary by operating system and hardware; if this command fails, use the installation instructions on the PyTorch website for your setup, then install Flask and Transformers separately. A CPU installation is sufficient for a basic test, though generation may be slow.
  4. Save a local text-generation service. Create a file named app.py in the folder and paste this code:
    from flask import Flask, request, jsonify
    from transformers import pipeline

    app = Flask(__name__)
    generator = pipeline("text-generation", model="gpt2", device=-1)

    @app.post("/generate")
    def generate():
    prompt = request.json.get("prompt", "")
    if not prompt.strip():
    return jsonify({"error": "Enter a prompt."}), 400
    result = generator(prompt, max_new_tokens=60, do_sample=True,
    temperature=0.8, pad_token_id=50256)
    return jsonify({"text": result[0]["generated_text"]})

    if __name__ == "__main__":
    app.run(host="127.0.0.1", port=5000)

    The first run downloads the GPT-2 model files and may take time. The service listens only on your own computer. Do not change the host to a public address or expose it through router settings.

  5. Start the service and test it. Run python app.py in the active environment and leave the terminal open. In a second terminal, test the endpoint with a tool such as curl:
    curl -X POST http://127.0.0.1:5000/generate -H "Content-Type: application/json" -d '{"prompt":"A small garden needs"}'
    Command quoting differs between shells. If curl syntax is troublesome, proceed to the Apps Script step and test from the sheet.
  6. Add a custom function in Google Sheets. In the sheet, select Extensions > Apps Script. Replace the editor contents with:
    function GPT2(prompt) {
    const response = UrlFetchApp.fetch("http://127.0.0.1:5000/generate", {
    method: "post",
    contentType: "application/json",
    payload: JSON.stringify({prompt: String(prompt)})
    });
    const data = JSON.parse(response.getContentText());
    return data.text || data.error || "No text returned";
    }

    Save the project. Apps Script runs on Google’s servers, not on your computer, so it cannot normally reach the private address 127.0.0.1. This connection works only when the request originates on the same machine or through a supported local bridge; a normal Google Sheets custom function cannot call your laptop’s localhost directly.
  7. Choose a connection method before calling the function. For a true Sheets-to-local setup, use a local spreadsheet application with a script or add-in that sends requests to localhost, or run a small authenticated relay on a server you control and configure Apps Script to call that relay. Do not make an unauthenticated public endpoint: anyone who finds it could consume your computer’s resources. For a hosted relay, use HTTPS, add authentication, and never put secret credentials in sheet cells.
  8. Call the function from a cell. Once your chosen bridge can reach the service, enter a prompt in A2 and put =GPT2(A2) in B2. Allow the script’s requested permissions if prompted, then check the result. Each recalculation can generate new text and use computing resources; avoid filling thousands of rows at once.

Common Mistakes

The biggest issue is assuming Google Apps Script can access a program running on your own computer. Its code executes remotely, so a local-only address is not reachable from Google’s servers. Use a suitable local spreadsheet integration or a secured relay instead. Also keep the Python terminal open: closing it stops the service.

Other frequent errors include installing packages into a different Python environment, mistyping the port or route, and treating GPT-2 output as verified information. GPT-2 is an older text-generation model; it can produce irrelevant, repetitive, or inaccurate continuations. Review generated text before relying on it.

Tips

  • Start with one short prompt and a small token limit. Increase max_new_tokens gradually if you need longer responses.
  • Keep prompts in one column and results in the next. Test a few rows manually before copying a formula down.
  • Use a separate spreadsheet for experiments and do not include private or sensitive information in prompts.
  • For repeatable results, set do_sample=False in the Python code. Sampling is useful for varied continuations but means the same prompt may produce different text.
  • Stop the service with Ctrl+C in its terminal when you are finished. Keep the Python environment and model files in the working folder so future runs are easier.

Troubleshooting

“Connection refused” or a timeout: Confirm python app.py is still running and that the request uses port 5000 and the /generate path. If calling from Apps Script, remember that localhost refers to Google’s server, not your computer; use a reachable, secured relay or a local integration.

Module not found: Activate the virtual environment and install the missing package there with python -m pip install package-name. If PyTorch installation fails, follow its platform-specific instructions rather than repeatedly trying a mismatched command.

The first request is slow or fails while downloading: Check internet access and available disk space, then retry. The model is downloaded on first use. A CPU-only computer may take noticeably longer to generate text.

Sheets shows an authorization or script error: Review the Apps Script execution details and grant the requested permissions. A custom function may also be restricted from certain services or from requesting authorization interactively; in that case, use a menu-triggered Apps Script function or a local spreadsheet integration rather than relying on a cell formula.

Frequently Asked Questions

Can GPT-2 run entirely inside a spreadsheet?
No. The spreadsheet provides the interface, while Python or another service runs the model and returns text.

Can Google Sheets call GPT-2 on my laptop directly?
Not with a standard Apps Script custom function. Apps Script runs remotely and cannot normally reach your laptop’s private localhost address.

Do I need a graphics card?
No. The example explicitly uses the CPU. A compatible GPU can improve speed, but setup depends on the computer and PyTorch installation.

Is this suitable for a shared spreadsheet?
Only with careful access controls. Anyone allowed to trigger a connected service may consume its resources, and a public relay needs authentication and HTTPS.

Why does the same prompt return different text?
The example enables sampling for varied output. Set do_sample=False for more repeatable generation, though results are not guaranteed to be useful or factually correct.

Related

General steps for current mainstream versions; menus can differ by device and version. See the disclaimer or report an error.

More in this bed

The whole Tech How-To bed →