Views
No views yet
microsoft/Phi-3-mini-4k-instruct, optimized for MBTI personality prediction based on text input. The model is trained using Lightning AI on L40S GPUs and supports lightweight inference on T4 GPUs with 4‑bit quantization.INTJ, ENFP, ISTP, etc.microsoft/Phi-3-mini-4k-instruct1import torch
2from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig
3from peft import PeftModel, LoraConfig
4import json
5from huggingface_hub import hf_hub_download
6import os
7
8# Check versions
9import transformers, peft
10print(f"Transformers: {transformers.__version__}")
11print(f"PEFT: {peft.__version__}")
12
13# Model paths
14model_name = "microsoft/Phi-3-mini-4k-instruct"\model_path = "alam1n/phi3-mbti-lora"
15
16# Step 1: Download and fix the config file
17print("Downloading and fixing adapter config...")
18config_file = hf_hub_download(repo_id=model_path, filename="adapter_config.json")
19
20with open(config_file, 'r') as f:
21 config_data = json.load(f)
22
23print(f"Original config keys: {list(config_data.keys())}")
24
25# Create clean config
26clean_config = {
27 "base_model_name_or_path": config_data.get("base_model_name_or_path", model_name),
28 "bias": config_data.get("bias", "none"),
29 "fan_in_fan_out": config_data.get("fan_in_fan_out", False),
30 "inference_mode": True,
31 "init_lora_weights": config_data.get("init_lora_weights", True),
32 "lora_alpha": config_data.get("lora_alpha", 32),
33 "lora_dropout": config_data.get("lora_dropout", 0.05),
34 "modules_to_save": config_data.get("modules_to_save"),
35 "peft_type": "LORA",
36 "r": config_data.get("r", 16),
37 "target_modules": config_data.get("target_modules", []),
38 "task_type": config_data.get("task_type", "CAUSAL_LM")
39}
40
41with open(config_file, 'w') as f:
42 json.dump(clean_config, f, indent=2)
43
44print("Config fixed!")
45
46# Step 2: Load model with quantization
47bnb_config = BitsAndBytesConfig(
48 load_in_4bit=True,
49 bnb_4bit_use_double_quant=True,
50 bnb_4bit_quant_type="nf4",
51 bnb_4bit_compute_dtype=torch.float16
52)
53
54print("Loading base model...")
55model = AutoModelForCausalLM.from_pretrained(
56 model_name,
57 quantization_config=bnb_config,
58 device_map="auto",
59 trust_remote_code=True
60)
61
62print("Loading LoRA adapter...")
63model = PeftModel.from_pretrained(model, model_path)
64
65# Load tokenizer
66tokenizer = AutoTokenizer.from_pretrained(model_name, use_fast=True, trust_remote_code=True)
67if tokenizer.pad_token is None:
68 tokenizer.pad_token = tokenizer.eos_token
69
70print("Model loaded successfully!")
71
72# MBTI prediction function
73def predict_mbti(person_text):
74 model.eval()
75 prompt = f"""<|system|>
76You are an expert in MBTI personality analysis. Return ONLY the MBTI type.
77<|end|>
78<|user|>
79Analyze this person's posts and determine their MBTI type:
80"{person_text}"<|end|>
81<|assistant|>
82"""
83
84 input_ids = tokenizer(prompt, return_tensors="pt").to(model.device)
85
86 with torch.no_grad():
87 outputs = model.generate(
88 **input_ids,
89 max_new_tokens=10,
90 do_sample=False,
91 eos_token_id=tokenizer.convert_tokens_to_ids(["<|end|"])[0],
92 pad_token_id=tokenizer.convert_tokens_to_ids(["<|end|"])[0]
93 )
94
95 generated_text = tokenizer.decode(outputs[:, input_ids['input_ids'].shape[-1]:][0], skip_special_tokens=False)
96 return generated_text.split("<|end|>")[0].strip()
97
98# Test example
99print(predict_mbti("I love analyzing systems and optimizing code."))AutoTokenizer from Phi-3.@model{phi3_mbti_lora,
title={Phi-3 Mini MBTI Classifier},
author={Md Al Amin},
year={2025},
publisher={HuggingFace}
}