Spaces:
Paused
Paused
Upload jairvis.py
Browse files- models/jairvis.py +47 -0
models/jairvis.py
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 2 |
+
import torch
|
| 3 |
+
from utils.self_awareness import SelfAwareness
|
| 4 |
+
from config import MODEL_NAME, DEVICE
|
| 5 |
+
|
| 6 |
+
class JAIRVISMKV:
|
| 7 |
+
def __init__(self):
|
| 8 |
+
self.model = AutoModelForCausalLM.from_pretrained(MODEL_NAME).to(DEVICE)
|
| 9 |
+
self.tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
| 10 |
+
self.self_awareness = SelfAwareness(self)
|
| 11 |
+
self.conversation_context = []
|
| 12 |
+
self.max_context_length = 1024
|
| 13 |
+
|
| 14 |
+
def generate_response(self, prompt):
|
| 15 |
+
self.conversation_context.append(f"Human: {prompt}")
|
| 16 |
+
context = "\n".join(self.conversation_context[-5:])
|
| 17 |
+
|
| 18 |
+
inputs = self.tokenizer.encode(context + "\nAI:", return_tensors="pt").to(DEVICE)
|
| 19 |
+
attention_mask = torch.ones(inputs.shape, dtype=torch.long, device=DEVICE)
|
| 20 |
+
|
| 21 |
+
outputs = self.model.generate(
|
| 22 |
+
inputs,
|
| 23 |
+
attention_mask=attention_mask,
|
| 24 |
+
max_length=self.max_context_length,
|
| 25 |
+
num_return_sequences=1,
|
| 26 |
+
no_repeat_ngram_size=2,
|
| 27 |
+
top_k=50,
|
| 28 |
+
top_p=0.95,
|
| 29 |
+
temperature=0.7
|
| 30 |
+
)
|
| 31 |
+
|
| 32 |
+
response = self.tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 33 |
+
ai_response = response.split("AI:")[-1].strip()
|
| 34 |
+
|
| 35 |
+
self.conversation_context.append(f"AI: {ai_response}")
|
| 36 |
+
self.self_awareness.update_self_model()
|
| 37 |
+
|
| 38 |
+
return ai_response
|
| 39 |
+
|
| 40 |
+
def introspect(self):
|
| 41 |
+
return self.self_awareness.generate_insights(str(self))
|
| 42 |
+
|
| 43 |
+
def implement_improvement(self, suggestion):
|
| 44 |
+
return self.self_awareness.implement_improvement(suggestion)
|
| 45 |
+
|
| 46 |
+
def __str__(self):
|
| 47 |
+
return f"JAIRVISMKV Model: {MODEL_NAME}, Device: {DEVICE}"
|