Skip to content

Commit 83bf56d

Browse files
committed
Initial (via gpt-2-cloud-run)
0 parents  commit 83bf56d

File tree

4 files changed

+97
-0
lines changed

4 files changed

+97
-0
lines changed

Dockerfile

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
FROM python:3.7.3-slim-stretch
2+
3+
RUN apt-get -y update && apt-get -y install gcc
4+
5+
WORKDIR /
6+
COPY checkpoint /checkpoint
7+
8+
# Make changes to the requirements/app here.
9+
# This Dockerfile order allows Docker to cache the checkpoint layer
10+
# and improve build times if making changes.
11+
RUN pip3 --no-cache-dir install tensorflow gpt-2-simple starlette uvicorn ujson
12+
COPY app.py /
13+
14+
# Clean up APT when done.
15+
RUN apt-get clean && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
16+
17+
ENTRYPOINT ["python3", "-X", "utf8", "app.py"]

LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2019 Max Woolf
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# reddit-gpt-2-cloud-run
2+
3+
## Maintainer/Creator
4+
5+
Max Woolf ([@minimaxir](https://minimaxir.com))
6+
7+
*Max's open-source projects are supported by his [Patreon](https://www.patreon.com/minimaxir). If you found this project helpful, any monetary contributions to the Patreon are appreciated and will be put to good creative use.*
8+
9+
## License
10+
11+
MIT
12+
13+
## Disclaimer
14+
15+
This repo has no affiliation or relationship with OpenAI.

app.py

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
from starlette.applications import Starlette
2+
from starlette.responses import UJSONResponse
3+
import gpt_2_simple as gpt2
4+
import uvicorn
5+
import os
6+
7+
app = Starlette(debug=False)
8+
9+
sess = gpt2.start_tf_sess(threads=1)
10+
gpt2.load_gpt2(sess)
11+
12+
# Needed to avoid cross-domain issues
13+
response_header = {
14+
'Access-Control-Allow-Origin': '*'
15+
}
16+
17+
18+
@app.route('/', methods=['GET', 'POST', 'HEAD'])
19+
async def homepage(request):
20+
if request.method == 'GET':
21+
params = request.query_params
22+
elif request.method == 'POST':
23+
params = await request.json()
24+
elif request.method == 'HEAD':
25+
return UJSONResponse({'text': ''},
26+
headers=response_header)
27+
28+
text = gpt2.generate(sess,
29+
length=int(params.get('length', 1023)),
30+
temperature=float(params.get('temperature', 0.7)),
31+
top_k=int(params.get('top_k', 0)),
32+
top_p=float(params.get('top_p', 0)),
33+
prefix=params.get('prefix', None),
34+
truncate=params.get('truncate', None),
35+
include_prefix=str(params.get(
36+
'include_prefix', True)).lower() == 'true',
37+
return_as_list=True
38+
)[0]
39+
40+
return UJSONResponse({'text': text},
41+
headers=response_header)
42+
43+
if __name__ == '__main__':
44+
uvicorn.run(app, host='0.0.0.0', port=int(os.environ.get('PORT', 8080)))

0 commit comments

Comments
 (0)