use multithreading instead of multiprocessing
Browse files1234567891011124 unmodified lines171819202122231 unmodified line2526272829303132333435368 unmodified lines45464748495051525353 unmodified lines107108109110111112113114115116117118119120121122123124125import jsonimport pandas as pdimport requestsfrom multiprocessing import Poolfrom functools import partialimport streamlit as stGITHUB_CODE = "https://huggingface.co/datasets/lvwerra/github-code"MODELS = ["CodeParrot", "InCoder", "CodeGen", "PolyCoder"]GENERATION_MODELS = ["CodeParrot", "InCoder", "CodeGen"]4 unmodified linesreturn examplesdef generate_code(model_name, gen_prompt, max_new_tokens, temperature, seed):url = (f"https://hf.space/embed/loubnabnl/{model_name.lower()}-subspace/+/api/predict/")1 unmodified lineurl=url, json={"data": [gen_prompt, max_new_tokens, temperature, seed]})generated_text = r.json()["data"][0]return generated_textdef 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:8 unmodified linesst.subheader("1 - Code datasets")read_markdown("datasets/intro.txt")read_markdown("datasets/github_code.txt")#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 Poolpool = 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])# Resourcesst.subheader("Resources")123456789104 unmodified lines15161718192021222324252627281 unmodified line3031323334353637383940414243444546474849508 unmodified lines596061626364656667686953 unmodified lines123124125126127128129130131132133134135136137138139140141142143import jsonimport pandas as pdimport requestsimport threadingimport streamlit as stMODELS = ["CodeParrot", "InCoder", "CodeGen", "PolyCoder"]GENERATION_MODELS = ["CodeParrot", "InCoder", "CodeGen"]4 unmodified linesreturn examplesdef read_markdown(path):with open(path, "r") as f:output = f.read()st.markdown(output, unsafe_allow_html=True)def generate_code(generations, model_name, gen_prompt, max_new_tokens, temperature, seed):# call space using its API endpointurl = (f"https://hf.space/embed/loubnabnl/{model_name.lower()}-subspace/+/api/predict/")1 unmodified lineurl=url, json={"data": [gen_prompt, max_new_tokens, temperature, seed]})generated_text = r.json()["data"][0]generations.append(generated_text)def generate_code_threads(generations, models, gen_prompt, max_new_tokens, temperature, seed):threads = []for model_name in models:# create the threadthreads.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..."):# use threadinggenerations = []generate_code_threads(generations,selected_models,gen_prompt=gen_prompt,max_new_tokens=max_new_tokens,temperature=temperature,seed=seed,)for i in range(len(generations)):print(generations[i])for i in range(len(generations)):st.markdown(f"**{selected_models[i]}**")st.code(generations[i])# Resourcesst.subheader("Resources")
app.py+34-16