Update app to fine-tuned Llama chat interface
Browse files- app.py +78 -4
- requirements.txt +8 -0
app.py
CHANGED
|
@@ -1,7 +1,81 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
-
|
| 4 |
-
|
|
|
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfig
|
| 4 |
+
from peft import PeftModel
|
| 5 |
+
import os
|
| 6 |
|
| 7 |
+
# Configuration
|
| 8 |
+
MODEL_ID = "meta-llama/Llama-3.2-1B"
|
| 9 |
+
ADAPTER_ID = "Zedel17/fine_tuned_llama_1b"
|
| 10 |
|
| 11 |
+
# Load model with 4-bit quantization
|
| 12 |
+
print("Loading model...")
|
| 13 |
+
bnb_config = BitsAndBytesConfig(
|
| 14 |
+
load_in_4bit=True,
|
| 15 |
+
bnb_4bit_use_double_quant=True,
|
| 16 |
+
bnb_4bit_quant_type="nf4",
|
| 17 |
+
bnb_4bit_compute_dtype=torch.bfloat16
|
| 18 |
+
)
|
| 19 |
+
|
| 20 |
+
# Get HF token from Space secrets
|
| 21 |
+
hf_token = os.environ.get("HF_TOKEN")
|
| 22 |
+
|
| 23 |
+
# Load base model
|
| 24 |
+
base_model = AutoModelForCausalLM.from_pretrained(
|
| 25 |
+
MODEL_ID,
|
| 26 |
+
quantization_config=bnb_config,
|
| 27 |
+
device_map="auto",
|
| 28 |
+
token=hf_token
|
| 29 |
+
)
|
| 30 |
+
|
| 31 |
+
# Load fine-tuned adapter
|
| 32 |
+
model = PeftModel.from_pretrained(base_model, ADAPTER_ID, token=hf_token)
|
| 33 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID, token=hf_token)
|
| 34 |
+
tokenizer.pad_token = tokenizer.eos_token
|
| 35 |
+
|
| 36 |
+
print("Model loaded successfully!")
|
| 37 |
+
|
| 38 |
+
def chat(message, history):
|
| 39 |
+
# Format the conversation
|
| 40 |
+
conversation = ""
|
| 41 |
+
for user_msg, bot_msg in history:
|
| 42 |
+
conversation += f"human: {user_msg}\ngpt: {bot_msg}\n"
|
| 43 |
+
conversation += f"human: {message}\ngpt: "
|
| 44 |
+
|
| 45 |
+
# Tokenize and generate
|
| 46 |
+
inputs = tokenizer(conversation, return_tensors="pt", truncation=True, max_length=512).to(model.device)
|
| 47 |
+
|
| 48 |
+
with torch.no_grad():
|
| 49 |
+
outputs = model.generate(
|
| 50 |
+
**inputs,
|
| 51 |
+
max_new_tokens=256,
|
| 52 |
+
temperature=0.7,
|
| 53 |
+
top_p=0.9,
|
| 54 |
+
do_sample=True,
|
| 55 |
+
pad_token_id=tokenizer.eos_token_id
|
| 56 |
+
)
|
| 57 |
+
|
| 58 |
+
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 59 |
+
|
| 60 |
+
# Extract just the new response
|
| 61 |
+
response = response.split("gpt: ")[-1].strip()
|
| 62 |
+
|
| 63 |
+
return response
|
| 64 |
+
|
| 65 |
+
# Create Gradio interface
|
| 66 |
+
demo = gr.ChatInterface(
|
| 67 |
+
fn=chat,
|
| 68 |
+
title="Fine-Tuned Llama 1B Chat",
|
| 69 |
+
description="Chat with a fine-tuned Llama-3.2-1B model",
|
| 70 |
+
examples=[
|
| 71 |
+
"Hello! How are you?",
|
| 72 |
+
"Can you help me with a coding question?",
|
| 73 |
+
"Tell me something interesting",
|
| 74 |
+
],
|
| 75 |
+
retry_btn=None,
|
| 76 |
+
undo_btn="Delete Previous",
|
| 77 |
+
clear_btn="Clear",
|
| 78 |
+
)
|
| 79 |
+
|
| 80 |
+
if __name__ == "__main__":
|
| 81 |
+
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio
|
| 2 |
+
transformers>=4.35.0
|
| 3 |
+
torch>=2.0.0
|
| 4 |
+
accelerate
|
| 5 |
+
peft
|
| 6 |
+
bitsandbytes
|
| 7 |
+
sentencepiece
|
| 8 |
+
protobuf
|