pateas commited on
Commit
226bbb9
·
unverified ·
1 Parent(s): 89cdc9f

rename app file

Browse files
Files changed (2) hide show
  1. app.py +16 -100
  2. app_dev.py +0 -133
app.py CHANGED
@@ -1,28 +1,25 @@
1
- import inspect
2
  import os
3
 
4
  import gradio as gr
5
  import pandas as pd
6
  import requests
 
 
7
 
8
- # (Keep Constants as is)
9
- # --- Constants ---
10
- DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
11
 
 
12
 
13
- # --- Basic Agent Definition ---
14
- # ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
15
- class BasicAgent:
16
- def __init__(self):
17
- print("BasicAgent initialized.")
18
-
19
- def __call__(self, question: str) -> str:
20
- print(f"Agent received question (first 50 chars): {question[:50]}...")
21
- fixed_answer = "This is a default answer."
22
- print(f"Agent returning fixed answer: {fixed_answer}")
23
- return fixed_answer
24
 
 
 
 
 
 
25
 
 
26
  def run_and_submit_all(profile: gr.OAuthProfile | None):
27
  """
28
  Fetches all questions, runs the BasicAgent on them, submits all answers,
@@ -37,74 +34,12 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
37
  else:
38
  print("User not logged in.")
39
  return "Please Login to Hugging Face with the button.", None
40
-
41
- api_url = DEFAULT_API_URL
42
- questions_url = f"{api_url}/questions"
43
- submit_url = f"{api_url}/submit"
44
-
45
- # 1. Instantiate Agent ( modify this part to create your agent)
46
- try:
47
- agent = BasicAgent()
48
- except Exception as e:
49
- print(f"Error instantiating agent: {e}")
50
- return f"Error initializing agent: {e}", None
51
  # 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)
52
  agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
53
  print(agent_code)
54
-
55
- # 2. Fetch Questions
56
- print(f"Fetching questions from: {questions_url}")
57
- try:
58
- response = requests.get(questions_url, timeout=15)
59
- response.raise_for_status()
60
- questions_data = response.json()
61
- if not questions_data:
62
- print("Fetched questions list is empty.")
63
- return "Fetched questions list is empty or invalid format.", None
64
- print(f"Fetched {len(questions_data)} questions.")
65
- except requests.exceptions.RequestException as e:
66
- print(f"Error fetching questions: {e}")
67
- return f"Error fetching questions: {e}", None
68
- except Exception as e:
69
- print(f"An unexpected error occurred fetching questions: {e}")
70
- return f"An unexpected error occurred fetching questions: {e}", None
71
-
72
- # 3. Run your Agent
73
- results_log = []
74
- answers_payload = []
75
- print(f"Running agent on {len(questions_data)} questions...")
76
- for item in questions_data:
77
- task_id = item.get("task_id")
78
- question_text = item.get("question")
79
- if not task_id or question_text is 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(
85
- {"task_id": task_id, "submitted_answer": submitted_answer}
86
- )
87
- results_log.append(
88
- {
89
- "Task ID": task_id,
90
- "Question": question_text,
91
- "Submitted Answer": submitted_answer,
92
- }
93
- )
94
- except Exception as e:
95
- print(f"Error running agent on task {task_id}: {e}")
96
- results_log.append(
97
- {
98
- "Task ID": task_id,
99
- "Question": question_text,
100
- "Submitted Answer": f"AGENT ERROR: {e}",
101
- }
102
- )
103
-
104
- if not answers_payload:
105
- print("Agent did not produce any answers to submit.")
106
- return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
107
-
108
  # 4. Prepare Submission
109
  submission_data = {
110
  "username": username.strip(),
@@ -188,30 +123,11 @@ with gr.Blocks() as demo:
188
 
189
  run_button.click(fn=run_and_submit_all, outputs=[status_output, results_table])
190
 
 
191
  if __name__ == "__main__":
192
  print("\n" + "-" * 30 + " App Starting " + "-" * 30)
193
  # Check for SPACE_HOST and SPACE_ID at startup for information
194
  space_host_startup = os.getenv("SPACE_HOST")
195
  space_id_startup = os.getenv("SPACE_ID") # Get SPACE_ID at startup
196
 
197
- if space_host_startup:
198
- print(f"✅ SPACE_HOST found: {space_host_startup}")
199
- print(f" Runtime URL should be: https://{space_host_startup}.hf.space")
200
- else:
201
- print("ℹ️ SPACE_HOST environment variable not found (running locally?).")
202
-
203
- if space_id_startup: # Print repo URLs if SPACE_ID is found
204
- print(f"✅ SPACE_ID found: {space_id_startup}")
205
- print(f" Repo URL: https://huggingface.co/spaces/{space_id_startup}")
206
- print(
207
- f" Repo Tree URL: https://huggingface.co/spaces/{space_id_startup}/tree/main"
208
- )
209
- else:
210
- print(
211
- "ℹ️ SPACE_ID environment variable not found (running locally?). Repo URL cannot be determined."
212
- )
213
-
214
- print("-" * (60 + len(" App Starting ")) + "\n")
215
-
216
- print("Launching Gradio Interface for Basic Agent Evaluation...")
217
  demo.launch(debug=True, share=False)
 
 
1
  import os
2
 
3
  import gradio as gr
4
  import pandas as pd
5
  import requests
6
+ from langfuse import get_client
7
+ from openinference.instrumentation.smolagents import SmolagentsInstrumentor
8
 
9
+ from agent import BasicAgent
10
+ from fetch import DEFAULT_API_URL, fetch_questions, run_agent
 
11
 
12
+ submit_url = f"{DEFAULT_API_URL}/submit"
13
 
14
+ langfuse = get_client()
 
 
 
 
 
 
 
 
 
 
15
 
16
+ # Verify connection
17
+ if langfuse.auth_check():
18
+ print("Langfuse client is authenticated and ready!")
19
+ else:
20
+ print("Authentication failed. Please check your credentials and host.")
21
 
22
+ SmolagentsInstrumentor().instrument()
23
  def run_and_submit_all(profile: gr.OAuthProfile | None):
24
  """
25
  Fetches all questions, runs the BasicAgent on them, submits all answers,
 
34
  else:
35
  print("User not logged in.")
36
  return "Please Login to Hugging Face with the button.", None
 
 
 
 
 
 
 
 
 
 
 
37
  # 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)
38
  agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
39
  print(agent_code)
40
+ agent = BasicAgent()
41
+ questions_data = fetch_questions()
42
+ answers_payload, results_log = run_agent(agent, questions_data)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
43
  # 4. Prepare Submission
44
  submission_data = {
45
  "username": username.strip(),
 
123
 
124
  run_button.click(fn=run_and_submit_all, outputs=[status_output, results_table])
125
 
126
+
127
  if __name__ == "__main__":
128
  print("\n" + "-" * 30 + " App Starting " + "-" * 30)
129
  # Check for SPACE_HOST and SPACE_ID at startup for information
130
  space_host_startup = os.getenv("SPACE_HOST")
131
  space_id_startup = os.getenv("SPACE_ID") # Get SPACE_ID at startup
132
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
133
  demo.launch(debug=True, share=False)
app_dev.py DELETED
@@ -1,133 +0,0 @@
1
- import os
2
-
3
- import gradio as gr
4
- import pandas as pd
5
- import requests
6
- from langfuse import get_client
7
- from openinference.instrumentation.smolagents import SmolagentsInstrumentor
8
-
9
- from agent import BasicAgent
10
- from fetch import DEFAULT_API_URL, fetch_questions, run_agent
11
-
12
- submit_url = f"{DEFAULT_API_URL}/submit"
13
-
14
- langfuse = get_client()
15
-
16
- # Verify connection
17
- if langfuse.auth_check():
18
- print("Langfuse client is authenticated and ready!")
19
- else:
20
- print("Authentication failed. Please check your credentials and host.")
21
-
22
- SmolagentsInstrumentor().instrument()
23
- def run_and_submit_all(profile: gr.OAuthProfile | None):
24
- """
25
- Fetches all questions, runs the BasicAgent on them, submits all answers,
26
- and displays the results.
27
- """
28
- # --- Determine HF Space Runtime URL and Repo URL ---
29
- space_id = os.getenv("SPACE_ID") # Get the SPACE_ID for sending link to the code
30
-
31
- if profile:
32
- username = f"{profile.username}"
33
- print(f"User logged in: {username}")
34
- else:
35
- print("User not logged in.")
36
- return "Please Login to Hugging Face with the button.", None
37
- # 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)
38
- agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
39
- print(agent_code)
40
- agent = BasicAgent()
41
- questions_data = fetch_questions()
42
- answers_payload, results_log = run_agent(agent, questions_data)
43
- # 4. Prepare Submission
44
- submission_data = {
45
- "username": username.strip(),
46
- "agent_code": agent_code,
47
- "answers": answers_payload,
48
- }
49
- status_update = f"Agent finished. Submitting {len(answers_payload)} answers for user '{username}'..."
50
- print(status_update)
51
-
52
- # 5. Submit
53
- print(f"Submitting {len(answers_payload)} answers to: {submit_url}")
54
- try:
55
- response = requests.post(submit_url, json=submission_data, timeout=60)
56
- response.raise_for_status()
57
- result_data = response.json()
58
- final_status = (
59
- f"Submission Successful!\n"
60
- f"User: {result_data.get('username')}\n"
61
- f"Overall Score: {result_data.get('score', 'N/A')}% "
62
- f"({result_data.get('correct_count', '?')}/{result_data.get('total_attempted', '?')} correct)\n"
63
- f"Message: {result_data.get('message', 'No message received.')}"
64
- )
65
- print("Submission successful.")
66
- results_df = pd.DataFrame(results_log)
67
- return final_status, results_df
68
- except requests.exceptions.HTTPError as e:
69
- error_detail = f"Server responded with status {e.response.status_code}."
70
- try:
71
- error_json = e.response.json()
72
- error_detail += f" Detail: {error_json.get('detail', e.response.text)}"
73
- except requests.exceptions.JSONDecodeError:
74
- error_detail += f" Response: {e.response.text[:500]}"
75
- status_message = f"Submission Failed: {error_detail}"
76
- print(status_message)
77
- results_df = pd.DataFrame(results_log)
78
- return status_message, results_df
79
- except requests.exceptions.Timeout:
80
- status_message = "Submission Failed: The request timed out."
81
- print(status_message)
82
- results_df = pd.DataFrame(results_log)
83
- return status_message, results_df
84
- except requests.exceptions.RequestException as e:
85
- status_message = f"Submission Failed: Network error - {e}"
86
- print(status_message)
87
- results_df = pd.DataFrame(results_log)
88
- return status_message, results_df
89
- except Exception as e:
90
- status_message = f"An unexpected error occurred during submission: {e}"
91
- print(status_message)
92
- results_df = pd.DataFrame(results_log)
93
- return status_message, results_df
94
-
95
-
96
- # --- Build Gradio Interface using Blocks ---
97
- with gr.Blocks() as demo:
98
- gr.Markdown("# Basic Agent Evaluation Runner")
99
- gr.Markdown(
100
- """
101
- **Instructions:**
102
-
103
- 1. Please clone this space, then modify the code to define your agent's logic, the tools, the necessary packages, etc ...
104
- 2. Log in to your Hugging Face account using the button below. This uses your HF username for submission.
105
- 3. Click 'Run Evaluation & Submit All Answers' to fetch questions, run your agent, submit answers, and see the score.
106
-
107
- ---
108
- **Disclaimers:**
109
- 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).
110
- This space provides a basic setup and is intentionally sub-optimal to encourage you to develop your own, more robust solution. For instance for the delay process of the submit button, a solution could be to cache the answers and submit in a seperate action or even to answer the questions in async.
111
- """
112
- )
113
-
114
- gr.LoginButton()
115
-
116
- run_button = gr.Button("Run Evaluation & Submit All Answers")
117
-
118
- status_output = gr.Textbox(
119
- label="Run Status / Submission Result", lines=5, interactive=False
120
- )
121
- # Removed max_rows=10 from DataFrame constructor
122
- results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
123
-
124
- run_button.click(fn=run_and_submit_all, outputs=[status_output, results_table])
125
-
126
-
127
- if __name__ == "__main__":
128
- print("\n" + "-" * 30 + " App Starting " + "-" * 30)
129
- # Check for SPACE_HOST and SPACE_ID at startup for information
130
- space_host_startup = os.getenv("SPACE_HOST")
131
- space_id_startup = os.getenv("SPACE_ID") # Get SPACE_ID at startup
132
-
133
- demo.launch(debug=True, share=False)