Frazer2810 commited on
Commit
0bebf4e
·
verified ·
1 Parent(s): 435c072

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +61 -162
app.py CHANGED
@@ -1,195 +1,94 @@
 
1
  import os
2
- import gradio as gr
3
  import requests
4
- import inspect
5
  import pandas as pd
6
- from dotenv import load_dotenv
7
- from agent import BasicAgent # Import the agent from agent.py
8
 
9
- # Load environment variables
10
- load_dotenv()
11
-
12
- # --- Constants ---
13
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
14
 
15
- def run_and_submit_all(profile: gr.OAuthProfile | None):
16
- """
17
- Fetches all questions, runs the BasicAgent on them, submits all answers,
18
- and displays the results.
19
- """
20
- # --- Determine HF Space Runtime URL and Repo URL ---
21
- space_id = os.getenv("SPACE_ID")
22
 
23
- if profile:
24
- username = f"{profile.username}"
25
- print(f"User logged in: {username}")
26
- else:
27
- print("User not logged in.")
28
- return "Please Login to Hugging Face with the button.", None
29
 
30
- api_url = DEFAULT_API_URL
31
- questions_url = f"{api_url}/questions"
32
- submit_url = f"{api_url}/submit"
33
 
34
- # 1. Instantiate Agent
 
 
 
 
 
 
 
 
35
  try:
36
  agent = BasicAgent()
37
  except Exception as e:
38
- print(f"Error instantiating agent: {e}")
39
  return f"Error initializing agent: {e}", None
40
-
 
 
41
  agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
42
- print(agent_code)
43
 
44
- # 2. Fetch Questions
45
- print(f"Fetching questions from: {questions_url}")
46
  try:
47
- response = requests.get(questions_url, timeout=15)
48
- response.raise_for_status()
49
- questions_data = response.json()
50
- if not questions_data:
51
- print("Fetched questions list is empty.")
52
- return "Fetched questions list is empty or invalid format.", None
53
- print(f"Fetched {len(questions_data)} questions.")
54
- except requests.exceptions.RequestException as e:
55
- print(f"Error fetching questions: {e}")
56
- return f"Error fetching questions: {e}", None
57
- except requests.exceptions.JSONDecodeError as e:
58
- print(f"Error decoding JSON response from questions endpoint: {e}")
59
- print(f"Response text: {response.text[:500]}")
60
- return f"Error decoding server response for questions: {e}", None
61
  except Exception as e:
62
- print(f"An unexpected error occurred fetching questions: {e}")
63
- return f"An unexpected error occurred fetching questions: {e}", None
64
 
65
- # 3. Run your Agent
66
  results_log = []
67
  answers_payload = []
68
- print(f"Running agent on {len(questions_data)} questions...")
69
  for item in questions_data:
70
  task_id = item.get("task_id")
71
- question_text = item.get("question")
72
- if not task_id or question_text is None:
73
- print(f"Skipping item with missing task_id or question: {item}")
74
- continue
75
  try:
76
- submitted_answer = agent(question_text)
77
- answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
78
- results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
79
  except Exception as e:
80
- print(f"Error running agent on task {task_id}: {e}")
81
- results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"AGENT ERROR: {e}"})
82
-
83
- if not answers_payload:
84
- print("Agent did not produce any answers to submit.")
85
- return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
86
-
87
- # 4. Prepare Submission
88
- submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload}
89
- status_update = f"Agent finished. Submitting {len(answers_payload)} answers for user '{username}'..."
90
- print(status_update)
91
 
92
- # 5. Submit
93
- print(f"Submitting {len(answers_payload)} answers to: {submit_url}")
94
  try:
95
- response = requests.post(submit_url, json=submission_data, timeout=60)
96
- response.raise_for_status()
97
- result_data = response.json()
98
- final_status = (
99
- f"Submission Successful!\n"
100
- f"User: {result_data.get('username')}\n"
101
- f"Overall Score: {result_data.get('score', 'N/A')}% "
102
- f"({result_data.get('correct_count', '?')}/{result_data.get('total_attempted', '?')} correct)\n"
103
- f"Message: {result_data.get('message', 'No message received.')}"
104
  )
105
- print("Submission successful.")
106
- results_df = pd.DataFrame(results_log)
107
- return final_status, results_df
108
- except requests.exceptions.HTTPError as e:
109
- error_detail = f"Server responded with status {e.response.status_code}."
110
- try:
111
- error_json = e.response.json()
112
- error_detail += f" Detail: {error_json.get('detail', e.response.text)}"
113
- except requests.exceptions.JSONDecodeError:
114
- error_detail += f" Response: {e.response.text[:500]}"
115
- status_message = f"Submission Failed: {error_detail}"
116
- print(status_message)
117
- results_df = pd.DataFrame(results_log)
118
- return status_message, results_df
119
- except requests.exceptions.Timeout:
120
- status_message = "Submission Failed: The request timed out."
121
- print(status_message)
122
- results_df = pd.DataFrame(results_log)
123
- return status_message, results_df
124
- except requests.exceptions.RequestException as e:
125
- status_message = f"Submission Failed: Network error - {e}"
126
- print(status_message)
127
- results_df = pd.DataFrame(results_log)
128
- return status_message, results_df
129
  except Exception as e:
130
- status_message = f"An unexpected error occurred during submission: {e}"
131
- print(status_message)
132
- results_df = pd.DataFrame(results_log)
133
- return status_message, results_df
134
-
135
- # --- Build Gradio Interface using Blocks ---
136
- with gr.Blocks() as demo:
137
- gr.Markdown("# LangGraph OpenAI Agent Evaluation")
138
- gr.Markdown(
139
- """
140
- **Instructions:**
141
 
142
- 1. This agent uses LangGraph with OpenAI GPT-4 to answer questions.
143
- 2. Log in to your Hugging Face account using the button below.
144
- 3. Click 'Run Evaluation & Submit All Answers' to start the evaluation.
145
-
146
- **Features:**
147
- - Mathematical calculations
148
- - Wikipedia search
149
- - Academic paper search on Arxiv
150
-
151
- **Note:** Processing all questions may take some time. Please be patient.
152
- """
153
- )
154
 
 
 
 
155
  gr.LoginButton()
156
-
157
- run_button = gr.Button("Run Evaluation & Submit All Answers")
158
-
159
- status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
160
- results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
161
-
162
- run_button.click(
163
- fn=run_and_submit_all,
164
- outputs=[status_output, results_table]
165
- )
166
 
167
  if __name__ == "__main__":
168
- print("\n" + "-"*30 + " App Starting " + "-"*30)
169
- # Check for environment variables
170
- space_host_startup = os.getenv("SPACE_HOST")
171
- space_id_startup = os.getenv("SPACE_ID")
172
- openai_key = os.getenv("OPENAI_KEY")
173
-
174
- if space_host_startup:
175
- print(f"✅ SPACE_HOST found: {space_host_startup}")
176
- print(f" Runtime URL should be: https://{space_host_startup}.hf.space")
177
- else:
178
- print("ℹ️ SPACE_HOST environment variable not found (running locally?).")
179
-
180
- if space_id_startup:
181
- print(f"✅ SPACE_ID found: {space_id_startup}")
182
- print(f" Repo URL: https://huggingface.co/spaces/{space_id_startup}")
183
- print(f" Repo Tree URL: https://huggingface.co/spaces/{space_id_startup}/tree/main")
184
- else:
185
- print("ℹ️ SPACE_ID environment variable not found (running locally?).")
186
-
187
- if openai_key:
188
- print("✅ OPENAI_KEY found")
189
- else:
190
- print("❌ OPENAI_KEY not found - Agent will not work without it!")
191
-
192
- print("-"*(60 + len(" App Starting ")) + "\n")
193
-
194
- print("Launching Gradio Interface for LangGraph OpenAI Agent Evaluation...")
195
- demo.launch(debug=True, share=False)
 
1
+ """Basic Agent Evaluation Runner – GPT-4.1 edition (HF Spaces)"""
2
  import os
 
3
  import requests
4
+ import gradio as gr
5
  import pandas as pd
6
+ from langchain_core.messages import HumanMessage
7
+ from agent import build_graph
8
 
9
+ # --- Constants ------------------------------------------------------------- #
 
 
 
10
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
11
 
12
+ # --- Agent wrapper --------------------------------------------------------- #
13
+ class BasicAgent:
14
+ """LangGraph agent ready for evaluation."""
15
+ def __init__(self):
16
+ print("BasicAgent initialized (using GPT-4.1).")
17
+ # provider="openai" di default
18
+ self.graph = build_graph(provider="openai")
19
 
20
+ def __call__(self, question: str) -> str:
21
+ print(f"Agent received question (first 50 chars): {question[:50]}...")
22
+ msgs = [HumanMessage(content=question)]
23
+ result = self.graph.invoke({"messages": msgs})
24
+ answer = result["messages"][-1].content
25
+ return answer[14:] # strip "FINAL ANSWER: "
26
 
 
 
 
27
 
28
+ # --- Main evaluation logic ------------------------------------------------- #
29
+ def run_and_submit_all(profile: gr.OAuthProfile | None):
30
+ # Verifica login
31
+ if not profile:
32
+ return "Please Login to Hugging Face with the button.", None
33
+ username = profile.username
34
+ print(f"User logged in: {username}")
35
+
36
+ # Crea agent
37
  try:
38
  agent = BasicAgent()
39
  except Exception as e:
 
40
  return f"Error initializing agent: {e}", None
41
+
42
+ # Determina URL spazio (link al codice)
43
+ space_id = os.getenv("SPACE_ID", "unknown-space")
44
  agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
 
45
 
46
+ # --- Fetch domande ------------------------------------------------------ #
 
47
  try:
48
+ resp = requests.get(f"{DEFAULT_API_URL}/questions", timeout=15)
49
+ resp.raise_for_status()
50
+ questions_data = resp.json()
 
 
 
 
 
 
 
 
 
 
 
51
  except Exception as e:
52
+ return f"Error fetching questions: {e}", None
 
53
 
54
+ # --- Rispondi con l'agente --------------------------------------------- #
55
  results_log = []
56
  answers_payload = []
 
57
  for item in questions_data:
58
  task_id = item.get("task_id")
59
+ q_text = item.get("question")
 
 
 
60
  try:
61
+ ans = agent(q_text)
62
+ answers_payload.append({"task_id": task_id, "submitted_answer": ans})
63
+ results_log.append({"Task ID": task_id, "Question": q_text, "Submitted Answer": ans})
64
  except Exception as e:
65
+ results_log.append({"Task ID": task_id, "Question": q_text, "Submitted Answer": f"AGENT ERROR: {e}"})
 
 
 
 
 
 
 
 
 
 
66
 
67
+ # --- Submit ------------------------------------------------------------- #
68
+ submission = {"username": username, "agent_code": agent_code, "answers": answers_payload}
69
  try:
70
+ resp = requests.post(f"{DEFAULT_API_URL}/submit", json=submission, timeout=60)
71
+ resp.raise_for_status()
72
+ data = resp.json()
73
+ status = (
74
+ f"Submission Successful!\nUser: {data.get('username')}\n"
75
+ f"Overall Score: {data.get('score', 'N/A')}% "
76
+ f"({data.get('correct_count', '?')}/{data.get('total_attempted', '?')} correct)\n"
77
+ f"Message: {data.get('message', 'No message received.')}"
 
78
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
79
  except Exception as e:
80
+ status = f"Submission Failed: {e}"
 
 
 
 
 
 
 
 
 
 
81
 
82
+ return status, pd.DataFrame(results_log)
 
 
 
 
 
 
 
 
 
 
 
83
 
84
+ # --- Gradio UI ------------------------------------------------------------- #
85
+ with gr.Blocks() as demo:
86
+ gr.Markdown("# Basic Agent Evaluation Runner (GPT-4.1)")
87
  gr.LoginButton()
88
+ run_btn = gr.Button("Run Evaluation & Submit All Answers")
89
+ status_box = gr.Textbox(lines=5, label="Run Status / Submission Result")
90
+ results_tbl = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
91
+ run_btn.click(fn=run_and_submit_all, outputs=[status_box, results_tbl])
 
 
 
 
 
 
92
 
93
  if __name__ == "__main__":
94
+ demo.launch(debug=True, share=False)