Views
No views yet
unsloth/Qwen2-7B-Instruct-bnb-4bit model. It has been fine-tuned for compliance-related question answering with a Chain-of-Thought reasoning process.HF_TOKEN.HF_TOKEN.1# Install the Unsloth library
2!pip install "unsloth[kaggle-new]" # Use "[colab-new]" for Colab
3
4# Import necessary libraries
5from unsloth import FastLanguageModel
6from transformers import pipeline
7import torch
8from kaggle_secrets import UserSecretsClient # Or `from google.colab import userdata` for Colab
9import huggingface_hub
10
11# Log in to Hugging Face
12user_secrets = UserSecretsClient()
13hf_token = user_secrets.get_secret("HF_TOKEN")
14huggingface_hub.login(token=hf_token)
15
16# Load the fine-tuned model from the Hub
17model, tokenizer = FastLanguageModel.from_pretrained(
18 model_name = "D-Khalid/qwen2-7b-instruct-compliance-cot", # This repository ID
19 load_in_4bit = True,
20)
21
22print("✅ Model loaded successfully!")
231# Install the Unsloth library
2!pip install "unsloth[colab-new]" # Use "[kaggle-new]" for Kaggle
3
4# Import necessary libraries and log in to Hugging Face
5from unsloth import FastLanguageModel
6from transformers import pipeline
7import torch
8# Replace this with the correct library for your environment (e.g., from kaggle_secrets import UserSecretsClient)
9from google.colab import userdata
10import huggingface_hub
11
12hf_token = userdata.get('HF_TOKEN')
13huggingface_hub.login(token=hf_token)
14
15# --- Load your fine-tuned model from the Hub ---
16# Unsloth automatically handles loading the base model and applying your adapter.
17model, tokenizer = FastLanguageModel.from_pretrained(
18 model_name = "D-Khalid/qwen2-7b-instruct-compliance-cot", # Your repo ID
19 load_in_4bit = True,
20)
21
221
2# Create the generation pipeline
3pipe = pipeline("text-generation", model=model, tokenizer=tokenizer)
4
5# Prepare your prompt using the chat template
6system_prompt = "You are a helpful compliance assistant. Provide a detailed chain of thought before the final response."
7user_query = "What are the key steps to ensure CCPA compliance for a mobile app?"
8
9messages = [
10 {"role": "system", "content": system_prompt},
11 {"role": "user", "content": user_query},
12]
13prompt = pipe.tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
14
15# Generate the response
16outputs = pipe(prompt, max_new_tokens=512, do_sample=True, temperature=0.7, top_p=0.95)
17
18print("\n--- Generated Response ---")
19# This correctly extracts ONLY the model's generated text
20print(outputs[0]['generated_text'].split("<|im_start|>assistant\n")[-1].replace("<|im_end|>", "").strip())
21