magdap116 commited on
Commit
4cf65bd
·
verified ·
1 Parent(s): 39d9be4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -5
app.py CHANGED
@@ -5,6 +5,9 @@ import inspect
5
  import pandas as pd
6
  from smolagents import DuckDuckGoSearchTool,HfApiModel,load_tool, CodeAgent
7
  from smolagents import CodeAgent
 
 
 
8
 
9
  # (Keep Constants as is)
10
  # --- Constants ---
@@ -12,7 +15,8 @@ DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
12
 
13
  # --- Basic Agent Definition ---
14
  # ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
15
-
 
16
 
17
  web_search = DuckDuckGoSearchTool()
18
 
@@ -21,7 +25,28 @@ web_search = DuckDuckGoSearchTool()
21
  # If the agent does not answer, the model is overloaded, please use another model or the following Hugging Face Endpoint that also contains qwen2.5 coder:
22
  # model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud'
23
 
24
- model = HfApiModel(model_id='meta-llama/Llama-3.3-70B-Instruct', max_tokens=1024)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25
 
26
 
27
  class BasicAgent:
@@ -106,12 +131,20 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
106
  print(f"Skipping item with missing task_id or question: {item}")
107
  continue
108
  try:
109
- submitted_answer = agent(question_text)
 
 
 
 
 
 
 
 
110
  answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
111
  results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
112
  except Exception as e:
113
- print(f"Error running agent on task {task_id}: {e}")
114
- results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"AGENT ERROR: {e}"})
115
 
116
  if not answers_payload:
117
  print("Agent did not produce any answers to submit.")
 
5
  import pandas as pd
6
  from smolagents import DuckDuckGoSearchTool,HfApiModel,load_tool, CodeAgent
7
  from smolagents import CodeAgent
8
+ import hashlib
9
+ import json
10
+
11
 
12
  # (Keep Constants as is)
13
  # --- Constants ---
 
15
 
16
  # --- Basic Agent Definition ---
17
  # ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
18
+ import os
19
+ os.makedirs("cache", exist_ok=True)
20
 
21
  web_search = DuckDuckGoSearchTool()
22
 
 
25
  # If the agent does not answer, the model is overloaded, please use another model or the following Hugging Face Endpoint that also contains qwen2.5 coder:
26
  # model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud'
27
 
28
+ model = HfApiModel(model_id='mistralai/Mistral-7B-Instruct-v0.2', max_tokens=512)
29
+
30
+
31
+
32
+ def get_cache_key(question: str) -> str:
33
+ return hashlib.sha256(question.encode()).hexdigest()
34
+
35
+ def load_cached_answer(question: str) -> str | None:
36
+ key = get_cache_key(question)
37
+ path = f"cache/{key}.json"
38
+ if os.path.exists(path):
39
+ with open(path, "r") as f:
40
+ data = json.load(f)
41
+ return data.get("answer")
42
+ return None
43
+
44
+ def cache_answer(question: str, answer: str):
45
+ key = get_cache_key(question)
46
+ path = f"cache/{key}.json"
47
+ with open(path, "w") as f:
48
+ json.dump({"question": question, "answer": answer}, f)
49
+
50
 
51
 
52
  class BasicAgent:
 
131
  print(f"Skipping item with missing task_id or question: {item}")
132
  continue
133
  try:
134
+ cached = load_cached_answer(question_text)
135
+ if cached:
136
+ submitted_answer = cached
137
+ print(f"Loaded cached answer for task {task_id}")
138
+ else:
139
+ submitted_answer = agent(question_text)
140
+ cache_answer(question_text, submitted_answer)
141
+ print(f"Generated and cached answer for task {task_id}")
142
+
143
  answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
144
  results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
145
  except Exception as e:
146
+ print(f"Error running agent on task {task_id}: {e}")
147
+ results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"AGENT ERROR: {e}"})
148
 
149
  if not answers_payload:
150
  print("Agent did not produce any answers to submit.")