File size: 49,010 Bytes
f5b302e 36fcf07 b5cc3fd 36fcf07 f5b302e deb6f27 f5b302e deb6f27 f5b302e deb6f27 f5b302e deb6f27 f5b302e deb6f27 f5b302e deb6f27 f5b302e deb6f27 f5b302e deb6f27 f5b302e deb6f27 f5b302e deb6f27 f5b302e deb6f27 f5b302e deb6f27 f5b302e deb6f27 f5b302e deb6f27 f5b302e deb6f27 f5b302e deb6f27 f5b302e deb6f27 f5b302e deb6f27 f5b302e deb6f27 f5b302e deb6f27 f5b302e deb6f27 f5b302e deb6f27 f5b302e deb6f27 f5b302e deb6f27 f5b302e deb6f27 f5b302e deb6f27 f5b302e deb6f27 f5b302e b5cc3fd deb6f27 f5b302e deb6f27 f5b302e deb6f27 f5b302e deb6f27 fa1bef5 759b2cf deb6f27 fa1bef5 f5b302e fa1bef5 f5b302e fa1bef5 deb6f27 fa1bef5 deb6f27 fa1bef5 36fcf07 fa1bef5 36fcf07 fa1bef5 759b2cf 36fcf07 fa1bef5 36fcf07 fa1bef5 238c097 36fcf07 238c097 36fcf07 fa1bef5 36fcf07 238c097 36fcf07 238c097 36fcf07 238c097 36fcf07 238c097 36fcf07 238c097 36fcf07 238c097 36fcf07 238c097 36fcf07 238c097 36fcf07 fa1bef5 36fcf07 fa1bef5 759b2cf fa1bef5 36fcf07 c8d94c7 deb6f27 36fcf07 deb6f27 36fcf07 1f87515 36fcf07 deb6f27 f5b302e deb6f27 f5b302e deb6f27 f5b302e deb6f27 f5b302e fa1bef5 f5b302e deb6f27 fa1bef5 c8d94c7 deb6f27 fa1bef5 deb6f27 fa1bef5 deb6f27 fa1bef5 deb6f27 fa1bef5 c8d94c7 fa1bef5 deb6f27 fa1bef5 daa76db f5b302e daa76db deb6f27 daa76db fa1bef5 36fcf07 fa1bef5 daa76db fa1bef5 36fcf07 fa1bef5 daa76db 36fcf07 daa76db fa1bef5 36fcf07 daa76db 36fcf07 fa1bef5 daa76db 36fcf07 daa76db deb6f27 f5b302e deb6f27 36fcf07 1f87515 36fcf07 f5b302e |
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 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 |
import json
import random
import threading
import time
import datetime
import os
from typing import Dict, List, Any, Optional
from sentence_transformers import SentenceTransformer
import numpy as np
from transformers import pipeline
class SocialGraphManager:
"""Manages the social graph and provides context for the AAC system."""
def __init__(self, graph_path: str = "social_graph.json"):
"""Initialize the social graph manager.
Args:
graph_path: Path to the social graph JSON file
"""
self.graph_path = graph_path
self.graph = self._load_graph()
# Initialize sentence transformer for semantic matching
try:
self.sentence_model = SentenceTransformer(
"sentence-transformers/paraphrase-multilingual-MiniLM-L12-v2"
)
self.embeddings_cache = {}
self._initialize_embeddings()
except Exception as e:
self.sentence_model = None
def _load_graph(self) -> Dict[str, Any]:
"""Load the social graph from the JSON file."""
try:
with open(self.graph_path, "r") as f:
return json.load(f)
except Exception:
return {"people": {}, "places": [], "topics": []}
def _initialize_embeddings(self):
"""Initialize embeddings for topics and phrases in the social graph."""
if not self.sentence_model:
return
# Create embeddings for topics
topics = self.graph.get("topics", [])
for topic in topics:
if topic not in self.embeddings_cache:
self.embeddings_cache[topic] = self.sentence_model.encode(topic)
# Create embeddings for common phrases
for person_id, person_data in self.graph.get("people", {}).items():
for phrase in person_data.get("common_phrases", []):
if phrase not in self.embeddings_cache:
self.embeddings_cache[phrase] = self.sentence_model.encode(phrase)
# Create embeddings for common utterances
for category, utterances in self.graph.get("common_utterances", {}).items():
for utterance in utterances:
if utterance not in self.embeddings_cache:
self.embeddings_cache[utterance] = self.sentence_model.encode(
utterance
)
def get_people_list(self) -> List[Dict[str, str]]:
"""Get a list of people from the social graph with their names and roles."""
people = []
for person_id, person_data in self.graph.get("people", {}).items():
people.append(
{
"id": person_id,
"name": person_data.get("name", person_id),
"role": person_data.get("role", ""),
}
)
return people
def get_person_context(self, person_id: str) -> Dict[str, Any]:
"""Get context information for a specific person."""
# Check if the person_id contains a display name (e.g., "Emma (wife)")
# and try to extract the actual ID
if person_id not in self.graph.get("people", {}):
# Try to find the person by name
for pid, pdata in self.graph.get("people", {}).items():
name = pdata.get("name", "")
role = pdata.get("role", "")
if f"{name} ({role})" == person_id:
person_id = pid
break
# If still not found, return empty dict
if person_id not in self.graph.get("people", {}):
return {}
person_data = self.graph["people"][person_id]
return person_data
def get_relevant_phrases(
self, person_id: str, user_input: Optional[str] = None
) -> List[str]:
"""Get relevant phrases for a specific person based on user input."""
if person_id not in self.graph.get("people", {}):
return []
person_data = self.graph["people"][person_id]
phrases = person_data.get("common_phrases", [])
# If no user input, return random phrases
if not user_input or not self.sentence_model:
return random.sample(phrases, min(3, len(phrases)))
# Use semantic search to find relevant phrases
user_embedding = self.sentence_model.encode(user_input)
phrase_scores = []
for phrase in phrases:
if phrase in self.embeddings_cache:
phrase_embedding = self.embeddings_cache[phrase]
else:
phrase_embedding = self.sentence_model.encode(phrase)
self.embeddings_cache[phrase] = phrase_embedding
similarity = np.dot(user_embedding, phrase_embedding) / (
np.linalg.norm(user_embedding) * np.linalg.norm(phrase_embedding)
)
phrase_scores.append((phrase, similarity))
# Sort by similarity score and return top phrases
phrase_scores.sort(key=lambda x: x[1], reverse=True)
return [phrase for phrase, _ in phrase_scores[:3]]
def get_common_utterances(self, category: Optional[str] = None) -> List[str]:
"""Get common utterances from the social graph, optionally filtered by category."""
utterances = []
if "common_utterances" not in self.graph:
return utterances
if category and category in self.graph["common_utterances"]:
return self.graph["common_utterances"][category]
# If no category specified, return a sample from each category
for category_utterances in self.graph["common_utterances"].values():
utterances.extend(
random.sample(category_utterances, min(2, len(category_utterances)))
)
return utterances
def get_conversation_history(
self, person_id: str, max_conversations: int = 2
) -> List[Dict[str, Any]]:
"""Get recent conversation history for a specific person.
Args:
person_id: ID of the person to get conversation history for
max_conversations: Maximum number of recent conversations to return
Returns:
List of conversation history entries, most recent first
"""
if person_id not in self.graph.get("people", {}):
return []
person_data = self.graph["people"][person_id]
conversation_history = person_data.get("conversation_history", [])
# Sort by timestamp (most recent first)
sorted_history = sorted(
conversation_history, key=lambda x: x.get("timestamp", ""), reverse=True
)
# Return the most recent conversations
return sorted_history[:max_conversations]
def add_conversation(self, person_id: str, messages: List[Dict[str, str]]) -> bool:
"""Add a new conversation to a person's history.
Args:
person_id: ID of the person to add conversation for
messages: List of message objects with "speaker" and "text" fields
Returns:
True if successful, False otherwise
"""
if person_id not in self.graph.get("people", {}):
return False
# Create a new conversation entry
import datetime
new_conversation = {
"timestamp": datetime.datetime.now().isoformat(),
"messages": messages,
}
# Add to the person's conversation history
if "conversation_history" not in self.graph["people"][person_id]:
self.graph["people"][person_id]["conversation_history"] = []
self.graph["people"][person_id]["conversation_history"].append(new_conversation)
# Save the updated graph
return self._save_graph()
def _save_graph(self) -> bool:
"""Save the social graph to the JSON file.
Returns:
True if successful, False otherwise
"""
try:
print(f"Saving social graph to {self.graph_path}")
# Check if the file is writable
if os.path.exists(self.graph_path):
if not os.access(self.graph_path, os.W_OK):
print(f"Error: No write permission for {self.graph_path}")
return False
# Save the graph
with open(self.graph_path, "w") as f:
json.dump(self.graph, f, indent=2)
print("Social graph saved successfully")
return True
except Exception as e:
print(f"Error saving social graph: {e}")
import traceback
traceback.print_exc()
return False
def summarize_conversation(self, conversation: Dict[str, Any]) -> str:
"""Generate a summary of a conversation.
Args:
conversation: Conversation entry with timestamp and messages
Returns:
A summary string of the conversation
"""
if not conversation or "messages" not in conversation:
return "No conversation data available"
messages = conversation.get("messages", [])
if not messages:
return "No messages in conversation"
# Extract timestamp and format it
timestamp = conversation.get("timestamp", "")
try:
dt = datetime.datetime.fromisoformat(timestamp)
formatted_date = dt.strftime("%B %d, %Y at %I:%M %p")
except (ValueError, TypeError):
formatted_date = timestamp
# Create a brief summary
topic_keywords = set()
for message in messages:
# Extract potential keywords from messages
text = message.get("text", "").lower()
# Simple keyword extraction - could be improved with NLP
words = [
w
for w in text.split()
if len(w) > 4
and w
not in [
"about",
"would",
"could",
"should",
"their",
"there",
"these",
"those",
"where",
"which",
"today",
"tomorrow",
]
]
topic_keywords.update(words[:3]) # Add up to 3 keywords per message
# Limit to 5 most representative keywords
topic_keywords = list(topic_keywords)[:5]
# Create summary
first_speaker = messages[0].get("speaker", "Unknown") if messages else "Unknown"
message_count = len(messages)
summary = f"Conversation on {formatted_date}: {first_speaker} initiated a {message_count}-message conversation"
if topic_keywords:
summary += f" about {', '.join(topic_keywords)}"
return summary
class SuggestionGenerator:
"""Generates contextual suggestions for the AAC system."""
def __init__(self, model_name: str = "distilgpt2"):
"""Initialize the suggestion generator.
Args:
model_name: Name of the HuggingFace model to use
"""
self.model_name = model_name
self.model_loaded = False
self.generator = None
self.aac_user_info = None
self.loaded_models = {} # Cache for loaded models
# Load AAC user information from social graph
try:
with open("social_graph.json", "r") as f:
social_graph = json.load(f)
self.aac_user_info = social_graph.get("aac_user", {})
except Exception as e:
print(f"Error loading AAC user info from social graph: {e}")
self.aac_user_info = {}
# Try to load the model
self.load_model(model_name)
# Fallback responses if model fails to load or generate
self.fallback_responses = [
"I'm not sure how to respond to that.",
"That's interesting. Tell me more.",
"I'd like to talk about that further.",
"I appreciate you sharing that with me.",
"Could we talk about something else?",
"I need some time to think about that.",
]
def load_model(self, model_name: str) -> bool:
"""Load a model (either Hugging Face model or API-based model).
Args:
model_name: Name of the model to use (HuggingFace model name or API identifier)
Returns:
bool: True if model loaded successfully, False otherwise
"""
self.model_name = model_name
self.model_loaded = False
# Check if model is already loaded in cache
if model_name in self.loaded_models:
print(f"Using cached model: {model_name}")
self.generator = self.loaded_models[model_name]
self.model_loaded = True
return True
# Check if this is a Gemini API model
if model_name.startswith("gemini-api:"):
try:
import os
import google.generativeai as genai
# Get API key from environment
api_key = os.environ.get("GEMINI_API_KEY")
if not api_key:
print("No GEMINI_API_KEY found in environment variables.")
print("Please set the GEMINI_API_KEY environment variable.")
return False
# Configure the Gemini API
genai.configure(api_key=api_key)
# Extract the specific model name after the prefix
gemini_model = model_name.split(":", 1)[1]
print(f"Using Gemini API with model: {gemini_model}")
# Store the model name and API client in the generator
self.generator = {
"type": "gemini-api",
"model": gemini_model,
"client": genai,
}
# Cache the API client
self.loaded_models[model_name] = self.generator
self.model_loaded = True
print(f"Gemini API configured successfully for model: {gemini_model}")
return True
except Exception as e:
print(f"Error configuring Gemini API: {e}")
self.model_loaded = False
return False
# Otherwise, try to load a Hugging Face model
try:
print(f"Loading Hugging Face model: {model_name}")
# Check if this is a gated model that requires authentication
is_gated_model = any(
name in model_name.lower()
for name in ["gemma", "llama", "mistral", "qwen", "phi"]
)
if is_gated_model:
# Try to get token from environment
import os
import torch
import time
from transformers import BitsAndBytesConfig
from requests.exceptions import ConnectionError, Timeout, HTTPError
token = os.environ.get("HUGGING_FACE_HUB_TOKEN") or os.environ.get(
"HF_TOKEN"
)
if token:
print(f"Using token for gated model: {model_name}")
from huggingface_hub import login
login(token=token, add_to_git_credential=False)
# Explicitly pass token to pipeline
from transformers import AutoTokenizer, AutoModelForCausalLM
# Implement retry mechanism for network issues
max_retries = 3
retry_delay = 2 # seconds
for attempt in range(max_retries):
try:
print(
f"Attempt {attempt+1}/{max_retries} to load model: {model_name}"
)
# First try to load just the tokenizer to check connectivity
print(f"Loading tokenizer for {model_name}...")
tokenizer = AutoTokenizer.from_pretrained(
model_name,
token=token,
use_fast=True,
local_files_only=False,
)
print(f"Tokenizer loaded successfully for {model_name}")
# Configure 4-bit quantization to save memory
print("Configuring quantization settings...")
quantization_config = BitsAndBytesConfig(
load_in_4bit=True,
bnb_4bit_compute_dtype=torch.float16,
bnb_4bit_quant_type="nf4",
bnb_4bit_use_double_quant=True,
)
# Load model with quantization
print(f"Loading model {model_name} with quantization...")
model = AutoModelForCausalLM.from_pretrained(
model_name,
token=token,
quantization_config=quantization_config,
device_map="auto",
low_cpu_mem_usage=True,
)
print(
f"Model {model_name} loaded successfully with quantization"
)
# Create pipeline
print("Creating text generation pipeline...")
self.generator = {
"type": "huggingface",
"pipeline": pipeline(
"text-generation",
model=model,
tokenizer=tokenizer,
torch_dtype=torch.float16,
),
}
print("Pipeline created successfully")
# If we got here, loading succeeded
break
except (ConnectionError, Timeout, HTTPError) as network_error:
# Handle network-related errors with retries
print(
f"Network error loading model (attempt {attempt+1}/{max_retries}): {network_error}"
)
if attempt < max_retries - 1:
print(f"Retrying in {retry_delay} seconds...")
time.sleep(retry_delay)
retry_delay *= 2 # Exponential backoff
else:
print(
"Maximum retries reached, falling back to alternative loading method"
)
raise network_error
except (RuntimeError, ValueError, OSError) as e:
# Handle memory errors or other issues
print(
f"Error loading gated model with token (attempt {attempt+1}/{max_retries}): {e}"
)
print(
"This may be due to memory limitations, network issues, or insufficient permissions."
)
if "CUDA out of memory" in str(
e
) or "DefaultCPUAllocator" in str(e):
print(
"Memory error detected. Trying with more aggressive memory optimization..."
)
break # Skip to non-quantized version with CPU offloading
if attempt < max_retries - 1:
print(f"Retrying in {retry_delay} seconds...")
time.sleep(retry_delay)
retry_delay *= 2 # Exponential backoff
else:
print(
"Maximum retries reached, falling back to alternative loading method"
)
# If the loop completed without success, try alternative loading methods
if not hasattr(self, "generator") or self.generator is None:
# Try loading without quantization as fallback
try:
print(
"Trying to load model without quantization (CPU only)..."
)
tokenizer = AutoTokenizer.from_pretrained(
model_name, token=token, use_fast=True
)
model = AutoModelForCausalLM.from_pretrained(
model_name,
token=token,
device_map="cpu",
low_cpu_mem_usage=True,
)
self.generator = {
"type": "huggingface",
"pipeline": pipeline(
"text-generation", model=model, tokenizer=tokenizer
),
}
print(
"Successfully loaded model on CPU without quantization"
)
except Exception as e2:
print(f"Fallback loading also failed: {e2}")
print(
"All loading attempts failed. Please try a different model or check your connection."
)
raise RuntimeError(
f"Failed to load model after multiple attempts: {str(e2)}"
)
else:
print("No Hugging Face token found in environment variables.")
print(
"To use gated models like Gemma, you need to set up a token with the right permissions."
)
print("1. Create a token at https://huggingface.co/settings/tokens")
print(
"2. Make sure to enable 'Access to public gated repositories'"
)
print(
"3. Set it as an environment variable: export HUGGING_FACE_HUB_TOKEN=your_token_here"
)
raise ValueError("Authentication token required for gated model")
else:
# For non-gated models, use the standard pipeline
from transformers import pipeline
self.generator = {
"type": "huggingface",
"pipeline": pipeline("text-generation", model=model_name),
}
# Cache the loaded model
self.loaded_models[model_name] = self.generator
self.model_loaded = True
print(f"Model loaded successfully: {model_name}")
return True
except Exception as e:
print(f"Error loading model: {e}")
self.model_loaded = False
return False
def _clean_small_model_response(self, response: str) -> str:
"""Clean up responses from small models that often repeat instructions or generate nonsense.
Args:
response: The raw response from the model
Returns:
A cleaned response
"""
# If response is too short, return as is
if len(response) < 5:
return response
# Remove common instruction repetitions
patterns_to_remove = [
"I want to respond to what",
"I'll use language appropriate for our relationship",
"I should speak in first person",
"I should use language appropriate",
"I want to respond directly",
"I'll speak as myself",
"I want to initiate a conversation",
"My response should be natural",
"My response to",
"Will's response to",
"Will says to",
]
# Check for and remove these patterns
cleaned_response = response
for pattern in patterns_to_remove:
if pattern in cleaned_response:
# Find the first occurrence and remove everything from there
index = cleaned_response.find(pattern)
if index > 10: # Keep some beginning text if available
cleaned_response = cleaned_response[:index].strip()
else:
# If pattern is at the beginning, remove just that pattern
parts = cleaned_response.split(pattern, 1)
if len(parts) > 1:
cleaned_response = parts[1].strip()
# Remove any lines that are just the name repeated
lines = cleaned_response.split("\n")
cleaned_lines = []
for line in lines:
# Skip lines that are just a name repeated
if line.strip() and not all(
word == line.split()[0] for word in line.split()
):
cleaned_lines.append(line)
cleaned_response = "\n".join(cleaned_lines).strip()
# If we've removed too much, use a fallback
if len(cleaned_response) < 5:
return "I'm not sure what to say about that."
# Limit to first 2 sentences to avoid rambling
sentences = cleaned_response.split(".")
if len(sentences) > 2:
cleaned_response = ".".join(sentences[:2]) + "."
return cleaned_response
def _get_mood_description(self, mood_value: int) -> str:
"""Convert mood value (1-5) to a descriptive string.
Args:
mood_value: Integer from 1-5 representing mood (1=sad, 5=happy)
Returns:
String description of the mood
"""
mood_descriptions = {
1: "I'm feeling quite down and sad today. My responses might be more subdued.",
2: "I'm feeling a bit low today. I might be less enthusiastic than usual.",
3: "I'm feeling okay today - neither particularly happy nor sad.",
4: "I'm feeling pretty good today. I'm in a positive mood.",
5: "I'm feeling really happy and upbeat today! I'm in a great mood.",
}
# Default to neutral if value is out of range
return mood_descriptions.get(mood_value, mood_descriptions[3])
def test_model(self) -> str:
"""Test if the model is working correctly."""
if not self.model_loaded:
return "Model not loaded"
try:
# Create a more explicit test prompt that clearly establishes Will's identity and role
test_prompt = """I am Will, a 38-year-old with MND (Motor Neuron Disease).
I am talking to my 7-year-old son Billy.
Billy just asked me about football.
I want to respond to Billy in a natural, brief way.
My response to Billy:"""
print(f"Testing model with prompt: {test_prompt}")
# Check if we're using the Gemini API or a Hugging Face model
if (
isinstance(self.generator, dict)
and self.generator.get("type") == "gemini-api"
):
try:
# Use Gemini API
genai = self.generator["client"]
model_name = self.generator["model"]
# Create a generative model
model = genai.GenerativeModel(model_name)
# Generate content with timeout
print("Sending test request to Gemini API...")
# Set a timeout for the test
import threading
import time
result = ["No response received yet"]
generation_complete = [False]
def generate_with_timeout():
try:
print("Starting Gemini API test request...")
response = model.generate_content(test_prompt)
print(f"Received response from Gemini API: {response}")
if response and hasattr(response, "text"):
result[0] = response.text
print(f"Extracted text from response: {result[0]}")
else:
result[0] = "No text in Gemini API response"
print("Response object has no text attribute")
generation_complete[0] = True
except Exception as e:
print(f"Error in Gemini test generation: {e}")
result[0] = f"Error: {str(e)}"
generation_complete[0] = True
# Start generation in a separate thread
generation_thread = threading.Thread(target=generate_with_timeout)
generation_thread.daemon = True
generation_thread.start()
# Wait for up to 10 seconds
timeout = 10
start_time = time.time()
while (
not generation_complete[0]
and time.time() - start_time < timeout
):
print(
f"Waiting for Gemini API response... ({int(time.time() - start_time)}s)"
)
time.sleep(1)
if not generation_complete[0]:
print("Gemini API test request timed out")
return "Gemini API test timed out after 10 seconds"
print(f"Test response from Gemini API: {result[0]}")
return f"Gemini API test successful: {result[0]}"
except Exception as e:
print(f"Error testing Gemini API: {e}")
return f"Gemini API test failed: {str(e)}"
elif (
isinstance(self.generator, dict)
and self.generator.get("type") == "huggingface"
):
# Use Hugging Face pipeline
pipeline = self.generator["pipeline"]
response = pipeline(test_prompt, max_new_tokens=30, do_sample=True)
full_text = response[0]["generated_text"]
if len(test_prompt) < len(full_text):
result = full_text[len(test_prompt) :].strip()
# Check if this is a small model that needs cleaning
is_small_model = any(
name in self.model_name.lower()
for name in ["distilgpt2", "gpt2-small", "tiny"]
)
if is_small_model:
result = self._clean_small_model_response(result)
else:
result = "No additional text generated"
print(f"Test response from Hugging Face: {result}")
return f"Hugging Face model test successful: {result}"
else:
# Legacy format (for backward compatibility)
response = self.generator(
test_prompt, max_new_tokens=30, do_sample=True
)
full_text = response[0]["generated_text"]
if len(test_prompt) < len(full_text):
result = full_text[len(test_prompt) :].strip()
# Check if this is a small model that needs cleaning
is_small_model = any(
name in self.model_name.lower()
for name in ["distilgpt2", "gpt2-small", "tiny"]
)
if is_small_model:
result = self._clean_small_model_response(result)
else:
result = "No additional text generated"
print(f"Test response: {result}")
return f"Model test successful: {result}"
except Exception as e:
print(f"Error testing model: {e}")
return f"Model test failed: {str(e)}"
def generate_suggestion(
self,
person_context: Dict[str, Any],
user_input: Optional[str] = None,
max_length: int = 50,
temperature: float = 0.7,
) -> str:
"""Generate a contextually appropriate suggestion.
Args:
person_context: Context information about the person
user_input: Optional user input to consider
max_length: Maximum length of the generated suggestion
temperature: Controls randomness in generation (higher = more random)
Returns:
A generated suggestion string
"""
if not self.model_loaded:
# Use fallback responses if model isn't loaded
import random
print("Model not loaded, using fallback responses")
return random.choice(self.fallback_responses)
# Extract context information
name = person_context.get("name", "")
role = person_context.get("role", "")
topics = person_context.get("topics", [])
context = person_context.get("context", "")
selected_topic = person_context.get("selected_topic", "")
common_phrases = person_context.get("common_phrases", [])
frequency = person_context.get("frequency", "")
mood = person_context.get("mood", 3) # Default to neutral mood (3)
# Get AAC user information
aac_user = self.aac_user_info
# Build enhanced prompt
prompt = f"""I am {aac_user.get('name', 'Will')}, a {aac_user.get('age', 38)}-year-old with MND (Motor Neuron Disease) from {aac_user.get('location', 'Manchester')}.
{aac_user.get('background', '')}
My communication needs: {aac_user.get('communication_needs', '')}
I am talking to {name}, who is my {role}.
About {name}: {context}
We typically talk about: {', '.join(topics)}
We communicate {frequency}.
My current mood: {self._get_mood_description(mood)}
"""
# Add communication style based on relationship
if role in ["wife", "son", "daughter", "mother", "father"]:
prompt += "I communicate with my family in a warm, loving way, sometimes using inside jokes.\n"
elif role in ["doctor", "therapist", "nurse"]:
prompt += "I communicate with healthcare providers in a direct, informative way.\n"
elif role in ["best mate", "friend"]:
prompt += "I communicate with friends casually, often with humor and sometimes swearing.\n"
elif role in ["work colleague", "boss"]:
prompt += (
"I communicate with colleagues professionally but still friendly.\n"
)
# Add topic information if provided
if selected_topic:
prompt += f"\nWe are currently discussing {selected_topic}.\n"
# Add specific context about this topic with this person
if selected_topic == "football" and "Manchester United" in context:
prompt += "We both support Manchester United and often discuss recent matches.\n"
elif selected_topic == "programming" and "software developer" in context:
prompt += "We both work in software development and share technical interests.\n"
elif selected_topic == "family plans" and role in ["wife", "husband"]:
prompt += (
"We make family decisions together, considering my condition.\n"
)
elif selected_topic == "old scout adventures" and role == "best mate":
prompt += "We often reminisce about our Scout camping trips in South East London.\n"
elif selected_topic == "cycling" and "cycling" in context:
prompt += "I miss being able to cycle but enjoy talking about past cycling adventures.\n"
# Add the user's message if provided, or set up for conversation initiation
if user_input:
# If user input is provided, we're responding to something
prompt += f'\n{name} just said to me: "{user_input}"\n'
prompt += f"I want to respond directly to what {name} just said.\n"
else:
# No user input means we're initiating a conversation
if selected_topic:
# If a topic is selected, initiate conversation about that topic
prompt += f"\nI'm about to start a conversation with {name} about {selected_topic}.\n"
# Add specific context about initiating this topic with this person
if selected_topic == "football" and "Manchester United" in context:
prompt += (
"We both support Manchester United and often discuss matches.\n"
)
elif selected_topic == "family" and role in [
"wife",
"husband",
"son",
"daughter",
]:
prompt += (
"I want to check in about our family plans or activities.\n"
)
elif selected_topic == "health" and role in [
"doctor",
"nurse",
"therapist",
]:
prompt += "I want to discuss my health condition or symptoms.\n"
elif selected_topic == "work" and role in ["work colleague", "boss"]:
prompt += "I want to discuss a work-related matter.\n"
prompt += f"I want to initiate a conversation about {selected_topic} in a natural way.\n"
elif common_phrases:
# Use context about our typical conversations if no specific topic
prompt += f"\nI'm about to start a conversation with {name}.\n"
default_message = common_phrases[0]
prompt += f'{name} typically says things like: "{default_message}"\n'
prompt += f"We typically talk about: {', '.join(topics)}\n"
prompt += "I want to initiate a conversation in a natural way based on our relationship.\n"
else:
# Generic conversation starter
prompt += f"\nI'm about to start a conversation with {name}.\n"
prompt += "I want to initiate a conversation in a natural way based on our relationship.\n"
# Add the response prompt with specific guidance
# Check if this is an instruction-tuned model
is_instruction_model = any(
marker in self.model_name.lower()
for marker in ["-it", "instruct", "chat", "phi-3", "phi-2"]
)
# Check if this is a very small model that needs simpler prompts
is_small_model = any(
name in self.model_name.lower()
for name in ["distilgpt2", "gpt2-small", "tiny"]
)
if is_small_model:
# Use a much simpler format for very small models
if user_input:
# Responding to something
prompt += f"""
{name} said: "{user_input}"
Will's response:"""
else:
# Initiating a conversation
if selected_topic:
prompt += f"""
Will starts a conversation with {name} about {selected_topic}.
Will says:"""
else:
prompt += f"""
Will starts a conversation with {name}.
Will says:"""
elif is_instruction_model:
# Use instruction format for instruction-tuned models
if user_input:
# Responding to something
prompt += f"""
<instruction>
I am Will, the person with MND. I need to respond to {name}'s message: "{user_input}"
My response should be natural, brief (1-2 sentences), and directly relevant to what {name} just said.
I should use language appropriate for our relationship.
I should speak in first person as myself (Will).
</instruction>
My response to {name}:"""
else:
# Initiating a conversation
prompt += f"""
<instruction>
I am Will, the person with MND. I need to start a conversation with {name}.
My conversation starter should be natural, brief (1-2 sentences), and appropriate for our relationship.
If a topic was selected, I should focus on that topic.
I should speak in first person as myself (Will).
</instruction>
My conversation starter to {name}:"""
else:
# Use standard format for other models
if user_input:
# Responding to something
prompt += f"""
I am Will, the person with MND. I want to respond to {name}'s message: "{user_input}"
My response should be natural, brief (1-2 sentences), and directly relevant to what {name} just said.
I'll use language appropriate for our relationship and speak as myself (Will).
My response to {name}:"""
else:
# Initiating a conversation
prompt += f"""
I am Will, the person with MND. I want to start a conversation with {name}.
My conversation starter should be natural, brief (1-2 sentences), and appropriate for our relationship.
I'll speak in first person as myself (Will).
My conversation starter to {name}:"""
# Generate suggestion
try:
print(f"Generating suggestion with prompt: {prompt}")
# Check if we're using the Gemini API or a Hugging Face model
if (
isinstance(self.generator, dict)
and self.generator.get("type") == "gemini-api"
):
try:
# Use Gemini API
try:
genai = self.generator["client"]
model_name = self.generator["model"]
# Create a generative model
model = genai.GenerativeModel(model_name)
# Set generation config
generation_config = {
"temperature": temperature,
"top_p": 0.92,
"top_k": 50,
"max_output_tokens": 100,
}
# Generate content with timeout
result = [
"I'm thinking about what to say..."
] # Default response
generation_complete = [False]
def generate_with_gemini():
try:
response = model.generate_content(
prompt, generation_config=generation_config
)
if response and hasattr(response, "text"):
result[0] = response.text.strip()
print(f"Gemini API response: {result[0]}")
else:
print("No response from Gemini API")
generation_complete[0] = True
except Exception as e:
print(f"Error in Gemini generation thread: {e}")
generation_complete[0] = True
# Start generation in a separate thread
generation_thread = threading.Thread(
target=generate_with_gemini
)
generation_thread.daemon = True
generation_thread.start()
# Wait for up to 10 seconds
timeout = 10
start_time = time.time()
while (
not generation_complete[0]
and time.time() - start_time < timeout
):
time.sleep(0.1)
if not generation_complete[0]:
print("Gemini API request timed out")
return "I'm thinking about what to say... (API timeout)"
return result[0]
except Exception as e:
print(f"Error setting up Gemini API: {e}")
return (
"I'm having trouble connecting to the Gemini API right now."
)
except Exception as e:
print(f"Error generating with Gemini API: {e}")
return "Could not generate a suggestion with Gemini API. Please try again."
elif (
isinstance(self.generator, dict)
and self.generator.get("type") == "huggingface"
):
# Use Hugging Face pipeline
pipeline = self.generator["pipeline"]
# Generate with Hugging Face
response = pipeline(
prompt,
max_new_tokens=100, # Generate more tokens to ensure we get a response
temperature=temperature,
do_sample=True,
top_p=0.92,
top_k=50,
truncation=False,
)
# Extract only the generated part, not the prompt
full_text = response[0]["generated_text"]
print(f"Full generated text length: {len(full_text)}")
print(f"Prompt length: {len(prompt)}")
# Make sure we're not trying to slice beyond the text length
if len(prompt) < len(full_text):
result = full_text[len(prompt) :].strip()
# Post-process the result for small models
if is_small_model:
result = self._clean_small_model_response(result)
print(f"Generated response: {result}")
return result
else:
# If the model didn't generate anything beyond the prompt
print("Model didn't generate text beyond prompt")
return "I'm thinking about what to say..."
else:
# Legacy format (for backward compatibility)
response = self.generator(
prompt,
max_new_tokens=100,
temperature=temperature,
do_sample=True,
top_p=0.92,
top_k=50,
truncation=False,
)
# Extract only the generated part, not the prompt
full_text = response[0]["generated_text"]
print(f"Full generated text length: {len(full_text)}")
print(f"Prompt length: {len(prompt)}")
# Make sure we're not trying to slice beyond the text length
if len(prompt) < len(full_text):
result = full_text[len(prompt) :].strip()
# Post-process the result for small models
if is_small_model:
result = self._clean_small_model_response(result)
print(f"Generated response: {result}")
return result
else:
# If the model didn't generate anything beyond the prompt
print("Model didn't generate text beyond prompt")
return "I'm thinking about what to say..."
except Exception as e:
print(f"Error generating suggestion: {e}")
return "Could not generate a suggestion. Please try again."
|