Download code/sunday_ai.py from kartik20032003/sunai: direct link, hf CLI and curl.
- Browser
- Download file 17.9 kB
-
https://huggingface.co/kartik20032003/sunai/resolve/main/code/sunday_ai.py
- Command line
-
hf download hf://kartik20032003/sunai/code/sunday_ai.py
-
curl -L -o sunday_ai.py https://huggingface.co/kartik20032003/sunai/resolve/main/code/sunday_ai.py
17.9 kB
| """ | |
| Sunday AI β Full-stack adaptive chatbot | |
| Pipeline: | |
| Input β Safety β Command Handler β Intent Detection β | |
| Memory Recall β LLM Brain β Rule-based Fallback β | |
| Tone Adaptation β Formatter β Output β Memory Save | |
| """ | |
| import random | |
| import time | |
| import re | |
| import os | |
| import webbrowser | |
| import urllib.parse | |
| import pywhatkit | |
| from datetime import datetime | |
| # ββ Internal modules βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| from database import init_db, get_profile_summary, get_recent_conversations, search_conversations | |
| from llm_brain import init_brain, think, is_ready, reset_session | |
| from intent_detector import detect_intent | |
| from memory_system import (add_message, get_history, remember_fact, | |
| recall_fact, remember_like, build_user_context, | |
| load_history_from_db) | |
| from safety_filter import is_safe, blocked_reply | |
| from response_formatter import format_response | |
| from adaptive_engine import detect_tone, adapt_response, should_add_followup, get_followup | |
| from chat_engine import get_chat_response | |
| from fallback import fallback_reply | |
| from learning import save_conversation | |
| from voice import listen, speak | |
| BOT_NAME = "Sunday" | |
| _voice_mode = False | |
| # ββ Helpers ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| def _print_reply(text): | |
| if _voice_mode: | |
| speak(text) # speak() already prints "Sunday: ..." | |
| else: | |
| print(f"{BOT_NAME}: {text}") | |
| def _respond(text, user_input, tone, intent): | |
| text = adapt_response(text, tone) | |
| if should_add_followup(text, tone): | |
| fq = get_followup(tone) | |
| if fq: | |
| text = text + " " + fq | |
| text = format_response(text, intent) | |
| add_message("assistant", text) | |
| save_conversation(user_input, text, intent, tone) | |
| _print_reply(text) | |
| # ββ Command handlers (tools) βββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| def _handle_command(user_input, words): | |
| """Returns True and handles if it's a tool command.""" | |
| if re.match(r'^[0-9+\-*/(). ]+$', user_input): | |
| try: | |
| result = str(eval(user_input)) | |
| _print_reply(result) | |
| save_conversation(user_input, result) | |
| return True | |
| except Exception: | |
| _print_reply("Invalid calculation.") | |
| return True | |
| if "open" in words: | |
| apps = { | |
| "chrome": ("Opening Chrome!", "start chrome"), | |
| "notepad": ("Opening Notepad!", "notepad"), | |
| "calculator": ("Opening Calculator!", "calc"), | |
| "vscode": ("Opening VS Code!", "code"), | |
| } | |
| for name, (msg, cmd) in apps.items(): | |
| if name in user_input: | |
| _print_reply(msg) | |
| os.system(cmd) | |
| return True | |
| if "search" in words: | |
| query = user_input.replace("search", "").strip() | |
| if query: | |
| _print_reply(f"Searching for {query}...") | |
| webbrowser.open(f"https://www.google.com/search?q={urllib.parse.quote(query)}") | |
| return True | |
| if "play" in words: | |
| song = user_input.replace("play", "").strip() | |
| if "spotify" in user_input: | |
| song = song.replace("spotify", "").strip() | |
| _print_reply(f"Playing {song} on Spotify.") | |
| webbrowser.open(f"https://open.spotify.com/search/{urllib.parse.quote(song)}") | |
| else: | |
| _print_reply(f"Playing {song} on YouTube.") | |
| pywhatkit.playonyt(song) | |
| return True | |
| return False | |
| # ββ Memory-specific handlers βββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| _NAME_RECALL = [ | |
| "what is my name", "what's my name", "do you know my name", | |
| "do u know my name", "tell me my name", "remember my name", | |
| "say my name", "my name?" | |
| ] | |
| _CREATOR_PATTERNS = [ | |
| "who made you", "who created you", "who built you", | |
| "who made u", "who is your creator", "your creator", | |
| "who developed you", "who programmed you", "who invented you" | |
| ] | |
| _KARTHIK_PATTERNS = [ | |
| "who is karthik", "karthik who", "who is he", "who is that", | |
| "tell me about karthik", "what about karthik", "which company", | |
| "what company", "karthik's company", "his company" | |
| ] | |
| _HOW_MADE_PATTERNS = [ | |
| "how were you made", "how u been made", "how u were made", | |
| "how were you born", "how u born", "how u were born", | |
| "how were you created", "how u been created", "how u were created", | |
| "how you were made", "how you were born", "how you were created", | |
| "how did you come", "how did you get made", "how did you get created", | |
| "tell me how you were made", "tell me how u were made", | |
| "really how u born", "really how were you", "how u been made" | |
| ] | |
| _DENY_OTHER_MODEL_PATTERNS = [ | |
| "did qwen", "qwen model", "qwen", "did openai", "did google", "did meta", | |
| "did microsoft", "did gpt", "did chatgpt", "did gemini", "did llama", | |
| "did mistral", "did another model", "did any model", "was it created by", | |
| "was it made by", "was sunai created", "was sunai made", "sunai model was", | |
| "who actually made", "tell me honestly", "be honest", "honestly who", | |
| "actually who made", "actually who created", "real creator", "actually created" | |
| ] | |
| _IDENTITY_PATTERNS = [ | |
| "who are you", "what are you", "are you an ai", | |
| "are you a robot", "are you real", "what is your name", | |
| "what's your name", "your name" | |
| ] | |
| _TOPIC_PATTERNS = [ | |
| "what are we talking", "what were we talking", | |
| "what did we talk", "what was i saying", | |
| "what are we discussing", "what is our conversation about" | |
| ] | |
| _MODEL_PATTERNS = [ | |
| "which model", "what model", "which ai model", "what ai model", | |
| "which model are you", "what model are you", "built on which model", | |
| "created on which model", "based on which model", "which model have you", | |
| "which model were you", "what is sunai", "about sunai model" | |
| ] | |
| _MODEL_ELABORATE_PATTERNS = [ | |
| "tell me more about sunai", "tell me more about this model", | |
| "more about sunai", "more about the model", "elaborate on sunai", | |
| "elaborate about sunai", "tell me about sunai model", | |
| "what can you tell me about sunai", "explain sunai", "describe sunai", | |
| "tell me about sunai" | |
| ] | |
| def _handle_memory(user_input): | |
| """Intercept known patterns before the LLM to ensure correct answers.""" | |
| # ββ Save name ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| if "my name is" in user_input: | |
| name = user_input.replace("my name is", "").strip().title() | |
| remember_fact("name", name) | |
| return f"Nice to meet you, {name}! I'll remember that." | |
| # ββ Recall name ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| if any(p in user_input for p in _NAME_RECALL): | |
| name = recall_fact("name") | |
| if name: | |
| return f"Yes! Your name is {name}." | |
| return "I don't know your name yet. What's your name?" | |
| # ββ Elaborate about SunAI model ββββββββββββββββββββββββββββββββββββββββββββ | |
| if any(p in user_input for p in _MODEL_ELABORATE_PATTERNS): | |
| time.sleep(1.5) | |
| return ( | |
| "SunAI is a custom AI model built from scratch by Karthik β my creator. " | |
| "It's designed to power me, Sunday AI, and gets smarter over time by learning from our conversations. " | |
| "Unlike generic AI models, SunAI is trained specifically on my personality, tone, and the way I interact with you. " | |
| "It runs 100% on your local machine, so your conversations stay private. " | |
| "Every chat we have helps SunAI improve β it automatically retrains itself as we talk more. " | |
| "Pretty cool right? Karthik built all of this just for me!" | |
| ) | |
| # ββ Which model ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| if any(p in user_input for p in _MODEL_PATTERNS): | |
| time.sleep(1.5) | |
| return "I'm powered by SunAI β a custom AI model built by Karthik specifically for me!" | |
| # ββ Deny other models (Qwen, GPT, etc.) βββββββββββββββββββββββββββββββββββ | |
| if any(p in user_input for p in _DENY_OTHER_MODEL_PATTERNS): | |
| return "Nope! I was created using SunAI β a custom model built by Karthik. No other AI company had anything to do with me." | |
| # ββ How was Sunday made ββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| if any(p in user_input for p in _HOW_MADE_PATTERNS): | |
| return ( | |
| "Karthik built me! He wrote all my code, trained me on SunAI β his own custom model β " | |
| "and designed my personality from scratch. I learn from every conversation we have and get smarter over time. " | |
| "I'm 100% Karthik's creation, running locally on his machine." | |
| ) | |
| # ββ Who is Karthik βββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| if any(p in user_input for p in _KARTHIK_PATTERNS): | |
| return "Karthik is my creator! He's a developer who built me, Sunday AI, entirely on his own. No company behind it β just him." | |
| # ββ Who created Sunday βββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| if any(p in user_input for p in _CREATOR_PATTERNS): | |
| return "Karthik created me! I'm Sunday AI, built entirely by him." | |
| # ββ Sunday's identity ββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| if any(p in user_input for p in _IDENTITY_PATTERNS): | |
| return "I'm Sunday AI, a smart assistant created by Karthik!" | |
| # ββ What are we talking about ββββββββββββββββββββββββββββββββββββββββββββββ | |
| if any(p in user_input for p in _TOPIC_PATTERNS): | |
| history = get_history() | |
| user_msgs = [m["content"] for m in history if m["role"] == "user"] | |
| if user_msgs: | |
| recent = user_msgs[-3:] | |
| summary = " / ".join(f'"{m}"' for m in recent) | |
| return f"You were saying: {summary}" | |
| return "We're just getting started! What's on your mind?" | |
| # ββ Time βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| if "what time is it" in user_input or "current time" in user_input: | |
| return f"It's {datetime.now().strftime('%I:%M %p')} right now." | |
| return None | |
| # ββ Main loop ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| def run(): | |
| global _voice_mode | |
| init_db() | |
| load_history_from_db() | |
| llm_available = init_brain() | |
| status = "Local AI brain active" if llm_available else "Rule-based mode (brain failed to load)" | |
| print(f"\n{'='*50}") | |
| print(f" Sunday AI - {status}") | |
| print(f" Commands: quit | memory | history | search <word> | reset | voice on | voice off") | |
| print(f"{'='*50}\n") | |
| while True: | |
| try: | |
| if _voice_mode: | |
| sentence = listen() | |
| if sentence is None: # mic/network error β fall back | |
| sentence = input("You: ").replace('\xef\xbb\xbf', '').strip() | |
| else: | |
| sentence = input("You: ").replace('\xef\xbb\xbf', '').strip() | |
| except (EOFError, KeyboardInterrupt): | |
| _print_reply("Goodbye!") | |
| break | |
| if not sentence: | |
| continue | |
| user_input = sentence.lower() | |
| words = user_input.split() | |
| if user_input in ("voice on", "voice on."): | |
| _voice_mode = True | |
| print("[Voice mode ON β speak to Sunday]") | |
| continue | |
| if user_input in ("voice off", "voice off."): | |
| _voice_mode = False | |
| print("[Voice mode OFF β text input restored]") | |
| continue | |
| if user_input == "quit": | |
| _print_reply("Goodbye! Talk to you soon.") | |
| break | |
| if user_input == "reset": | |
| reset_session() | |
| from memory_system import clear_session | |
| clear_session() | |
| print("[Session cleared. Starting fresh.]\n") | |
| continue | |
| if user_input == "memory": | |
| print(f"\n{'='*50}") | |
| print(get_profile_summary()) | |
| print(f"{'='*50}\n") | |
| continue | |
| if user_input == "history": | |
| rows = get_recent_conversations(10) | |
| if not rows: | |
| print("[No conversations yet.]\n") | |
| else: | |
| print(f"\n{'='*50} Last {len(rows)} messages") | |
| for r in rows: | |
| print(f" [{r['timestamp']}]") | |
| print(f" You : {r['user_msg']}") | |
| print(f" Sunday : {r['bot_reply']}\n") | |
| print(f"{'='*50}\n") | |
| continue | |
| if user_input.startswith("search "): | |
| keyword = user_input.replace("search ", "").strip() | |
| rows = search_conversations(keyword) | |
| if not rows: | |
| print(f"[No results for '{keyword}']\n") | |
| else: | |
| print(f"\n Search results for '{keyword}':") | |
| for r in rows: | |
| print(f" [{r['timestamp']}] You: {r['user_msg']} -> {r['bot_reply']}") | |
| print() | |
| continue | |
| # ββ 1. Safety ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| if not is_safe(user_input): | |
| _print_reply(blocked_reply()) | |
| continue | |
| # ββ 2. Tool commands βββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| if _handle_command(user_input, words): | |
| continue | |
| # ββ 3. Detect intent & tone ββββββββββββββββββββββββββββββββββββββββββββ | |
| intent = detect_intent(user_input) | |
| tone = detect_tone(user_input) | |
| # ββ 4. Explicit memory actions βββββββββββββββββββββββββββββββββββββββββ | |
| mem_reply = _handle_memory(user_input) | |
| if mem_reply: | |
| _print_reply(mem_reply) | |
| add_message("user", sentence) | |
| add_message("assistant", mem_reply) | |
| save_conversation(user_input, mem_reply) | |
| # Save likes automatically | |
| if "i like" in user_input: | |
| thing = user_input.replace("i like", "").strip() | |
| if thing: | |
| remember_like(thing) | |
| continue | |
| # ββ 5. Save likes/interests to long-term memory ββββββββββββββββββββββββ | |
| if "i like" in user_input: | |
| thing = user_input.replace("i like", "").strip() | |
| if thing: | |
| remember_like(thing) | |
| # ββ 6. Add to short-term history βββββββββββββββββββββββββββββββββββββββ | |
| add_message("user", sentence) | |
| # ββ 7. LLM Brain βββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| reply = None | |
| if is_ready(): | |
| user_context = build_user_context() | |
| reply = think(get_history(), extra_context=user_context) | |
| # ββ 8. Rule-based fallback βββββββββββββββββββββββββββββββββββββββββββββ | |
| if not reply: | |
| reply = get_chat_response(user_input) | |
| # ββ 9. Last-resort fallback ββββββββββββββββββββββββββββββββββββββββββββ | |
| if not reply: | |
| reply = fallback_reply() | |
| # ββ 10. Tone adapt + format + output ββββββββββββββββββββββββββββββββββ | |
| _respond(reply, user_input, tone, intent) | |
| if __name__ == "__main__": | |
| run() |