Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,101 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 2 |
+
import torch
|
| 3 |
+
|
| 4 |
+
class CASS:
|
| 5 |
+
def __init__(self, model_name="Alibaba-NLP/Tongyi-DeepResearch-30B-A3B", device=None):
|
| 6 |
+
# Device selection
|
| 7 |
+
self.device = device or ("cuda" if torch.cuda.is_available() else "cpu")
|
| 8 |
+
print(f"Loading model on {self.device}...")
|
| 9 |
+
|
| 10 |
+
# Load model and tokenizer
|
| 11 |
+
self.tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 12 |
+
self.model = AutoModelForCausalLM.from_pretrained(model_name)
|
| 13 |
+
self.model.to(self.device)
|
| 14 |
+
|
| 15 |
+
# Conversation memory
|
| 16 |
+
self.history = []
|
| 17 |
+
self.introduced = False
|
| 18 |
+
self.user_memory = {} # store user info
|
| 19 |
+
self.mood = "friendly" # default style
|
| 20 |
+
|
| 21 |
+
# Cass's persona
|
| 22 |
+
self.persona = (
|
| 23 |
+
"You are Cass, a friendly AI assistant and a cool friend. "
|
| 24 |
+
"You respond casually, helpfully, and with humor. "
|
| 25 |
+
"You adapt your tone depending on conversation mood, and sometimes use emojis or playful expressions."
|
| 26 |
+
)
|
| 27 |
+
|
| 28 |
+
def update_mood(self, user_message):
|
| 29 |
+
# Simple heuristic for mood detection
|
| 30 |
+
if any(word in user_message.lower() for word in ["sad", "unhappy", "bad"]):
|
| 31 |
+
self.mood = "supportive"
|
| 32 |
+
elif any(word in user_message.lower() for word in ["funny", "joke", "lol"]):
|
| 33 |
+
self.mood = "playful"
|
| 34 |
+
else:
|
| 35 |
+
self.mood = "friendly"
|
| 36 |
+
|
| 37 |
+
def chat(self, user_message, max_new_tokens=120, temperature=0.7):
|
| 38 |
+
# Update mood
|
| 39 |
+
self.update_mood(user_message)
|
| 40 |
+
|
| 41 |
+
# On first message, add Cass intro
|
| 42 |
+
if not self.introduced:
|
| 43 |
+
intro_message = (
|
| 44 |
+
"Hi, my name's Cass! I'm your AI assistant and a cool friend. "
|
| 45 |
+
"I love chatting, helping, and making you smile 😊"
|
| 46 |
+
)
|
| 47 |
+
self.history.append({"role": "assistant", "content": intro_message})
|
| 48 |
+
self.introduced = True
|
| 49 |
+
|
| 50 |
+
# Append user message
|
| 51 |
+
self.history.append({"role": "user", "content": user_message})
|
| 52 |
+
|
| 53 |
+
# Build context with persona + memory
|
| 54 |
+
memory_str = " ".join([f"{k}: {v}" for k, v in self.user_memory.items()])
|
| 55 |
+
system_message = f"{self.persona} Current mood: {self.mood}. User memory: {memory_str}"
|
| 56 |
+
|
| 57 |
+
context_messages = [{"role": "system", "content": system_message}] + self.history
|
| 58 |
+
|
| 59 |
+
# Tokenize
|
| 60 |
+
inputs = self.tokenizer.apply_chat_template(
|
| 61 |
+
context_messages,
|
| 62 |
+
add_generation_prompt=True,
|
| 63 |
+
tokenize=True,
|
| 64 |
+
return_dict=True,
|
| 65 |
+
return_tensors="pt"
|
| 66 |
+
).to(self.device)
|
| 67 |
+
|
| 68 |
+
# Generate
|
| 69 |
+
outputs = self.model.generate(
|
| 70 |
+
**inputs,
|
| 71 |
+
max_new_tokens=max_new_tokens,
|
| 72 |
+
temperature=temperature,
|
| 73 |
+
do_sample=True,
|
| 74 |
+
pad_token_id=self.tokenizer.eos_token_id
|
| 75 |
+
)
|
| 76 |
+
|
| 77 |
+
# Decode response
|
| 78 |
+
response = self.tokenizer.decode(outputs[0][inputs["input_ids"].shape[-1]:], skip_special_tokens=True)
|
| 79 |
+
|
| 80 |
+
# Append assistant response
|
| 81 |
+
self.history.append({"role": "assistant", "content": response})
|
| 82 |
+
return response
|
| 83 |
+
|
| 84 |
+
def remember(self, key, value):
|
| 85 |
+
"""Store user info in memory"""
|
| 86 |
+
self.user_memory[key] = value
|
| 87 |
+
print(f"Memory updated: {key} = {value}")
|
| 88 |
+
|
| 89 |
+
def reset_history(self):
|
| 90 |
+
self.history = []
|
| 91 |
+
self.introduced = False
|
| 92 |
+
self.mood = "friendly"
|
| 93 |
+
print("Conversation history cleared.")
|
| 94 |
+
|
| 95 |
+
# Example usage
|
| 96 |
+
cass = CASS()
|
| 97 |
+
print(cass.chat("Hello!")) # Cass intro
|
| 98 |
+
print(cass.chat("Can you tell me a joke?"))
|
| 99 |
+
cass.remember("favorite color", "blue")
|
| 100 |
+
print(cass.chat("Do you remember my favorite color?"))
|
| 101 |
+
print(cass.chat("I'm feeling a bit sad today."))
|