Untitled

Python Guest 9 Views Size: 6.85 KB Posted on: Apr 3, 26 @ 11:30 PM
  1. # ------------------------------------------------------------------------------
  2. # Intent detection patterns
  3. # ------------------------------------------------------------------------------
  4. TIME_PATTERNS = [
  5.     r"\bwhat(?:'s|\s+is)?\s+(?:the\s+)?time\s*(?:is\s+it|right\s+now|now)?\s*[?.]?\s*$",
  6.     r"\bwhat(?:'s|\s+is)?\s+(?:the\s+)?time\s+(?:in|for)\s+(?!an?\s+(?:hour|minute|second|moment|sec|min)\b)(?!a\s+while\b)(?!about\b)(?!\d+\b).+",
  7.     r"\b(?:current|local)\s+time\b",
  8.     r"\btime\s+(?:right\s+)?now\b",
  9.     r"^\s*now\??\s*$",
  10.     r"\bwhat(?:'s|\s+is)?\s+(?:the\s+)?date\b",
  11.     r"\btoday'?s?\s+date\b",
  12.     r"\bdate\s+today\b",
  13.     r"\bwhat\s+day\s+is\s+it\b",
  14.     r"\bday\s+of\s+week\b",
  15.     r"\byesterday\b",
  16.     r"\btime\s+(?:in|for)\s+(?!an?\s+(?:hour|minute|second|moment|sec|min)\b)(?!\d+\b)(.+)",
  17. ]
  18.  
  19. def has_time_intent(msg: str) -> bool:
  20.     m = (msg or "").strip().lower()
  21.     return any(re.search(p, m, re.IGNORECASE) for p in TIME_PATTERNS)
  22.  
  23. WEATHER_PATTERNS = [
  24.     r"\bweather\b",
  25.     r"\bforecast\b",
  26.     r"\bwhat(?:'s|\s+is)?\s+(?:the\s+)?weather\b",
  27.     r"\bweather\s+(?:in|for)\s+.+",
  28.     r"\bwhat(?:'s|\s+is)?\s+(?:the\s+)?temperature\s*(?:in\b|for\b|\?|$)",
  29.     r"\btemperature\s+(?:in|for)\s+\S",
  30.     r"\bhow\s+(?:hot|cold|warm)\s+is\s+it\b",
  31.     r"\brain(?:ing)?\s+(?:in|for|today|now|outside|tomorrow)\b",
  32.     r"\b(?:in|for|today|now|outside|tomorrow)\s+rain(?:ing)?\b",
  33.     r"\bsnow(?:ing)?\b",
  34. ]
  35.  
  36. def has_weather_intent(msg: str) -> bool:
  37.     m = (msg or "").strip().lower()
  38.     matches = [re.search(p, m, re.IGNORECASE) for p in WEATHER_PATTERNS]
  39.     if any(matches):
  40.         if re.search(r"\bsnow(?:ing)?\b", m, re.IGNORECASE):
  41.             if not re.search(r"\b(in|for|today|tomorrow|now|outside)\b", m, re.IGNORECASE):
  42.                 return False
  43.         return True
  44.     return False
  45.  
  46.  
  47. IMAGE_INTENT_PATTERNS = [
  48.     r"\b(generate|create|draw|make)\s+(?:an?\s+)?image\b",
  49.     r"\bimage\s+of\s+(?!my|your|his|her|our|their|this|that\b)",
  50.     r"\bpicture\s+of\s+(?!my|your|his|her|our|their|this|that\b)",
  51.     r"\bgenerate\s+(?:art|illustration|photo|graphic)\b",
  52.     r"\bdraw\s+me\b",
  53.     r"\bimagine\s+.+",
  54. ]
  55.  
  56. def has_image_intent(msg: str) -> bool:
  57.     m = (msg or "").strip().lower()
  58.     if has_generate_video_intent(msg):
  59.         return False
  60.     return any(re.search(p, m, re.IGNORECASE) for p in IMAGE_INTENT_PATTERNS)
  61.  
  62.  
  63. VIDEO_INTENT_PATTERNS = [
  64.     r"\byoutube\s+link\b",
  65.     r"\blink\s+to\s+(?:youtube|video)\b",
  66.     r"\b(?:give|find|share|play|watch)\s+(?:me\s+)?a\s+(?:youtube|video)\s+link\b",
  67.     r"\bsong\s+(?:video|link)\b",
  68.     r"\bmusic\s+video\b",
  69.     r"\bfunny\s+video\b"
  70. ]
  71.  
  72. VIDEO_GEN_KEYWORDS = re.compile(
  73.     r"\b(animate|generate\s+(?:a\s+)?video|create\s+(?:a\s+)?video|make\s+(?:a\s+)?video)\b", re.IGNORECASE
  74. )
  75.  
  76. def has_video_intent(msg: str) -> bool:
  77.     m = (msg or "").strip().lower()
  78.     if VIDEO_GEN_KEYWORDS.search(m):
  79.         return False
  80.     return any(re.search(p, m, re.IGNORECASE) for p in VIDEO_INTENT_PATTERNS)
  81.  
  82.  
  83. EMAIL_PATTERNS = [
  84.     r"\bsend\s+(?:an?\s+)?email\b",
  85.     r"\bping\s+.+@.+\b",
  86.     r"\bcontact\s+ceo\b"
  87. ]
  88.  
  89. def has_email_intent(msg: str) -> bool:
  90.     m = (msg or "").strip().lower()
  91.     return any(re.search(p, m, re.IGNORECASE) for p in EMAIL_PATTERNS)
  92.  
  93.  
  94. NEWS_PATTERNS = [
  95.     r"\bnews\b",
  96.     r"\b(give|tell|what's|what is) (?:me )?(?:the )?news\b",
  97.     r"\bnews (?:for|in|about) (.+)\b",
  98.     r"\bheadlines\b",
  99.     r"\btop stories\b",
  100.     r"\bcurrent events\b",
  101.     r"\bbreaking news\b"
  102. ]
  103.  
  104. def has_news_intent(msg: str) -> bool:
  105.     m = (msg or "").strip().lower()
  106.     return any(re.search(p, m, re.IGNORECASE) for p in NEWS_PATTERNS)
  107.  
  108. ANIMATE_LAST_PATTERNS = [
  109.     r"\b(turn|make|convert|transform|animate)\s+(?:the\s+)?(?:last|previous|recent)\s+(?:image|picture|photo)\b",
  110.     r"\bvideo\s+of\s+(?:the\s+)?last\b",
  111.     r"\badd\s+motion\s+to\s+last\b"
  112. ]
  113.  
  114. def has_animate_last_image_intent(msg: str) -> bool:
  115.     return any(re.search(p, msg.lower()) for p in ANIMATE_LAST_PATTERNS)
  116.  
  117.  
  118. # Direct video generation (animate a cat, generate video of X)
  119. GENERATE_VIDEO_PATTERNS = [
  120.     r"\banimate\s+(?:an?\s+)?(.+)",
  121.     r"\b(?:generate|create|make)\s+(?:an?\s+)?video\s+(?:of\s+)?(.+)",
  122.     r"\bvideo\s+of\s+(.+)",
  123. ]
  124.  
  125. def has_generate_video_intent(msg: str) -> bool:
  126.     m = (msg or "").strip().lower()
  127.     if has_animate_last_image_intent(msg):
  128.         return False
  129.     return any(re.search(p, m, re.IGNORECASE) for p in GENERATE_VIDEO_PATTERNS)
  130.  
  131.  
  132. def extract_video_prompt(msg: str) -> str:
  133.     """Extract the subject/prompt from a direct video generation request."""
  134.     for p in GENERATE_VIDEO_PATTERNS:
  135.         m = re.search(p, msg.lower(), re.IGNORECASE)
  136.         if m and m.group(1):
  137.             prompt = m.group(1).strip()
  138.             prompt = re.sub(r"[?.!,;:\s]+$", "", prompt)
  139.             if len(prompt) > 3:
  140.                 return prompt[:300]
  141.     return msg.strip()[:300]
  142.  
  143.  
  144. def extract_motion_description(msg: str) -> str:
  145.     lower = msg.lower()
  146.     for kw in ["move", "dance", "walk", "run", "fly", "live", "cinematic", "animated"]:
  147.         if kw in lower:
  148.             idx = lower.find(kw)
  149.             tail = msg[idx:].strip()
  150.             tail = re.sub(r"^(?:the\s+)?(?:last|previous)\s+(?:image|picture)\s*", "", tail, flags=re.I)
  151.             if len(tail.strip()) > 5:
  152.                 return tail[:150]
  153.     return "smooth cinematic animation, dynamic motion, high quality"
  154.  
  155.  
  156. def is_hard_query(msg: str) -> bool:
  157.     lower_msg = msg.lower().strip()
  158.     if len(msg) < 50 or has_time_intent(msg) or has_weather_intent(msg) or "joke" in lower_msg or has_image_intent(msg) or has_video_intent(msg) or has_email_intent(msg) or has_news_intent(msg) or lower_msg.startswith(("hi", "hello", "sup", "what is", "who is", "tell me about", "what should i")):
  159.         return False
  160.     if any(kw in lower_msg for kw in ["math", "solve", "prove", "code", "program", "puzzle", "explain why", "how does", "think step by step", "analyze", "reason", "multi-step", "complex"]) or len(msg) > 100:
  161.         return True
  162.     return False
  163.  
  164.  
  165. # Help intent
  166. HELP_PATTERNS = [r"^help$", r"^what can you do", r"^commands$", r"^capabilities$"]
  167.  
  168. def has_help_intent(msg: str) -> bool:
  169.     m = (msg or "").strip().lower()
  170.     return any(re.search(p, m, re.IGNORECASE) for p in HELP_PATTERNS)
  171.  
  172.  
  173. HELP_TEXT = (
  174.     "Here's what I can do:\n"
  175.     "Images: 'draw me a cat' / 'generate image of a sunset' / 'imagine a dragon'\n"
  176.     "Video: 'animate a dancing robot' / 'make a video of fireworks' / 'animate the last image'\n"
  177.     "Weather: 'weather in London' / 'forecast for NYC'\n"
  178.     "Time: 'what time is it in Tokyo'\n"
  179.     "News: 'give me the news' / 'news for UK'\n"
  180.     "YouTube: 'youtube link to Bohemian Rhapsody'\n"
  181.     "Email: 'send an email to user@example.com'\n"
  182.     "Clear context: 'clear my context'\n"
  183.     "Or just chat with me about anything!"
  184. )

Raw Paste

Comments 0
Login to post a comment.
  • No comments yet. Be the first.
Login to post a comment. Login or Register
We use cookies. To comply with GDPR in the EU and the UK we have to show you these.

We use cookies and similar technologies to keep this website functional (including spam protection via Google reCAPTCHA or Cloudflare Turnstile), and — with your consent — to measure usage and show ads. See Privacy.