Spaces:
Build error
Build error
Update app.py
Browse files1 unmodified line2345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364653 unmodified lines6970717273747576777879808182838434 unmodified lines1191201211221231241 unmodified linefrom PIL import Imageimport gradio as grimport spacesfrom transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig, TextIteratorStreamerimport osfrom threading import ThreadHF_TOKEN = os.environ.get("HF_TOKEN", None)MODEL_ID = "CohereForAI/aya-23-8B"MODEL_ID2 = "CohereForAI/aya-23-35B"MODELS = os.environ.get("MODELS")MODEL_NAME = MODELS.split("/")[-1]TITLE = "<h1><center>Aya-23-Chatbox</center></h1>"DESCRIPTION = f'<h3><center>MODEL: <a href="https://hf.co/{MODELS}">{MODEL_NAME}</a></center></h3>'CSS = """.duplicate-button {margin: auto !important;color: white !important;background: black !important;border-radius: 100vh !important;}"""#QUANTIZEQUANTIZE_4BIT = TrueUSE_GRAD_CHECKPOINTING = TrueTRAIN_BATCH_SIZE = 2TRAIN_MAX_SEQ_LENGTH = 512USE_FLASH_ATTENTION = FalseGRAD_ACC_STEPS = 16quantization_config = Noneif QUANTIZE_4BIT:quantization_config = BitsAndBytesConfig(load_in_4bit=True,bnb_4bit_quant_type="nf4",bnb_4bit_use_double_quant=True,bnb_4bit_compute_dtype=torch.bfloat16,)attn_implementation = Noneif USE_FLASH_ATTENTION:attn_implementation="flash_attention_2"model = AutoModelForCausalLM.from_pretrained(MODELS,quantization_config=quantization_config,attn_implementation=attn_implementation,torch_dtype=torch.bfloat16,device_map="auto",)tokenizer = AutoTokenizer.from_pretrained(MODELS)@spaces.GPUdef stream_chat(message: str, history: list, temperature: float, max_new_tokens: int):print(f'message is - {message}')print(f'history is - {history}')conversation = []3 unmodified linesprint(f"Conversation is -\n{conversation}")input_ids = tokenizer.apply_chat_template(conversation, tokenize=True, add_generation_prompt=True, return_tensors="pt").to(model.device)streamer = TextIteratorStreamer(tokenizer, **{"skip_special_tokens": True, "skip_prompt": True, 'clean_up_tokenization_spaces':False,})generate_kwargs = dict(input_ids=input_ids,streamer=streamer,max_new_tokens=max_new_tokens,do_sample=True,temperature=temperature,)thread = Thread(target=model.generate, kwargs=generate_kwargs)34 unmodified lineslabel="Max new tokens",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 line2345678910111213141516171819202122232425262728293031323334353637383940414243444546474849503 unmodified lines54555657585960616263646566676869707172737434 unmodified lines1091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381 unmodified linefrom PIL import Imageimport gradio as grimport spacesfrom transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamerimport osfrom threading import ThreadHF_TOKEN = os.environ.get("HF_TOKEN", None)MODEL_ID = "Qwen/Qwen1.5-7B-Chat"MODELS = os.environ.get("MODELS")MODEL_NAME = MODELS.split("/")[-1]TITLE = "<h1><center>Qwen2-Chatbox</center></h1>"DESCRIPTION = f"""<h3>MODEL: <a href="https://hf.co/{MODELS}">{MODEL_NAME}</a></h3><center><p>Qwen is the large language model built by Alibaba Cloud.<br>Feel free to test without log.</p></center>"""CSS = """.duplicate-button {margin: auto !important;color: white !important;background: black !important;border-radius: 100vh !important;}h3 {text-align: center;}"""model = AutoModelForCausalLM.from_pretrained(MODELS,torch_dtype=torch.float16,device_map="auto",)tokenizer = AutoTokenizer.from_pretrained(MODELS)@spaces.GPUdef stream_chat(message: str, history: list, temperature: float, max_new_tokens: int, top_p: float, top_k: int, penalty: float):print(f'message is - {message}')print(f'history is - {history}')conversation = []3 unmodified linesprint(f"Conversation is -\n{conversation}")input_ids = tokenizer.apply_chat_template(conversation, tokenize=False, add_generation_prompt=True)inputs = tokenizer(input_ids, return_tensors="pt").to(0)streamer = TextIteratorStreamer(tokenizer, timeout=10., skip_prompt=True, skip_special_tokens=True)generate_kwargs = dict(inputs,streamer=streamer,top_k=top_k,top_p=top_p,repetition_penalty=penalty,max_new_tokens=max_new_tokens,do_sample=True,temperature=temperature,eos_token_id = [151645, 151643],)thread = Thread(target=model.generate, kwargs=generate_kwargs)34 unmodified lineslabel="Max new tokens",render=False,),gr.Slider(minimum=0.0,maximum=1.0,step=0.1,value=0.8,label="top_p",render=False,),gr.Slider(minimum=1,maximum=20,step=1,value=20,label="top_k",render=False,),gr.Slider(minimum=0.0,maximum=2.0,step=0.1,value=1.0,label="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."],
app.py+53-39