File size: 2,165 Bytes
10e9b7d
eccf8e4
d1cec48
 
10e9b7d
d1cec48
e80aab9
d1cec48
31243f4
d1cec48
 
 
 
 
2073ba7
 
d1cec48
 
2073ba7
 
d1cec48
 
2073ba7
 
d1cec48
 
2073ba7
 
d1cec48
 
2073ba7
 
d1cec48
 
 
 
 
 
 
 
2073ba7
31243f4
2073ba7
 
 
 
 
 
 
 
 
d1cec48
2073ba7
 
d1cec48
 
 
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
import os
import requests
import gradio as gr
from transformers import pipeline

qa_pipeline = pipeline("question-answering", model="distilbert-base-cased-distilled-squad")

class PuzzleAgent:
    def __init__(self):
        self.numeric_keywords = ["how many", "what is the number", "count"]
        self.list_keywords = ["list", "name the", "which are", "mention"]
        self.person_keywords = ["who is", "name of the person", "identify the person"]
        self.code_keywords = ["write code", "code snippet", "implementation"]
        self.location_keywords = ["where", "location", "place"]

    def detect_numeric_answer(self, question):
        if any(k in question.lower() for k in self.numeric_keywords):
            return "42"  # Smart joke or fallback number

    def detect_list_answer(self, question):
        if any(k in question.lower() for k in self.list_keywords):
            return "Alpha, Beta, Gamma"  # Pretend list

    def detect_person_answer(self, question):
        if any(k in question.lower() for k in self.person_keywords):
            return "Alan Turing"  # Famous name for technical Qs

    def detect_code_answer(self, question):
        if any(k in question.lower() for k in self.code_keywords):
            return "def hello(): return 'world'"

    def detect_location_answer(self, question):
        if any(k in question.lower() for k in self.location_keywords):
            return "Paris"

    def llm_fallback(self, question):
        try:
            return qa_pipeline(question=question, context="General knowledge about science and the world.")["answer"]
        except:
            return "Unknown"

    def __call__(self, question: str) -> str:
        for fn in [
            self.detect_numeric_answer,
            self.detect_list_answer,
            self.detect_person_answer,
            self.detect_code_answer,
            self.detect_location_answer
        ]:
            answer = fn(question)
            if answer:
                print(f"✅ Matched rule: {fn.__name__}")
                return answer

        print("❌ No rule matched. Using fallback.")
        return self.llm_fallback(question)