Update app.py
Browse files
app.py
CHANGED
|
@@ -14,9 +14,6 @@ import pytz
|
|
| 14 |
# --- Constants ---
|
| 15 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 16 |
|
| 17 |
-
# ==============================================================================
|
| 18 |
-
# RULE BASED SOLVER (FAST + FREE)
|
| 19 |
-
# ==============================================================================
|
| 20 |
|
| 21 |
def rule_solver(question: str):
|
| 22 |
|
|
@@ -48,20 +45,22 @@ def rule_solver(question: str):
|
|
| 48 |
|
| 49 |
# --- Basic Agent Definition ---
|
| 50 |
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
| 51 |
-
# ==============================================================================
|
| 52 |
-
# SMART BASIC AGENT (FREE + STABLE)
|
| 53 |
-
# ==============================================================================
|
| 54 |
|
| 55 |
class BasicAgent:
|
| 56 |
|
| 57 |
def __init__(self):
|
| 58 |
print("Smart Agent initialized.")
|
| 59 |
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
|
| 66 |
def clean_answer(self, text: str) -> str:
|
| 67 |
text = text.strip()
|
|
@@ -73,45 +72,42 @@ class BasicAgent:
|
|
| 73 |
|
| 74 |
def llm_fallback(self, question: str):
|
| 75 |
|
| 76 |
-
messages = [
|
| 77 |
-
{
|
| 78 |
-
"role": "system",
|
| 79 |
-
"content": (
|
| 80 |
-
"Return ONLY the final answer. "
|
| 81 |
-
"No explanation. One short line."
|
| 82 |
-
),
|
| 83 |
-
},
|
| 84 |
-
{"role": "user", "content": question},
|
| 85 |
-
]
|
| 86 |
-
|
| 87 |
completion = self.client.chat_completion(
|
| 88 |
-
messages=
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
max_tokens=80,
|
| 90 |
temperature=0.1,
|
| 91 |
)
|
| 92 |
|
| 93 |
return completion.choices[0].message.content
|
| 94 |
|
| 95 |
-
def __call__(self, question: str)
|
| 96 |
|
| 97 |
print("Solving question...")
|
| 98 |
|
| 99 |
-
# β
|
| 100 |
rule_answer = rule_solver(question)
|
| 101 |
if rule_answer:
|
| 102 |
-
print("Solved by
|
| 103 |
return rule_answer
|
| 104 |
|
| 105 |
-
# β
|
|
|
|
|
|
|
|
|
|
|
|
|
| 106 |
try:
|
| 107 |
response = self.llm_fallback(question)
|
| 108 |
answer = self.clean_answer(response)
|
| 109 |
-
print("LLM Answer:", answer)
|
| 110 |
return answer
|
| 111 |
-
except Exception as e:
|
| 112 |
-
print("LLM failed:", e)
|
| 113 |
-
return "Error"
|
| 114 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 115 |
|
| 116 |
|
| 117 |
|
|
|
|
| 14 |
# --- Constants ---
|
| 15 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 16 |
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
def rule_solver(question: str):
|
| 19 |
|
|
|
|
| 45 |
|
| 46 |
# --- Basic Agent Definition ---
|
| 47 |
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
|
|
|
|
|
|
|
|
|
| 48 |
|
| 49 |
class BasicAgent:
|
| 50 |
|
| 51 |
def __init__(self):
|
| 52 |
print("Smart Agent initialized.")
|
| 53 |
|
| 54 |
+
self.llm_available = True
|
| 55 |
+
|
| 56 |
+
try:
|
| 57 |
+
self.client = InferenceClient(
|
| 58 |
+
model="microsoft/Phi-3-mini-4k-instruct",
|
| 59 |
+
token=os.getenv("HF_TOKEN")
|
| 60 |
+
)
|
| 61 |
+
except Exception as e:
|
| 62 |
+
print("LLM init failed:", e)
|
| 63 |
+
self.llm_available = False
|
| 64 |
|
| 65 |
def clean_answer(self, text: str) -> str:
|
| 66 |
text = text.strip()
|
|
|
|
| 72 |
|
| 73 |
def llm_fallback(self, question: str):
|
| 74 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
completion = self.client.chat_completion(
|
| 76 |
+
messages=[
|
| 77 |
+
{"role": "system",
|
| 78 |
+
"content": "Return ONLY the final answer. No explanation."},
|
| 79 |
+
{"role": "user", "content": question},
|
| 80 |
+
],
|
| 81 |
max_tokens=80,
|
| 82 |
temperature=0.1,
|
| 83 |
)
|
| 84 |
|
| 85 |
return completion.choices[0].message.content
|
| 86 |
|
| 87 |
+
def __call__(self, question: str):
|
| 88 |
|
| 89 |
print("Solving question...")
|
| 90 |
|
| 91 |
+
# β
RULE ENGINE FIRST
|
| 92 |
rule_answer = rule_solver(question)
|
| 93 |
if rule_answer:
|
| 94 |
+
print("Solved by rules:", rule_answer)
|
| 95 |
return rule_answer
|
| 96 |
|
| 97 |
+
# β
IF LLM ALREADY FAILED β SKIP
|
| 98 |
+
if not self.llm_available:
|
| 99 |
+
return "Unknown"
|
| 100 |
+
|
| 101 |
+
# β
TRY LLM ONLY ONCE
|
| 102 |
try:
|
| 103 |
response = self.llm_fallback(question)
|
| 104 |
answer = self.clean_answer(response)
|
|
|
|
| 105 |
return answer
|
|
|
|
|
|
|
|
|
|
| 106 |
|
| 107 |
+
except Exception as e:
|
| 108 |
+
print("LLM disabled (provider unavailable):", e)
|
| 109 |
+
self.llm_available = False
|
| 110 |
+
return "Unknown"
|
| 111 |
|
| 112 |
|
| 113 |
|