Add standalone inference script
Browse files- inference.py +87 -0
inference.py
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
========================================
|
| 3 |
+
INFERENCE SCRIPT FOR MINI CODING AGENT
|
| 4 |
+
Load your fine-tuned Gemma-3-1B-IT coding model and chat with it.
|
| 5 |
+
========================================
|
| 6 |
+
"""
|
| 7 |
+
|
| 8 |
+
import torch
|
| 9 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 10 |
+
|
| 11 |
+
# Change this to your trained model path or Hub ID
|
| 12 |
+
MODEL_PATH = "./gemma-code-agent-merged"
|
| 13 |
+
# MODEL_PATH = "YOUR_USERNAME/gemma-3-1b-code-agent" # if pushed to Hub
|
| 14 |
+
|
| 15 |
+
def load_model(path: str):
|
| 16 |
+
"""Load the fine-tuned coding agent model."""
|
| 17 |
+
print(f"Loading model from: {path}")
|
| 18 |
+
tokenizer = AutoTokenizer.from_pretrained(path, trust_remote_code=True)
|
| 19 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 20 |
+
path,
|
| 21 |
+
torch_dtype=torch.bfloat16,
|
| 22 |
+
device_map="auto",
|
| 23 |
+
trust_remote_code=True,
|
| 24 |
+
)
|
| 25 |
+
if tokenizer.pad_token is None:
|
| 26 |
+
tokenizer.pad_token = tokenizer.eos_token
|
| 27 |
+
return model, tokenizer
|
| 28 |
+
|
| 29 |
+
def chat(model, tokenizer, prompt: str, max_new_tokens: int = 512, temperature: float = 0.7) -> str:
|
| 30 |
+
"""Generate a response for a coding prompt."""
|
| 31 |
+
messages = [{"role": "user", "content": prompt}]
|
| 32 |
+
|
| 33 |
+
inputs = tokenizer.apply_chat_template(
|
| 34 |
+
messages,
|
| 35 |
+
tokenize=True,
|
| 36 |
+
return_tensors="pt",
|
| 37 |
+
add_generation_prompt=True,
|
| 38 |
+
return_dict=True,
|
| 39 |
+
).to(model.device)
|
| 40 |
+
|
| 41 |
+
with torch.no_grad():
|
| 42 |
+
outputs = model.generate(
|
| 43 |
+
**inputs,
|
| 44 |
+
max_new_tokens=max_new_tokens,
|
| 45 |
+
do_sample=True,
|
| 46 |
+
temperature=temperature,
|
| 47 |
+
top_p=0.95,
|
| 48 |
+
pad_token_id=tokenizer.pad_token_id,
|
| 49 |
+
)
|
| 50 |
+
|
| 51 |
+
response = tokenizer.decode(
|
| 52 |
+
outputs[0][inputs["input_ids"].shape[-1]:],
|
| 53 |
+
skip_special_tokens=True
|
| 54 |
+
)
|
| 55 |
+
return response
|
| 56 |
+
|
| 57 |
+
|
| 58 |
+
def interactive_chat(model, tokenizer):
|
| 59 |
+
"""Run an interactive chat loop."""
|
| 60 |
+
print("\n" + "=" * 60)
|
| 61 |
+
print(" MINI CODING AGENT - Interactive Chat")
|
| 62 |
+
print(" Type 'exit' or 'quit' to stop")
|
| 63 |
+
print("=" * 60 + "\n")
|
| 64 |
+
|
| 65 |
+
while True:
|
| 66 |
+
user_input = input("You: ").strip()
|
| 67 |
+
if user_input.lower() in ("exit", "quit", "q"):
|
| 68 |
+
print("Goodbye!")
|
| 69 |
+
break
|
| 70 |
+
|
| 71 |
+
print("\nAgent: ", end="", flush=True)
|
| 72 |
+
response = chat(model, tokenizer, user_input)
|
| 73 |
+
print(response)
|
| 74 |
+
print("-" * 60)
|
| 75 |
+
|
| 76 |
+
|
| 77 |
+
if __name__ == "__main__":
|
| 78 |
+
model, tokenizer = load_model(MODEL_PATH)
|
| 79 |
+
|
| 80 |
+
# Quick test
|
| 81 |
+
print("\nQuick test:")
|
| 82 |
+
test = "Write a Python function to reverse a string without using built-in reverse methods."
|
| 83 |
+
print(f"You: {test}")
|
| 84 |
+
print(f"\nAgent: {chat(model, tokenizer, test)}")
|
| 85 |
+
|
| 86 |
+
# Interactive mode
|
| 87 |
+
interactive_chat(model, tokenizer)
|