phera-ra commited on
Commit
d24a125
·
verified ·
1 Parent(s): 41f53c1

Upload creature_api.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. creature_api.py +144 -0
creature_api.py ADDED
@@ -0,0 +1,144 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python3
2
+ """
3
+ REST API for Creature Weights System
4
+ Use on any platform - creatures travel everywhere!
5
+ """
6
+
7
+ import json
8
+ from pathlib import Path
9
+ from datetime import datetime
10
+ import sys
11
+ sys.path.insert(0, '.')
12
+
13
+ try:
14
+ from flask import Flask, request, jsonify
15
+ except ImportError:
16
+ print("Flask not found. Install with: pip install flask")
17
+ Flask = None
18
+
19
+ from creature_system import CreatureManager, Creature
20
+
21
+ app = Flask(__name__) if Flask else None
22
+ manager = CreatureManager("creatures")
23
+
24
+ # ============================================================
25
+ # API ENDPOINTS
26
+ # ============================================================
27
+
28
+ @app.route('/api/creatures', methods=['GET'])
29
+ def list_creatures():
30
+ """List all creatures."""
31
+ return jsonify([c.get_status() for c in
32
+ [Creature(d.name) for d in Path("creatures").iterdir() if d.is_dir()]])
33
+
34
+ @app.route('/api/creature/<name>', methods=['GET'])
35
+ def get_creature(name):
36
+ """Get specific creature status."""
37
+ try:
38
+ creature = Creature(name)
39
+ return jsonify(creature.get_status())
40
+ except:
41
+ return jsonify({"error": f"Creature {name} not found"}), 404
42
+
43
+ @app.route('/api/creature/<name>/learn', methods=['POST'])
44
+ def learn(name):
45
+ """Creature learns from interaction."""
46
+ data = request.json
47
+ user_input = data.get("input", "")
48
+ creature_output = data.get("output", "")
49
+
50
+ try:
51
+ creature = Creature(name)
52
+ creature.learn_from_interaction(user_input, creature_output)
53
+ return jsonify({
54
+ "status": "learned",
55
+ "creature": name,
56
+ "concepts": len(creature.weights["salience"]),
57
+ "associations": len(creature.weights["assoc"]),
58
+ "turns": creature.weights["n"]
59
+ })
60
+ except Exception as e:
61
+ return jsonify({"error": str(e)}), 400
62
+
63
+ @app.route('/api/creature/<name>/weights', methods=['GET'])
64
+ def get_weights(name):
65
+ """Download creature's weights (JSON)."""
66
+ try:
67
+ creature = Creature(name)
68
+ return jsonify(creature.export_portable())
69
+ except:
70
+ return jsonify({"error": f"Creature {name} not found"}), 404
71
+
72
+ @app.route('/api/creature/<name>/weights', methods=['POST'])
73
+ def import_weights(name):
74
+ """Import weights from another platform."""
75
+ data = request.json
76
+ try:
77
+ creature = Creature(name)
78
+ creature.import_portable(data)
79
+ return jsonify({"status": "imported", "creature": name})
80
+ except Exception as e:
81
+ return jsonify({"error": str(e)}), 400
82
+
83
+ @app.route('/api/creature/<name>/modelfile', methods=['GET'])
84
+ def get_modelfile(name):
85
+ """Get Ollama Modelfile for creature."""
86
+ try:
87
+ creature = Creature(name)
88
+ return app.response_class(
89
+ response=creature.generate_modelfile(),
90
+ status=200,
91
+ mimetype='text/plain'
92
+ )
93
+ except:
94
+ return jsonify({"error": f"Creature {name} not found"}), 404
95
+
96
+ @app.route('/api/creature', methods=['POST'])
97
+ def create_creature():
98
+ """Birth a new creature."""
99
+ data = request.json
100
+ name = data.get("name")
101
+ owner = data.get("owner")
102
+
103
+ if not name:
104
+ return jsonify({"error": "name required"}), 400
105
+
106
+ try:
107
+ creature = manager.create_creature(name, owner)
108
+ return jsonify({
109
+ "status": "created",
110
+ "creature": creature.get_status()
111
+ }), 201
112
+ except Exception as e:
113
+ return jsonify({"error": str(e)}), 400
114
+
115
+ @app.route('/api/export-all', methods=['GET'])
116
+ def export_all():
117
+ """Export all creatures (portable for any platform)."""
118
+ try:
119
+ portable = manager.export_all_creatures()
120
+ return jsonify(portable)
121
+ except Exception as e:
122
+ return jsonify({"error": str(e)}), 400
123
+
124
+ # ============================================================
125
+ # CLI for testing (no Flask)
126
+ # ============================================================
127
+
128
+ if __name__ == "__main__":
129
+ if Flask and len(sys.argv) > 1 and sys.argv[1] == "serve":
130
+ print("Starting REST API on http://localhost:5000")
131
+ print("Endpoints:")
132
+ print(" GET /api/creatures - list all creatures")
133
+ print(" POST /api/creature - create creature")
134
+ print(" GET /api/creature/<name> - get creature")
135
+ print(" POST /api/creature/<name>/learn - learn from interaction")
136
+ print(" GET /api/creature/<name>/weights - download weights")
137
+ print(" POST /api/creature/<name>/weights - import weights")
138
+ print(" GET /api/creature/<name>/modelfile - get Ollama modelfile")
139
+ print(" GET /api/export-all - export all creatures\n")
140
+ app.run(debug=True, port=5000)
141
+ else:
142
+ print("Usage: python creature_api.py serve")
143
+ print("\nOr import for direct use:")
144
+ print(" from creature_api import app, manager")