use multithreading instead of multiprocessing
Browse files123454567989104 unmodified lines1516171819202122232024252627281 unmodified line30313228333430313233353637383940414243444546474849508 unmodified lines596061626364656667686953 unmodified lines123124125110111112113126127128129130131132133134135119120136137138139122140141142143import jsonimport pandas as pdimport requestsfrom multiprocessing import Poolfrom functools import partialimport threadingimport streamlit as st
GITHUB_CODE = "https://huggingface.co/datasets/lvwerra/github-code"MODELS = ["CodeParrot", "InCoder", "CodeGen", "PolyCoder"]GENERATION_MODELS = ["CodeParrot", "InCoder", "CodeGen"]
4 unmodified lines return examples
def read_markdown(path): with open(path, "r") as f: output = f.read() st.markdown(output, unsafe_allow_html=True) def generate_code(model_name, gen_prompt, max_new_tokens, temperature, seed):def generate_code(generations, model_name, gen_prompt, max_new_tokens, temperature, seed): # call space using its API endpoint url = ( f"https://hf.space/embed/loubnabnl/{model_name.lower()}-subspace/+/api/predict/" )1 unmodified line url=url, json={"data": [gen_prompt, max_new_tokens, temperature, seed]} ) generated_text = r.json()["data"][0] return generated_text generations.append(generated_text)
def read_markdown(path): with open(path, "r") as f: output = f.read() st.markdown(output, unsafe_allow_html=True) def generate_code_threads(generations, models, gen_prompt, max_new_tokens, temperature, seed): threads = [] for model_name in models: # create the thread threads.append( threading.Thread(target=generate_code, args=(generations, model_name, gen_prompt, max_new_tokens, temperature, seed)) ) threads[-1].start() for t in threads: t.join()
st.set_page_config(page_icon=":laptop:", layout="wide")with open("utils/table_contents.txt", "r") as f:8 unmodified linesst.subheader("1 - Code datasets")read_markdown("datasets/intro.txt")read_markdown("datasets/github_code.txt")#GITHUB_CODE = "https://huggingface.co/datasets/lvwerra/github-code"#st.markdown(f"Preview of some code files from Github repositories in [Github-code dataset]({GITHUB_CODE}):")#df = pd.read_csv("utils/data_preview.csv")#st.dataframe(df)
col1, col2= st.columns([1,2])with col1: selected_model = st.selectbox("", MODELS, key=1)53 unmodified lines).strip()if st.button("Generate code!"): with st.spinner("Generating code..."): # Create a multiprocessing Pool pool = Pool() generate_parallel = partial( generate_code, # use threading generations = [] generate_code_threads( generations, selected_models, 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)): for i in range(len(generations)): print(generations[i]) for i in range(len(generations)): st.markdown(f"**{selected_models[i]}**") st.code(output[i]) st.code(generations[i])
# Resourcesst.subheader("Resources")app.py+34-16