Spaces:
Build error
Build error
Update app.py
Browse files1 unmodified line23456789101112131415161718192021222310 unmodified lines343536373839404142434445463 unmodified lines505152535455564 unmodified lines616263646566676869707172737475767778798081828384858641 unmodified lines1281291301311321331 unmodified lineimport time#import spacesimport torchfrom transformers import AutoModelForCausalLM, AutoTokenizerimport gradio as grMODEL_LIST = ["openbmb/MiniCPM-1B-sft-bf16", "openbmb/MiniCPM-S-1B-sft"]HF_TOKEN = os.environ.get("HF_TOKEN", None)MODEL_ID = os.environ.get("MODEL_ID", None)MODEL_NAME = MODEL_ID.split("/")[-1]TITLE = "<h1><center>MiniCPM-S-1B-chat</center></h1>"DESCRIPTION = f"""<h3>MODEL NOW: <a href="https://hf.co/{MODEL_ID}">{MODEL_NAME}</a></h3>"""PLACEHOLDER = """<center><p>MiniCPM is an End-Size LLM with only 1.2B parameters excluding embeddings.</p></center>"""10 unmodified lines}"""model = AutoModelForCausalLM.from_pretrained(MODEL_ID,torch_dtype=torch.bfloat16,device_map='auto',low_cpu_mem_usage=True,trust_remote_code=True)tokenizer = AutoTokenizer.from_pretrained(MODEL_ID, torch_dtype=torch.bfloat16)#@spaces.GPU()def stream_chat(3 unmodified linesmax_new_tokens: int = 1024,top_p: float = 1.0,top_k: int = 20,penalty: float = 1.2):print(f'message: {message}')print(f'history: {history}')4 unmodified lines{"role": "user", "content": prompt},{"role": "assistant", "content": answer},])torch.manual_seed(0)resp, history = model.chat(tokenizer,query = message,history = conversation,max_length = max_new_tokens,do_sample = False if temperature == 0 else True,top_p = top_p,top_k = top_k,temperature = temperature,)return respchatbot = gr.Chatbot(height=600, placeholder=PLACEHOLDER)with gr.Blocks(css=CSS, theme="soft") as demo:gr.HTML(TITLE)gr.HTML(DESCRIPTION)gr.DuplicateButton(value="Duplicate Space for private use", elem_classes="duplicate-button")gr.ChatInterface(fn=stream_chat,41 unmodified lineslabel="Repetition penalty",render=False,),],examples=[["Help me study vocabulary: write a sentence for me to fill in the blank, and I'll try to pick the correct option."],1 unmodified line234567891011121314151617181910 unmodified lines303132333435363738394041424344454647484950513 unmodified lines55565758596061624 unmodified lines67686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411541 unmodified lines1571581591601611621631641651661671681 unmodified lineimport time#import spacesimport torchfrom transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamerimport gradio as grfrom threading import ThreadMODEL_LIST = ["HuggingFaceTB/SmolLM-1.7B-Instruct", "HuggingFaceTB/SmolLM-135M-Instruct", "HuggingFaceTB/SmolLM-360M-Instruct"]HF_TOKEN = os.environ.get("HF_TOKEN", None)TITLE = "<h1><center>SmolLM-Instruct</center></h1>"PLACEHOLDER = """<center><pSmolLM is a series of state-of-the-art small language models available in three sizes: 135M, 360M, and 1.7B parameters.</p></center>"""10 unmodified lines}"""# pip install transformersfrom transformers import AutoModelForCausalLM, AutoTokenizerdevice = "cpu" # for GPU usage or "cpu" for CPU usagetokenizer0 = AutoTokenizer.from_pretrained(MODEL_LIST[0])model0 = AutoModelForCausalLM.from_pretrained(MODEL_LIST[0]).to(device)tokenizer1 = AutoTokenizer.from_pretrained(MODEL_LIST[1])model1 = AutoModelForCausalLM.from_pretrained(MODEL_LIST[1]).to(device)tokenizer2 = AutoTokenizer.from_pretrained(MODEL_LIST[2])model2 = AutoModelForCausalLM.from_pretrained(MODEL_LIST[2]).to(device)messages = [{"role": "user", "content": "List the steps to bake a chocolate cake from scratch."}]#@spaces.GPU()def stream_chat(3 unmodified linesmax_new_tokens: int = 1024,top_p: float = 1.0,top_k: int = 20,penalty: float = 1.2,choice: str = "1.7B"):print(f'message: {message}')print(f'history: {history}')4 unmodified lines{"role": "user", "content": prompt},{"role": "assistant", "content": answer},])conversation.append({"role": "user", "content": message})if choice == "1.7B":tokenizer = tokenizer0model = model0elif choice == "135M":model = model1tokenizer = tokenizer1else:model = model2tokenizer = tokenizer2input_text=tokenizer.apply_chat_template(conversation, tokenize=False)inputs = tokenizer.encode(input_text, return_tensors="pt").to(device)streamer = TextIteratorStreamer(tokenizer, timeout=60.0, skip_prompt=True, skip_special_tokens=True)generate_kwargs = dict(inputs,max_new_tokens = max_new_tokens,do_sample = False if temperature == 0 else True,top_p = top_p,top_k = top_k,temperature = temperature,streamer=streamer,)with torch.no_grad():thread = Thread(target=model.generate, kwargs=gen_kwargs)thread.start()buffer = ""for new_text in streamer:buffer += new_textyield buffer#print(tokenizer.decode(outputs[0]))chatbot = gr.Chatbot(height=600, placeholder=PLACEHOLDER)with gr.Blocks(css=CSS, theme="soft") as demo:gr.HTML(TITLE)gr.DuplicateButton(value="Duplicate Space for private use", elem_classes="duplicate-button")gr.ChatInterface(fn=stream_chat,41 unmodified lineslabel="Repetition penalty",render=False,),gr.Radio(["135M", "360M", "1.7B"],value="1.7B",label="Load Model",render=False,),],examples=[["Help me study vocabulary: write a sentence for me to fill in the blank, and I'll try to pick the correct option."],
app.py+60-25