Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,6 +3,7 @@ import gradio as gr
|
|
| 3 |
import requests
|
| 4 |
import inspect
|
| 5 |
import pandas as pd
|
|
|
|
| 6 |
from langgraph.graph import StateGraph, END
|
| 7 |
from typing import TypedDict
|
| 8 |
import string
|
|
@@ -21,6 +22,7 @@ class AgentState(TypedDict):
|
|
| 21 |
response: str
|
| 22 |
is_reversed: bool
|
| 23 |
is_riddle: bool
|
|
|
|
| 24 |
|
| 25 |
class SuperSmartAgent:
|
| 26 |
def __init__(self):
|
|
@@ -33,6 +35,8 @@ class SuperSmartAgent:
|
|
| 33 |
workflow.add_node("fix_question", self.fix_question)
|
| 34 |
workflow.add_node("check_riddle_or_trick", self.check_riddle_or_trick)
|
| 35 |
workflow.add_node("solve_riddle", self.solve_riddle)
|
|
|
|
|
|
|
| 36 |
|
| 37 |
workflow.set_entry_point("check_reversed")
|
| 38 |
|
|
@@ -42,10 +46,15 @@ class SuperSmartAgent:
|
|
| 42 |
)
|
| 43 |
workflow.add_conditional_edges(
|
| 44 |
"check_riddle_or_trick",
|
| 45 |
-
lambda state: "solve_riddle" if state["is_riddle"] else
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
)
|
| 47 |
workflow.add_edge("fix_question", "check_riddle_or_trick")
|
| 48 |
workflow.add_edge("solve_riddle", END)
|
|
|
|
| 49 |
|
| 50 |
return workflow.compile()
|
| 51 |
|
|
@@ -54,7 +63,8 @@ class SuperSmartAgent:
|
|
| 54 |
question=question,
|
| 55 |
response="",
|
| 56 |
is_reversed=False,
|
| 57 |
-
is_riddle=False
|
|
|
|
| 58 |
)
|
| 59 |
final_state = self.graph.invoke(initial_state)
|
| 60 |
return final_state["response"]
|
|
@@ -106,6 +116,39 @@ class SuperSmartAgent:
|
|
| 106 |
state["response"] = "Could not solve riddle."
|
| 107 |
return state
|
| 108 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 109 |
|
| 110 |
|
| 111 |
|
|
|
|
| 3 |
import requests
|
| 4 |
import inspect
|
| 5 |
import pandas as pd
|
| 6 |
+
|
| 7 |
from langgraph.graph import StateGraph, END
|
| 8 |
from typing import TypedDict
|
| 9 |
import string
|
|
|
|
| 22 |
response: str
|
| 23 |
is_reversed: bool
|
| 24 |
is_riddle: bool
|
| 25 |
+
is_python: bool
|
| 26 |
|
| 27 |
class SuperSmartAgent:
|
| 28 |
def __init__(self):
|
|
|
|
| 35 |
workflow.add_node("fix_question", self.fix_question)
|
| 36 |
workflow.add_node("check_riddle_or_trick", self.check_riddle_or_trick)
|
| 37 |
workflow.add_node("solve_riddle", self.solve_riddle)
|
| 38 |
+
workflow.add_node("check_python_suitability", self.check_python_suitability)
|
| 39 |
+
workflow.add_node("execute_python_code", self.execute_python_code)
|
| 40 |
|
| 41 |
workflow.set_entry_point("check_reversed")
|
| 42 |
|
|
|
|
| 46 |
)
|
| 47 |
workflow.add_conditional_edges(
|
| 48 |
"check_riddle_or_trick",
|
| 49 |
+
lambda state: "solve_riddle" if state["is_riddle"] else "check_python_suitability",
|
| 50 |
+
)
|
| 51 |
+
workflow.add_conditional_edges(
|
| 52 |
+
"check_python_suitability",
|
| 53 |
+
lambda state: "execute_python_code" if state["is_python"] else END,
|
| 54 |
)
|
| 55 |
workflow.add_edge("fix_question", "check_riddle_or_trick")
|
| 56 |
workflow.add_edge("solve_riddle", END)
|
| 57 |
+
workflow.add_edge("execute_python_code", END)
|
| 58 |
|
| 59 |
return workflow.compile()
|
| 60 |
|
|
|
|
| 63 |
question=question,
|
| 64 |
response="",
|
| 65 |
is_reversed=False,
|
| 66 |
+
is_riddle=False,
|
| 67 |
+
is_python=False
|
| 68 |
)
|
| 69 |
final_state = self.graph.invoke(initial_state)
|
| 70 |
return final_state["response"]
|
|
|
|
| 116 |
state["response"] = "Could not solve riddle."
|
| 117 |
return state
|
| 118 |
|
| 119 |
+
def check_python_suitability(self, state):
|
| 120 |
+
question = state["question"].lower()
|
| 121 |
+
patterns = ["output", "python", "execute", "run", "code", "script"]
|
| 122 |
+
state["is_python"] = any(word in question for word in patterns)
|
| 123 |
+
return state
|
| 124 |
+
|
| 125 |
+
def execute_python_code(self, state):
|
| 126 |
+
question = state["question"]
|
| 127 |
+
|
| 128 |
+
# Check if the question is about executing Python code
|
| 129 |
+
if "output from the attached Python code" in question.lower():
|
| 130 |
+
# Placeholder for actual file handling and execution logic
|
| 131 |
+
# In a real implementation, you would read the attached file and execute it
|
| 132 |
+
python_code = """
|
| 133 |
+
# Example Python code
|
| 134 |
+
def calculate():
|
| 135 |
+
return 42
|
| 136 |
+
|
| 137 |
+
result = calculate()
|
| 138 |
+
print(result)
|
| 139 |
+
"""
|
| 140 |
+
try:
|
| 141 |
+
# Use code_interpreter to execute the Python code
|
| 142 |
+
result = code_interpreter(code=python_code)
|
| 143 |
+
state["response"] = f"The final numeric output is: {result}"
|
| 144 |
+
except Exception as e:
|
| 145 |
+
state["response"] = f"Error executing Python code: {str(e)}"
|
| 146 |
+
else:
|
| 147 |
+
state["response"] = "This question does not seem to involve executing Python code."
|
| 148 |
+
|
| 149 |
+
return state
|
| 150 |
+
|
| 151 |
+
|
| 152 |
|
| 153 |
|
| 154 |
|