NeuraAI2025's picture
Upload 14 files
78b8357 verified
Raw History Blame Contribute Delete
4.53 kB
"""
main.py — Flask backend for NeuraAI_v500
Author: CHATGPT + Joshua•Dav
"""
from flask import Flask, request, jsonify
from flask_cors import CORS
from bot_engine import BotEngine
from voice_engine import VoiceBot
from mini_games import random_game
from automation import AutomationEngine
import os, json, time
# ----------------------------
# Initialize app, bot, voice, automation
# ----------------------------
app = Flask(__name__)
CORS(app)
# Bot engine handles both premium (OpenAI) and free demo
bot = BotEngine()
# Voice bots
voice_bot_female = VoiceBot(gender="female", rate=160)
voice_bot_male = VoiceBot(gender="male", rate=140)
# Automation engine
automation_engine = AutomationEngine()
# ----------------------------
# Helper: get user_id
# ----------------------------
def get_user_id(req):
return req.args.get("user_id", "local_user")
# ----------------------------
# Helper: log chat messages
# ----------------------------
def log_chat(user_id, role, msg):
path = "chat_logs.json"
logs = {}
if os.path.exists(path):
with open(path, "r") as f:
logs = json.load(f)
if user_id not in logs:
logs[user_id] = []
logs[user_id].append({"role": role, "msg": msg, "timestamp": time.time()})
with open(path, "w") as f:
json.dump(logs, f, indent=2)
# ----------------------------
# Chat endpoint
# ----------------------------
@app.route("/chat", methods=["GET"])
def chat():
user_input = request.args.get("msg", "")
user_id = get_user_id(request)
premium_flag = request.args.get("premium", "false").lower() == "true"
voice_mode = request.args.get("voice", "false").lower() == "true"
# Start session
bot._start_session(user_id=user_id, is_premium=premium_flag)
# Generate response (premium or free demo)
reply = bot.generate_response(user_input, user_id=user_id)
# Log chat
log_chat(user_id, "user", user_input)
log_chat(user_id, "bot", reply)
# Voice output if enabled
if voice_mode:
if premium_flag:
voice_bot_female.speak(reply)
else:
voice_bot_male.speak(reply)
remaining_hours = bot.get_remaining_session_hours(user_id=user_id)
return jsonify({
"reply": reply,
"remaining_hours": remaining_hours
})
# ----------------------------
# Upgrade endpoint
# ----------------------------
@app.route("/upgrade", methods=["POST"])
def upgrade():
user_id = get_user_id(request)
msg = bot.upgrade_to_premium(user_id)
remaining_hours = bot.get_remaining_session_hours(user_id=user_id)
return jsonify({"message": msg, "remaining_hours": remaining_hours})
# ----------------------------
# Mini-game endpoint
# ----------------------------
@app.route("/mini_game", methods=["GET"])
def mini_game():
game = random_game()
return jsonify({"game": game})
# ----------------------------
# Automation endpoint
# ----------------------------
@app.route("/automation", methods=["POST"])
def automation():
task_name = request.json.get("task")
if hasattr(automation_engine, task_name):
func = getattr(automation_engine, task_name)
func()
return jsonify({"message": f"Automation '{task_name}' executed"})
return jsonify({"message": "Task not found"}), 404
# ----------------------------
# Memory store endpoint
# ----------------------------
@app.route("/memory_store", methods=["GET"])
def memory_store():
mem_path = "memory_store.json"
if os.path.exists(mem_path):
with open(mem_path, "r") as f:
memory = json.load(f)
return jsonify(memory)
return jsonify({})
# ----------------------------
# Session info
# ----------------------------
@app.route("/session_info", methods=["GET"])
def session_info():
user_id = get_user_id(request)
return jsonify({
"remaining_hours": bot.get_remaining_session_hours(user_id),
"is_premium": bot.is_premium(user_id)
})
# ----------------------------
# Voice list endpoint
# ----------------------------
@app.route("/voice_list", methods=["GET"])
def voice_list():
voices = {
"female": ["Siri", "Eva", "Clara", "Luna", "Nova", "Mia", "Aria", "Zara", "Lily", "Sophia"],
"male": ["Alex", "John", "Leo", "Ethan", "Max", "Ryan", "Oliver", "Jack", "Noah", "Liam"]
}
return jsonify(voices)
# ----------------------------
# Run the Flask app
# ----------------------------
if __name__ == "__main__":
app.run(host="0.0.0.0", port=5000, debug=True)