Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -7,6 +7,7 @@ import pandas as pd
|
|
| 7 |
from langgraph.graph import StateGraph, END
|
| 8 |
from typing import TypedDict
|
| 9 |
import re
|
|
|
|
| 10 |
|
| 11 |
|
| 12 |
# (Keep Constants as is)
|
|
@@ -30,15 +31,34 @@ class SuperSmartAgent:
|
|
| 30 |
|
| 31 |
def _build_graph(self):
|
| 32 |
############## NODES ###################
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
def check_reversed(state):
|
| 34 |
question = state["question"]
|
| 35 |
reversed_candidate = question[::-1]
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
|
|
|
|
|
|
| 42 |
print("Detected reversed text. Marking as reversed.")
|
| 43 |
state["is_reversed"] = True
|
| 44 |
else:
|
|
@@ -47,9 +67,10 @@ class SuperSmartAgent:
|
|
| 47 |
return state
|
| 48 |
|
| 49 |
def fix_question(state):
|
| 50 |
-
if state
|
| 51 |
print(f"Fixing question by reversing: {state['question']}")
|
| 52 |
state["question"] = state["question"][::-1]
|
|
|
|
| 53 |
return state
|
| 54 |
|
| 55 |
def check_riddle_or_trick(state):
|
|
@@ -101,10 +122,11 @@ class SuperSmartAgent:
|
|
| 101 |
state["response"] = "This question doesn't require Python or is unclear."
|
| 102 |
return state
|
| 103 |
|
| 104 |
-
class AgentState(TypedDict):
|
| 105 |
question: str
|
| 106 |
is_reversed: bool
|
| 107 |
is_python: bool
|
|
|
|
| 108 |
response: str
|
| 109 |
|
| 110 |
builder = StateGraph(AgentState)
|
|
|
|
| 7 |
from langgraph.graph import StateGraph, END
|
| 8 |
from typing import TypedDict
|
| 9 |
import re
|
| 10 |
+
import string
|
| 11 |
|
| 12 |
|
| 13 |
# (Keep Constants as is)
|
|
|
|
| 31 |
|
| 32 |
def _build_graph(self):
|
| 33 |
############## NODES ###################
|
| 34 |
+
def score_text(text):
|
| 35 |
+
# Count alphanumeric characters
|
| 36 |
+
alnum_count = sum(c.isalnum() for c in text)
|
| 37 |
+
# Count spaces
|
| 38 |
+
space_count = text.count(' ')
|
| 39 |
+
# Count punctuation marks
|
| 40 |
+
punctuation_count = sum(c in string.punctuation for c in text)
|
| 41 |
+
# Check if text ends with sentence punctuation (., !, ?)
|
| 42 |
+
ends_properly = text[-1] in '.!?'
|
| 43 |
+
|
| 44 |
+
# Simple heuristic score: alnum + spaces + bonus if ends properly
|
| 45 |
+
score = alnum_count + space_count
|
| 46 |
+
if ends_properly:
|
| 47 |
+
score += 5 # give bonus if text ends with punctuation
|
| 48 |
+
|
| 49 |
+
return score
|
| 50 |
+
|
| 51 |
def check_reversed(state):
|
| 52 |
question = state["question"]
|
| 53 |
reversed_candidate = question[::-1]
|
| 54 |
+
|
| 55 |
+
original_score = score_text(question)
|
| 56 |
+
reversed_score = score_text(reversed_candidate)
|
| 57 |
+
|
| 58 |
+
print(f"Original text score: {original_score}")
|
| 59 |
+
print(f"Reversed text score: {reversed_score}")
|
| 60 |
+
|
| 61 |
+
if reversed_score > original_score:
|
| 62 |
print("Detected reversed text. Marking as reversed.")
|
| 63 |
state["is_reversed"] = True
|
| 64 |
else:
|
|
|
|
| 67 |
return state
|
| 68 |
|
| 69 |
def fix_question(state):
|
| 70 |
+
if state.get("is_reversed", False):
|
| 71 |
print(f"Fixing question by reversing: {state['question']}")
|
| 72 |
state["question"] = state["question"][::-1]
|
| 73 |
+
print(f"Fixed question: {state['question']}")
|
| 74 |
return state
|
| 75 |
|
| 76 |
def check_riddle_or_trick(state):
|
|
|
|
| 122 |
state["response"] = "This question doesn't require Python or is unclear."
|
| 123 |
return state
|
| 124 |
|
| 125 |
+
class AgentState(TypedDict, total=False):
|
| 126 |
question: str
|
| 127 |
is_reversed: bool
|
| 128 |
is_python: bool
|
| 129 |
+
is_riddle:bool
|
| 130 |
response: str
|
| 131 |
|
| 132 |
builder = StateGraph(AgentState)
|