File size: 1,212 Bytes
3dcd671
db57385
 
 
3dcd671
db57385
 
 
 
 
 
 
 
 
 
 
 
3dcd671
 
db57385
 
 
 
 
 
 
3dcd671
db57385
 
 
3dcd671
db57385
 
 
3dcd671
db57385
 
3dcd671
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import gradio as gr
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer
from peft import PeftModel

# 1. Load the Base Model and your Adapters
model_id = "Qwen/Qwen2.5-Coder-7B-Instruct" # Base model
adapter_id = "SALEETAI/coding-agent-qwen-sft" # Your trained adapters

print("Loading model... this may take a few minutes on CPU.")
tokenizer = AutoTokenizer.from_pretrained(model_id)

# Load base model in 8-bit to save RAM (CPU friendly)
base_model = AutoModelForCausalLM.from_pretrained(
    model_id,
    torch_dtype=torch.float32,
    device_map="cpu"
)

# Merge your trained SFT weights
model = PeftModel.from_pretrained(base_model, adapter_id)
print("Model Loaded!")

def chat(message, history):
    # Prepare the prompt
    inputs = tokenizer(message, return_tensors="pt").to("cpu")
    
    # Generate
    with torch.no_grad():
        outputs = model.generate(**inputs, max_new_tokens=200)
    
    response = tokenizer.decode(outputs[0], skip_special_tokens=True)
    # Remove the prompt from the response
    return response.replace(message, "").strip()

# Build UI
demo = gr.ChatInterface(fn=chat, title="Coding Agent (CPU Mode)")

if __name__ == "__main__":
    demo.launch()