Views
No views yet
1# 0. If in Colab and it's a new session, or if model is private, authenticate:
2# from huggingface_hub import notebook_login; notebook_login()
3
4# 1. Import necessary libraries:
5from transformers import AutoModelForCausalLM, AutoTokenizer
6# The following torch imports might be needed if you were to define the classes manually,
7# but trust_remote_code=True should handle it by loading them from the Hub.
8# import torch
9# import torch.nn as nn
10
11# 2. Define your model ID:
12MODEL_ID = "moelanoby/Sensitive-Qwen-0.5B"
13
14# 3. Load tokenizer and model (trust_remote_code=True is CRUCIAL):
15# This allows Transformers to download and use the Python file ('LLMadd.py')
16# from your Hub repository, which contains the definitions for
17# `SensitivityModule` and `SensitiveBottleneckLayer`.
18
19try:
20 tokenizer = AutoTokenizer.from_pretrained(MODEL_ID, trust_remote_code=True)
21 model = AutoModelForCausalLM.from_pretrained(MODEL_ID, trust_remote_code=True, device_map='auto') # Add other params as needed
22 print(f'Model {MODEL_ID} loaded successfully!')
23except Exception as e:
24 print(f'Error loading model: {e}')
25 print('Ensure the custom code file (LLMadd.py) in the Hub repo is correct and classes are defined.')
26
27# 4. Example generation (adjust based on your model's chat template, e.g., Qwen2-Instruct):
28# prompt = "What is the capital of France?"
29# messages = [{"role": "user", "content": prompt}]
30# text_input = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
31# model_inputs = tokenizer([text_input], return_tensors="pt").to(model.device)
32# generated_ids = model.generate(model_inputs.input_ids, max_new_tokens=50)
33# result = tokenizer.batch_decode(generated_ids[:, model_inputs.input_ids.shape[-1]:], skip_special_tokens=True)[0]
34# print(f'Generated: {result}')trust_remote_code=True allows the execution of Python code
from the 'moelanoby/Sensitive-Qwen-0.5B' repository on Hugging Face Hub.