|
import json |
|
import pandas as pd |
|
import requests |
|
from multiprocessing import Pool |
|
from functools import partial |
|
import streamlit as st |
|
|
|
|
|
GITHUB_CODE = "https://huggingface.co/datasets/lvwerra/github-code" |
|
MODELS = ["CodeParrot", "InCoder", "CodeGen", "PolyCoder"] |
|
GENERATION_MODELS = ["CodeParrot", "InCoder"] |
|
|
|
@st.cache() |
|
def load_examples(): |
|
with open("utils/examples.json", "r") as f: |
|
examples = json.load(f) |
|
return examples |
|
|
|
|
|
def generate_code(model_name, gen_prompt, max_new_tokens, temperature, seed): |
|
url = ( |
|
f"https://hf.space/embed/loubnabnl/{model_name.lower()}-subspace/+/api/predict/" |
|
) |
|
r = requests.post( |
|
url=url, json={"data": [gen_prompt, max_new_tokens, temperature, seed]} |
|
) |
|
generated_text = r.json()["data"][0] |
|
return generated_text |
|
|
|
def read_markdown(path): |
|
with open(path, "r") as f: |
|
output = f.read() |
|
st.markdown(output, unsafe_allow_html=True) |
|
|
|
st.set_page_config(page_icon=":laptop:", layout="wide") |
|
with open("utils/table_contents.txt", "r") as f: |
|
contents = f.read() |
|
st.sidebar.markdown(contents) |
|
|
|
|
|
st.title("Code generation with 🤗") |
|
read_markdown("utils/intro.txt") |
|
|
|
|
|
st.subheader("1 - Code datasets") |
|
read_markdown("datasets/intro.txt") |
|
read_markdown("datasets/github_code.txt") |
|
|
|
|
|
|
|
col1, col2= st.columns([1,2]) |
|
with col1: |
|
selected_model = st.selectbox("", MODELS, key=1) |
|
read_markdown(f"datasets/{selected_model.lower()}.txt") |
|
|
|
|
|
|
|
st.subheader("2 - Model architecture") |
|
read_markdown("architectures/intro.txt") |
|
col1, col2= st.columns([1,2]) |
|
with col1: |
|
selected_model = st.selectbox("", MODELS, key=2) |
|
read_markdown(f"architectures/{selected_model.lower()}.txt") |
|
|
|
|
|
st.subheader("3 - Code models evaluation") |
|
read_markdown("evaluation/intro.txt") |
|
read_markdown("evaluation/demo_humaneval.txt") |
|
|
|
|
|
st.subheader("4 - Code generation ✨") |
|
col1, col2, col3 = st.columns([7,1,6]) |
|
with col1: |
|
st.markdown("**Models**") |
|
selected_models = st.multiselect( |
|
"Select code generation models to compare:", GENERATION_MODELS, default=["CodeParrot"], key=3 |
|
) |
|
st.markdown(" ") |
|
st.markdown("**Examples**") |
|
examples = load_examples() |
|
example_names = [example["name"] for example in examples] |
|
name2id = dict([(name, i) for i, name in enumerate(example_names)]) |
|
selected_example = st.selectbox( |
|
"Select one of the following examples or implement yours:", example_names |
|
) |
|
example_text = examples[name2id[selected_example]]["value"] |
|
default_length = examples[name2id[selected_example]]["length"] |
|
with col3: |
|
st.markdown("**Generation settings**") |
|
temperature = st.slider( |
|
"Temperature:", value=0.2, min_value=0.0, step=0.1, max_value=2.0 |
|
) |
|
max_new_tokens = st.slider( |
|
"Number of tokens to generate:", |
|
value=default_length, |
|
min_value=8, |
|
step=8, |
|
max_value=256, |
|
) |
|
seed = st.slider( |
|
"Random seed:", value=42, min_value=0, step=1, max_value=1000 |
|
) |
|
gen_prompt = st.text_area( |
|
"Generate code with prompt:", |
|
value=example_text, |
|
height=200, |
|
).strip() |
|
if st.button("Generate code!"): |
|
with st.spinner("Generating code..."): |
|
|
|
pool = Pool() |
|
generate_parallel = partial( |
|
generate_code, |
|
gen_prompt=gen_prompt, |
|
max_new_tokens=max_new_tokens, |
|
temperature=temperature, |
|
seed=seed, |
|
) |
|
output = pool.map(generate_parallel, selected_models) |
|
for i in range(len(output)): |
|
st.markdown(f"**{selected_models[i]}**") |
|
st.code(output[i]) |
|
|
|
|
|
st.subheader("Resources") |
|
read_markdown("utils/resources.txt") |