File size: 2,230 Bytes
9bb3cc5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# =============================
# app.py for Cass Beta 2 Chat
# =============================

import gradio as gr
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer
from peft import PeftModel

# =============================
# MODEL SETTINGS
# =============================
BASE_MODEL_ID = "ibm-granite/granite-4.0-micro-base"
PEFT_MODEL_ID = "DSDUDEd/Cass-Beta2.0"

print("πŸš€ Loading base model and PEFT adapter...")
tokenizer = AutoTokenizer.from_pretrained(BASE_MODEL_ID)

# Load base model
base_model = AutoModelForCausalLM.from_pretrained(BASE_MODEL_ID)
# Load PEFT model
model = PeftModel.from_pretrained(base_model, PEFT_MODEL_ID)

# Move to device
device = "cuda" if torch.cuda.is_available() else "cpu"
model.to(device)
model.eval()

print(f"βœ… Model loaded on {device.upper()}")

# =============================
# CHAT HISTORY
# =============================
history = []

# =============================
# GENERATION FUNCTION
# =============================
def chat(user_input):
    """Generates AI response given user input"""
    global history
    history.append(("User", user_input))

    # Prepare input
    inputs = tokenizer(user_input, return_tensors="pt").to(device)

    # Generate output
    outputs = model.generate(
        **inputs,
        max_new_tokens=150,
        pad_token_id=tokenizer.eos_token_id
    )
    ai_response = tokenizer.decode(outputs[0], skip_special_tokens=True)

    history.append(("Cass", ai_response))

    # Format chat
    chat_text = ""
    for role, message in history:
        chat_text += f"{role}: {message}\n\n"

    return chat_text

# =============================
# GRADIO INTERFACE
# =============================
with gr.Blocks() as demo:
    gr.Markdown("## Chat with Cass Beta 2 πŸ€–")
    
    chatbox = gr.Textbox(label="Your message", placeholder="Type your message here...", lines=2)
    send_button = gr.Button("Send")
    output = gr.Textbox(label="Chat History", interactive=False, lines=20)

    send_button.click(chat, inputs=chatbox, outputs=output)
    chatbox.submit(chat, inputs=chatbox, outputs=output)

# =============================
# RUN APP
# =============================
if __name__ == "__main__":
    demo.launch()