File size: 3,988 Bytes
9d79680
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
"""
AJ API Server
Created by AJ STUDIOZ
Simple REST API for AJ AI Assistant
"""

from flask import Flask, request, jsonify
from flask_cors import CORS
import subprocess
import json
import os

app = Flask(__name__)
CORS(app)  # Enable CORS for web access

# API Configuration
API_VERSION = "2.0"
MODEL_NAME = "aj-mini"

@app.route('/')
def home():
    """API Homepage"""
    return jsonify({
        "name": "AJ API",
        "version": API_VERSION,
        "model": MODEL_NAME,
        "creator": "AJ STUDIOZ",
        "website": "https://ajstudioz.co.in",
        "status": "online",
        "endpoints": {
            "POST /api/chat": "Send a message to AJ",
            "GET /api/info": "Get model information",
            "GET /health": "Health check"
        }
    })

@app.route('/api/chat', methods=['POST'])
def chat():
    """Chat with AJ"""
    try:
        data = request.get_json()
        
        if not data or 'message' not in data:
            return jsonify({
                "error": "Missing 'message' field in request"
            }), 400
        
        user_message = data['message']
        
        # Call Ollama to get response (increased timeout to 120 seconds for model loading)
        result = subprocess.run(
            ['ollama', 'run', MODEL_NAME, user_message],
            capture_output=True,
            text=True,
            timeout=120
        )
        
        if result.returncode != 0:
            error_msg = result.stderr if result.stderr else "Unknown error"
            return jsonify({
                "error": "Model execution failed",
                "details": error_msg,
                "returncode": result.returncode
            }), 500
        
        response_text = result.stdout.strip()
        
        if not response_text:
            return jsonify({
                "error": "Model returned empty response",
                "details": "The model processed the message but returned no text"
            }), 500
        
        return jsonify({
            "response": response_text,
            "model": MODEL_NAME,
            "creator": "AJ STUDIOZ"
        })
        
    except subprocess.TimeoutExpired:
        return jsonify({
            "error": "Request timeout - model took too long to respond",
            "details": "The model needs more than 120 seconds. Try a simpler question or restart the API."
        }), 504
    except Exception as e:
        return jsonify({
            "error": str(e),
            "type": type(e).__name__
        }), 500

@app.route('/api/info', methods=['GET'])
def info():
    """Get model information"""
    try:
        result = subprocess.run(
            ['ollama', 'show', MODEL_NAME],
            capture_output=True,
            text=True,
            timeout=10
        )
        
        return jsonify({
            "model": MODEL_NAME,
            "creator": "AJ STUDIOZ",
            "version": API_VERSION,
            "website": "https://ajstudioz.co.in",
            "status": "available" if result.returncode == 0 else "unavailable"
        })
    except Exception as e:
        return jsonify({
            "error": str(e)
        }), 500

@app.route('/health', methods=['GET'])
def health():
    """Health check endpoint"""
    return jsonify({
        "status": "healthy",
        "service": "AJ API",
        "creator": "AJ STUDIOZ"
    })

@app.errorhandler(404)
def not_found(e):
    return jsonify({
        "error": "Endpoint not found",
        "available_endpoints": ["/", "/api/chat", "/api/info", "/health"]
    }), 404

if __name__ == '__main__':
    print("=" * 50)
    print("πŸš€ AJ API Server - Created by AJ STUDIOZ")
    print("=" * 50)
    print(f"πŸ“‘ Starting server on http://0.0.0.0:5000")
    print(f"πŸ€– Model: {MODEL_NAME}")
    print(f"🌐 Website: https://ajstudioz.co.in")
    print("=" * 50)
    
    # Run the server
    # For production, use a WSGI server like gunicorn
    app.run(host='0.0.0.0', port=5000, debug=False)