File size: 5,696 Bytes
2f8f51f 5257ec0 2f8f51f 5257ec0 2f8f51f 5257ec0 2f8f51f 5257ec0 2f8f51f |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
import os
import gradio as gr
from transformers import AutoModelForCausalLM, AutoTokenizer, GenerationConfig, TextIteratorStreamer
import torch
from threading import Thread
from huggingface_hub import Repository
import json
theme = gr.themes.Monochrome(
primary_hue="indigo",
secondary_hue="blue",
neutral_hue="slate",
radius_size=gr.themes.sizes.radius_sm,
font=[gr.themes.GoogleFont("Open Sans"), "ui-sans-serif", "system-ui", "sans-serif"],
)
os.environ["TOKENIZERS_PARALLELISM"] = "false"
# Load peft config for pre-trained checkpoint etc.
device = "cuda" if torch.cuda.is_available() else "cpu"
model_id = "SebastianSchramm/Cerebras-GPT-111M-instruction"
if device == "cpu":
model = AutoModelForCausalLM.from_pretrained(model_id)
else:
model = AutoModelForCausalLM.from_pretrained(model_id, device_map="auto")
tokenizer = AutoTokenizer.from_pretrained(model_id)
prompt_template = "Below is an instruction that describes a task, paired with an input that provides further context.\n" \
"Write a response that appropriately completes the request.\n\n" \
"### Instruction:\n{instruction}\n\n### Input:\n{input}\n\n### Response:"
def generate(instruction, input='', temperature=1.0, max_new_tokens=256, top_p=0.9, length_penalty=1.0):
formatted_instruction = prompt_template.format(instruction=instruction, input=input)
# make sure temperature top_p and length_penalty are floats
temperature = float(temperature)
top_p = float(top_p)
length_penalty = float(length_penalty)
# STREAMING BASED ON git+https://github.com/gante/transformers.git@streamer_iterator
# streaming
streamer = TextIteratorStreamer(tokenizer)
model_inputs = tokenizer(formatted_instruction, return_tensors="pt", truncation=True, max_length=2048)
# move to gpu
model_inputs = {k: v.to(device) for k, v in model_inputs.items()}
generate_kwargs = dict(
top_p=top_p,
top_k=0,
temperature=temperature,
do_sample=True,
max_new_tokens=max_new_tokens,
early_stopping=True,
length_penalty=length_penalty,
eos_token_id=tokenizer.eos_token_id,
pad_token_id=tokenizer.eos_token_id,
)
t = Thread(target=model.generate, kwargs={**dict(model_inputs, streamer=streamer), **generate_kwargs})
t.start()
output = ""
hidden_output = ""
for new_text in streamer:
# skip streaming until new text is available
if len(hidden_output) <= len(formatted_instruction):
hidden_output += new_text
continue
# replace eos token
if tokenizer.eos_token in new_text:
new_text = new_text.replace(tokenizer.eos_token, "")
output += new_text
yield output
return output
examples = []
def process_example(args):
for x in generate(args):
pass
return x
with gr.Blocks(theme=theme) as demo:
with gr.Column():
gr.Markdown(
"""<h1><center>Instruction-tuned Cerebras GPT 111M Language Model for Text</center></h1>
<p>
Link to model: [Cerebras-GPT-111M-instruction](SebastianSchramm/Cerebras-GPT-111M-instruction)
</p>
"""
)
with gr.Row():
with gr.Column(scale=3):
instruction = gr.Textbox(placeholder="Instruction...", label="Instruction")
input = gr.Textbox(placeholder="Input...", label="Input")
output = gr.Textbox(
interactive=False,
lines=8,
label="Response",
placeholder="Response will be shown here...",
)
submit = gr.Button("Generate", variant="primary")
gr.Examples(
examples=examples,
inputs=[instruction, input],
cache_examples=True,
fn=process_example,
outputs=[output],
)
with gr.Column(scale=1):
temperature = gr.Slider(
label="Temperature",
value=1.0,
minimum=0.01,
maximum=1.0,
step=0.1,
interactive=True,
info="The higher more random",
)
max_new_tokens = gr.Slider(
label="Max new tokens",
value=256,
minimum=0,
maximum=2048,
step=5,
interactive=True,
info="The maximum numbers of new tokens",
)
top_p = gr.Slider(
label="Top p",
value=0.9,
minimum=0.01,
maximum=1,
step=0.05,
interactive=True,
info="probabilities that add up are kept",
)
length_penalty = gr.Slider(
label="Length penalty",
value=1.0,
minimum=-10.0,
maximum=10.0,
step=0.1,
interactive=True,
info="> 0.0 longer, < 0.0 shorter",
)
submit.click(generate, inputs=[instruction, input, temperature, max_new_tokens, top_p, length_penalty], outputs=[output])
instruction.submit(
generate, inputs=[instruction, input, temperature, max_new_tokens, top_p, length_penalty], outputs=[output]
)
demo.queue(concurrency_count=1)
demo.launch(enable_queue=True)
|