lvwerra's picture
lvwerra HF staff
Create app.py
fd41877
raw
history blame
1.93 kB
import gradio as gr
from transformers import AutoTokenizer, AutoModelForCausalLM, set_seed
from transformers import pipeline
import os
description = """# SantaCoder Endpoint"""
token = os.environ["HUB_TOKEN"]
device="cuda:0"
tokenizer = AutoTokenizer.from_pretrained("bigcode/christmas-models", use_auth_token=token)
model = AutoModelForCausalLM.from_pretrained("bigcode/christmas-models", trust_remote_code=True, use_auth_token=token)
def code_generation(gen_prompt, max_tokens, temperature=0.6, seed=42):
set_seed(seed)
pipe = pipeline("text-generation", model=model, tokenizer=tokenizer)
generated_text = pipe(gen_prompt, do_sample=True, top_p=0.95, temperature=temperature, max_new_tokens=max_tokens)[0]['generated_text']
return generated_text
demo = gr.Blocks()
with demo:
with gr.Row():
gr.Markdown(value=description)
with gr.Row():
with gr.Column():
code = gr.Textbox(lines=10, label="Input code")
max_tokens= gr.Slider(
minimum=8,
maximum=1000,
step=1,
label="Number of tokens to generate",
)
temperature = gr.Slider(
minimum=0.1,
maximum=2.5,
step=0.1,
label="Temperature",
)
seed = gr.Slider(
minimum=0,
maximum=1000,
step=1,
label="Random seed to use for the generation"
)
run = gr.Button()
with gr.Column():
output = gr.Textbox(lines=10, label="Generated code")
event = run.click(code_generation, [code, max_tokens, temperature, seed], output)
gr.HTML(label="Contact", value="<img src='https://huggingface.co/datasets/bigcode/admin/resolve/main/bigcode_contact.png' alt='contact' style='display: block; margin: auto; max-width: 800px;'>")
demo.launch()