from transformers import AutoModelForCausalLM, AutoTokenizer import torch from peft import PeftModel model = AutoModelForCausalLM.from_pretrained("DAMO-NLP-MT/polylm-1.7b") model = PeftModel.from_pretrained(model, "fadliaulawi/polylm-1.7b-finetuned") tokenizer = AutoTokenizer.from_pretrained("DAMO-NLP-MT/polylm-1.7b", padding_side="left", use_fast = False) def generate_prompt( instruction, input, label ): # template = { # "description": "Template used by Alpaca-LoRA.", # "prompt_input": "Below is an instruction that describes a task, paired with an input that provides further context. Write a response that appropriately completes the request.\n\n### Instruction:\n{instruction}\n\n### Input:\n{input}\n\n### Response:\n", # "prompt_no_input": "Below is an instruction that describes a task. Write a response that appropriately completes the request.\n\n### Instruction:\n{instruction}\n\n### Response:\n", # "response_split": "### Response:" # } # [INST] <> # {{ system_prompt }} # <> # {{ user_message }} [/INST] # return '''[INST] <>\n{0}\n<>\n\n{1} {2} [/INST]'''.format(template['prompt_input'].format(instruction=instruction, input=input), template['response_split'], label) template = { "description": "Template used by Alpaca-LoRA.", "prompt_input": "Di bawah ini adalah instruksi yang menjelaskan tugas, dipasangkan dengan masukan yang memberikan konteks lebih lanjut. Tulis tanggapan yang melengkapi permintaan dengan tepat.\n\n### Instruksi:\n{instruction}\n\n### Masukan:\n{input}", #"prompt_no_input": "Below is an instruction that describes a task. Write a response that appropriately completes the request.\n\n### Instruction:\n{instruction}\n\n### Response:\n", "response_split": "### Tanggapan:" } if input: res = template["prompt_input"].format(instruction=instruction, input=input) #else: # res = template["prompt_no_input"].format(instruction=instruction) res = f"{res} \n\n### Tanggapan:\n" if label: res = f"{res}{label}" return res def user(message, history): return "", history + [[message, None]] def generate_and_tokenize_prompt(data_point): full_prompt = generate_prompt( data_point["instruction"], data_point["input"], data_point["output"], ) # print(full_prompt) # return cutoff_len = 256 tokenizer.pad_token = tokenizer.eos_token result = tokenizer( full_prompt, truncation=True, max_length=cutoff_len, padding=True, return_tensors=None, ) if (result["input_ids"][-1] != tokenizer.eos_token_id and len(result["input_ids"]) < cutoff_len): result["input_ids"].append(tokenizer.eos_token_id) result["attention_mask"].append(1) # result["labels"] = result["input_ids"].copy() return result def chatbot(history,temperature, max_new_tokens, top_p,top_k): user_message = history[-1][0] data = { 'instruction': "Jika Anda seorang dokter, silakan menjawab pertanyaan medis berdasarkan deskripsi pasien.", 'input': user_message, 'output': '' } new_user_input_ids = generate_and_tokenize_prompt(data) # append the new user input tokens to the chat history bot_input_ids = torch.LongTensor([new_user_input_ids['input_ids']]) # generate a response response = model.generate( input_ids=bot_input_ids, pad_token_id=tokenizer.eos_token_id, temperature = float(temperature), max_new_tokens=max_new_tokens, top_p=float(top_p), top_k=top_k, do_sample=True ) # convert the tokens to text, and then split the responses into lines response = tokenizer.batch_decode(response, skip_special_tokens=True, clean_up_tokenization_spaces=False)[0] #response = response.split("### Tanggapan:") #response = response[1].strip() if len(response) > 1 else "" sections = response.split("###") # Ambil potongan yang berisi "Tanggapan" yang pertama response = sections[3] #return response.split("Tanggapan:")[1].strip() history[-1] = response.split("Tanggapan:")[1].strip() return history with gr.Blocks() as demo: temperature = gr.Slider(0, 5, value=0.8, step=0.1, label='Temperature') max_length = gr.Slider(0, 8192, value=256, step=1, label='Max Length') top_p = gr.Slider(0, 1, value=0.8, step=0.1, label='Top P') top_k = gr.Slider(0, 50, value=50, step=1, label='Top K') chatbot = gr.Chatbot() msg = gr.Textbox() submit = gr.Button("Submit") clear = gr.Button("Clear") examples = gr.Examples(examples=["Halo dokter", "Dokter aku flu, aku harus apa?"],inputs=[msg]) #submit.click(bot,[msg,chatbot,temperature, max_length, top_p,top_k],chatbot) submit.click(user, [msg, chatbot], [msg, chatbot], queue=False).then( bot, [chatbot,temperature,max_length,top_p,top_k], chatbot ) clear.click(lambda: None, None, chatbot, queue=False) demo.launch()