Spaces:
Build error
Build error
Update app.py
Browse files1 unmodified line23456789101112131415161718192021222313 unmodified lines373839404142434445464748494 unmodified lines545556575859606162636465661 unmodified line686970717273746 unmodified lines81828384858687888913 unmodified lines1031041051061071081091 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/Qwen2-7B-Instruct"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>13 unmodified lines"""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}')4 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,1 unmodified linemax_new_tokens=max_new_tokens,do_sample=True,temperature=temperature,eos_token_id = [151645, 151643],)thread = Thread(target=model.generate, kwargs=generate_kwargs)6 unmodified lineschatbot = gr.Chatbot(height=450)with gr.Blocks(css=CSS) as demo:gr.HTML(TITLE)gr.HTML(DESCRIPTION)gr.DuplicateButton(value="Duplicate Space for private use", elem_classes="duplicate-button")13 unmodified lines),gr.Slider(minimum=128,maximum=4096,step=1,value=1024,label="Max new tokens",1 unmodified line2345678910111213141516171819202122232413 unmodified lines3839404142434445464748495051524 unmodified lines575859606162636465666768697071721 unmodified line74757677787980816 unmodified lines88899091929394959613 unmodified lines1101111121131141151161 unmodified linefrom PIL import Imageimport gradio as grimport spacesfrom transformers import AutoModelForCausalLM, GemmaTokenizerFast, TextIteratorStreamer,BitsAndBytesConfigimport osfrom threading import ThreadHF_TOKEN = os.environ.get("HF_TOKEN", None)MODEL_ID = "google/gemma-2-27b-it"MODELS = os.environ.get("MODELS")MODEL_NAME = MODELS.split("/")[-1]MAX_INPUT_TOKEN_LENGTH = int(os.environ.get("MAX_INPUT_TOKEN_LENGTH", "4096"))TITLE = "<h1><center>Qwen2-Chatbox</center></h1>"DESCRIPTION = f"""<h3>MODEL: <a href="https://hf.co/{MODELS}">{MODEL_NAME}</a></h3><center><p>Gemma is the large language model built by Google.<br>Feel free to test without log.</p>13 unmodified lines"""model = AutoModelForCausalLM.from_pretrained(MODELS,device_map="auto",quantization_config=BitsAndBytesConfig(load_in_4bit=True))tokenizer = GemmaTokenizerFast.from_pretrained(MODELS)model.config.sliding_window = 4096model.eval()@spaces.GPU(duration=90)def 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}')4 unmodified linesprint(f"Conversation is -\n{conversation}")input_ids = tokenizer.apply_chat_template(conversation, add_generation_prompt=True, return_tensors="pt")if input_ids.shape[1] > MAX_INPUT_TOKEN_LENGTH:input_ids = input_ids[:, -MAX_INPUT_TOKEN_LENGTH:]gr.Warning(f"Trimmed input from conversation as it was longer than {MAX_INPUT_TOKEN_LENGTH} tokens.")input_ids = input_ids.to(0)streamer = TextIteratorStreamer(tokenizer, timeout=20.0, skip_prompt=True, skip_special_tokens=True)generate_kwargs = dict({"input_ids": input_ids},streamer=streamer,top_k=top_k,top_p=top_p,1 unmodified linemax_new_tokens=max_new_tokens,do_sample=True,temperature=temperature,num_beams=1,repetition_penalty=repetition_penalty,)thread = Thread(target=model.generate, kwargs=generate_kwargs)6 unmodified lineschatbot = gr.Chatbot(height=600)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")13 unmodified lines),gr.Slider(minimum=128,maximum=2048,step=1,value=1024,label="Max new tokens",
app.py+23-16