import gradio as gr from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline import torch from peft import PeftModel, PeftConfig, LoraConfig # Import PeftModel only once # Model paths base_model_name = "microsoft/phi-2" new_model_path = "Sunirmala/phi2-Phi2Med" # Adjusted path for clarity # Load tokenizer and model device = "cuda" if torch.cuda.is_available() else "cpu" # Load tokenizer and model tokenizer = AutoTokenizer.from_pretrained( "microsoft/phi-2", trust_remote_code=True) tokenizer.pad_token = tokenizer.eos_token tokenizer.padding_side = "right" from peft import PeftModel, PeftConfig from transformers import AutoModelForCausalLM config = PeftConfig.from_pretrained("Sunirmala/phi2-Phi2Med") model1 = AutoModelForCausalLM.from_pretrained(base_model_name, trust_remote_code=True) model = PeftModel.from_pretrained(model1, "Sunirmala/phi2-Phi2Med") # Define the chat function using appropriate prompt format def QLoRA_Chatgpt(prompt): print(prompt) pipe = pipeline(task="text-generation", model=model, tokenizer=tokenizer, max_length=200) result = pipe(f"[INST] {prompt} [/INST]") return(result[0]['generated_text']) # Define Interface description = 'An AI assistant that works on the Microsoft Phi 2 model, which has been finetuned on the MedQuad dataset using the QLora method, operates effectively. Created by Sunirmala Mohanta and Junaid Naeemulla Khan' title = 'AI Chat bot finetuned on Microsoft Phi 2 model using QLORA' iface = gr.Interface( fn=QLoRA_Chatgpt, inputs=gr.Textbox("how can help you today", label='prompt'), outputs=gr.Textbox(label='Generated-output',scale = 2), title = title, description = description) iface.launch(share=True)