supratipb commited on
Commit
b61a634
·
verified ·
1 Parent(s): f632a0e

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -18
app.py CHANGED
@@ -1,13 +1,11 @@
1
- """ Basic Agent Evaluation Runner"""
2
  import os
3
- import inspect
4
  import gradio as gr
5
  import requests
 
6
  import pandas as pd
7
- from langchain_core.messages import HumanMessage
8
- from agent import build_graph
9
-
10
 
 
 
11
 
12
  # (Keep Constants as is)
13
  # --- Constants ---
@@ -15,22 +13,20 @@ DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
15
 
16
  # --- Basic Agent Definition ---
17
  # ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
18
-
19
-
20
  class BasicAgent:
21
- """A langgraph agent."""
22
  def __init__(self):
 
 
 
 
 
23
  print("BasicAgent initialized.")
24
- self.graph = build_graph()
25
-
26
  def __call__(self, question: str) -> str:
27
  print(f"Agent received question (first 50 chars): {question[:50]}...")
28
- # Wrap the question in a HumanMessage from langchain_core
29
- messages = [HumanMessage(content=question)]
30
- messages = self.graph.invoke({"messages": messages})
31
- answer = messages['messages'][-1].content
32
- print(f"answer is - {answer}")
33
- return answer[14:]
34
 
35
 
36
  def run_and_submit_all( profile: gr.OAuthProfile | None):
@@ -95,6 +91,8 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
95
  continue
96
  try:
97
  submitted_answer = agent(question_text)
 
 
98
  answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
99
  results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
100
  except Exception as e:
@@ -160,11 +158,9 @@ with gr.Blocks() as demo:
160
  gr.Markdown(
161
  """
162
  **Instructions:**
163
-
164
  1. Please clone this space, then modify the code to define your agent's logic, the tools, the necessary packages, etc ...
165
  2. Log in to your Hugging Face account using the button below. This uses your HF username for submission.
166
  3. Click 'Run Evaluation & Submit All Answers' to fetch questions, run your agent, submit answers, and see the score.
167
-
168
  ---
169
  **Disclaimers:**
170
  Once clicking on the "submit button, it can take quite some time ( this is the time for the agent to go through all the questions).
 
 
1
  import os
 
2
  import gradio as gr
3
  import requests
4
+ import inspect
5
  import pandas as pd
 
 
 
6
 
7
+ from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
8
+ from smolagents import OpenAIServerModel, DuckDuckGoSearchTool, CodeAgent, WikipediaSearchTool
9
 
10
  # (Keep Constants as is)
11
  # --- Constants ---
 
13
 
14
  # --- Basic Agent Definition ---
15
  # ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
 
 
16
  class BasicAgent:
 
17
  def __init__(self):
18
+ self.agent = CodeAgent(
19
+ model=OpenAIServerModel(model_id="gpt-4o-mini", api_key="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"),
20
+ tools=[DuckDuckGoSearchTool(), WikipediaSearchTool()],
21
+ add_base_tools=True,
22
+ )
23
  print("BasicAgent initialized.")
 
 
24
  def __call__(self, question: str) -> str:
25
  print(f"Agent received question (first 50 chars): {question[:50]}...")
26
+ # fixed_answer = "This is a default answer."
27
+ fixed_answer = self.agent.run(question)
28
+ print(f"Agent returning fixed answer: {fixed_answer}")
29
+ return fixed_answer
 
 
30
 
31
 
32
  def run_and_submit_all( profile: gr.OAuthProfile | None):
 
91
  continue
92
  try:
93
  submitted_answer = agent(question_text)
94
+ if not isinstance(submitted_answer, str):
95
+ submitted_answer = str(submitted_answer)
96
  answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
97
  results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
98
  except Exception as e:
 
158
  gr.Markdown(
159
  """
160
  **Instructions:**
 
161
  1. Please clone this space, then modify the code to define your agent's logic, the tools, the necessary packages, etc ...
162
  2. Log in to your Hugging Face account using the button below. This uses your HF username for submission.
163
  3. Click 'Run Evaluation & Submit All Answers' to fetch questions, run your agent, submit answers, and see the score.
 
164
  ---
165
  **Disclaimers:**
166
  Once clicking on the "submit button, it can take quite some time ( this is the time for the agent to go through all the questions).