Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -27,82 +27,82 @@ class SuperSmartAgent:
|
|
| 27 |
self.graph = self._build_graph()
|
| 28 |
|
| 29 |
def _build_graph(self):
|
| 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 |
-
def generate_code(state):
|
| 55 |
-
q = state["question"].lower()
|
| 56 |
-
if "sum" in q:
|
| 57 |
-
state["response"] = "numbers = [1, 2, 3]\nprint(sum(numbers))"
|
| 58 |
-
elif "average" in q:
|
| 59 |
-
state["response"] = "numbers = [1, 2, 3]\nprint(sum(numbers) / len(numbers))"
|
| 60 |
-
elif "sort" in q:
|
| 61 |
-
state["response"] = "data = [3, 1, 2]\ndata.sort()\nprint(data)"
|
| 62 |
-
else:
|
| 63 |
-
state["response"] = "# Code generation not implemented for this case."
|
| 64 |
-
return state
|
| 65 |
-
|
| 66 |
-
def fallback(state):
|
| 67 |
-
state["response"] = "This question doesn't require Python or is unclear."
|
| 68 |
-
return state
|
| 69 |
-
|
| 70 |
-
# State schema
|
| 71 |
-
state_schema = {
|
| 72 |
-
"question": str,
|
| 73 |
-
"is_reversed": bool,
|
| 74 |
-
"is_python": bool,
|
| 75 |
-
"response": str,
|
| 76 |
-
}
|
| 77 |
|
| 78 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
builder.add_node("check_python_suitability", check_python_suitability)
|
| 84 |
-
builder.add_node("generate_code", generate_code)
|
| 85 |
-
builder.add_node("fallback", fallback)
|
| 86 |
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
)
|
| 95 |
-
builder.add_edge("generate_code", END)
|
| 96 |
-
builder.add_edge("fallback", END)
|
| 97 |
|
|
|
|
| 98 |
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 106 |
|
| 107 |
|
| 108 |
########################################
|
|
|
|
| 27 |
self.graph = self._build_graph()
|
| 28 |
|
| 29 |
def _build_graph(self):
|
| 30 |
+
############## NODES ###################
|
| 31 |
+
def check_reversed(state):
|
| 32 |
+
question = state["question"]
|
| 33 |
+
reversed_text = question[::-1]
|
| 34 |
+
if reversed_text.count(' ') > question.count(' '):
|
| 35 |
+
state["is_reversed"] = True
|
| 36 |
+
else:
|
| 37 |
+
state["is_reversed"] = False
|
| 38 |
+
return state
|
| 39 |
+
|
| 40 |
+
def fix_question(state):
|
| 41 |
+
if state["is_reversed"]:
|
| 42 |
+
state["question"] = state["question"][::-1]
|
| 43 |
+
return state
|
| 44 |
+
|
| 45 |
+
def check_python_suitability(state):
|
| 46 |
+
question = state["question"].lower()
|
| 47 |
+
patterns = ["sum", "average", "count", "sort", "generate", "regex", "convert"]
|
| 48 |
+
if any(word in question for word in patterns):
|
| 49 |
+
state["is_python"] = True
|
| 50 |
+
else:
|
| 51 |
+
state["is_python"] = False
|
| 52 |
+
return state
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
|
| 54 |
+
def generate_code(state):
|
| 55 |
+
q = state["question"].lower()
|
| 56 |
+
if "sum" in q:
|
| 57 |
+
state["response"] = "numbers = [1, 2, 3]\nprint(sum(numbers))"
|
| 58 |
+
elif "average" in q:
|
| 59 |
+
state["response"] = "numbers = [1, 2, 3]\nprint(sum(numbers) / len(numbers))"
|
| 60 |
+
elif "sort" in q:
|
| 61 |
+
state["response"] = "data = [3, 1, 2]\ndata.sort()\nprint(data)"
|
| 62 |
+
else:
|
| 63 |
+
state["response"] = "# Code generation not implemented for this case."
|
| 64 |
+
return state
|
| 65 |
|
| 66 |
+
def fallback(state):
|
| 67 |
+
state["response"] = "This question doesn't require Python or is unclear."
|
| 68 |
+
return state
|
|
|
|
|
|
|
|
|
|
| 69 |
|
| 70 |
+
# State schema
|
| 71 |
+
state_schema = {
|
| 72 |
+
"question": str,
|
| 73 |
+
"is_reversed": bool,
|
| 74 |
+
"is_python": bool,
|
| 75 |
+
"response": str,
|
| 76 |
+
}
|
|
|
|
|
|
|
|
|
|
| 77 |
|
| 78 |
+
builder = StateGraph(state_schema)
|
| 79 |
|
| 80 |
+
# Nodes
|
| 81 |
+
builder.add_node("check_reversed", check_reversed)
|
| 82 |
+
builder.add_node("fix_question", fix_question)
|
| 83 |
+
builder.add_node("check_python_suitability", check_python_suitability)
|
| 84 |
+
builder.add_node("generate_code", generate_code)
|
| 85 |
+
builder.add_node("fallback", fallback)
|
| 86 |
+
|
| 87 |
+
# Edges
|
| 88 |
+
builder.set_entry_point("check_reversed")
|
| 89 |
+
builder.add_edge("check_reversed", "fix_question")
|
| 90 |
+
builder.add_edge("fix_question", "check_python_suitability")
|
| 91 |
+
builder.add_conditional_edges(
|
| 92 |
+
"check_python_suitability",
|
| 93 |
+
lambda s: "generate_code" if s["is_python"] else "fallback"
|
| 94 |
+
)
|
| 95 |
+
builder.add_edge("generate_code", END)
|
| 96 |
+
builder.add_edge("fallback", END)
|
| 97 |
+
|
| 98 |
+
|
| 99 |
+
graph = builder.compile()
|
| 100 |
+
|
| 101 |
+
def __call__(self, question: str) -> str:
|
| 102 |
+
# use self.graph to process the question
|
| 103 |
+
state = {"question": question}
|
| 104 |
+
result = self.graph.invoke(state)
|
| 105 |
+
return result.get("response", "No answer generated.")
|
| 106 |
|
| 107 |
|
| 108 |
########################################
|