Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# =============================
|
| 2 |
+
# app.py for Cass Beta 2 Chat
|
| 3 |
+
# =============================
|
| 4 |
+
|
| 5 |
+
import gradio as gr
|
| 6 |
+
import torch
|
| 7 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 8 |
+
from peft import PeftModel
|
| 9 |
+
|
| 10 |
+
# =============================
|
| 11 |
+
# MODEL SETTINGS
|
| 12 |
+
# =============================
|
| 13 |
+
BASE_MODEL_ID = "ibm-granite/granite-4.0-micro-base"
|
| 14 |
+
PEFT_MODEL_ID = "DSDUDEd/Cass-Beta2.0"
|
| 15 |
+
|
| 16 |
+
print("π Loading base model and PEFT adapter...")
|
| 17 |
+
tokenizer = AutoTokenizer.from_pretrained(BASE_MODEL_ID)
|
| 18 |
+
|
| 19 |
+
# Load base model
|
| 20 |
+
base_model = AutoModelForCausalLM.from_pretrained(BASE_MODEL_ID)
|
| 21 |
+
# Load PEFT model
|
| 22 |
+
model = PeftModel.from_pretrained(base_model, PEFT_MODEL_ID)
|
| 23 |
+
|
| 24 |
+
# Move to device
|
| 25 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 26 |
+
model.to(device)
|
| 27 |
+
model.eval()
|
| 28 |
+
|
| 29 |
+
print(f"β
Model loaded on {device.upper()}")
|
| 30 |
+
|
| 31 |
+
# =============================
|
| 32 |
+
# CHAT HISTORY
|
| 33 |
+
# =============================
|
| 34 |
+
history = []
|
| 35 |
+
|
| 36 |
+
# =============================
|
| 37 |
+
# GENERATION FUNCTION
|
| 38 |
+
# =============================
|
| 39 |
+
def chat(user_input):
|
| 40 |
+
"""Generates AI response given user input"""
|
| 41 |
+
global history
|
| 42 |
+
history.append(("User", user_input))
|
| 43 |
+
|
| 44 |
+
# Prepare input
|
| 45 |
+
inputs = tokenizer(user_input, return_tensors="pt").to(device)
|
| 46 |
+
|
| 47 |
+
# Generate output
|
| 48 |
+
outputs = model.generate(
|
| 49 |
+
**inputs,
|
| 50 |
+
max_new_tokens=150,
|
| 51 |
+
pad_token_id=tokenizer.eos_token_id
|
| 52 |
+
)
|
| 53 |
+
ai_response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 54 |
+
|
| 55 |
+
history.append(("Cass", ai_response))
|
| 56 |
+
|
| 57 |
+
# Format chat
|
| 58 |
+
chat_text = ""
|
| 59 |
+
for role, message in history:
|
| 60 |
+
chat_text += f"{role}: {message}\n\n"
|
| 61 |
+
|
| 62 |
+
return chat_text
|
| 63 |
+
|
| 64 |
+
# =============================
|
| 65 |
+
# GRADIO INTERFACE
|
| 66 |
+
# =============================
|
| 67 |
+
with gr.Blocks() as demo:
|
| 68 |
+
gr.Markdown("## Chat with Cass Beta 2 π€")
|
| 69 |
+
|
| 70 |
+
chatbox = gr.Textbox(label="Your message", placeholder="Type your message here...", lines=2)
|
| 71 |
+
send_button = gr.Button("Send")
|
| 72 |
+
output = gr.Textbox(label="Chat History", interactive=False, lines=20)
|
| 73 |
+
|
| 74 |
+
send_button.click(chat, inputs=chatbox, outputs=output)
|
| 75 |
+
chatbox.submit(chat, inputs=chatbox, outputs=output)
|
| 76 |
+
|
| 77 |
+
# =============================
|
| 78 |
+
# RUN APP
|
| 79 |
+
# =============================
|
| 80 |
+
if __name__ == "__main__":
|
| 81 |
+
demo.launch()
|