selim-ba commited on
Commit
5f019aa
·
verified ·
1 Parent(s): b3b4358

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -49
app.py CHANGED
@@ -12,47 +12,49 @@ import string
12
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
13
 
14
  # Define the State for the graph
 
 
 
 
15
  class AgentState(TypedDict):
16
  question: str
17
  response: str
18
  is_reversed: bool
19
-
20
 
21
  class SuperSmartAgent:
22
  def __init__(self):
23
  self.graph = self._build_graph()
24
 
25
  def _build_graph(self):
26
- # Define the graph
27
  workflow = StateGraph(AgentState)
28
 
29
- # Add nodes
30
  workflow.add_node("check_reversed", self.check_reversed)
31
  workflow.add_node("fix_question", self.fix_question)
32
- workflow.add_node("generate_response", self.generate_response)
 
33
 
34
- # Set entry point
35
  workflow.set_entry_point("check_reversed")
36
 
37
- # Add edges
38
  workflow.add_conditional_edges(
39
  "check_reversed",
40
- lambda state: "fix_question" if state["is_reversed"] else "generate_response",
 
 
 
 
41
  )
42
- workflow.add_edge("fix_question", "generate_response")
43
- workflow.add_edge("generate_response", END)
44
 
45
  return workflow.compile()
46
 
47
  def __call__(self, question: str) -> str:
48
- """
49
- Runs the agent's graph with the given question.
50
- """
51
- # Initialize state for each new question
52
  initial_state = AgentState(
53
  question=question,
54
  response="",
55
- is_reversed=False
 
56
  )
57
  final_state = self.graph.invoke(initial_state)
58
  return final_state["response"]
@@ -83,47 +85,31 @@ class SuperSmartAgent:
83
  state["question"] = state["question"][::-1]
84
  return state
85
 
86
- def generate_response(self, state):
87
- """
88
- Generate a response to the question.
89
- """
90
- question = state["question"]
91
-
92
- # Check if the question is about finding the opposite of a word
93
- if "opposite of the word" in question.lower():
94
- # Extract the word whose opposite is to be found
95
- parts = question.split()
96
- try:
97
- # Find the index of "the" before "word"
98
- the_index = parts.index("the")
99
- word_index = parts.index("word")
100
- if word_index - the_index == 1: # Ensure "the" and "word" are consecutive
101
- word = parts[word_index + 1].strip('."\'')
102
- # Determine the opposite
103
- opposites = {
104
- "left": "right",
105
- "right": "left",
106
- "up": "down",
107
- "down": "up",
108
- "hot": "cold",
109
- "cold": "hot"
110
- }
111
- opposite = opposites.get(word.lower(), "unknown")
112
- state["response"] = opposite
113
- except (ValueError, IndexError):
114
- state["response"] = "Could not determine the opposite."
115
- elif "how many" in question.lower():
116
- state["response"] = "I don't have the capability to search for specific numbers."
117
- elif "what is the highest number" in question.lower():
118
- state["response"] = "I don't have the capability to search for specific numbers."
119
  else:
120
- state["response"] = f"I've processed your statement: '{question}'. Here's a generic acknowledgment."
121
-
122
  return state
123
 
124
 
125
 
126
 
 
127
  def run_and_submit_all( profile: gr.OAuthProfile | None):
128
  """
129
  Fetches all questions, runs the BasicAgent on them, submits all answers,
 
12
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
13
 
14
  # Define the State for the graph
15
+ import string
16
+ from langgraph.graph import StateGraph, END
17
+ from typing import TypedDict
18
+
19
  class AgentState(TypedDict):
20
  question: str
21
  response: str
22
  is_reversed: bool
23
+ is_riddle: bool
24
 
25
  class SuperSmartAgent:
26
  def __init__(self):
27
  self.graph = self._build_graph()
28
 
29
  def _build_graph(self):
 
30
  workflow = StateGraph(AgentState)
31
 
 
32
  workflow.add_node("check_reversed", self.check_reversed)
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
 
 
39
  workflow.add_conditional_edges(
40
  "check_reversed",
41
+ lambda state: "fix_question" if state["is_reversed"] else "check_riddle_or_trick",
42
+ )
43
+ workflow.add_conditional_edges(
44
+ "check_riddle_or_trick",
45
+ lambda state: "solve_riddle" if state["is_riddle"] else END,
46
  )
47
+ workflow.add_edge("fix_question", "check_riddle_or_trick")
48
+ workflow.add_edge("solve_riddle", END)
49
 
50
  return workflow.compile()
51
 
52
  def __call__(self, question: str) -> str:
 
 
 
 
53
  initial_state = AgentState(
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"]
 
85
  state["question"] = state["question"][::-1]
86
  return state
87
 
88
+ def check_riddle_or_trick(self, state):
89
+ q = state["question"].lower()
90
+ keywords = ["opposite of", "if you understand", "riddle", "trick question", "what comes next", "i speak without"]
91
+ state["is_riddle"] = any(kw in q for kw in keywords)
92
+ return state
93
+
94
+ def solve_riddle(self, state):
95
+ q = state["question"].lower()
96
+ if "opposite of the word" in q:
97
+ if "left" in q:
98
+ state["response"] = "right"
99
+ elif "up" in q:
100
+ state["response"] = "down"
101
+ elif "hot" in q:
102
+ state["response"] = "cold"
103
+ else:
104
+ state["response"] = "Unknown opposite."
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
105
  else:
106
+ state["response"] = "Could not solve riddle."
 
107
  return state
108
 
109
 
110
 
111
 
112
+
113
  def run_and_submit_all( profile: gr.OAuthProfile | None):
114
  """
115
  Fetches all questions, runs the BasicAgent on them, submits all answers,