Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -24,42 +24,32 @@ class BasicAgent:
|
|
24 |
If you are asked for a comma separated list, apply the above rules depending of whether the element
|
25 |
to be put in the list is a number or a string."""
|
26 |
)
|
27 |
-
|
28 |
-
|
|
|
|
|
|
|
|
|
29 |
def check_commutativity(self):
|
30 |
-
# Define the set S
|
31 |
S = ['a', 'b', 'c', 'd', 'e']
|
32 |
counter_example_elements = set()
|
33 |
-
|
34 |
-
# Define a map for converting letters to table indices
|
35 |
index = {'a': 0, 'b': 1, 'c': 2, 'd': 3, 'e': 4}
|
36 |
-
|
37 |
-
# Operation table as provided
|
38 |
self.operation_table = [
|
39 |
-
['a', 'b', 'c', 'b', 'd'],
|
40 |
-
['b', 'c', 'a', 'e', 'c'],
|
41 |
-
['c', 'a', 'b', 'b', 'a'],
|
42 |
-
['b', 'e', 'b', 'e', 'd'],
|
43 |
-
['d', 'b', 'a', 'd', 'c']
|
44 |
]
|
45 |
-
|
46 |
-
# Check for commutativity violations (x * y != y * x)
|
47 |
for x in S:
|
48 |
for y in S:
|
49 |
-
# Convert x and y to their corresponding table indices
|
50 |
x_idx = index[x]
|
51 |
y_idx = index[y]
|
52 |
-
|
53 |
-
# Check if the operation does not commute
|
54 |
if self.operation_table[x_idx][y_idx] != self.operation_table[y_idx][x_idx]:
|
55 |
-
# If not commutative, add both elements to the set
|
56 |
counter_example_elements.add(x)
|
57 |
counter_example_elements.add(y)
|
58 |
-
|
59 |
-
# Return the sorted list of elements involved in violations
|
60 |
-
return sorted(counter_example_elements)
|
61 |
|
62 |
-
|
63 |
def maybe_reversed(self, text: str) -> bool:
|
64 |
words = text.split()
|
65 |
reversed_ratio = sum(
|
@@ -71,7 +61,7 @@ class BasicAgent:
|
|
71 |
return reversed_ratio > 0.3
|
72 |
|
73 |
def solve_riddle(self, question: str) -> str:
|
74 |
-
question = question[::-1]
|
75 |
if "opposite of the word" in question:
|
76 |
match = re.search(r"opposite of the word ['\"](\w+)['\"]", question)
|
77 |
if match:
|
@@ -81,27 +71,21 @@ class BasicAgent:
|
|
81 |
"true": "false", "yes": "no", "black": "white"
|
82 |
}
|
83 |
opposite = opposites.get(word, f"UNKNOWN_OPPOSITE_OF_{word}")
|
84 |
-
|
85 |
-
|
86 |
-
return "FINAL ANSWER: COULD_NOT_SOLVE"
|
87 |
|
88 |
def __call__(self, question: str) -> str:
|
89 |
print(f"Received question: {question[:50]}...")
|
90 |
-
# If question relates to commutativity
|
91 |
if "commutative" in question.lower():
|
92 |
-
|
93 |
-
final_answer = ", ".join(counter_example_elements)
|
94 |
-
return f"FINAL ANSWER:{final_answer}"
|
95 |
-
|
96 |
if self.maybe_reversed(question):
|
97 |
print("Detected likely reversed riddle.")
|
98 |
return self.solve_riddle(question)
|
99 |
-
return "
|
100 |
|
101 |
|
102 |
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
103 |
space_id = os.getenv("SPACE_ID")
|
104 |
-
|
105 |
if profile:
|
106 |
username = f"{profile.username}"
|
107 |
print("User logged in.")
|
@@ -116,12 +100,9 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
116 |
try:
|
117 |
agent = BasicAgent()
|
118 |
except Exception as e:
|
119 |
-
print(f"Error instantiating agent: {e}")
|
120 |
return f"Error initializing agent: {e}", None
|
121 |
|
122 |
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
|
123 |
-
print(agent_code)
|
124 |
-
|
125 |
try:
|
126 |
response = requests.get(questions_url, timeout=15)
|
127 |
response.raise_for_status()
|
@@ -185,4 +166,4 @@ with gr.Blocks() as demo:
|
|
185 |
|
186 |
if __name__ == "__main__":
|
187 |
print("Launching Gradio Interface for Basic Agent Evaluation...")
|
188 |
-
demo.launch(debug=True, share=False)
|
|
|
24 |
If you are asked for a comma separated list, apply the above rules depending of whether the element
|
25 |
to be put in the list is a number or a string."""
|
26 |
)
|
27 |
+
|
28 |
+
def format_final_answer(self, answer: str) -> str:
|
29 |
+
"""Ensure final answer matches required format"""
|
30 |
+
cleaned = " ".join(answer.split())
|
31 |
+
return f"FINAL ANSWER: {cleaned}"
|
32 |
+
|
33 |
def check_commutativity(self):
|
|
|
34 |
S = ['a', 'b', 'c', 'd', 'e']
|
35 |
counter_example_elements = set()
|
|
|
|
|
36 |
index = {'a': 0, 'b': 1, 'c': 2, 'd': 3, 'e': 4}
|
|
|
|
|
37 |
self.operation_table = [
|
38 |
+
['a', 'b', 'c', 'b', 'd'],
|
39 |
+
['b', 'c', 'a', 'e', 'c'],
|
40 |
+
['c', 'a', 'b', 'b', 'a'],
|
41 |
+
['b', 'e', 'b', 'e', 'd'],
|
42 |
+
['d', 'b', 'a', 'd', 'c']
|
43 |
]
|
|
|
|
|
44 |
for x in S:
|
45 |
for y in S:
|
|
|
46 |
x_idx = index[x]
|
47 |
y_idx = index[y]
|
|
|
|
|
48 |
if self.operation_table[x_idx][y_idx] != self.operation_table[y_idx][x_idx]:
|
|
|
49 |
counter_example_elements.add(x)
|
50 |
counter_example_elements.add(y)
|
51 |
+
return self.format_final_answer(", ".join(sorted(counter_example_elements)))
|
|
|
|
|
52 |
|
|
|
53 |
def maybe_reversed(self, text: str) -> bool:
|
54 |
words = text.split()
|
55 |
reversed_ratio = sum(
|
|
|
61 |
return reversed_ratio > 0.3
|
62 |
|
63 |
def solve_riddle(self, question: str) -> str:
|
64 |
+
question = question[::-1]
|
65 |
if "opposite of the word" in question:
|
66 |
match = re.search(r"opposite of the word ['\"](\w+)['\"]", question)
|
67 |
if match:
|
|
|
71 |
"true": "false", "yes": "no", "black": "white"
|
72 |
}
|
73 |
opposite = opposites.get(word, f"UNKNOWN_OPPOSITE_OF_{word}")
|
74 |
+
return self.format_final_answer(opposite)
|
75 |
+
return self.format_final_answer("COULD_NOT_SOLVE")
|
|
|
76 |
|
77 |
def __call__(self, question: str) -> str:
|
78 |
print(f"Received question: {question[:50]}...")
|
|
|
79 |
if "commutative" in question.lower():
|
80 |
+
return self.check_commutativity()
|
|
|
|
|
|
|
81 |
if self.maybe_reversed(question):
|
82 |
print("Detected likely reversed riddle.")
|
83 |
return self.solve_riddle(question)
|
84 |
+
return self.format_final_answer("NOT_A_RIDDLE")
|
85 |
|
86 |
|
87 |
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
88 |
space_id = os.getenv("SPACE_ID")
|
|
|
89 |
if profile:
|
90 |
username = f"{profile.username}"
|
91 |
print("User logged in.")
|
|
|
100 |
try:
|
101 |
agent = BasicAgent()
|
102 |
except Exception as e:
|
|
|
103 |
return f"Error initializing agent: {e}", None
|
104 |
|
105 |
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
|
|
|
|
|
106 |
try:
|
107 |
response = requests.get(questions_url, timeout=15)
|
108 |
response.raise_for_status()
|
|
|
166 |
|
167 |
if __name__ == "__main__":
|
168 |
print("Launching Gradio Interface for Basic Agent Evaluation...")
|
169 |
+
demo.launch(debug=True, share=False)
|