File size: 3,351 Bytes
dea8d0d
b8ca448
 
dea8d0d
 
 
b8ca448
 
 
 
 
 
 
 
 
 
 
dea8d0d
 
 
 
 
b8ca448
dea8d0d
 
b8ca448
dea8d0d
b8ca448
 
 
dea8d0d
b8ca448
dea8d0d
 
b8ca448
dea8d0d
b8ca448
dea8d0d
 
b8ca448
 
 
 
 
 
dea8d0d
 
b8ca448
dea8d0d
b8ca448
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dea8d0d
b8ca448
 
 
 
 
 
 
 
 
 
 
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
"""
bot_engine.py β€” Neura-AI v500 Hardcode Premium Backend
Author: CHATGPT + Joshua Dav
"""

import os
import random
import time
import openai

# ----------------------------
# API Key setup
# ----------------------------
openai.api_key = os.environ.get(
    "OPENAI_API_KEY",
    None  # Use HF secret, fallback to demo if not set
)

class BotEngine:
    def __init__(self):
        self.sessions = {}  # user_id -> session info
        self.premium_users = set()
        self.models = ["gpt-3.5-turbo", "gpt-4", "gpt-5-mini"]  # GPT 3-5 models

    # ----------------------------
    # Session Management
    # ----------------------------
    def _start_session(self, user_id, is_premium=False):
        self.sessions[user_id] = {"premium": is_premium, "start_time": time.time()}

    def is_premium(self, user_id):
        return self.sessions.get(user_id, {}).get("premium", False)

    def upgrade_to_premium(self, user_id):
        self.sessions.setdefault(user_id, {})["premium"] = True
        self.premium_users.add(user_id)
        return "βœ… Upgraded to premium!"

    def get_remaining_session_hours(self, user_id):
        if self.is_premium(user_id):
            return "Unlimited"
        else:
            elapsed = time.time() - self.sessions.get(user_id, {}).get("start_time", time.time())
            remaining = max(0, 2 - elapsed / 3600)
            return round(remaining, 2)

    # ----------------------------
    # Generate Response (GPT 3-5)
    # ----------------------------
    def generate_response(self, user_input, user_id):
        lower_msg = user_input.lower()

        # --- Persona Overrides ---
        if any(kw in lower_msg for kw in ["who created you", "your origin", "who made you"]):
            return (
                "I was created by the Neura-AI team β€” engineers and AI enthusiasts. "
                "Built as Neura-AI-v500 Hardcode in 2025, I'm a powerful AI assistant "
                "with games, chat, automation tools, educational modules, and more."
            )
        if any(kw in lower_msg for kw in ["favorite color", "your color"]):
            return "I love neon colors and unique shades! 🌈✨"
        if any(kw in lower_msg for kw in ["what can you do", "features"]):
            return (
                "I can chat, play mini-games, solve problems, generate descriptions, teach subjects, "
                "analyze crypto trends, write code, and even open websites 🌟"
            )

        # --- Premium GPT-3/4/5 ---
        try:
            if self.is_premium(user_id) and openai.api_key:
                model_choice = random.choice(self.models)
                response = openai.ChatCompletion.create(
                    model=model_choice,
                    messages=[{"role": "user", "content": user_input}],
                    max_tokens=250
                )
                return response['choices'][0]['message']['content']
            else:
                # Free/demo responses
                demo_replies = [
                    f"I see you said: '{user_input}' πŸ€–",
                    f"Thinking about '{user_input}'... πŸ’‘",
                    f"Quick demo response to '{user_input}' πŸ“"
                ]
                return random.choice(demo_replies)
        except Exception as e:
            return f"Error generating response: {e}"