aelin commited on
Commit
0056801
·
1 Parent(s): a1074ab

Adds cache loading and verbose cache updates

Browse files
Files changed (2) hide show
  1. app.py +3 -18
  2. utils.py +10 -0
app.py CHANGED
@@ -8,7 +8,7 @@ from llama_index.llms.huggingface_api import HuggingFaceInferenceAPI
8
  from llama_index.core.agent.workflow import AgentWorkflow
9
  from _tools import tools
10
  import asyncio
11
- from utils import cache_answers, update_cache_answer, get_cached_answer
12
 
13
  # (Keep Constants as is)
14
  # --- Constants ---
@@ -114,23 +114,6 @@ def fetch_questions(questions_url):
114
 
115
  return None, f"An unexpected error occurred fetching questions: {e}"
116
 
117
- async def fetch_file(question: Question) -> str | None:
118
- """
119
- Fetch files from the provided list of file paths.
120
- """
121
- file_url = f"{DEFAULT_API_URL}/files"
122
-
123
- try:
124
- response = requests.get(f"{file_url}/{question['task_id']}", timeout=15)
125
- response.raise_for_status()
126
-
127
- print(f"File {question['task_id']} fetched successfully.")
128
- return response.content
129
-
130
- except requests.exceptions.RequestException as e:
131
- print(f"Error fetching file {question['task_id']}: {e}")
132
- return None
133
-
134
  async def run_agent_on_questions(agent: BasicAgent, questions_data: Questions):
135
  results_log = []
136
  answers_payload = []
@@ -256,7 +239,9 @@ async def run_and_submit_all(profile: gr.OAuthProfile | None):
256
 
257
  submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload}
258
  status_update = f"Agent finished. Submitting {len(answers_payload)} answers for user '{username}'..."
 
259
  print(status_update)
 
260
 
261
  return submit_answers(submit_url, submission_data, results_log)
262
 
 
8
  from llama_index.core.agent.workflow import AgentWorkflow
9
  from _tools import tools
10
  import asyncio
11
+ from utils import cache_answers, update_cache_answer, get_cached_answer, load_cache
12
 
13
  # (Keep Constants as is)
14
  # --- Constants ---
 
114
 
115
  return None, f"An unexpected error occurred fetching questions: {e}"
116
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
117
  async def run_agent_on_questions(agent: BasicAgent, questions_data: Questions):
118
  results_log = []
119
  answers_payload = []
 
239
 
240
  submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload}
241
  status_update = f"Agent finished. Submitting {len(answers_payload)} answers for user '{username}'..."
242
+
243
  print(status_update)
244
+ print(f"Cached answers: {load_cache()}")
245
 
246
  return submit_answers(submit_url, submission_data, results_log)
247
 
utils.py CHANGED
@@ -33,11 +33,21 @@ def cache_answers(questions, cache_path: str = CACHE_FILE) -> AnswerCache:
33
  return cache
34
 
35
  def update_cache_answer(task_id: str, answer: str, is_correct: bool = False, cache_path: str = CACHE_FILE):
 
 
 
 
 
36
  cache = load_cache(cache_path)
37
  cache[task_id] = {"answer": answer, "isCorrect": is_correct}
38
  save_cache(cache, cache_path)
39
 
40
 
41
  def get_cached_answer(task_id: str, cache_path: str = CACHE_FILE) -> Optional[AnswerCacheEntry]:
 
 
 
42
  cache = load_cache(cache_path)
 
 
43
  return cache.get(task_id)
 
33
  return cache
34
 
35
  def update_cache_answer(task_id: str, answer: str, is_correct: bool = False, cache_path: str = CACHE_FILE):
36
+ """
37
+ Update the cache with the answer and its correctness status.
38
+ """
39
+ print(f"Updating cache for task ID {task_id} with answer: {answer} and isCorrect: {is_correct}")
40
+
41
  cache = load_cache(cache_path)
42
  cache[task_id] = {"answer": answer, "isCorrect": is_correct}
43
  save_cache(cache, cache_path)
44
 
45
 
46
  def get_cached_answer(task_id: str, cache_path: str = CACHE_FILE) -> Optional[AnswerCacheEntry]:
47
+ """
48
+ Retrieve the cached answer for a given task ID.
49
+ """
50
  cache = load_cache(cache_path)
51
+ print(f"Cached answer for task ID {task_id}: {cache.get(task_id)}")
52
+
53
  return cache.get(task_id)