desert commited on
Commit ·
f613acc
1
Parent(s): 77156ce
update with my model
Browse files1234567891011122 unmodified lines1516171819202122232425262728293031323334353637383940414243444546474810 unmodified lines596061626364import gradio as grfrom huggingface_hub import InferenceClient"""For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference"""client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")def respond(message,history: list[tuple[str, str]],2 unmodified linestemperature,top_p,):messages = [{"role": "system", "content": system_message}]for val in history:if val[0]:messages.append({"role": "user", "content": val[0]})if val[1]:messages.append({"role": "assistant", "content": val[1]})messages.append({"role": "user", "content": message})response = ""for message in client.chat_completion(messages,max_tokens=max_tokens,stream=True,temperature=temperature,top_p=top_p,):token = message.choices[0].delta.contentresponse += tokenyield response"""For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface"""demo = gr.ChatInterface(respond,additional_inputs=[10 unmodified lines],)if __name__ == "__main__":demo.launch()123456789101112131415161718192021222324252 unmodified lines282930313233343536373839404142434445464748495051525354555657585960616263646566676869707110 unmodified lines828384858687import gradio as grfrom unsloth import FastLanguageModelimport torch# Load your model and tokenizer (make sure to adjust the path to where your model is stored)max_seq_length = 2048 # Adjust as necessaryload_in_4bit = True # Enable 4-bit quantization for reduced memory usagemodel_path = "/content/drive/My Drive/llama_lora_model_1" # Path to your custom model# Load the model and tokenizermodel, tokenizer = FastLanguageModel.from_pretrained(model_name=model_path,max_seq_length=max_seq_length,load_in_4bit=load_in_4bit,)# Move model to GPU if availabledevice = "cuda" if torch.cuda.is_available() else "cpu"model = model.to(device)# Respond functiondef respond(message,history: list[tuple[str, str]],2 unmodified linestemperature,top_p,):# Prepare the system messagemessages = [{"role": "system", "content": system_message}]# Add history to the messagesfor val in history:if val[0]:messages.append({"role": "user", "content": val[0]})if val[1]:messages.append({"role": "assistant", "content": val[1]})# Add the current message from the usermessages.append({"role": "user", "content": message})# Prepare the inputs for the modelinputs = tokenizer.apply_chat_template(messages,tokenize=True,add_generation_prompt=True,return_tensors="pt",).to(device)# Generate the response using your modeloutputs = model.generate(input_ids=inputs["input_ids"],max_new_tokens=max_tokens,temperature=temperature,top_p=top_p,use_cache=True,)# Decode the generated outputresponse = tokenizer.batch_decode(outputs, skip_special_tokens=True)# Return the responsereturn response[0]# Gradio interface setupdemo = gr.ChatInterface(respond,additional_inputs=[10 unmodified lines],)if __name__ == "__main__":demo.launch()
app.py+41-18