rakesh-dvg commited on
Commit
a2d4dd5
·
verified ·
1 Parent(s): e0ce801

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +204 -0
app.py ADDED
@@ -0,0 +1,204 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import gradio as gr
3
+ import requests
4
+ import pandas as pd
5
+ from dotenv import load_dotenv
6
+ from gemini_agent import GeminiAgent
7
+
8
+ # Constants
9
+ DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
10
+
11
+ class BasicAgent:
12
+ def __init__(self):
13
+ print("Initializing the BasicAgent")
14
+
15
+ # Get Gemini API key
16
+ api_key = os.getenv('GOOGLE_API_KEY')
17
+ if not api_key:
18
+ raise ValueError("GOOGLE_API_KEY environment variable not set.")
19
+
20
+ # Initialize GeminiAgent
21
+ self.agent = GeminiAgent(api_key=api_key)
22
+ print("GeminiAgent initialized successfully")
23
+
24
+ def __call__(self, question: str) -> str:
25
+ print(f"Agent received question (first 50 chars): {question[:50]}...")
26
+ final_answer = self.agent.run(question)
27
+ print(f"Agent returning fixed answer: {final_answer}")
28
+ return final_answer
29
+
30
+ def run_and_submit_all(profile: gr.OAuthProfile | None):
31
+ """
32
+ Fetches all questions, runs the BasicAgent on them, submits all answers,
33
+ and displays the results.
34
+ """
35
+ # --- Determine HF Space Runtime URL and Repo URL ---
36
+ space_id = os.getenv("SPACE_ID") # Get the SPACE_ID for sending link to the code
37
+
38
+ if profile:
39
+ username= f"{profile.username}"
40
+ print(f"User logged in: {username}")
41
+ else:
42
+ print("User not logged in.")
43
+ return "Please Login to Hugging Face with the button.", None
44
+
45
+ api_url = DEFAULT_API_URL
46
+ questions_url = f"{api_url}/questions"
47
+ submit_url = f"{api_url}/submit"
48
+
49
+ # 1. Instantiate Agent ( modify this part to create your agent)
50
+ try:
51
+ agent = BasicAgent()
52
+ except Exception as e:
53
+ print(f"Error instantiating agent: {e}")
54
+ return f"Error initializing agent: {e}", None
55
+ # 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)
56
+ agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
57
+ print(agent_code)
58
+
59
+ # 2. Fetch Questions
60
+ print(f"Fetching questions from: {questions_url}")
61
+ try:
62
+ response = requests.get(questions_url, timeout=15)
63
+ response.raise_for_status()
64
+ questions_data = response.json()
65
+ if not questions_data:
66
+ print("Fetched questions list is empty.")
67
+ return "Fetched questions list is empty or invalid format.", None
68
+ print(f"Fetched {len(questions_data)} questions.")
69
+ except requests.exceptions.RequestException as e:
70
+ print(f"Error fetching questions: {e}")
71
+ return f"Error fetching questions: {e}", None
72
+ except requests.exceptions.JSONDecodeError as e:
73
+ print(f"Error decoding JSON response from questions endpoint: {e}")
74
+ print(f"Response text: {response.text[:500]}")
75
+ return f"Error decoding server response for questions: {e}", None
76
+ except Exception as e:
77
+ print(f"An unexpected error occurred fetching questions: {e}")
78
+ return f"An unexpected error occurred fetching questions: {e}", None
79
+
80
+ # 3. Run your Agent
81
+ results_log = []
82
+ answers_payload = []
83
+ print(f"Running agent on {len(questions_data)} questions...")
84
+ for item in questions_data:
85
+ task_id = item.get("task_id")
86
+ question_text = item.get("question")
87
+ if not task_id or question_text is None:
88
+ print(f"Skipping item with missing task_id or question: {item}")
89
+ continue
90
+ try:
91
+ submitted_answer = agent(question_text)
92
+ answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
93
+ results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
94
+ except Exception as e:
95
+ print(f"Error running agent on task {task_id}: {e}")
96
+ results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"AGENT ERROR: {e}"})
97
+
98
+ if not answers_payload:
99
+ print("Agent did not produce any answers to submit.")
100
+ return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
101
+
102
+ # 4. Prepare Submission
103
+ submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload}
104
+ status_update = f"Agent finished. Submitting {len(answers_payload)} answers for user '{username}'..."
105
+ print(status_update)
106
+
107
+ # 5. Submit
108
+ print(f"Submitting {len(answers_payload)} answers to: {submit_url}")
109
+ try:
110
+ response = requests.post(submit_url, json=submission_data, timeout=60)
111
+ response.raise_for_status()
112
+ result_data = response.json()
113
+ final_status = (
114
+ f"Submission Successful!\n"
115
+ f"User: {result_data.get('username')}\n"
116
+ f"Overall Score: {result_data.get('score', 'N/A')}% "
117
+ f"({result_data.get('correct_count', '?')}/{result_data.get('total_attempted', '?')} correct)\n"
118
+ f"Message: {result_data.get('message', 'No message received.')}"
119
+ )
120
+ print("Submission successful.")
121
+ results_df = pd.DataFrame(results_log)
122
+ return final_status, results_df
123
+ except requests.exceptions.HTTPError as e:
124
+ error_detail = f"Server responded with status {e.response.status_code}."
125
+ try:
126
+ error_json = e.response.json()
127
+ error_detail += f" Detail: {error_json.get('detail', e.response.text)}"
128
+ except requests.exceptions.JSONDecodeError:
129
+ error_detail += f" Response: {e.response.text[:500]}"
130
+ status_message = f"Submission Failed: {error_detail}"
131
+ print(status_message)
132
+ results_df = pd.DataFrame(results_log)
133
+ return status_message, results_df
134
+ except requests.exceptions.Timeout:
135
+ status_message = "Submission Failed: The request timed out."
136
+ print(status_message)
137
+ results_df = pd.DataFrame(results_log)
138
+ return status_message, results_df
139
+ except requests.exceptions.RequestException as e:
140
+ status_message = f"Submission Failed: Network error - {e}"
141
+ print(status_message)
142
+ results_df = pd.DataFrame(results_log)
143
+ return status_message, results_df
144
+ except Exception as e:
145
+ status_message = f"An unexpected error occurred during submission: {e}"
146
+ print(status_message)
147
+ results_df = pd.DataFrame(results_log)
148
+ return status_message, results_df
149
+
150
+
151
+ # --- Build Gradio Interface using Blocks ---
152
+ with gr.Blocks() as demo:
153
+ gr.Markdown("# Basic Agent Evaluation Runner")
154
+ gr.Markdown(
155
+ """
156
+ **Instructions:**
157
+
158
+ 1. Please clone this space, then modify the code to define your agent's logic, the tools, the necessary packages, etc ...
159
+ 2. Log in to your Hugging Face account using the button below. This uses your HF username for submission.
160
+ 3. Click 'Run Evaluation & Submit All Answers' to fetch questions, run your agent, submit answers, and see the score.
161
+
162
+ ---
163
+ **Disclaimers:**
164
+ 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).
165
+ 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.
166
+ """
167
+ )
168
+
169
+ gr.LoginButton()
170
+
171
+ run_button = gr.Button("Run Evaluation & Submit All Answers")
172
+
173
+ status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
174
+ # Removed max_rows=10 from DataFrame constructor
175
+ results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
176
+
177
+ run_button.click(
178
+ fn=run_and_submit_all,
179
+ outputs=[status_output, results_table]
180
+ )
181
+
182
+ if __name__ == "__main__":
183
+ print("\n" + "-"*30 + " App Starting " + "-"*30)
184
+ # Check for SPACE_HOST and SPACE_ID at startup for information
185
+ space_host_startup = os.getenv("SPACE_HOST")
186
+ space_id_startup = os.getenv("SPACE_ID") # Get SPACE_ID at startup
187
+
188
+ if space_host_startup:
189
+ print(f"✅ SPACE_HOST found: {space_host_startup}")
190
+ print(f" Runtime URL should be: https://{space_host_startup}.hf.space")
191
+ else:
192
+ print("ℹ️ SPACE_HOST environment variable not found (running locally?).")
193
+
194
+ if space_id_startup: # Print repo URLs if SPACE_ID is found
195
+ print(f"✅ SPACE_ID found: {space_id_startup}")
196
+ print(f" Repo URL: https://huggingface.co/spaces/{space_id_startup}")
197
+ print(f" Repo Tree URL: https://huggingface.co/spaces/{space_id_startup}/tree/main")
198
+ else:
199
+ print("ℹ️ SPACE_ID environment variable not found (running locally?). Repo URL cannot be determined.")
200
+
201
+ print("-"*(60 + len(" App Starting ")) + "\n")
202
+
203
+ print("Launching Gradio Interface for Basic Agent Evaluation...")
204
+ demo.launch(debug=True, share=False)