Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
@app.route('/')
|
| 11 |
-
def
|
| 12 |
-
|
| 13 |
return render_template('index.html')
|
| 14 |
|
| 15 |
@app.route('/chat', methods=['POST'])
|
| 16 |
def chat():
|
| 17 |
try:
|
| 18 |
data = request.json
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
except Exception as e:
|
| 31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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)
|