Spaces:
Runtime error
Runtime error
Update app.py
Browse files123456789101112131415161718192021222324257 unmodified lines3334353637387 unmodified lines46474849505152535455565758596061626364import gradio as grfrom unsloth import FastLanguageModelimport torchimport time# ----------------------------# 🚀 Load Model (cached)# ----------------------------@gr.cachedef load_model():max_seq_length = 1024model, tokenizer = FastLanguageModel.from_pretrained(model_name="umarfarzan/my-finetuned-model2-lora",max_seq_length=max_seq_length,dtype=None,load_in_4bit=True)FastLanguageModel.for_inference(model)return model, tokenizerprint("Loading model...")model, tokenizer = load_model()print("✅ Model loaded successfully!")# ----------------------------7 unmodified lines):"""Generate a training program based on user instruction"""# Build prompt in Alpaca formatprompt_text = f"""Below is an instruction that describes a task, paired with an input that provides further context. Write a response that appropriately completes the request.7 unmodified lines"""# Tokenizeinputs = tokenizer([prompt_text], return_tensors="pt").to("cuda")# Generate with progressstart_time = time.time()outputs = model.generate(**inputs,max_new_tokens=max_tokens,temperature=temperature,top_p=top_p,do_sample=True,use_cache=True)generation_time = time.time() - start_time12345678910111213141516171819202122232425262728293031323334357 unmodified lines4344454647484950517 unmodified lines596061626364656667686970717273747576777879import gradio as grfrom transformers import AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfigimport torchimport time# ----------------------------# 🚀 Load Model (cached)# ----------------------------print("Loading model...")model_name = "umarfarzan/my-finetuned-model2-lora"max_seq_length = 1024# Configure quantization for efficient inferencebnb_config = BitsAndBytesConfig(load_in_4bit=True,bnb_4bit_quant_type="nf4",bnb_4bit_use_double_quant=True,bnb_4bit_compute_dtype=torch.bfloat16)# Load tokenizertokenizer = AutoTokenizer.from_pretrained(model_name)# Load model with 4-bit quantizationmodel = AutoModelForCausalLM.from_pretrained(model_name,quantization_config=bnb_config,device_map="auto",trust_remote_code=True)print("✅ Model loaded successfully!")# ----------------------------7 unmodified lines):"""Generate a training program based on user instruction"""if not instruction.strip():return "⚠️ Please enter a training program description.", ""# Build prompt in Alpaca formatprompt_text = f"""Below is an instruction that describes a task, paired with an input that provides further context. Write a response that appropriately completes the request.7 unmodified lines"""# Tokenizeinputs = tokenizer([prompt_text], return_tensors="pt").to(model.device)# Generate with progressstart_time = time.time()with torch.inference_mode():outputs = model.generate(**inputs,max_new_tokens=max_tokens,temperature=temperature,top_p=top_p,do_sample=True,use_cache=True,pad_token_id=tokenizer.pad_token_id or tokenizer.eos_token_id)generation_time = time.time() - start_time
app.py+38-23