cowrycode commited on
Commit
b53dcea
·
verified ·
1 Parent(s): ba42855

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -66
app.py CHANGED
@@ -46,48 +46,8 @@ class BasicAgent:
46
 
47
  def __call__(self, question: str) -> str:
48
  #return asyncio.run(self.run(question))
49
- return self.run(question)
50
- #return self.agent.run(question)
51
-
52
-
53
- async def smart_agent(questions_data: List[Dict[str, Any]]) -> List[Dict[str, Any]]:
54
- print("BasicAgent initialized.")
55
- api_key = os.getenv("HF_TOKEN")
56
- llm = HuggingFaceInferenceAPI(
57
- model_name="deepseek-ai/DeepSeek-R1-0528",
58
- token=api_key,
59
- provider="auto"
60
- )
61
-
62
- llamaindex_agent = AgentWorkflow.from_tools_or_functions(
63
- [wikipedia_search_tool, youtube_transcript_tool, youtube_transcript_snippet_tool, round_to_two_decimals_tool, text_inverter_tool, google_web_search_tool],
64
- llm=llm,
65
- )
66
-
67
- results_log = []
68
- answers_payload = []
69
-
70
- for item in questions_data:
71
- task_id = item.get("task_id")
72
- question_text = item.get("question")
73
- if not task_id or question_text is None:
74
- print(f"Skipping item with missing task_id or question: {item}")
75
- continue
76
- try:
77
- submitted_answer = await llamaindex_agent(question_text)
78
- answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
79
- results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
80
- except Exception as e:
81
- print(f"Error running agent on task {task_id}: {e}")
82
- results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"AGENT ERROR: {e}"})
83
-
84
- if not answers_payload:
85
- print("Agent did not produce any answers to submit.")
86
- return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
87
-
88
-
89
- print("✅ All tasks completed.")
90
- return answers_payload
91
 
92
  def run_and_submit_all( profile: gr.OAuthProfile | None):
93
  """
@@ -109,11 +69,11 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
109
  submit_url = f"{api_url}/submit"
110
 
111
  # 1. Instantiate Agent ( modify this part to create your agent)
112
- # try:
113
- # agent = BasicAgent()
114
- # except Exception as e:
115
- # print(f"Error instantiating agent: {e}")
116
- # return f"Error initializing agent: {e}", None
117
  #In the case of an app running as a hugging Face space, this link points toward your codebase ( usefull for others so please keep it public)
118
  agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
119
  print(agent_code)
@@ -163,25 +123,24 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
163
 
164
  #3A
165
 
166
- # async def run_all_questions(questions_data):
167
- # answers_payload = []
168
- # for item in questions_data:
169
- # task_id = item.get("task_id")
170
- # question_text = item.get("question")
171
- # if not task_id or question_text is None:
172
- # print(f"Skipping item with missing task_id or question: {item}")
173
- # continue
174
- # try:
175
- # answer = await agent.run(question_text) # await coroutine
176
- # answers_payload.append({"task_id": task_id, "submitted_answer": answer})
177
- # print(f"Answered Task {task_id}: {answer[:50]}...")
178
- # except Exception as e:
179
- # answers_payload.append({"task_id": task_id, "submitted_answer": f"AGENT ERROR: {e}"})
180
- # print(f"Error on Task {task_id}: {e}")
181
- # return answers_payload
182
-
183
- #answers_payload = asyncio.run(run_all_questions(questions_data))
184
- answers_payload = asyncio.run(smart_agent(questions_data))
185
 
186
  #3B
187
 
 
46
 
47
  def __call__(self, question: str) -> str:
48
  #return asyncio.run(self.run(question))
49
+ #return self.run(question)
50
+ return self.agent.run(question)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
51
 
52
  def run_and_submit_all( profile: gr.OAuthProfile | None):
53
  """
 
69
  submit_url = f"{api_url}/submit"
70
 
71
  # 1. Instantiate Agent ( modify this part to create your agent)
72
+ try:
73
+ agent = BasicAgent()
74
+ except Exception as e:
75
+ print(f"Error instantiating agent: {e}")
76
+ return f"Error initializing agent: {e}", None
77
  #In the case of an app running as a hugging Face space, this link points toward your codebase ( usefull for others so please keep it public)
78
  agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
79
  print(agent_code)
 
123
 
124
  #3A
125
 
126
+ async def run_all_questions(questions_data):
127
+ answers_payload = []
128
+ for item in questions_data:
129
+ task_id = item.get("task_id")
130
+ question_text = item.get("question")
131
+ if not task_id or question_text is None:
132
+ print(f"Skipping item with missing task_id or question: {item}")
133
+ continue
134
+ try:
135
+ answer = await agent.run(question_text) # await coroutine
136
+ answers_payload.append({"task_id": task_id, "submitted_answer": answer})
137
+ print(f"Answered Task {task_id}: {answer[:50]}...")
138
+ except Exception as e:
139
+ answers_payload.append({"task_id": task_id, "submitted_answer": f"AGENT ERROR: {e}"})
140
+ print(f"Error on Task {task_id}: {e}")
141
+ return answers_payload
142
+
143
+ answers_payload = asyncio.run(run_all_questions(questions_data))
 
144
 
145
  #3B
146