SmolLM2 / app.py
jatingocodeo's picture
Update app.py
cd9eb25 verified
raw
history blame
6.86 kB
import torch
import gradio as gr
from transformers import AutoModelForCausalLM, AutoTokenizer, PreTrainedModel, PretrainedConfig
from huggingface_hub import hf_hub_download
import json
# Define the model architecture
class SmolLM2Config(PretrainedConfig):
model_type = "smollm2"
def __init__(
self,
vocab_size=49152,
hidden_size=576,
intermediate_size=1536,
num_hidden_layers=30,
num_attention_heads=9,
num_key_value_heads=3,
hidden_act="silu",
max_position_embeddings=2048,
initializer_range=0.02,
rms_norm_eps=1e-5,
use_cache=True,
pad_token_id=None,
bos_token_id=0,
eos_token_id=0,
tie_word_embeddings=True,
**kwargs
):
self.vocab_size = vocab_size
self.hidden_size = hidden_size
self.intermediate_size = intermediate_size
self.num_hidden_layers = num_hidden_layers
self.num_attention_heads = num_attention_heads
self.num_key_value_heads = num_key_value_heads
self.hidden_act = hidden_act
self.max_position_embeddings = max_position_embeddings
self.initializer_range = initializer_range
self.rms_norm_eps = rms_norm_eps
self.use_cache = use_cache
super().__init__(
pad_token_id=pad_token_id,
bos_token_id=bos_token_id,
eos_token_id=eos_token_id,
tie_word_embeddings=tie_word_embeddings,
**kwargs
)
# Register the model architecture
from transformers import AutoConfig
AutoConfig.register("smollm2", SmolLM2Config)
class SmolLM2ForCausalLM(PreTrainedModel):
config_class = SmolLM2Config
def __init__(self, config):
super().__init__(config)
self.config = config
# Initialize model weights from your checkpoint
self.model = AutoModelForCausalLM.from_pretrained(
"jatingocodeo/SmolLM2",
config=config,
torch_dtype=torch.float16,
low_cpu_mem_usage=True,
trust_remote_code=True
)
def forward(self, input_ids=None, attention_mask=None, labels=None, **kwargs):
return self.model(
input_ids=input_ids,
attention_mask=attention_mask,
labels=labels,
**kwargs
)
def prepare_inputs_for_generation(self, input_ids, **kwargs):
return self.model.prepare_inputs_for_generation(input_ids, **kwargs)
# Register the model
AutoModelForCausalLM.register(SmolLM2Config, SmolLM2ForCausalLM)
# Cache for model and tokenizer
MODEL = None
TOKENIZER = None
def initialize():
global MODEL, TOKENIZER
if MODEL is None:
print("Loading model and tokenizer...")
model_id = "jatingocodeo/SmolLM2"
try:
# Download and load config
print("Loading config...")
config_path = hf_hub_download(repo_id=model_id, filename="config.json")
with open(config_path, 'r') as f:
config_dict = json.load(f)
config = SmolLM2Config(**config_dict)
# Load tokenizer
print("Loading tokenizer...")
TOKENIZER = AutoTokenizer.from_pretrained(model_id)
# Add special tokens if needed
special_tokens = {
'pad_token': '[PAD]',
'eos_token': '</s>',
'bos_token': '<s>'
}
TOKENIZER.add_special_tokens(special_tokens)
# Load model
print("Loading model...")
MODEL = SmolLM2ForCausalLM.from_pretrained(
model_id,
config=config,
torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
trust_remote_code=True,
low_cpu_mem_usage=True
)
# Move model to device
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
MODEL.to(device)
print(f"Model loaded successfully on {device}")
except Exception as e:
print(f"Error initializing: {str(e)}")
raise
def generate_text(prompt, max_length=100, temperature=0.7, top_k=50):
# Initialize if not already done
if MODEL is None:
try:
initialize()
except Exception as e:
return f"Failed to initialize model: {str(e)}"
try:
# Process prompt
if not prompt.strip():
return "Please enter a prompt."
# Add BOS token if needed
if not prompt.startswith(TOKENIZER.bos_token):
prompt = TOKENIZER.bos_token + prompt
# Encode prompt
input_ids = TOKENIZER.encode(prompt, return_tensors="pt", truncation=True, max_length=2048)
input_ids = input_ids.to(MODEL.device)
# Generate
with torch.no_grad():
outputs = MODEL.generate(
input_ids,
max_length=min(max_length + len(input_ids[0]), 2048),
temperature=max(0.1, min(temperature, 1.0)), # Clamp temperature
top_k=max(1, min(top_k, 100)), # Clamp top_k
do_sample=True if temperature > 0 else False,
num_return_sequences=1,
pad_token_id=TOKENIZER.pad_token_id,
eos_token_id=TOKENIZER.eos_token_id,
)
# Decode and return
generated_text = TOKENIZER.decode(outputs[0], skip_special_tokens=True)
return generated_text.strip()
except Exception as e:
import traceback
traceback.print_exc()
return f"Error during text generation: {str(e)}"
# Create Gradio interface
iface = gr.Interface(
fn=generate_text,
inputs=[
gr.Textbox(label="Prompt", placeholder="Enter your prompt here...", lines=2),
gr.Slider(minimum=10, maximum=200, value=100, step=1, label="Max Length"),
gr.Slider(minimum=0.1, maximum=1.0, value=0.7, step=0.1, label="Temperature"),
gr.Slider(minimum=1, maximum=100, value=50, step=1, label="Top K"),
],
outputs=gr.Textbox(label="Generated Text", lines=5),
title="SmolLM2 Text Generator",
description="Generate text using the fine-tuned SmolLM2 model. Adjust parameters to control the generation.",
examples=[
["Once upon a time", 100, 0.7, 50],
["The quick brown fox", 150, 0.8, 40],
],
allow_flagging="never"
)
# Initialize on startup
try:
initialize()
except Exception as e:
print(f"Warning: Model initialization failed: {str(e)}")
print("Model will be initialized on first request")
if __name__ == "__main__":
iface.launch()