CyberCoder225 commited on
Commit
5f02ff5
·
verified ·
1 Parent(s): 98a3cf8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -15
app.py CHANGED
@@ -1,3 +1,5 @@
 
 
1
  from flask import Flask, request, jsonify, render_template
2
  from flask_cors import CORS
3
  from brain import MairaBrain
@@ -5,27 +7,57 @@ from brain import MairaBrain
5
  app = Flask(__name__)
6
  CORS(app)
7
 
8
- # Initialize models here... (keep your current initialization code)
 
 
 
 
 
 
 
 
 
 
 
 
 
9
 
10
  @app.route('/')
11
- def index():
12
- # Looks for /templates/index.html
13
  return render_template('index.html')
14
 
15
  @app.route('/chat', methods=['POST'])
16
  def chat():
17
  try:
18
  data = request.json
19
- msg = data.get("message")
20
- m_type = data.get("model_type", "small")
21
-
22
- # Select brain based on user's manual choice
23
- if m_type == "qwen": answer = maira_logic.get_response("boss", msg)
24
- elif m_type == "granite": answer = maira_art.get_response("boss", msg)
25
- elif m_type == "medium": answer = maira_prime.get_response("boss", msg)
26
- elif m_type == "danube": answer = maira_chat.get_response("boss", msg)
27
- else: answer = maira_lite.get_response("boss", msg)
28
-
29
- return jsonify({"maira": answer})
 
 
 
 
 
 
 
 
 
 
30
  except Exception as e:
31
- return jsonify({"error": str(e)}), 500
 
 
 
 
 
 
 
 
1
+ import os
2
+ import gc
3
  from flask import Flask, request, jsonify, render_template
4
  from flask_cors import CORS
5
  from brain import MairaBrain
 
7
  app = Flask(__name__)
8
  CORS(app)
9
 
10
+ # --- Configuration for the 5 Neural Cores ---
11
+ # These match the filenames downloaded in your Dockerfile
12
+ MODELS = {
13
+ "small": {"repo": "CyberCoder225/maira-model", "file": "SmolLM2-360M-Instruct.Q4_K_M.gguf"},
14
+ "medium": {"repo": "bartowski/Llama-3.2-1B-Instruct-GGUF", "file": "Llama-3.2-1B-Instruct-Q4_K_M.gguf"},
15
+ "qwen": {"repo": "Qwen/Qwen2.5-1.5B-Instruct-GGUF", "file": "qwen2.5-1.5b-instruct-q4_k_m.gguf"},
16
+ "danube": {"repo": "h2oai/h2o-danube3-500m-chat-GGUF", "file": "h2o-danube3-500m-chat-Q4_K_M.gguf"},
17
+ "granite": {"repo": "bartowski/granite-3.0-2b-instruct-GGUF", "file": "granite-3.0-2b-instruct-Q4_K_M.gguf"}
18
+ }
19
+
20
+ # --- Initialize Neural Cores (Lazy State) ---
21
+ print("🌌 Initializing Neural Core Registry...")
22
+ cores = {name: MairaBrain(cfg["repo"], cfg["file"]) for name, cfg in MODELS.items()}
23
+ print("✅ Registry Ready. Standby for user input.")
24
 
25
  @app.route('/')
26
+ def home():
27
+ """Serves the Chat UI from /templates/index.html"""
28
  return render_template('index.html')
29
 
30
  @app.route('/chat', methods=['POST'])
31
  def chat():
32
  try:
33
  data = request.json
34
+ user_input = data.get("message", "")
35
+ model_type = data.get("model_type", "small")
36
+ user_id = data.get("user_id", "CyberCoder225")
37
+
38
+ # --- RAM PROTECTION: UNLOAD OTHER CORES ---
39
+ # Hugging Face Free Tier has 16GB. We only keep ONE core in RAM at a time.
40
+ for name, core in cores.items():
41
+ if name != model_type:
42
+ core.unload() # Ensure brain.py has this method!
43
+
44
+ # --- GET RESPONSE ---
45
+ if model_type in cores:
46
+ active_core = cores[model_type]
47
+ answer = active_core.get_response(user_id, user_input)
48
+ return jsonify({
49
+ "maira": answer,
50
+ "metadata": {"core": model_type, "status": "active"}
51
+ })
52
+ else:
53
+ return jsonify({"error": "Invalid neural core selection"}), 400
54
+
55
  except Exception as e:
56
+ print(f"❌ CRITICAL ERROR: {str(e)}")
57
+ return jsonify({"error": "Neural Core Failure", "details": str(e)}), 500
58
+
59
+ # --- START SERVER ---
60
+ if __name__ == "__main__":
61
+ # PYTHONUNBUFFERED=1 in Dockerfile ensures these logs show up instantly
62
+ print("🚀 Maira Quintessence starting on port 7860...")
63
+ app.run(host="0.0.0.0", port=7860, debug=False)