Spaces:
No application file
No application file
Upload gradio_with_codegen.py
Browse files- gradio_with_codegen.py +121 -0
gradio_with_codegen.py
ADDED
|
@@ -0,0 +1,121 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# -*- coding: utf-8 -*-
|
| 2 |
+
"""gradio_with_CodeGen.ipynb
|
| 3 |
+
|
| 4 |
+
Automatically generated by Colaboratory.
|
| 5 |
+
|
| 6 |
+
Original file is located at
|
| 7 |
+
https://colab.research.google.com/drive/1sZPTjB9cF90Ivu8LltJPEYSawXCE1LRv
|
| 8 |
+
|
| 9 |
+
# Interacting with [CodeGen](https://github.com/salesforce/CodeGen/)
|
| 10 |
+
"""
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
# Commented out IPython magic to ensure Python compatibility.
|
| 15 |
+
|
| 16 |
+
!git clone https://github.com/salesforce/CodeGen
|
| 17 |
+
# %cd CodeGen
|
| 18 |
+
!pip install --upgrade pip setuptools
|
| 19 |
+
!pip install gradio
|
| 20 |
+
!pip install -r requirements.txt
|
| 21 |
+
|
| 22 |
+
chosen_model = "codegen-350M-nl" #@param ["codegen-350M-nl", "codegen-350M-multi", "codegen-350M-mono", "codegen-2B-nl", "codegen-2B-multi", "codegen-2B-mono", "codegen-6B-nl", "codegen-6B-multi", "codegen-6B-mono", "codegen-16B-nl", "codegen-16B-multi", "codegen-16B-mono"]
|
| 23 |
+
fp16 = True #param {type:"boolean"}
|
| 24 |
+
|
| 25 |
+
import os
|
| 26 |
+
|
| 27 |
+
if not os.path.exists(f'./checkpoints/{chosen_model}'):
|
| 28 |
+
!wget -P checkpoints https://storage.googleapis.com/sfr-codegen-research/checkpoints/{chosen_model}.tar.gz && tar -xvf checkpoints/{chosen_model}.tar.gz -C checkpoints/
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
import torch
|
| 32 |
+
from jaxformer.hf.sample import truncate as do_truncate
|
| 33 |
+
from jaxformer.hf.sample import set_env, set_seed, print_time, create_model, create_custom_gpt2_tokenizer, create_tokenizer, sample
|
| 34 |
+
|
| 35 |
+
# (0) constants
|
| 36 |
+
|
| 37 |
+
models_nl = ['codegen-350M-nl', 'codegen-2B-nl', 'codegen-6B-nl', 'codegen-16B-nl']
|
| 38 |
+
models_pl = ['codegen-350M-multi', 'codegen-2B-multi', 'codegen-6B-multi', 'codegen-16B-multi', 'codegen-350M-mono', 'codegen-2B-mono', 'codegen-6B-mono', 'codegen-16B-mono']
|
| 39 |
+
models = models_nl + models_pl
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
# (2) preamble
|
| 43 |
+
|
| 44 |
+
set_env()
|
| 45 |
+
|
| 46 |
+
pad = 50256
|
| 47 |
+
# device = torch.device('cuda:0')
|
| 48 |
+
device = torch.device("cpu")
|
| 49 |
+
ckpt = f'./checkpoints/{chosen_model}'
|
| 50 |
+
|
| 51 |
+
# if device.type == "cpu":
|
| 52 |
+
# print()
|
| 53 |
+
# print("force full precision for cpu!!")
|
| 54 |
+
# print()
|
| 55 |
+
|
| 56 |
+
fp16 = False
|
| 57 |
+
|
| 58 |
+
|
| 59 |
+
# (3) load
|
| 60 |
+
|
| 61 |
+
with print_time('loading parameters'):
|
| 62 |
+
model = create_model(ckpt=ckpt, fp16=fp16).to(device)
|
| 63 |
+
|
| 64 |
+
|
| 65 |
+
with print_time('loading tokenizer'):
|
| 66 |
+
if chosen_model in models_pl:
|
| 67 |
+
tokenizer = create_custom_gpt2_tokenizer()
|
| 68 |
+
else:
|
| 69 |
+
tokenizer = create_tokenizer()
|
| 70 |
+
tokenizer.padding_side = 'left'
|
| 71 |
+
tokenizer.pad_token = pad
|
| 72 |
+
|
| 73 |
+
def codegen(context):
|
| 74 |
+
#param {type:"string"}
|
| 75 |
+
|
| 76 |
+
rng_seed = 42 #param {type:"integer"}
|
| 77 |
+
rng_deterministic = True #param {type:"boolean"}
|
| 78 |
+
p = 0.95 #param {type:"number"}
|
| 79 |
+
t = 0.1 #param {type:"number"}
|
| 80 |
+
max_length = 128 #param {type:"integer"}
|
| 81 |
+
batch_size = 1 #param {type:"integer"}
|
| 82 |
+
set_seed(rng_seed, deterministic=rng_deterministic)
|
| 83 |
+
|
| 84 |
+
# (4) sample
|
| 85 |
+
|
| 86 |
+
with print_time('sampling'):
|
| 87 |
+
completion = sample(device=device, model=model, tokenizer=tokenizer, context=context, pad_token_id=pad, num_return_sequences=batch_size, temp=t, top_p=p, max_length_sample=max_length)[0]
|
| 88 |
+
truncation = do_truncate(completion)
|
| 89 |
+
|
| 90 |
+
# print('=' * 100)
|
| 91 |
+
# print(completion)
|
| 92 |
+
# print('=' * 100)
|
| 93 |
+
# print(context+truncation)
|
| 94 |
+
# print('=' * 100)
|
| 95 |
+
|
| 96 |
+
|
| 97 |
+
return completion
|
| 98 |
+
|
| 99 |
+
# !python -m jaxformer.hf.sample --model $chosen_model \
|
| 100 |
+
# --rng-seed $rng_seed \
|
| 101 |
+
# --p $p \
|
| 102 |
+
# --t $t \
|
| 103 |
+
# --max-length $max_length \
|
| 104 |
+
# --batch-size $batch_size \
|
| 105 |
+
# --context '$context'
|
| 106 |
+
|
| 107 |
+
# context = "def hello_world():"
|
| 108 |
+
# codegen(context)
|
| 109 |
+
|
| 110 |
+
import numpy as np
|
| 111 |
+
import gradio as gr
|
| 112 |
+
|
| 113 |
+
|
| 114 |
+
iface = gr.Interface(
|
| 115 |
+
codegen,
|
| 116 |
+
[ gr.inputs.Textbox(type='str', label="input prompt"),
|
| 117 |
+
],
|
| 118 |
+
"text",
|
| 119 |
+
)
|
| 120 |
+
|
| 121 |
+
iface.launch(debug=True)
|