Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,25 +1,35 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from
|
| 3 |
import torch
|
| 4 |
import time
|
| 5 |
|
| 6 |
# ----------------------------
|
| 7 |
# 🚀 Load Model (cached)
|
| 8 |
# ----------------------------
|
| 9 |
-
@gr.cache
|
| 10 |
-
def load_model():
|
| 11 |
-
max_seq_length = 1024
|
| 12 |
-
model, tokenizer = FastLanguageModel.from_pretrained(
|
| 13 |
-
model_name="umarfarzan/my-finetuned-model2-lora",
|
| 14 |
-
max_seq_length=max_seq_length,
|
| 15 |
-
dtype=None,
|
| 16 |
-
load_in_4bit=True
|
| 17 |
-
)
|
| 18 |
-
FastLanguageModel.for_inference(model)
|
| 19 |
-
return model, tokenizer
|
| 20 |
-
|
| 21 |
print("Loading model...")
|
| 22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
print("✅ Model loaded successfully!")
|
| 24 |
|
| 25 |
# ----------------------------
|
|
@@ -33,6 +43,9 @@ def generate_training_program(
|
|
| 33 |
):
|
| 34 |
"""Generate a training program based on user instruction"""
|
| 35 |
|
|
|
|
|
|
|
|
|
|
| 36 |
# Build prompt in Alpaca format
|
| 37 |
prompt_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.
|
| 38 |
|
|
@@ -46,19 +59,21 @@ def generate_training_program(
|
|
| 46 |
"""
|
| 47 |
|
| 48 |
# Tokenize
|
| 49 |
-
inputs = tokenizer([prompt_text], return_tensors="pt").to(
|
| 50 |
|
| 51 |
# Generate with progress
|
| 52 |
start_time = time.time()
|
| 53 |
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
|
|
|
|
|
|
| 62 |
|
| 63 |
generation_time = time.time() - start_time
|
| 64 |
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfig
|
| 3 |
import torch
|
| 4 |
import time
|
| 5 |
|
| 6 |
# ----------------------------
|
| 7 |
# 🚀 Load Model (cached)
|
| 8 |
# ----------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
print("Loading model...")
|
| 10 |
+
|
| 11 |
+
model_name = "umarfarzan/my-finetuned-model2-lora"
|
| 12 |
+
max_seq_length = 1024
|
| 13 |
+
|
| 14 |
+
# Configure quantization for efficient inference
|
| 15 |
+
bnb_config = BitsAndBytesConfig(
|
| 16 |
+
load_in_4bit=True,
|
| 17 |
+
bnb_4bit_quant_type="nf4",
|
| 18 |
+
bnb_4bit_use_double_quant=True,
|
| 19 |
+
bnb_4bit_compute_dtype=torch.bfloat16
|
| 20 |
+
)
|
| 21 |
+
|
| 22 |
+
# Load tokenizer
|
| 23 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 24 |
+
|
| 25 |
+
# Load model with 4-bit quantization
|
| 26 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 27 |
+
model_name,
|
| 28 |
+
quantization_config=bnb_config,
|
| 29 |
+
device_map="auto",
|
| 30 |
+
trust_remote_code=True
|
| 31 |
+
)
|
| 32 |
+
|
| 33 |
print("✅ Model loaded successfully!")
|
| 34 |
|
| 35 |
# ----------------------------
|
|
|
|
| 43 |
):
|
| 44 |
"""Generate a training program based on user instruction"""
|
| 45 |
|
| 46 |
+
if not instruction.strip():
|
| 47 |
+
return "⚠️ Please enter a training program description.", ""
|
| 48 |
+
|
| 49 |
# Build prompt in Alpaca format
|
| 50 |
prompt_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.
|
| 51 |
|
|
|
|
| 59 |
"""
|
| 60 |
|
| 61 |
# Tokenize
|
| 62 |
+
inputs = tokenizer([prompt_text], return_tensors="pt").to(model.device)
|
| 63 |
|
| 64 |
# Generate with progress
|
| 65 |
start_time = time.time()
|
| 66 |
|
| 67 |
+
with torch.inference_mode():
|
| 68 |
+
outputs = model.generate(
|
| 69 |
+
**inputs,
|
| 70 |
+
max_new_tokens=max_tokens,
|
| 71 |
+
temperature=temperature,
|
| 72 |
+
top_p=top_p,
|
| 73 |
+
do_sample=True,
|
| 74 |
+
use_cache=True,
|
| 75 |
+
pad_token_id=tokenizer.pad_token_id or tokenizer.eos_token_id
|
| 76 |
+
)
|
| 77 |
|
| 78 |
generation_time = time.time() - start_time
|
| 79 |
|