Update app.py
Browse files
app.py
CHANGED
@@ -3,23 +3,37 @@ import gradio as gr
|
|
3 |
import requests
|
4 |
import inspect
|
5 |
import pandas as pd
|
|
|
|
|
6 |
|
7 |
-
# (Keep Constants as is)
|
8 |
# --- Constants ---
|
9 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
10 |
|
|
|
|
|
|
|
11 |
# --- Basic Agent Definition ---
|
12 |
-
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
13 |
class BasicAgent:
|
14 |
def __init__(self):
|
15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
def __call__(self, question: str) -> str:
|
17 |
print(f"Agent received question (first 50 chars): {question[:50]}...")
|
18 |
-
|
19 |
-
|
20 |
-
|
|
|
|
|
|
|
|
|
|
|
21 |
|
22 |
-
def run_and_submit_all(
|
23 |
"""
|
24 |
Fetches all questions, runs the BasicAgent on them, submits all answers,
|
25 |
and displays the results.
|
@@ -44,7 +58,7 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
44 |
except Exception as e:
|
45 |
print(f"Error instantiating agent: {e}")
|
46 |
return f"Error initializing agent: {e}", None
|
47 |
-
# In the case of an app running as a hugging Face space, this link points toward your codebase (
|
48 |
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
|
49 |
print(agent_code)
|
50 |
|
@@ -80,7 +94,7 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
80 |
print(f"Skipping item with missing task_id or question: {item}")
|
81 |
continue
|
82 |
try:
|
83 |
-
submitted_answer = agent(question_text)
|
84 |
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
85 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
86 |
except Exception as e:
|
@@ -139,18 +153,15 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
139 |
results_df = pd.DataFrame(results_log)
|
140 |
return status_message, results_df
|
141 |
|
142 |
-
|
143 |
# --- Build Gradio Interface using Blocks ---
|
144 |
with gr.Blocks() as demo:
|
145 |
gr.Markdown("# Basic Agent Evaluation Runner")
|
146 |
gr.Markdown(
|
147 |
"""
|
148 |
**Instructions:**
|
149 |
-
|
150 |
1. Please clone this space, then modify the code to define your agent's logic, the tools, the necessary packages, etc ...
|
151 |
2. Log in to your Hugging Face account using the button below. This uses your HF username for submission.
|
152 |
3. Click 'Run Evaluation & Submit All Answers' to fetch questions, run your agent, submit answers, and see the score.
|
153 |
-
|
154 |
---
|
155 |
**Disclaimers:**
|
156 |
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).
|
@@ -163,7 +174,6 @@ with gr.Blocks() as demo:
|
|
163 |
run_button = gr.Button("Run Evaluation & Submit All Answers")
|
164 |
|
165 |
status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
|
166 |
-
# Removed max_rows=10 from DataFrame constructor
|
167 |
results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
|
168 |
|
169 |
run_button.click(
|
@@ -193,4 +203,4 @@ if __name__ == "__main__":
|
|
193 |
print("-"*(60 + len(" App Starting ")) + "\n")
|
194 |
|
195 |
print("Launching Gradio Interface for Basic Agent Evaluation...")
|
196 |
-
demo.launch(debug=True, share=False)
|
|
|
3 |
import requests
|
4 |
import inspect
|
5 |
import pandas as pd
|
6 |
+
import openai
|
7 |
+
from dotenv import load_dotenv
|
8 |
|
|
|
9 |
# --- Constants ---
|
10 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
11 |
|
12 |
+
# --- Load environment variables (for OPENAI_API_KEY) ---
|
13 |
+
load_dotenv()
|
14 |
+
|
15 |
# --- Basic Agent Definition ---
|
|
|
16 |
class BasicAgent:
|
17 |
def __init__(self):
|
18 |
+
# Set up OpenAI client
|
19 |
+
self.api_key = os.getenv("OPENAI_API_KEY")
|
20 |
+
if not self.api_key:
|
21 |
+
raise ValueError("OPENAI_API_KEY environment variable not set.")
|
22 |
+
self.client = openai.OpenAI(api_key=self.api_key)
|
23 |
+
self.model = "gpt-3.5-turbo" # Or "gpt-4o", "gpt-4-turbo", etc.
|
24 |
+
|
25 |
def __call__(self, question: str) -> str:
|
26 |
print(f"Agent received question (first 50 chars): {question[:50]}...")
|
27 |
+
response = self.client.chat.completions.create(
|
28 |
+
model=self.model,
|
29 |
+
messages=[{"role": "user", "content": question}],
|
30 |
+
temperature=0
|
31 |
+
)
|
32 |
+
answer = response.choices[0].message.content.strip()
|
33 |
+
print(f"Agent returning answer: {answer}")
|
34 |
+
return answer
|
35 |
|
36 |
+
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
37 |
"""
|
38 |
Fetches all questions, runs the BasicAgent on them, submits all answers,
|
39 |
and displays the results.
|
|
|
58 |
except Exception as e:
|
59 |
print(f"Error instantiating agent: {e}")
|
60 |
return f"Error initializing agent: {e}", None
|
61 |
+
# In the case of an app running as a hugging Face space, this link points toward your codebase ( useful for others so please keep it public)
|
62 |
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
|
63 |
print(agent_code)
|
64 |
|
|
|
94 |
print(f"Skipping item with missing task_id or question: {item}")
|
95 |
continue
|
96 |
try:
|
97 |
+
submitted_answer = agent(question_text) # FIX: use 'agent', not 'self.agent'
|
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:
|
|
|
153 |
results_df = pd.DataFrame(results_log)
|
154 |
return status_message, results_df
|
155 |
|
|
|
156 |
# --- Build Gradio Interface using Blocks ---
|
157 |
with gr.Blocks() as demo:
|
158 |
gr.Markdown("# Basic Agent Evaluation Runner")
|
159 |
gr.Markdown(
|
160 |
"""
|
161 |
**Instructions:**
|
|
|
162 |
1. Please clone this space, then modify the code to define your agent's logic, the tools, the necessary packages, etc ...
|
163 |
2. Log in to your Hugging Face account using the button below. This uses your HF username for submission.
|
164 |
3. Click 'Run Evaluation & Submit All Answers' to fetch questions, run your agent, submit answers, and see the score.
|
|
|
165 |
---
|
166 |
**Disclaimers:**
|
167 |
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).
|
|
|
174 |
run_button = gr.Button("Run Evaluation & Submit All Answers")
|
175 |
|
176 |
status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
|
|
|
177 |
results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
|
178 |
|
179 |
run_button.click(
|
|
|
203 |
print("-"*(60 + len(" App Starting ")) + "\n")
|
204 |
|
205 |
print("Launching Gradio Interface for Basic Agent Evaluation...")
|
206 |
+
demo.launch(debug=True, share=False)
|