Update app.py
Browse files
app.py
CHANGED
|
@@ -5,20 +5,33 @@
|
|
| 5 |
# No Gradio interface - Pure backend
|
| 6 |
# =====================
|
| 7 |
|
| 8 |
-
|
| 9 |
-
|
| 10 |
import torch
|
| 11 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 12 |
from sentence_transformers import SentenceTransformer
|
| 13 |
import faiss
|
| 14 |
import numpy as np
|
| 15 |
-
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
print("π Initializing Simba AI Backend API...")
|
| 19 |
|
| 20 |
-
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
# =====================
|
| 24 |
# LOAD AI MODEL
|
|
@@ -208,80 +221,199 @@ def generate_simba_response(message):
|
|
| 208 |
}
|
| 209 |
|
| 210 |
# =====================
|
| 211 |
-
# API ROUTES
|
| 212 |
# =====================
|
| 213 |
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
"
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
|
|
|
| 225 |
|
| 226 |
-
@app.route('/api/health')
|
| 227 |
-
def health_check():
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
|
| 233 |
|
| 234 |
-
@app.route('/api/info')
|
| 235 |
-
def api_info():
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
|
| 246 |
-
|
| 247 |
-
|
| 248 |
|
| 249 |
-
@app.route('/api/chat', methods=['POST'])
|
| 250 |
-
def chat():
|
| 251 |
-
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 255 |
return jsonify({
|
| 256 |
-
"
|
| 257 |
-
|
| 258 |
-
|
| 259 |
-
|
| 260 |
-
|
| 261 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 262 |
return jsonify({
|
| 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 |
# LAUNCH
|
|
@@ -293,5 +425,12 @@ if __name__ == '__main__':
|
|
| 293 |
print(" GET /api/health - Health check")
|
| 294 |
print(" GET /api/info - API information")
|
| 295 |
print(" POST /api/chat - Chat with Simba AI")
|
| 296 |
-
|
| 297 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
# No Gradio interface - Pure backend
|
| 6 |
# =====================
|
| 7 |
|
| 8 |
+
import os
|
| 9 |
+
import time
|
| 10 |
import torch
|
| 11 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 12 |
from sentence_transformers import SentenceTransformer
|
| 13 |
import faiss
|
| 14 |
import numpy as np
|
| 15 |
+
|
| 16 |
+
# Import Flask with fallback
|
| 17 |
+
try:
|
| 18 |
+
from flask import Flask, request, jsonify
|
| 19 |
+
from flask_cors import CORS
|
| 20 |
+
FLASK_AVAILABLE = True
|
| 21 |
+
except ImportError:
|
| 22 |
+
FLASK_AVAILABLE = False
|
| 23 |
+
print("β Flask not available - using basic HTTP server")
|
| 24 |
|
| 25 |
print("π Initializing Simba AI Backend API...")
|
| 26 |
|
| 27 |
+
# Create Flask app if available
|
| 28 |
+
if FLASK_AVAILABLE:
|
| 29 |
+
app = Flask(__name__)
|
| 30 |
+
CORS(app)
|
| 31 |
+
else:
|
| 32 |
+
# Fallback to basic server
|
| 33 |
+
from http.server import HTTPServer, BaseHTTPRequestHandler
|
| 34 |
+
import json
|
| 35 |
|
| 36 |
# =====================
|
| 37 |
# LOAD AI MODEL
|
|
|
|
| 221 |
}
|
| 222 |
|
| 223 |
# =====================
|
| 224 |
+
# FLASK API ROUTES (If Flask is available)
|
| 225 |
# =====================
|
| 226 |
|
| 227 |
+
if FLASK_AVAILABLE:
|
| 228 |
+
@app.route('/')
|
| 229 |
+
def home():
|
| 230 |
+
return jsonify({
|
| 231 |
+
"message": "π¦ Simba AI Backend API - First African LLM",
|
| 232 |
+
"status": "running",
|
| 233 |
+
"endpoints": {
|
| 234 |
+
"/api/chat": "POST - Chat with Simba AI",
|
| 235 |
+
"/api/health": "GET - Health check",
|
| 236 |
+
"/api/info": "GET - API information"
|
| 237 |
+
}
|
| 238 |
+
})
|
| 239 |
|
| 240 |
+
@app.route('/api/health')
|
| 241 |
+
def health_check():
|
| 242 |
+
return jsonify({
|
| 243 |
+
"status": "healthy",
|
| 244 |
+
"model": model_name,
|
| 245 |
+
"timestamp": time.time()
|
| 246 |
+
})
|
| 247 |
|
| 248 |
+
@app.route('/api/info')
|
| 249 |
+
def api_info():
|
| 250 |
+
return jsonify({
|
| 251 |
+
"name": "Simba AI - First African LLM",
|
| 252 |
+
"version": "1.0",
|
| 253 |
+
"model": model_name,
|
| 254 |
+
"capabilities": [
|
| 255 |
+
"African Languages: Yoruba, Swahili, Igbo, Hausa",
|
| 256 |
+
"Python Coding & Programming",
|
| 257 |
+
"Mathematics & Problem Solving",
|
| 258 |
+
"African Innovation Knowledge"
|
| 259 |
+
],
|
| 260 |
+
"knowledge_base_size": len(simba_knowledge_base)
|
| 261 |
+
})
|
| 262 |
|
| 263 |
+
@app.route('/api/chat', methods=['POST'])
|
| 264 |
+
def chat():
|
| 265 |
+
try:
|
| 266 |
+
data = request.get_json()
|
| 267 |
+
|
| 268 |
+
if not data or 'message' not in data:
|
| 269 |
+
return jsonify({
|
| 270 |
+
"error": "Missing 'message' in request body"
|
| 271 |
+
}), 400
|
| 272 |
+
|
| 273 |
+
user_message = data['message']
|
| 274 |
+
|
| 275 |
+
if not user_message.strip():
|
| 276 |
+
return jsonify({
|
| 277 |
+
"error": "Message cannot be empty"
|
| 278 |
+
}), 400
|
| 279 |
+
|
| 280 |
+
# Generate response
|
| 281 |
+
result = generate_simba_response(user_message)
|
| 282 |
+
|
| 283 |
return jsonify({
|
| 284 |
+
"success": True,
|
| 285 |
+
"user_message": user_message,
|
| 286 |
+
"simba_response": result["response"],
|
| 287 |
+
"response_time": result["response_time"],
|
| 288 |
+
"knowledge_used": result.get("knowledge_used", []),
|
| 289 |
+
"model": result.get("model", model_name),
|
| 290 |
+
"timestamp": time.time()
|
| 291 |
+
})
|
| 292 |
+
|
| 293 |
+
except Exception as e:
|
| 294 |
return jsonify({
|
| 295 |
+
"success": False,
|
| 296 |
+
"error": str(e),
|
| 297 |
+
"timestamp": time.time()
|
| 298 |
+
}), 500
|
| 299 |
+
|
| 300 |
+
# =====================
|
| 301 |
+
# BASIC HTTP SERVER (Fallback if no Flask)
|
| 302 |
+
# =====================
|
| 303 |
+
|
| 304 |
+
if not FLASK_AVAILABLE:
|
| 305 |
+
class SimbaAIHandler(BaseHTTPRequestHandler):
|
| 306 |
+
def do_GET(self):
|
| 307 |
+
if self.path == '/api/health':
|
| 308 |
+
self.send_response(200)
|
| 309 |
+
self.send_header('Content-type', 'application/json')
|
| 310 |
+
self.send_header('Access-Control-Allow-Origin', '*')
|
| 311 |
+
self.end_headers()
|
| 312 |
+
response = json.dumps({
|
| 313 |
+
"status": "healthy",
|
| 314 |
+
"model": model_name,
|
| 315 |
+
"timestamp": time.time()
|
| 316 |
+
})
|
| 317 |
+
self.wfile.write(response.encode())
|
| 318 |
+
|
| 319 |
+
elif self.path == '/api/info':
|
| 320 |
+
self.send_response(200)
|
| 321 |
+
self.send_header('Content-type', 'application/json')
|
| 322 |
+
self.send_header('Access-Control-Allow-Origin', '*')
|
| 323 |
+
self.end_headers()
|
| 324 |
+
response = json.dumps({
|
| 325 |
+
"name": "Simba AI - First African LLM",
|
| 326 |
+
"version": "1.0",
|
| 327 |
+
"model": model_name,
|
| 328 |
+
"capabilities": [
|
| 329 |
+
"African Languages: Yoruba, Swahili, Igbo, Hausa",
|
| 330 |
+
"Python Coding & Programming",
|
| 331 |
+
"Mathematics & Problem Solving",
|
| 332 |
+
"African Innovation Knowledge"
|
| 333 |
+
],
|
| 334 |
+
"knowledge_base_size": len(simba_knowledge_base)
|
| 335 |
+
})
|
| 336 |
+
self.wfile.write(response.encode())
|
| 337 |
+
|
| 338 |
+
else:
|
| 339 |
+
self.send_response(200)
|
| 340 |
+
self.send_header('Content-type', 'application/json')
|
| 341 |
+
self.send_header('Access-Control-Allow-Origin', '*')
|
| 342 |
+
self.end_headers()
|
| 343 |
+
response = json.dumps({
|
| 344 |
+
"message": "π¦ Simba AI Backend API - First African LLM",
|
| 345 |
+
"status": "running",
|
| 346 |
+
"endpoints": {
|
| 347 |
+
"/api/chat": "POST - Chat with Simba AI",
|
| 348 |
+
"/api/health": "GET - Health check",
|
| 349 |
+
"/api/info": "GET - API information"
|
| 350 |
+
}
|
| 351 |
+
})
|
| 352 |
+
self.wfile.write(response.encode())
|
| 353 |
|
| 354 |
+
def do_POST(self):
|
| 355 |
+
if self.path == '/api/chat':
|
| 356 |
+
content_length = int(self.headers['Content-Length'])
|
| 357 |
+
post_data = self.rfile.read(content_length)
|
| 358 |
+
|
| 359 |
+
try:
|
| 360 |
+
data = json.loads(post_data.decode())
|
| 361 |
+
user_message = data.get('message', '')
|
| 362 |
+
|
| 363 |
+
if not user_message.strip():
|
| 364 |
+
self.send_response(400)
|
| 365 |
+
self.send_header('Content-type', 'application/json')
|
| 366 |
+
self.send_header('Access-Control-Allow-Origin', '*')
|
| 367 |
+
self.end_headers()
|
| 368 |
+
response = json.dumps({"error": "Message cannot be empty"})
|
| 369 |
+
self.wfile.write(response.encode())
|
| 370 |
+
return
|
| 371 |
+
|
| 372 |
+
# Generate response
|
| 373 |
+
result = generate_simba_response(user_message)
|
| 374 |
+
|
| 375 |
+
self.send_response(200)
|
| 376 |
+
self.send_header('Content-type', 'application/json')
|
| 377 |
+
self.send_header('Access-Control-Allow-Origin', '*')
|
| 378 |
+
self.end_headers()
|
| 379 |
+
|
| 380 |
+
response = json.dumps({
|
| 381 |
+
"success": True,
|
| 382 |
+
"user_message": user_message,
|
| 383 |
+
"simba_response": result["response"],
|
| 384 |
+
"response_time": result["response_time"],
|
| 385 |
+
"knowledge_used": result.get("knowledge_used", []),
|
| 386 |
+
"model": result.get("model", model_name),
|
| 387 |
+
"timestamp": time.time()
|
| 388 |
+
})
|
| 389 |
+
self.wfile.write(response.encode())
|
| 390 |
+
|
| 391 |
+
except Exception as e:
|
| 392 |
+
self.send_response(500)
|
| 393 |
+
self.send_header('Content-type', 'application/json')
|
| 394 |
+
self.send_header('Access-Control-Allow-Origin', '*')
|
| 395 |
+
self.end_headers()
|
| 396 |
+
response = json.dumps({
|
| 397 |
+
"success": False,
|
| 398 |
+
"error": str(e),
|
| 399 |
+
"timestamp": time.time()
|
| 400 |
+
})
|
| 401 |
+
self.wfile.write(response.encode())
|
| 402 |
+
|
| 403 |
+
else:
|
| 404 |
+
self.send_response(404)
|
| 405 |
+
self.send_header('Content-type', 'application/json')
|
| 406 |
+
self.send_header('Access-Control-Allow-Origin', '*')
|
| 407 |
+
self.end_headers()
|
| 408 |
+
response = json.dumps({"error": "Endpoint not found"})
|
| 409 |
+
self.wfile.write(response.encode())
|
| 410 |
|
| 411 |
+
def do_OPTIONS(self):
|
| 412 |
+
self.send_response(200)
|
| 413 |
+
self.send_header('Access-Control-Allow-Origin', '*')
|
| 414 |
+
self.send_header('Access-Control-Allow-Methods', 'GET, POST, OPTIONS')
|
| 415 |
+
self.send_header('Access-Control-Allow-Headers', 'Content-Type')
|
| 416 |
+
self.end_headers()
|
| 417 |
|
| 418 |
# =====================
|
| 419 |
# LAUNCH
|
|
|
|
| 425 |
print(" GET /api/health - Health check")
|
| 426 |
print(" GET /api/info - API information")
|
| 427 |
print(" POST /api/chat - Chat with Simba AI")
|
| 428 |
+
|
| 429 |
+
if FLASK_AVAILABLE:
|
| 430 |
+
print("π Starting Flask server...")
|
| 431 |
+
app.run(host='0.0.0.0', port=7860, debug=False)
|
| 432 |
+
else:
|
| 433 |
+
print("π Starting basic HTTP server...")
|
| 434 |
+
server = HTTPServer(('0.0.0.0', 7860), SimbaAIHandler)
|
| 435 |
+
print("β
Server running on http://0.0.0.0:7860")
|
| 436 |
+
server.serve_forever()
|