Upload test_model.py with huggingface_hub
Browse files- test_model.py +29 -26
test_model.py
CHANGED
@@ -1,24 +1,30 @@
|
|
1 |
-
from transformers import
|
2 |
from peft import PeftModel
|
3 |
import torch
|
4 |
import json
|
5 |
|
6 |
-
|
7 |
-
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
8 |
print(f"Device set to use: {device}")
|
9 |
|
10 |
# Load base model and tokenizer
|
11 |
-
|
12 |
-
|
13 |
|
14 |
-
|
15 |
-
|
16 |
-
model
|
17 |
-
model = model.to(device)
|
18 |
model.eval()
|
19 |
|
20 |
-
#
|
21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
"Git": "How do I create a new branch and switch to it in Git?",
|
23 |
"Bash": "How to list all files including hidden ones?",
|
24 |
"Grep": "How do I search for a pattern in multiple files using grep?",
|
@@ -26,21 +32,18 @@ test_prompts = {
|
|
26 |
"Python venv": "How do I activate a virtual environment on Windows?"
|
27 |
}
|
28 |
|
29 |
-
# Run test and
|
30 |
results = {}
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
answer =
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
# Save to file
|
43 |
-
with open("test_outputs.json", "w", encoding="utf-8") as f:
|
44 |
-
json.dump(results, f, indent=4)
|
45 |
|
46 |
print("\n✅ All outputs saved to test_outputs.json")
|
|
|
1 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
2 |
from peft import PeftModel
|
3 |
import torch
|
4 |
import json
|
5 |
|
6 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
|
|
7 |
print(f"Device set to use: {device}")
|
8 |
|
9 |
# Load base model and tokenizer
|
10 |
+
base_model = AutoModelForCausalLM.from_pretrained("TinyLlama/TinyLlama-1.1B-Chat-v1.0").to(device)
|
11 |
+
tokenizer = AutoTokenizer.from_pretrained("TinyLlama/TinyLlama-1.1B-Chat-v1.0")
|
12 |
|
13 |
+
# Load LoRA adapter
|
14 |
+
model = PeftModel.from_pretrained(base_model, "Harish2002/cli-lora-tinyllama")
|
15 |
+
model.to(device)
|
|
|
16 |
model.eval()
|
17 |
|
18 |
+
# Utility function to generate answers
|
19 |
+
def generate_answer(question):
|
20 |
+
prompt = f"{question}\nAnswer:"
|
21 |
+
inputs = tokenizer(prompt, return_tensors="pt").to(device)
|
22 |
+
with torch.no_grad():
|
23 |
+
outputs = model.generate(**inputs, max_new_tokens=128)
|
24 |
+
return tokenizer.decode(outputs[0], skip_special_tokens=True).replace(prompt, "").strip()
|
25 |
+
|
26 |
+
# Questions to test
|
27 |
+
questions = {
|
28 |
"Git": "How do I create a new branch and switch to it in Git?",
|
29 |
"Bash": "How to list all files including hidden ones?",
|
30 |
"Grep": "How do I search for a pattern in multiple files using grep?",
|
|
|
32 |
"Python venv": "How do I activate a virtual environment on Windows?"
|
33 |
}
|
34 |
|
35 |
+
# Run test and save results
|
36 |
results = {}
|
37 |
+
|
38 |
+
for category, question in questions.items():
|
39 |
+
print(f"\n🧪 {category}:")
|
40 |
+
print(f"Q: {question}")
|
41 |
+
answer = generate_answer(question)
|
42 |
+
print(f"A: {answer}\n")
|
43 |
+
results[category] = {"question": question, "answer": answer}
|
44 |
+
|
45 |
+
# Save to JSON
|
46 |
+
with open("test_outputs.json", "w") as f:
|
47 |
+
json.dump(results, f, indent=2)
|
|
|
|
|
|
|
48 |
|
49 |
print("\n✅ All outputs saved to test_outputs.json")
|