cowrycode commited on
Commit
b222a79
·
verified ·
1 Parent(s): 5bb104e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -30
app.py CHANGED
@@ -48,21 +48,34 @@ class BasicAgent:
48
  return self.run(question)
49
  #return self.agent.run(question)
50
 
51
- def create_llamaindex_agent() -> AgentWorkflow:
52
- api_key = os.getenv("HF_TOKEN")
53
  llm = HuggingFaceInferenceAPI(
54
- model_name="deepseek-ai/DeepSeek-R1-0528",
55
  token=api_key,
56
- provider="auto",
57
- max_iterations=10
58
- )
59
- agent = AgentWorkflow.from_tools_or_functions(
60
- [wikipedia_search_tool, youtube_transcript_tool, youtube_transcript_snippet_tool,
61
- round_to_two_decimals_tool, text_inverter_tool, google_web_search_tool],
62
- llm=llm
63
  )
 
 
 
 
 
64
 
65
- return agent
 
 
 
 
 
 
 
 
 
 
 
 
 
 
66
 
67
  def run_and_submit_all( profile: gr.OAuthProfile | None):
68
  """
@@ -84,7 +97,6 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
84
  submit_url = f"{api_url}/submit"
85
 
86
  # 1. Instantiate Agent ( modify this part to create your agent)
87
- agent = create_llamaindex_agent()
88
  # try:
89
  # agent = BasicAgent()
90
  # except Exception as e:
@@ -139,24 +151,25 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
139
 
140
  #3A
141
 
142
- async def run_all_questions(questions_data):
143
- answers_payload = []
144
- for item in questions_data:
145
- task_id = item.get("task_id")
146
- question_text = item.get("question")
147
- if not task_id or question_text is None:
148
- print(f"Skipping item with missing task_id or question: {item}")
149
- continue
150
- try:
151
- answer = await agent.run(question_text) # await coroutine
152
- answers_payload.append({"task_id": task_id, "submitted_answer": answer})
153
- print(f"Answered Task {task_id}: {answer[:50]}...")
154
- except Exception as e:
155
- answers_payload.append({"task_id": task_id, "submitted_answer": f"AGENT ERROR: {e}"})
156
- print(f"Error on Task {task_id}: {e}")
157
- return answers_payload
158
-
159
- answers_payload = asyncio.run(run_all_questions(questions_data))
 
160
 
161
  #3B
162
 
 
48
  return self.run(question)
49
  #return self.agent.run(question)
50
 
51
+
52
+ async def smart_agent(questions_data: List[Dict[str, Any]]) -> List[Dict[str, Any]]:
53
  llm = HuggingFaceInferenceAPI(
54
+ model_name="deepseek-ai/DeepSeek-R1-0528",
55
  token=api_key,
56
+ provider="auto"
 
 
 
 
 
 
57
  )
58
+
59
+ llamaindex_agent = AgentWorkflow.from_tools_or_functions(
60
+ [wikipedia_search_tool, youtube_transcript_tool, youtube_transcript_snippet_tool, round_to_two_decimals_tool, text_inverter_tool, google_web_search_tool],
61
+ llm=llm,
62
+ )
63
 
64
+ answers_payload = []
65
+ for item in questions_data:
66
+ task_id = item.get("task_id")
67
+ question_text = item.get("question")
68
+ if not task_id or question_text is None:
69
+ print(f"Skipping item with missing task_id or question: {item}")
70
+ continue
71
+ try:
72
+ answer = await llamaindex_agent.run(question_text) # await coroutine
73
+ answers_payload.append({"task_id": task_id, "submitted_answer": answer})
74
+ print(f"Answered Task {task_id}: {answer[:50]}...")
75
+ except Exception as e:
76
+ answers_payload.append({"task_id": task_id, "submitted_answer": f"AGENT ERROR: {e}"})
77
+ print(f"Error on Task {task_id}: {e}")
78
+ return answers_payload
79
 
80
  def run_and_submit_all( profile: gr.OAuthProfile | None):
81
  """
 
97
  submit_url = f"{api_url}/submit"
98
 
99
  # 1. Instantiate Agent ( modify this part to create your agent)
 
100
  # try:
101
  # agent = BasicAgent()
102
  # except Exception as e:
 
151
 
152
  #3A
153
 
154
+ # async def run_all_questions(questions_data):
155
+ # answers_payload = []
156
+ # for item in questions_data:
157
+ # task_id = item.get("task_id")
158
+ # question_text = item.get("question")
159
+ # if not task_id or question_text is None:
160
+ # print(f"Skipping item with missing task_id or question: {item}")
161
+ # continue
162
+ # try:
163
+ # answer = await agent.run(question_text) # await coroutine
164
+ # answers_payload.append({"task_id": task_id, "submitted_answer": answer})
165
+ # print(f"Answered Task {task_id}: {answer[:50]}...")
166
+ # except Exception as e:
167
+ # answers_payload.append({"task_id": task_id, "submitted_answer": f"AGENT ERROR: {e}"})
168
+ # print(f"Error on Task {task_id}: {e}")
169
+ # return answers_payload
170
+
171
+ #answers_payload = asyncio.run(run_all_questions(questions_data))
172
+ answers_payload = asyncio.run(smart_agent())
173
 
174
  #3B
175