Travellers commited on
Commit
2b4b2a9
·
verified ·
1 Parent(s): 7b9c075

Upload model.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. model.py +110 -0
model.py ADDED
@@ -0,0 +1,110 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Xe AI Model Loader for Hugging Face
3
+ Updated: July 21, 2026
4
+ """
5
+ import json
6
+ import numpy as np
7
+ from pathlib import Path
8
+ from huggingface_hub import hf_hub_download, list_repo_files
9
+
10
+
11
+ class XeModel:
12
+ """Load and use the Xe AI model from Hugging Face."""
13
+
14
+ def __init__(self, repo_id=None, local_path=None):
15
+ """
16
+ Initialize Xe model.
17
+
18
+ Args:
19
+ repo_id: Hugging Face repo ID (e.g., "username/xe-ai")
20
+ local_path: Local path to model files (alternative to repo_id)
21
+ """
22
+ self.repo_id = repo_id
23
+ self.local_path = Path(local_path) if local_path else None
24
+ self.model_config = None
25
+ self.brain_data = None
26
+
27
+ self._load_model()
28
+ self._load_brain()
29
+
30
+ def _load_model(self):
31
+ """Load the neural network model."""
32
+ if self.repo_id:
33
+ model_path = hf_hub_download(repo_id=self.repo_id, filename="xe_model.json")
34
+ elif self.local_path:
35
+ model_path = str(self.local_path / "xe_model.json")
36
+ else:
37
+ raise ValueError("Either repo_id or local_path must be provided")
38
+
39
+ with open(model_path, 'r') as f:
40
+ self.model_config = json.load(f)
41
+
42
+ self.layer_dims = self.model_config["layer_dims"]
43
+ self.learning_rate = self.model_config["learning_rate"]
44
+ self.weights = [np.array(w) for w in self.model_config["layers"]]
45
+
46
+ def _load_brain(self):
47
+ """Load the conversational brain data."""
48
+ if self.repo_id:
49
+ brain_path = hf_hub_download(repo_id=self.repo_id, filename="xe_brain.json")
50
+ elif self.local_path:
51
+ brain_path = str(self.local_path / "xe_brain.json")
52
+ else:
53
+ raise ValueError("Either repo_id or local_path must be provided")
54
+
55
+ with open(brain_path, 'r') as f:
56
+ self.brain_data = json.load(f)
57
+
58
+ def predict(self, x):
59
+ """
60
+ Make a prediction using the neural network.
61
+
62
+ Args:
63
+ x: Input array of shape (batch_size, input_dim)
64
+
65
+ Returns:
66
+ Output array of shape (batch_size, output_dim)
67
+ """
68
+ x = np.array(x, dtype=np.float32)
69
+ for i, layer in enumerate(self.weights):
70
+ x = np.dot(x, layer['weights']) + layer['bias']
71
+ if i < len(self.weights) - 1:
72
+ x = np.maximum(0, x)
73
+ return x
74
+
75
+ def get_knowledge(self):
76
+ """Get the knowledge base from brain data."""
77
+ return self.brain_data.get("knowledge", {})
78
+
79
+ def get_learned_responses(self):
80
+ """Get the learned responses."""
81
+ return self.brain_data.get("learned_responses", {})
82
+
83
+ def get_conversations(self):
84
+ """Get conversation history."""
85
+ return self.brain_data.get("conversations", [])
86
+
87
+
88
+ def load_model(repo_id, token=None):
89
+ """
90
+ Convenience function to load Xe model from Hugging Face.
91
+
92
+ Args:
93
+ repo_id: Hugging Face repository ID
94
+ token: Hugging Face token (optional, for private repos)
95
+
96
+ Returns:
97
+ XeModel instance
98
+ """
99
+ return XeModel(repo_id=repo_id)
100
+
101
+
102
+ if __name__ == "__main__":
103
+ import sys
104
+ if len(sys.argv) > 1:
105
+ model = XeModel(repo_id=sys.argv[1])
106
+ print(f"Loaded Xe model from {sys.argv[1]}")
107
+ print(f"Layer dimensions: {model.layer_dims}")
108
+ print(f"Knowledge keys: {list(model.get_knowledge().keys())}")
109
+ else:
110
+ print("Usage: python model.py <repo_id>")