selim-ba commited on
Commit
79267da
·
verified ·
1 Parent(s): 08d0a02

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +95 -8
app.py CHANGED
@@ -4,21 +4,108 @@ import requests
4
  import inspect
5
  import pandas as pd
6
 
 
 
 
7
  # (Keep Constants as is)
8
  # --- Constants ---
9
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
10
 
11
  # --- Basic Agent Definition ---
12
  # ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
13
- class BasicAgent:
 
 
 
 
 
 
 
 
 
14
  def __init__(self):
15
- print("BasicAgent initialized.")
16
- def __call__(self, question: str) -> str:
17
- print(f"Agent received question (first 50 chars): {question[:50]}...")
18
- fixed_answer = "This is a default answer."
19
- print(f"Agent returning fixed answer: {fixed_answer}")
20
- return fixed_answer
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21
 
 
22
  def run_and_submit_all( profile: gr.OAuthProfile | None):
23
  """
24
  Fetches all questions, runs the BasicAgent on them, submits all answers,
@@ -40,7 +127,7 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
40
 
41
  # 1. Instantiate Agent ( modify this part to create your agent)
42
  try:
43
- agent = BasicAgent()
44
  except Exception as e:
45
  print(f"Error instantiating agent: {e}")
46
  return f"Error initializing agent: {e}", None
 
4
  import inspect
5
  import pandas as pd
6
 
7
+ from langgraph.graph import StateGraph, END
8
+
9
+
10
  # (Keep Constants as is)
11
  # --- Constants ---
12
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
13
 
14
  # --- Basic Agent Definition ---
15
  # ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
16
+ # class BasicAgent:
17
+ # def __init__(self):
18
+ # print("BasicAgent initialized.")
19
+ # def __call__(self, question: str) -> str:
20
+ # print(f"Agent received question (first 50 chars): {question[:50]}...")
21
+ # fixed_answer = "This is a default answer."
22
+ # print(f"Agent returning fixed answer: {fixed_answer}")
23
+ # return fixed_answer
24
+
25
+ class SuperSmartAgent:
26
  def __init__(self):
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
+ ########################################
109
  def run_and_submit_all( profile: gr.OAuthProfile | None):
110
  """
111
  Fetches all questions, runs the BasicAgent on them, submits all answers,
 
127
 
128
  # 1. Instantiate Agent ( modify this part to create your agent)
129
  try:
130
+ agent = SuperSmartAgent() #BasicAgent()
131
  except Exception as e:
132
  print(f"Error instantiating agent: {e}")
133
  return f"Error initializing agent: {e}", None