Coder Social home page Coder Social logo

dzynin / writing-with-gpt2 Goto Github PK

View Code? Open in Web Editor NEW

This project forked from andfanilo/writing-with-gpt2

0.0 0.0 0.0 131 KB

Reproducing "Writing with Transformer" demo, using aitextgen/FastAPI in backend, Quill/React in frontend

License: MIT License

HTML 4.35% CSS 7.57% JavaScript 45.41% Python 42.54% Shell 0.13%

writing-with-gpt2's Introduction

Writing with GPT-2

Development

Python Backend

Make sure you are in the backend folder:

cd backend/

Install a virtual environment:

# If using venv
python3 -m venv venv
. venv/bin/activate

# If using conda
conda create -n write-with-gpt2 python=3.7
conda activate write-with-gpt2

# On Windows I use Conda to install pytorch separately
conda install pytorch cpuonly -c pytorch

# When environment is activated
pip install -r requirements.txt
python aitextgen_app.py

To run in hot module reloading mode:

uvicorn aitextgen_app:app --host 0.0.0.0 --reload

To run with multiple workers:

uvicorn aitextgen_app:app --host 0.0.0.0 --workers 4

Runs on http://localhost:8000. You can consult interactive API on http://localhost:8000/docs.

Configuration is made via environment variable or .env file. Available are:

  • MODEL_NAME:
    • to use a custom model, point to the location of the pytorch_model.bin. You will also need to pass config.json through CONFIG_FILE.
    • otherwise model from Huggingface's repository of models, defaults to distilgpt2.
  • CONFIG_FILE: path to JSON file of model architecture.
  • USE_GPU: True to generate text from GPU.

From gpt-2-simple to Pytorch

To convert gpt-2-simple model to Pytorch, see Importing from gpt-2-simple:

transformers-cli convert --model_type gpt2 --tf_checkpoint checkpoint/run1 --pytorch_dump_output pytorch --config checkpoint/run1/hparams.json

This will put a pytorch_model.bin and config.json in the pytorch folder, which is what you'll need to pass to .env file to load the model.

Run gpt-2-simple version

Added back the older gpt-2-simple version we add in backend/gpt2_app.

To download a model:

import gpt_2_simple as gpt2
gpt2.download_gpt2(model_name='124M')

To run app:

set MODEL_NAME=124M
uvicorn aitextgen_app:app --host 0.0.0.0

Set MODEL_NAME to any model folder inside models, or edit .env.

Streamlit Debug

You can run the Streamlit app to debug the model.

streamlit run st_app.py

React Frontend

Make sure you are in the frontend folder, and ensure backend API is working.

cd frontend/
npm install # Install npm dependencies
npm run start # Start Webpack dev server

Web app now available on http://localhost:3000.

Building the frontend

To create a production build:

npm run build

Now your React built app will be statically served by FastAPI on http://localhost:8000/app along with the other APIs. You don't need to run the Webpack devserver anymore.

Using GPU

Miniconda/Anaconda recommended on Windows.

conda command : conda install pytorch cudatoolkit=10.2 -c pytorch.

If you install manually, you can check your currently installed CUDA toolkit version with nvcc --version. Once you have CUDA toolkit installed, you can verify it by running nvidia-smi.

Beware: after installing CUDA, it seems you shouldn't try to update GPU driver though GeForce or else you'll have to reinstall CUDA toolkit ?

References

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.