John Doe commited on
Commit
dd74f4c
1 Parent(s): b9b1799
.gitignore ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ debug
2
+ __pycache__
3
+ incoder-6B
modules/app.py ADDED
@@ -0,0 +1,237 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import sys
2
+ from typing import List
3
+ import traceback
4
+ import os
5
+ import base64
6
+
7
+ import logging
8
+ logging.basicConfig(level=logging.INFO)
9
+ import modules.cloud_logging
10
+
11
+ import tokenizers
12
+ import torch
13
+ from transformers import AutoModelForCausalLM, AutoTokenizer
14
+ import json
15
+ import pprint
16
+
17
+ # needs to be imported *before* transformers
18
+ if os.path.exists('debug'):
19
+ BIG_MODEL = False
20
+ CUDA = False
21
+ else:
22
+ BIG_MODEL = True
23
+ CUDA = True
24
+
25
+ # from flask import Flask, request, render_template
26
+ # from flask_cors import CORS
27
+ # app = Flask(__name__, static_folder='static')
28
+ # app.config['TEMPLATES_AUTO_RELOAD'] = Tru
29
+ # CORS(app, resources= {
30
+ # r"/generate": {"origins": origins},
31
+ # r"/infill": {"origins": origins},
32
+ # })
33
+ # origins=[f"http://localhost:{PORT}", "https://huggingface.co", "https://hf.space"]
34
+
35
+ PORT = 7860
36
+ VERBOSE = False
37
+
38
+ MAX_LENGTH = 256+64
39
+ TRUNCATION_MESSAGE = f'warning: This demo is limited to {MAX_LENGTH} tokens in the document for efficiency.'
40
+
41
+ if BIG_MODEL:
42
+ model_name = "facebook/incoder-6B"
43
+ kwargs = dict(
44
+ revision="float16",
45
+ torch_dtype=torch.float16,
46
+ low_cpu_mem_usage=True,
47
+ )
48
+ else:
49
+ model_name = "facebook/incoder-1B"
50
+ kwargs = dict()
51
+
52
+ from fastapi import FastAPI, Request
53
+ from fastapi.staticfiles import StaticFiles
54
+ from fastapi.responses import FileResponse, StreamingResponse
55
+ app = FastAPI(docs_url=None, redoc_url=None)
56
+ app.mount("/static", StaticFiles(directory="static"), name="static")
57
+
58
+
59
+ logging.info("loading model")
60
+ model = AutoModelForCausalLM.from_pretrained(model_name, **kwargs)
61
+ logging.info("loading tokenizer")
62
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
63
+ logging.info("loading complete")
64
+
65
+ if CUDA:
66
+ model = model.half().cuda()
67
+
68
+ BOS = "<|endoftext|>"
69
+ EOM = "<|endofmask|>"
70
+
71
+ def make_sentinel(i):
72
+ return f"<|mask:{i}|>"
73
+
74
+ SPECIAL_TOKENS = [make_sentinel(i) for i in range(256)] + [EOM]
75
+
76
+ def generate(input, length_limit=None, temperature=None):
77
+ input_ids = tokenizer(input, return_tensors="pt").input_ids
78
+ if CUDA:
79
+ input_ids = input_ids.cuda()
80
+ current_length = input_ids.flatten().size(0)
81
+ max_length = length_limit + current_length
82
+ truncated = False
83
+ if max_length > MAX_LENGTH:
84
+ max_length = MAX_LENGTH
85
+ truncated = True
86
+ if max_length == current_length:
87
+ return input, True
88
+ output = model.generate(input_ids=input_ids, do_sample=True, top_p=0.95, temperature=temperature, max_length=max_length)
89
+ detok_hypo_str = tokenizer.decode(output.flatten())
90
+ if detok_hypo_str.startswith(BOS):
91
+ detok_hypo_str = detok_hypo_str[len(BOS):]
92
+ return detok_hypo_str, truncated
93
+
94
+ def infill(parts: List[str], length_limit=None, temperature=None, extra_sentinel=False, max_retries=1):
95
+ assert isinstance(parts, list)
96
+ retries_attempted = 0
97
+ done = False
98
+
99
+
100
+ while (not done) and (retries_attempted < max_retries):
101
+ any_truncated = False
102
+ retries_attempted += 1
103
+ if VERBOSE:
104
+ logging.info(f"retry {retries_attempted}")
105
+ if len(parts) == 1:
106
+ prompt = parts[0]
107
+ else:
108
+ prompt = ""
109
+ # encode parts separated by sentinel
110
+ for sentinel_ix, part in enumerate(parts):
111
+ prompt += part
112
+ if extra_sentinel or (sentinel_ix < len(parts) - 1):
113
+ prompt += make_sentinel(sentinel_ix)
114
+
115
+ # prompt += TokenizerWrapper.make_sentinel(0)
116
+
117
+ infills = []
118
+ complete = []
119
+
120
+ done = True
121
+
122
+ for sentinel_ix, part in enumerate(parts[:-1]):
123
+ complete.append(part)
124
+ prompt += make_sentinel(sentinel_ix)
125
+ completion, this_truncated = generate(prompt, length_limit, temperature)
126
+ any_truncated |= this_truncated
127
+ completion = completion[len(prompt):]
128
+ if EOM not in completion:
129
+ if VERBOSE:
130
+ logging.info(f"warning: {EOM} not found")
131
+ completion += EOM
132
+ # TODO: break inner loop here
133
+ done = False
134
+ completion = completion[:completion.index(EOM) + len(EOM)]
135
+ infilled = completion[:-len(EOM)]
136
+ infills.append(infilled)
137
+ complete.append(infilled)
138
+ prompt += completion
139
+ complete.append(parts[-1])
140
+ text = ''.join(complete)
141
+
142
+ if VERBOSE:
143
+ logging.info("generated text:")
144
+ logging.info(prompt)
145
+ logging.info()
146
+ logging.info("parts:")
147
+ logging.info(parts)
148
+ logging.info()
149
+ logging.info("infills:")
150
+ logging.info(infills)
151
+ logging.info()
152
+ logging.info("restitched text:")
153
+ logging.info(text)
154
+ logging.info()
155
+
156
+ return {
157
+ 'text': text,
158
+ 'parts': parts,
159
+ 'infills': infills,
160
+ 'retries_attempted': retries_attempted,
161
+ 'truncated': any_truncated,
162
+ }
163
+
164
+
165
+ @app.head("/")
166
+ @app.get("/")
167
+ def index() -> FileResponse:
168
+ return FileResponse(path="static/index.html", media_type="text/html")
169
+
170
+ @app.get('/generate')
171
+ # async def generate_maybe(request: Request):
172
+ async def generate_maybe(info: str):
173
+ # form = await info.json()
174
+ # form = await request.json()
175
+ # info is a base64-encoded, url-escaped json string (since GET doesn't support a body, and POST leads to CORS issues)
176
+ # fix padding, following https://stackoverflow.com/a/9956217/1319683
177
+ info = base64.urlsafe_b64decode(info + '=' * (4 - len(info) % 4)).decode('utf-8')
178
+ form = json.loads(info)
179
+ # print(form)
180
+ prompt = form['prompt']
181
+ length_limit = int(form['length'])
182
+ temperature = float(form['temperature'])
183
+ logging.info(json.dumps({
184
+ 'length': length_limit,
185
+ 'temperature': temperature,
186
+ 'prompt': prompt,
187
+ }))
188
+ try:
189
+ generation, truncated = generate(prompt, length_limit, temperature)
190
+ if truncated:
191
+ message = TRUNCATION_MESSAGE
192
+ else:
193
+ message = ''
194
+ return {'result': 'success', 'type': 'generate', 'prompt': prompt, 'text': generation, 'message': message}
195
+ except Exception as e:
196
+ traceback.print_exception(*sys.exc_info())
197
+ logging.error(e)
198
+ return {'result': 'error', 'type': 'generate', 'prompt': prompt, 'message': f'Error: {e}.'}
199
+
200
+ @app.get('/infill')
201
+ # async def infill_maybe(request: Request):
202
+ async def infill_maybe(info: str):
203
+ # form = await info.json()
204
+ # form = await request.json()
205
+ # info is a base64-encoded, url-escaped json string (since GET doesn't support a body, and POST leads to CORS issues)
206
+ # fix padding, following https://stackoverflow.com/a/9956217/1319683
207
+ info = base64.urlsafe_b64decode(info + '=' * (4 - len(info) % 4)).decode('utf-8')
208
+ form = json.loads(info)
209
+ length_limit = int(form['length'])
210
+ temperature = float(form['temperature'])
211
+ max_retries = 1
212
+ extra_sentinel = True
213
+ logging.info(json.dumps({
214
+ 'length': length_limit,
215
+ 'temperature': temperature,
216
+ 'parts_joined': '<infill>'.join(form['parts']),
217
+ }))
218
+ try:
219
+ if len(form['parts']) > 4:
220
+ return {'result': 'error', 'text': ''.join(form['parts']), 'type': 'infill', 'message': f"error: Can't use more than 3 <infill> tokens in this demo (for efficiency)."}
221
+ generation = infill(form['parts'], length_limit, temperature, extra_sentinel=extra_sentinel, max_retries=max_retries)
222
+ generation['result'] = 'success'
223
+ generation['type'] = 'infill'
224
+ if generation['truncated']:
225
+ generation['message'] = TRUNCATION_MESSAGE
226
+ else:
227
+ generation['message'] = ''
228
+ return generation
229
+ # return {'result': 'success', 'prefix': prefix, 'suffix': suffix, 'text': generation['text']}
230
+ except Exception as e:
231
+ traceback.print_exception(*sys.exc_info())
232
+ logging.error(e)
233
+ return {'result': 'error', 'type': 'infill', 'message': f'Error: {e}.'}
234
+
235
+
236
+ if __name__ == "__main__":
237
+ app.run(host='0.0.0.0', port=PORT, threaded=False)
modules/cloud_logging.py ADDED
@@ -0,0 +1,21 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ def make_logging_client():
3
+ cred_filename = os.environ.get('GOOGLE_APPLICATION_CREDENTIALS')
4
+ if not cred_filename:
5
+ return None
6
+ print("cred filename:", cred_filename)
7
+ cred_string = os.environ.get('GOOGLE_APPLICATION_CREDENTIALS_STRING')
8
+ print("cred string:", bool(cred_string))
9
+ if not os.path.exists(cred_filename):
10
+ if cred_string:
11
+ print(f"writing cred string to {cred_filename}")
12
+ with open(cred_filename, 'w') as f:
13
+ f.write(cred_string)
14
+ else:
15
+ return None
16
+ from google.cloud import logging
17
+ logging_client = logging.Client()
18
+ logging_client.setup_logging()
19
+ return logging_client
20
+
21
+ logging_client = make_logging_client()
packages.txt ADDED
@@ -0,0 +1 @@
 
 
1
+ rustc
requirements.txt ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ fastapi==0.74.*
2
+ requests==2.27.*
3
+ torch==1.11.*
4
+ uvicorn[standard]==0.17.*
5
+ tokenizers==0.12.1
6
+ git+https://github.com/huggingface/transformers.git@b18dfd95e1f60ae65a959a7b255fc06522170d1b
7
+ google-cloud-logging
start.py ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ import subprocess
2
+
3
+ subprocess.run("uvicorn modules.app:app --timeout-keep-alive 300 --host 0.0.0.0 --port 7860", shell=True)
static/frame.html ADDED
@@ -0,0 +1 @@
 
 
1
+ <iframe src="index.html"></iframe>
static/index.html ADDED
@@ -0,0 +1,641 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <meta charset="utf-8"/>
5
+ <meta name="viewport" contents="width=device-width, initial-scale=1.0" />
6
+ <title>InCoder</title>
7
+ <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
8
+ <script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.4.14/ace.min.js"></script>
9
+ <script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.4.14/mode-plain_text.min.js"></script>
10
+ <script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.4.14/mode-c_cpp.min.js"></script>
11
+ <script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.4.14/mode-csharp.min.js"></script>
12
+ <script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.4.14/mode-clojure.min.js"></script>
13
+ <script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.4.14/mode-coffee.min.js"></script>
14
+ <script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.4.14/mode-golang.min.js"></script>
15
+ <script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.4.14/mode-haskell.min.js"></script>
16
+ <script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.4.14/mode-python.min.js"></script>
17
+ <script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.4.14/mode-java.min.js"></script>
18
+ <script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.4.14/mode-javascript.min.js"></script>
19
+ <script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.4.14/mode-lua.min.js"></script>
20
+ <script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.4.14/mode-objectivec.min.js"></script>
21
+ <script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.4.14/mode-perl.min.js"></script>
22
+ <script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.4.14/mode-php.min.js"></script>
23
+ <script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.4.14/mode-python.min.js"></script>
24
+ <script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.4.14/mode-ruby.min.js"></script>
25
+ <script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.4.14/mode-rust.min.js"></script>
26
+ <script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.4.14/mode-scala.min.js"></script>
27
+ <script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.4.14/mode-sh.min.js"></script>
28
+ <script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.4.14/mode-swift.min.js"></script>
29
+ <script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.4.14/mode-typescript.min.js"></script>
30
+ <link rel="stylesheet" href="static/style.css">
31
+ </head>
32
+ <style type="text/css">
33
+ /* body {
34
+ font-family: sans-serif;
35
+ } */
36
+ /* .leftside {
37
+ } */
38
+ main {
39
+ max-width: 80rem;
40
+ }
41
+ .rightside {
42
+ width: 30em;
43
+ }
44
+ .submit-holder {
45
+ margin-top: 2em;
46
+ }
47
+ .submit input {
48
+ font-size: 16pt;
49
+ }
50
+ .slider {
51
+ width: 20em;
52
+ }
53
+ #faq {
54
+ max-width: 60em;
55
+ }
56
+ #result {
57
+ font-family: monospace;
58
+ white-space: pre-wrap;
59
+ word-wrap: break-word;
60
+ font-size: 12pt;
61
+ clear: both;
62
+ margin-top: 1em;
63
+ border: 1px solid black;
64
+ padding: 1em;
65
+ width: 60em;
66
+ min-height: 12em;
67
+ }
68
+ #prompt {
69
+ font-weight: bold;
70
+ }
71
+ .loader {
72
+ border: 4px solid #f3f3f3;
73
+ border-radius: 50%;
74
+ border-top: 4px solid #3498db;
75
+ width: 30px;
76
+ height: 30px;
77
+ animation: spin 2s linear infinite;
78
+ margin-right: 1em;
79
+ }
80
+ @keyframes spin {
81
+ 0% { transform: rotate(0deg); }
82
+ 100% { transform: rotate(360deg); }
83
+ }
84
+ #loader_holder {
85
+ visibility: hidden;
86
+ display: flex;
87
+ align-items: center;
88
+ }
89
+
90
+ label {
91
+ margin-top: 1em;
92
+ display: inline-elock;
93
+ width: 10em;
94
+ text-align: right;
95
+ font-size: 80%;
96
+ }
97
+ #loader_holder_super {
98
+ }
99
+ #error {
100
+ color: red;
101
+ width: 100%;
102
+ }
103
+ #warning {
104
+ color: darkorange;
105
+ width: 100%;
106
+ }
107
+ #examples span {
108
+ margin-right: 1em;
109
+ }
110
+ #editor {
111
+ position: relative;
112
+ width: 100%;
113
+ height: 400px;
114
+ }
115
+ #editor-holder {
116
+ position: relative;
117
+ width: 100%;
118
+ height: 400px;
119
+ }
120
+ .ace_infill {
121
+ color: red;
122
+ }
123
+ </style>
124
+ <body>
125
+ <main>
126
+ <div class="card" id="about">
127
+ <div class="header"> <h1>InCoder: A Generative Model for Code Infilling and Synthesis</h1> </div>
128
+ <p>Demo of the 6.7B parameter version of InCoder: a decoder-only Transformer model that can both extend and insert/infill code.</p>
129
+ <p>Select one of the examples below, or input your own code into the editor. You can type &lt;infill&gt; to mark a location you want the model to insert code at.</p>
130
+ <p>Click "Extend" to append text at the end of the editor. Click "Infill" to replace all &lt;infill&gt; masks. (Click "Add &lt;infill&gt; mask" to add a mask at the cursor or replace the current selection.) </p>
131
+ </div>
132
+ <div class="card" id="examples">
133
+ <div id="examples-infill">
134
+ <span class="softspan">Infill Examples:</span>
135
+ <br>
136
+ <span class="softspan"><a href='javascript:select_example("type-pred");'>Type prediction</a></span>
137
+ <span class="softspan"><a href='javascript:select_example("multi-region");'>Docstring to function</a></span>
138
+ <span class="softspan"><a href='javascript:select_example("docstring-2");'>Function to docstring</a></span>
139
+ <!--
140
+ <span class="softspan"><a href='javascript:select_example("python-infill2");'>Docstring to function</a></span>
141
+ -->
142
+ <span class="softspan"><a href='javascript:select_example("class");'>Class generation</a></span>
143
+ </div>
144
+ <div id="examples-extend">
145
+ <span class="softspan">Extend Examples:</span>
146
+ <br>
147
+ <span class="softspan"><a href='javascript:select_example("python");'>Python</a></span>
148
+ <span class="softspan"><a href='javascript:select_example("javascript");'>JavaScript</a></span>
149
+ <span class="softspan"><a href='javascript:select_example("jupyter");'>Jupyter</a></span>
150
+ <span class="softspan"><a href='javascript:select_example("stackoverflow");'>StackOverflow</a></span>
151
+ <span class="softspan"><a href='javascript:select_example("metadata-conditioning");'>Metadata Conditioning</a></span>
152
+ <span class="softspan"><a href='javascript:select_example("metadata-prediction");'>Metadata Prediction</a></span>
153
+ </div>
154
+ </div>
155
+ <div class="card" id="controls">
156
+ <div>
157
+ <label>Num Tokens:</label>
158
+ <input type="range" value="64" min="16" max="256" step="16" class="slider"
159
+ oninput="this.nextElementSibling.value = this.value" name="length" id='length_slider'>
160
+ <output class='a' id="length_slider_output">64</output>
161
+ </div>
162
+ <div>
163
+ <label>Temperature:</label>
164
+ <input type="range" value="0.6" min="0.1" max="1.0" step="0.10" class="slider"
165
+ oninput="this.nextElementSibling.value = this.value" name="temp" id='temp_slider'>
166
+ <output class='a' id="temp_slider_output">0.6</output>
167
+ </div>
168
+ <div id="buttons">
169
+ <br>
170
+ <input type="button" value="Extend" id="extend-form-button" />
171
+ <input type="button" value="Infill" id="infill-form-button" />
172
+ <br>
173
+ <br>
174
+ <input type="button" value="Add <infill> mask" id="insert-mask-button" title="add the infill marker at cursor or selection" />
175
+ </div>
176
+ </div>
177
+ <div id="edit-container" class="card">
178
+ <div id="syntax">
179
+ <span class="softspan">Syntax:</span>
180
+ <select name="mode" id="mode">
181
+ <option value="text">Text</option>
182
+ <option value="c_cpp">C/C++</option>
183
+ <option value="csharp">C#</option>
184
+ <option value="clojure">Clojure</option>
185
+ <option value="coffee">CoffeeScript</option>
186
+ <option value="golang">Go</option>
187
+ <option value="haskell">Haskell</option>
188
+ <option value="java">Java</option>
189
+ <option value="javascript">JavaScript</option>
190
+ <option value="lua">Lua</option>
191
+ <option value="objectivec">Objective C</option>
192
+ <option value="perl">Perl</option>
193
+ <option value="php">PHP</option>
194
+ <option value="python">Python</option>
195
+ <option value="ruby">Ruby</option>
196
+ <option value="rust">Rust</option>
197
+ <option value="scala">Scala</option>
198
+ <option value="sh">Shell</option>
199
+ <option value="swift">Swift</option>
200
+ <option value="typescript">Typescript</option>
201
+ </select>
202
+ </div>
203
+ <div id="editor"></div>
204
+ </div>
205
+ <div id="loader_holder_super" class="card">
206
+ <h1>Messages</h1>
207
+ <div id="error"></div>
208
+ <div id="warning"></div>
209
+ <div id="loader_holder">
210
+ <div class="loader"></div>
211
+ <div>
212
+ Generation queued, please wait...
213
+ </div>
214
+ </div>
215
+ </div>
216
+ <div id="info" class="card">
217
+ <h1 id="debug-info">More Info</h3>
218
+ <p>
219
+ See <a href="https://sites.google.com/view/incoder-code-models" target="_blank" rel="noopener noreferrer">our project site</a> for more information on
220
+ these models, including a paper and examples.
221
+ </p>
222
+
223
+ <p>
224
+ For instructions on setting up and using the models (via HuggingFace transformers), see
225
+ <a href="https://github.com/dpfried/incoder/blob/main/README.md" target="_blank" rel="noopener noreferrer">our readme</a>.
226
+ </p>
227
+
228
+ <h1 id="debug-info">Credits</h3>
229
+ <p>This model was developed at Facebook AI Research by Daniel Fried*, Armen Aghajanyan*, Jessy Lin, Sida Wang, Eric Wallace, Freda Shi, Ruiqi Zhong,
230
+ Wen-tau Yih, Luke Zettlemoyer, and Mike Lewis.</p>
231
+ <p>Thanks to Naman Goyal and Stephen Roller for writing the code this demo was based on. Extensions by Daniel Fried and
232
+ Sida Wang.</p>
233
+ </div>
234
+ </main>
235
+ <script type="text/javascript">
236
+ // these constants are only used for providing user expectations.
237
+ var OVERHEAD = 3;
238
+ var PER_TOKEN = 0.12;
239
+ var SPLIT_TOKEN = "<infill>"
240
+
241
+ var Range = require("ace/range").Range;
242
+
243
+ // examples for the user
244
+ var EXAMPLES = {
245
+ "python-infill2": {
246
+ "prompt":
247
+ `<| file ext=.py |>
248
+ from collections import Counter
249
+
250
+ def <infill>
251
+ """Count the number of occurrences of each word in the file."""
252
+ <infill>
253
+ `,
254
+ "length": 64,
255
+ "temperature": 0.2,
256
+ "mode": "python"
257
+ },
258
+ "multi-region": {
259
+ "prompt":
260
+ `<| file ext=.py |>
261
+ <infill>
262
+ """ Load the given gzip jsonl file. """
263
+ <infill>
264
+ `,
265
+ "length": 64,
266
+ "temperature": 0.2,
267
+ "mode": "python"
268
+ },
269
+ "type-pred": {
270
+ "prompt":
271
+ `def count_words(filename: str) -> <infill>
272
+ """Count the number of occurrences of each word in the file."""
273
+ with open(filename, 'r') as f:
274
+ word_counts = {}
275
+ for line in f:
276
+ for word in line.split():
277
+ if word in word_counts:
278
+ word_counts[word] = 1
279
+ else:
280
+ word_counts[word] = 1
281
+ return word_counts
282
+ `,
283
+ "length": 4,
284
+ "temperature": 0.2,
285
+ "mode": "python"
286
+ },
287
+ "docstring-2": {
288
+ "prompt":
289
+ `def _minimize_in_graph(build_loss_fn, num_steps=200, optimizer=None):
290
+ """
291
+ <infill>
292
+ """
293
+ optimizer = tf.compat.v1.train.AdamOptimizer(
294
+ 0.1) if optimizer is None else optimizer
295
+
296
+ def train_loop_body(step):
297
+ train_op = optimizer.minimize(
298
+ build_loss_fn if tf.executing_eagerly() else build_loss_fn())
299
+ return tf.tuple(tensors=[tf.add(step, 1)], control_inputs=[train_op])
300
+
301
+ minimize_op = tf.compat.v1.while_loop(
302
+ cond=lambda step: step < num_steps,
303
+ body=train_loop_body,
304
+ loop_vars=[tf.constant(0)],
305
+ return_same_structure=True)[0]
306
+ return minimize_op`,
307
+ "length": 64,
308
+ "temperature": 0.3,
309
+ "mode": "python",
310
+ },
311
+ "docstring": {
312
+ "prompt":
313
+ `<| file ext=.py |>
314
+
315
+ def count_words(filename: str) -> Dict[str, int]:
316
+ """<infill>
317
+ """
318
+ with open(filename, 'r') as f:
319
+ word_counts = {}
320
+ for line in f:
321
+ for word in line.split():
322
+ if word in word_counts:
323
+ word_counts[word] = 1
324
+ else:
325
+ word_counts[word] = 1
326
+ return word_counts
327
+ `,
328
+ "length": 32,
329
+ "temperature": 0.2,
330
+ "mode": "python"
331
+ },
332
+ "python": {
333
+ "prompt":
334
+ `<| file ext=.py |>
335
+ def count_words(filename):
336
+ """Count the number of occurrences of each word in the file"""`,
337
+ "length": 64,
338
+ "temperature": 0.6,
339
+ "mode": "python"
340
+ },
341
+ "class": {
342
+ "prompt": "<| file ext=.py |>\nclass Person:\n" + SPLIT_TOKEN + "\np = Person('Eren', 18, 'Male')",
343
+ "length": 64,
344
+ "temperature": 0.2,
345
+ "mode": "python"
346
+ },
347
+ "javascript": {
348
+ "prompt": "// fetch from the given URL and load the response contents into a new div",
349
+ "length": 64,
350
+ "temperature": 0.6,
351
+ "mode": "javascript"
352
+ },
353
+ "jupyter": {
354
+ "prompt": "<| file ext=.ipynb:python |>\n<text>\nThis notebook demonstrates using scikit-learn to perform PCA.\n</text>\n<cell>",
355
+ "length": 64,
356
+ "temperature": 0.6,
357
+ "mode": "python"
358
+ },
359
+ "stackoverflow": {
360
+ "prompt": "<| q tags=regex,html |>\nParsing HTML with regular expressions\nHow do I do this? Is it a good idea?\n<|/ q dscore=3 |>\n<| a dscore=4 |>",
361
+ "length": 64,
362
+ "temperature": 0.6,
363
+ "mode": "text"
364
+ },
365
+ "metadata-conditioning": {
366
+ "prompt": "<| file ext=.py filename=train_model.py source=github dstars=4 |>\n",
367
+ "length": 64,
368
+ "temperature": 0.6,
369
+ "mode": "python"
370
+ },
371
+ "metadata-prediction": {
372
+ "prompt": "<| file source=github ext=.py |>\nfrom setuptools import setup\nfrom setuptools_rust import Binding, RustExtension\n\nextras = {}\nextras[\"testing\"] = [\"pytest\", \"requests\", \"numpy\", \"datasets\"]\nextras[\"docs\"] = [\"sphinx\", \"sphinx_rtd_theme\", \"setuptools_rust\"]\n\nsetup(\n name=\"tokenizers\",\n version=\"0.11\",\n description=\"Fast and Customizable Tokenizers\",\n long_description=open(\"README.md\", \"r\", encoding=\"utf-8\").read(),\n)\n\n<|/ file filename=",
373
+ "length": 1,
374
+ "temperature": 0.2,
375
+ "mode": "python"
376
+ },
377
+ "humaneval": {
378
+ "prompt": "from typing import List, Optional\n\n\ndef longest(strings: List[str]) -> Optional[str]:\n \"\"\" Out of list of strings, return the longest one. Return the first one in case of multiple\n strings of the same length. Return None in case the input list is empty.\n >>> longest([])\n\n >>> longest(['a', 'b', 'c'])\n 'a'\n >>> longest(['a', 'bb', 'ccc'])\n 'ccc'\n \"\"\"\n",
379
+ "temperature": 0.6,
380
+ "length": 64,
381
+ "mode": "python"
382
+ },
383
+ };
384
+
385
+ var editor = ace.edit("editor");
386
+ //var editor = null;
387
+
388
+ function set_editor_mode(mode) {
389
+ session = editor.session
390
+ session.setMode("ace/mode/" + mode, function() {
391
+ var rules = session.$mode.$highlightRules.getRules();
392
+ for (var stateName in rules) {
393
+ if (Object.prototype.hasOwnProperty.call(rules, stateName)) {
394
+ rules[stateName].unshift({
395
+ token: 'infill',
396
+ regex: SPLIT_TOKEN
397
+ });
398
+ }
399
+ }
400
+ // force recreation of tokenizer
401
+ session.$mode.$tokenizer = null;
402
+ session.bgTokenizer.setTokenizer(session.$mode.getTokenizer());
403
+ // force re-highlight whole document
404
+ session.bgTokenizer.start(0);
405
+ });
406
+ }
407
+
408
+ /*
409
+ var textarea = $('textarea[name="prompt"]').hide();
410
+ var prefix_textarea = $('textarea[name="prefix"]').hide();
411
+ var suffix_textarea = $('textarea[name="suffix"]').hide();
412
+ editor.getSession().on('change', function () {
413
+ textarea.val(editor.getSession().getValue());
414
+ });
415
+ */
416
+
417
+ function set_text(text) {
418
+ editor.getSession().setValue(text);
419
+ // textarea.val(text);
420
+ }
421
+
422
+ function set_selection(data) {
423
+ var lines = editor.getSession().doc.$lines;
424
+ var lines_flat = join_lines(lines);
425
+ if (data['type'] == 'generate') {
426
+ doc_length = lines_flat.length;
427
+ var start = convert_string_index_to_location(data['prompt'].length, lines);
428
+ var end = convert_string_index_to_location(doc_length, lines);
429
+ // reverse this so that we can shift select to shorten and delete extra stuff
430
+ editor.selection.setRange(new Range(end.row, end.column, start.row, start.column));
431
+ } else if (data['type'] == 'infill') {
432
+ var length_so_far = 0;
433
+ for (var i = 0; i < data['infills'].length; i++) {
434
+ var prefix = data['parts'][i];
435
+ var suffix = data['parts'][i+1];
436
+ var infilled = data['infills'][i];
437
+ var start = convert_string_index_to_location(length_so_far + prefix.length, lines);
438
+ var end = convert_string_index_to_location(length_so_far + (prefix + infilled).length, lines);
439
+ var range = null;
440
+ if (data['infills'].length == 1) {
441
+ range = new Range(end.row, end.column, start.row, start.column)
442
+ } else {
443
+ range = new Range(start.row, start.column, end.row, end.column)
444
+ }
445
+ if (i == 0) {
446
+ editor.selection.setRange(range);
447
+ } else {
448
+ editor.selection.addRange(range);
449
+ }
450
+ length_so_far += (prefix + infilled).length;
451
+ }
452
+ }
453
+ editor.focus();
454
+ }
455
+
456
+ function select_example(name) {
457
+ $("#length_slider").val(EXAMPLES[name]["length"]);
458
+ $("#length_slider_output").text(EXAMPLES[name]["length"]);
459
+ $("#temp_slider").val(EXAMPLES[name]["temperature"]);
460
+ $("#temp_slider_output").text(EXAMPLES[name]["temperature"]);
461
+ set_text(EXAMPLES[name]["prompt"])
462
+ var mode = EXAMPLES[name]["mode"];
463
+
464
+ set_editor_mode(mode);
465
+ $("#mode").val(mode).change();
466
+ }
467
+
468
+ function newline_character() {
469
+ return editor.getSession().doc.getNewLineCharacter();
470
+ }
471
+
472
+ function join_lines(lines) {
473
+ return lines.join(newline_character());
474
+ }
475
+
476
+ function get_prefix(location, lines) {
477
+ if (!(location.hasOwnProperty('row') && location.hasOwnProperty('column'))) {
478
+ console.error("invalid location " + location);
479
+ }
480
+ if (location.row == 0) {
481
+ return lines[location.row].substring(0, location.column);
482
+ } else {
483
+ return join_lines(lines.slice(0, location.row)) + newline_character() + lines[location.row].substring(0, location.column);
484
+ }
485
+ }
486
+
487
+ function convert_location_to_string_index(location, lines) {
488
+ return get_prefix(location, lines).length;
489
+ }
490
+
491
+ function convert_string_index_to_location(string_index, lines) {
492
+ var column = 0;
493
+ var row = 0;
494
+ var char_count = 0;
495
+ var line_sep_length = editor.getSession().doc.getNewLineCharacter().length;
496
+ for (var i = 0; i < lines.length; i++) {
497
+ var line = lines[i];
498
+ var new_char_count = char_count + line.length + line_sep_length;
499
+ if (string_index < new_char_count) {
500
+ return {
501
+ 'row': i,
502
+ 'column': string_index - char_count,
503
+ }
504
+ }
505
+ char_count = new_char_count;
506
+ }
507
+ console.error("did not find index " + string_index + " in lines " + lines);
508
+ return null;
509
+ }
510
+
511
+ function get_infill_parts(warn_on_single) {
512
+ var lines = editor.getSession().doc.$lines;
513
+ var lines_flat = join_lines(lines);
514
+ parts = lines_flat.split(SPLIT_TOKEN)
515
+ if (warn_on_single && parts.length == 1) {
516
+ window.alert('There are no infill masks, add some <infill> masks before requesting an infill')
517
+ }
518
+ return parts
519
+ }
520
+
521
+ function insert_mask() {
522
+ if (editor.selection.ranges.length > 1) {
523
+ for (var i = 0; i < editor.selection.ranges.length; i++) {
524
+ console.log('range is', editor.selection.ranges[i])
525
+ editor.session.replace(editor.selection.ranges[i], SPLIT_TOKEN)
526
+ }
527
+ } else {
528
+ editor.session.replace(editor.selection.getRange(), SPLIT_TOKEN)
529
+ }
530
+ }
531
+
532
+
533
+ function make_generate_listener(url) {
534
+ return async function(event) {
535
+ var length = $("#length_slider").val();
536
+ var eta = PER_TOKEN * length + OVERHEAD;
537
+ // $("#eta").text(eta);
538
+ // $("#infill-form-button").click(function (event) { console.log(editor.selection.getCursor()); });
539
+
540
+ // get temperature and response length parameters
541
+ var send_data = {
542
+ length: $("#length_slider").val(),
543
+ temperature: $("#temp_slider").val(),
544
+ extra_sentinel: $('#extra_sentinel_checkbox').is(":checked"),
545
+ max_retries: $('#max_retries_slider').val(),
546
+ parts: get_infill_parts(url == "infill"),
547
+ prompt: editor.getSession().getValue(),
548
+ }
549
+ console.log("send_data:");
550
+ console.log(send_data);
551
+
552
+ $("#loader_holder").css("visibility", "visible");
553
+ $("#extend-form-button").prop("disabled", true);
554
+ $("#infill-form-button").prop("disabled", true);
555
+ $("#error").text("");
556
+
557
+ function complete() {
558
+ $("#loader_holder").css("visibility", "hidden");
559
+ $("#extend-form-button").prop("disabled", false);
560
+ $("#infill-form-button").prop("disabled", false);
561
+ }
562
+
563
+ function success(receive_data) {
564
+ console.log("Response:");
565
+ console.log(receive_data);
566
+ if (receive_data["result"] == "success") {
567
+ console.log("success");
568
+ // $("#prompt").text(data["prompt"]);
569
+ // $("#response").text(data["text"]);
570
+ set_text(receive_data["text"]);
571
+ set_selection(receive_data);
572
+ $("#error").text("");
573
+ if (receive_data["message"] != "") {
574
+ $("#warning").text(receive_data["message"]);
575
+ } else {
576
+ $("#warning").text("");
577
+ }
578
+ } else {
579
+ console.log("error");
580
+ set_text(receive_data["text"])
581
+ $("#error").text(receive_data["message"]);
582
+ }
583
+ }
584
+
585
+ function error(err) {
586
+ console.log(err);
587
+ $("#editor").text("");
588
+ $("#prompt").text("");
589
+ $("#error").text(err);
590
+ }
591
+
592
+ encoded_data = encodeURIComponent(btoa(JSON.stringify(send_data)))
593
+
594
+ try {
595
+ const response = await fetch(`${url}?info=${encoded_data}`);
596
+ // const response = await fetch(`${url}` {
597
+ // method: 'GET',
598
+ // body: encoded_data,
599
+ // });
600
+ if (response.status >= 400) {
601
+ error(response.statusText);
602
+ console.log("here");
603
+ console.log(response.status);
604
+ } else {
605
+ response.json().then(success).catch(error).finally(complete);
606
+ }
607
+ } catch (e) {
608
+ error(e);
609
+ } finally {
610
+ complete();
611
+ }
612
+
613
+ /*
614
+ $.ajax({
615
+ url: url,
616
+ type: "GET",
617
+ // processData: true,
618
+ // data: send_data,
619
+ data: JSON.stringify(send_data),
620
+ contentType: 'application/json;charset=UTF-8',
621
+ });
622
+ */
623
+ }
624
+ }
625
+
626
+ // actual logic
627
+ $(document).ready(function() {
628
+ $("#insert-mask-button").click(insert_mask);
629
+ $("#extend-form-button").click(make_generate_listener("generate"));
630
+ $("#infill-form-button").click(make_generate_listener("infill"));
631
+ $("#mode").change(function (e) {
632
+ var mode = $("#mode").val();
633
+ set_editor_mode(mode);
634
+ });
635
+ select_example("python")
636
+ // set_editor_mode("python");
637
+ });
638
+ </script>
639
+ <script src="https://cdnjs.cloudflare.com/ajax/libs/iframe-resizer/4.3.2/iframeResizer.contentWindow.min.js"></script>
640
+ </body>
641
+ </html>
static/style.css ADDED
@@ -0,0 +1,39 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ body {
2
+ padding: 2rem;
3
+ font-family: -apple-system, BlinkMacSystemFont, "Arial", sans-serif;
4
+ }
5
+
6
+ h1 {
7
+ font-size: 16px;
8
+ margin-top: 0;
9
+ }
10
+
11
+ p {
12
+ color: rgb(107, 114, 128);
13
+ font-size: 15px;
14
+ margin-bottom: 10px;
15
+ margin-top: 5px;
16
+ }
17
+
18
+ button {
19
+ font-size: 15px;
20
+ }
21
+
22
+ .softspan {
23
+ color: rgb(127, 134, 148);
24
+ font-size: 15px;
25
+ margin-bottom: 10px;
26
+ margin-top: 5px;
27
+ }
28
+
29
+ .card {
30
+ max-width: 800px;
31
+ margin: 0 auto;
32
+ padding: 16px;
33
+ border: 1px solid lightgray;
34
+ border-radius: 16px;
35
+ }
36
+
37
+ .card p:last-child {
38
+ margin-bottom: 0;
39
+ }
templates/index.html ADDED
@@ -0,0 +1 @@
 
 
1
+ ../static/index.html
tokenizers_patch.py ADDED
@@ -0,0 +1,26 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import sys
3
+ import subprocess
4
+
5
+
6
+ print("Getting rustup")
7
+ subprocess.run(
8
+ "curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y",
9
+ shell=True,
10
+ )
11
+ print("Got rustup")
12
+ myenv = os.environ.copy()
13
+ myenv["PATH"] = os.path.expanduser("~/.cargo/bin:") + myenv["PATH"]
14
+ print("RUSTC", os.path.isfile(os.path.expanduser("~/.cargo/bin/rustc")))
15
+ subprocess.run("rustc --version", shell=True, env=myenv)
16
+ subprocess.run(
17
+ "pip install -e git+https://github.com/huggingface/tokenizers/#egg=tokenizers\&subdirectory=bindings/python",
18
+ shell=True,
19
+ env=myenv,
20
+ )
21
+ sys.path.append(
22
+ os.path.join(os.getcwd(), "src", "tokenizers", "bindings", "python", "py_src")
23
+ )
24
+
25
+
26
+ import tokenizers