BlueGod01 commited on
Commit
7499168
·
verified ·
1 Parent(s): 08a4e46

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +124 -124
app.py CHANGED
@@ -1,41 +1,96 @@
1
  import os
2
  import gradio as gr
3
  from PIL import Image
4
- from smolagents import (
5
- CodeAgent,
6
- LiteLLMModel, # <-- NEW: for Gemini
7
- DuckDuckGoSearchTool,
8
- Tool
9
- )
10
  import requests
11
- import inspect
12
  import pandas as pd
 
13
 
14
- # (Keep Constants as is)
15
- # --- Constants ---
 
 
 
 
 
16
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
17
 
18
- # --- Basic Agent Definition ---
19
- # ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
20
- class BasicAgent:
21
- def __init__(self):
22
- print("BasicAgent initialized.")
23
- def __call__(self, question: str) -> str:
24
- print(f"Agent received question (first 50 chars): {question[:50]}...")
25
- fixed_answer = "This is a default answer."
26
- print(f"Agent returning fixed answer: {fixed_answer}")
27
- return fixed_answer
28
-
29
- def run_and_submit_all( profile: gr.OAuthProfile | None):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
30
  """
31
- Fetches all questions, runs the BasicAgent on them, submits all answers,
32
  and displays the results.
33
  """
34
- # --- Determine HF Space Runtime URL and Repo URL ---
35
- space_id = os.getenv("SPACE_ID") # Get the SPACE_ID for sending link to the code
36
-
37
  if profile:
38
- username= f"{profile.username}"
39
  print(f"User logged in: {username}")
40
  else:
41
  print("User not logged in.")
@@ -45,75 +100,34 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
45
  questions_url = f"{api_url}/questions"
46
  submit_url = f"{api_url}/submit"
47
 
48
- # 1. Instantiate Agent ( modify this part to create your agent)
 
 
49
  try:
50
-
51
- # 2. Instantiate Internet Search & Browsing Tools
52
- ddg_search = DuckDuckGoSearchTool()
53
-
54
- # 3. Create an Image Execution Tool from the Hugging Face Spaces Engine
55
- image_generation_tool = Tool.from_space(
56
- space_id="black-forest-labs/FLUX.1-schnell",
57
- name="image_generator",
58
- description="Generates a high-quality visual image based on a descriptive text prompt. Returns a PIL Image object."
59
- )
60
-
61
- # 4. Consolidate your toolbox array
62
- all_tools = [
63
- ddg_search,
64
- #visit_webpage,
65
- image_generation_tool
66
- ]
67
-
68
- # 5. Initialize Google Gemini 2.0 Flash model using LiteLLMModel
69
- # The API key must be stored as a secret named GEMINI_API_KEY in the HF Space.
70
- gemini_api_key = os.getenv("GEMINI_API_KEY")
71
- if not gemini_api_key:
72
- raise ValueError("GEMINI_API_KEY environment variable not set. Please add it to Hugging Face Space secrets.")
73
- model = LiteLLMModel(
74
- model_id="gemini/gemini-2.0-flash",
75
- api_key=gemini_api_key
76
- )
77
-
78
- # 6. Initialize the CodeAgent with the Gemini model
79
- agent = CodeAgent(
80
- tools=all_tools,
81
- model=model,
82
- add_base_tools=True,
83
- additional_authorized_imports=["time", "math", "json", "PIL"]
84
- )
85
  except Exception as e:
86
- print(f"Error instantiating agent: {e}")
87
  return f"Error initializing agent: {e}", None
88
- # 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)
89
  agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
90
  print(agent_code)
91
 
92
- # 2. Fetch Questions
93
  print(f"Fetching questions from: {questions_url}")
94
  try:
95
  response = requests.get(questions_url, timeout=15)
96
  response.raise_for_status()
97
  questions_data = response.json()
98
  if not questions_data:
99
- print("Fetched questions list is empty.")
100
- return "Fetched questions list is empty or invalid format.", None
101
  print(f"Fetched {len(questions_data)} questions.")
102
- except requests.exceptions.RequestException as e:
103
- print(f"Error fetching questions: {e}")
104
- return f"Error fetching questions: {e}", None
105
- except requests.exceptions.JSONDecodeError as e:
106
- print(f"Error decoding JSON response from questions endpoint: {e}")
107
- print(f"Response text: {response.text[:500]}")
108
- return f"Error decoding server response for questions: {e}", None
109
  except Exception as e:
110
- print(f"An unexpected error occurred fetching questions: {e}")
111
- return f"An unexpected error occurred fetching questions: {e}", None
112
 
113
- # 3. Run your Agent
114
  results_log = []
115
  answers_payload = []
116
- print(f"Running agent on {len(questions_data)} questions...")
117
  for item in questions_data:
118
  task_id = item.get("task_id")
119
  question_text = item.get("question")
@@ -121,23 +135,38 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
121
  print(f"Skipping item with missing task_id or question: {item}")
122
  continue
123
  try:
124
- submitted_answer = agent(question_text)
 
 
 
 
125
  answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
126
- results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
 
 
 
 
127
  except Exception as e:
128
- print(f"Error running agent on task {task_id}: {e}")
129
- results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"AGENT ERROR: {e}"})
 
 
 
 
130
 
131
  if not answers_payload:
132
- print("Agent did not produce any answers to submit.")
133
  return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
134
 
135
- # 4. Prepare Submission
136
- submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload}
 
 
 
 
137
  status_update = f"Agent finished. Submitting {len(answers_payload)} answers for user '{username}'..."
138
  print(status_update)
139
 
140
- # 5. Submit
141
  print(f"Submitting {len(answers_payload)} answers to: {submit_url}")
142
  try:
143
  response = requests.post(submit_url, json=submission_data, timeout=60)
@@ -153,58 +182,34 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
153
  print("Submission successful.")
154
  results_df = pd.DataFrame(results_log)
155
  return final_status, results_df
156
- except requests.exceptions.HTTPError as e:
157
- error_detail = f"Server responded with status {e.response.status_code}."
158
- try:
159
- error_json = e.response.json()
160
- error_detail += f" Detail: {error_json.get('detail', e.response.text)}"
161
- except requests.exceptions.JSONDecodeError:
162
- error_detail += f" Response: {e.response.text[:500]}"
163
- status_message = f"Submission Failed: {error_detail}"
164
- print(status_message)
165
- results_df = pd.DataFrame(results_log)
166
- return status_message, results_df
167
- except requests.exceptions.Timeout:
168
- status_message = "Submission Failed: The request timed out."
169
- print(status_message)
170
- results_df = pd.DataFrame(results_log)
171
- return status_message, results_df
172
- except requests.exceptions.RequestException as e:
173
- status_message = f"Submission Failed: Network error - {e}"
174
- print(status_message)
175
- results_df = pd.DataFrame(results_log)
176
- return status_message, results_df
177
  except Exception as e:
178
- status_message = f"An unexpected error occurred during submission: {e}"
179
  print(status_message)
180
  results_df = pd.DataFrame(results_log)
181
  return status_message, results_df
182
 
183
-
184
- # --- Build Gradio Interface using Blocks ---
 
185
  with gr.Blocks() as demo:
186
  gr.Markdown("# Basic Agent Evaluation Runner")
187
  gr.Markdown(
188
  """
189
  **Instructions:**
190
-
191
  1. Please clone this space, then modify the code to define your agent's logic, the tools, the necessary packages, etc ...
192
  2. Log in to your Hugging Face account using the button below. This uses your HF username for submission.
193
  3. Click 'Run Evaluation & Submit All Answers' to fetch questions, run your agent, submit answers, and see the score.
194
 
195
  ---
196
  **Disclaimers:**
197
- 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).
198
- 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.
199
  """
200
  )
201
 
202
  gr.LoginButton()
203
-
204
  run_button = gr.Button("Run Evaluation & Submit All Answers")
205
-
206
  status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
207
- # Removed max_rows=10 from DataFrame constructor
208
  results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
209
 
210
  run_button.click(
@@ -214,24 +219,19 @@ with gr.Blocks() as demo:
214
 
215
  if __name__ == "__main__":
216
  print("\n" + "-"*30 + " App Starting " + "-"*30)
217
- # Check for SPACE_HOST and SPACE_ID at startup for information
218
  space_host_startup = os.getenv("SPACE_HOST")
219
- space_id_startup = os.getenv("SPACE_ID") # Get SPACE_ID at startup
220
-
221
  if space_host_startup:
222
  print(f"✅ SPACE_HOST found: {space_host_startup}")
223
  print(f" Runtime URL should be: https://{space_host_startup}.hf.space")
224
  else:
225
  print("ℹ️ SPACE_HOST environment variable not found (running locally?).")
226
-
227
- if space_id_startup: # Print repo URLs if SPACE_ID is found
228
  print(f"✅ SPACE_ID found: {space_id_startup}")
229
  print(f" Repo URL: https://huggingface.co/spaces/{space_id_startup}")
230
  print(f" Repo Tree URL: https://huggingface.co/spaces/{space_id_startup}/tree/main")
231
  else:
232
  print("ℹ️ SPACE_ID environment variable not found (running locally?). Repo URL cannot be determined.")
233
-
234
  print("-"*(60 + len(" App Starting ")) + "\n")
235
-
236
- print("Launching Gradio Interface for Basic Agent Evaluation...")
237
  demo.launch(debug=True, share=False)
 
1
  import os
2
  import gradio as gr
3
  from PIL import Image
 
 
 
 
 
 
4
  import requests
 
5
  import pandas as pd
6
+ import io
7
 
8
+ # Import LangChain and LangGraph components
9
+ from langgraph.prebuilt import create_react_agent
10
+ from langchain_google_genai import ChatGoogleGenerativeAI
11
+ from langchain_community.tools import DuckDuckGoSearchRun
12
+ from langchain.tools import tool
13
+
14
+ # Constants (unchanged)
15
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
16
 
17
+ # ------------------------------------------------------------------
18
+ # 1. Define your LangGraph ReAct Agent with Gemini 2.0 Flash
19
+ # ------------------------------------------------------------------
20
+ def get_agent():
21
+ """Initialize the LangGraph ReAct agent with Gemini 2.0 Flash and tools."""
22
+ # Gemini API key from Hugging Face secrets
23
+ gemini_api_key = os.getenv("GEMINI_API_KEY")
24
+ if not gemini_api_key:
25
+ raise ValueError("GEMINI_API_KEY environment variable not set. Please add it to Hugging Face Space secrets.")
26
+
27
+ # Initialize the Gemini model
28
+ model = ChatGoogleGenerativeAI(
29
+ model="gemini-2.0-flash",
30
+ api_key=gemini_api_key,
31
+ temperature=0.7,
32
+ timeout=60,
33
+ max_retries=2
34
+ )
35
+
36
+ # --------------------------------------------------------------
37
+ # 2. Define your custom tools using LangChain's @tool decorator
38
+ # --------------------------------------------------------------
39
+
40
+ # Tool: DuckDuckGo Search (free web search)
41
+ ddg_search = DuckDuckGoSearchRun()
42
+
43
+ # Tool: Image generation using FLUX.1 Schnell on Hugging Face
44
+ @tool
45
+ def image_generator(prompt: str) -> dict:
46
+ """
47
+ Generates a high-quality visual image based on a descriptive text prompt.
48
+ Uses FLUX.1 Schnell from Hugging Face.
49
+ Returns the image as a base64 string or URL.
50
+ """
51
+ HF_TOKEN = os.getenv("HF_TOKEN") # Optional but helps with rate limits
52
+ API_URL = "https://api-inference.huggingface.co/models/black-forest-labs/FLUX.1-schnell"
53
+ headers = {"Authorization": f"Bearer {HF_TOKEN}"} if HF_TOKEN else {}
54
+
55
+ response = requests.post(API_URL, headers=headers, json={"inputs": prompt})
56
+ if response.status_code == 200:
57
+ return {
58
+ "success": True,
59
+ "image": response.content,
60
+ "message": "Image generated successfully."
61
+ }
62
+ else:
63
+ return {
64
+ "success": False,
65
+ "message": f"Image generation failed: {response.status_code} - {response.text}"
66
+ }
67
+
68
+ # Combine tools into a list
69
+ tools = [ddg_search, image_generator]
70
+
71
+ # --------------------------------------------------------------
72
+ # 3. Create the ReAct agent with LangGraph
73
+ # --------------------------------------------------------------
74
+ agent = create_react_agent(
75
+ model=model,
76
+ tools=tools,
77
+ prompt=(
78
+ "You are a helpful AI assistant with access to web search and image generation. "
79
+ "Always think step by step before using tools. For image requests, use the image_generator tool. "
80
+ "For web information, use the duckduckgo_search tool."
81
+ )
82
+ )
83
+
84
+ return agent
85
+
86
+ def run_and_submit_all(profile: gr.OAuthProfile | None):
87
  """
88
+ Fetches all questions, runs the LangGraph ReAct agent, submits all answers,
89
  and displays the results.
90
  """
91
+ space_id = os.getenv("SPACE_ID")
 
 
92
  if profile:
93
+ username = profile.username
94
  print(f"User logged in: {username}")
95
  else:
96
  print("User not logged in.")
 
100
  questions_url = f"{api_url}/questions"
101
  submit_url = f"{api_url}/submit"
102
 
103
+ # ------------------------------------------------------------------
104
+ # Initialize LangGraph ReAct Agent
105
+ # ------------------------------------------------------------------
106
  try:
107
+ agent = get_agent()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
108
  except Exception as e:
109
+ print(f"Error initializing LangGraph agent: {e}")
110
  return f"Error initializing agent: {e}", None
111
+
112
  agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
113
  print(agent_code)
114
 
115
+ # Fetch questions
116
  print(f"Fetching questions from: {questions_url}")
117
  try:
118
  response = requests.get(questions_url, timeout=15)
119
  response.raise_for_status()
120
  questions_data = response.json()
121
  if not questions_data:
122
+ return "Fetched questions list is empty or invalid format.", None
 
123
  print(f"Fetched {len(questions_data)} questions.")
 
 
 
 
 
 
 
124
  except Exception as e:
125
+ return f"Error fetching questions: {e}", None
 
126
 
127
+ # Run agent on each question
128
  results_log = []
129
  answers_payload = []
130
+ print(f"Running LangGraph ReAct agent on {len(questions_data)} questions...")
131
  for item in questions_data:
132
  task_id = item.get("task_id")
133
  question_text = item.get("question")
 
135
  print(f"Skipping item with missing task_id or question: {item}")
136
  continue
137
  try:
138
+ # LangGraph agent invocation returns a dict with 'output' key
139
+ result = agent.invoke({
140
+ "messages": [("user", question_text)]
141
+ })
142
+ submitted_answer = result["output"]
143
  answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
144
+ results_log.append({
145
+ "Task ID": task_id,
146
+ "Question": question_text,
147
+ "Submitted Answer": submitted_answer
148
+ })
149
  except Exception as e:
150
+ print(f"Error running agent on task {task_id}: {e}")
151
+ results_log.append({
152
+ "Task ID": task_id,
153
+ "Question": question_text,
154
+ "Submitted Answer": f"AGENT ERROR: {e}"
155
+ })
156
 
157
  if not answers_payload:
 
158
  return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
159
 
160
+ # Prepare submission
161
+ submission_data = {
162
+ "username": username.strip(),
163
+ "agent_code": agent_code,
164
+ "answers": answers_payload
165
+ }
166
  status_update = f"Agent finished. Submitting {len(answers_payload)} answers for user '{username}'..."
167
  print(status_update)
168
 
169
+ # Submit answers
170
  print(f"Submitting {len(answers_payload)} answers to: {submit_url}")
171
  try:
172
  response = requests.post(submit_url, json=submission_data, timeout=60)
 
182
  print("Submission successful.")
183
  results_df = pd.DataFrame(results_log)
184
  return final_status, results_df
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
185
  except Exception as e:
186
+ status_message = f"Submission Failed: {e}"
187
  print(status_message)
188
  results_df = pd.DataFrame(results_log)
189
  return status_message, results_df
190
 
191
+ # ------------------------------------------------------------------
192
+ # Gradio Interface (unchanged)
193
+ # ------------------------------------------------------------------
194
  with gr.Blocks() as demo:
195
  gr.Markdown("# Basic Agent Evaluation Runner")
196
  gr.Markdown(
197
  """
198
  **Instructions:**
 
199
  1. Please clone this space, then modify the code to define your agent's logic, the tools, the necessary packages, etc ...
200
  2. Log in to your Hugging Face account using the button below. This uses your HF username for submission.
201
  3. Click 'Run Evaluation & Submit All Answers' to fetch questions, run your agent, submit answers, and see the score.
202
 
203
  ---
204
  **Disclaimers:**
205
+ 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).
206
+ This space provides a basic setup and is intentionally sub-optimal to encourage you to develop your own, more robust solution.
207
  """
208
  )
209
 
210
  gr.LoginButton()
 
211
  run_button = gr.Button("Run Evaluation & Submit All Answers")
 
212
  status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
 
213
  results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
214
 
215
  run_button.click(
 
219
 
220
  if __name__ == "__main__":
221
  print("\n" + "-"*30 + " App Starting " + "-"*30)
 
222
  space_host_startup = os.getenv("SPACE_HOST")
223
+ space_id_startup = os.getenv("SPACE_ID")
 
224
  if space_host_startup:
225
  print(f"✅ SPACE_HOST found: {space_host_startup}")
226
  print(f" Runtime URL should be: https://{space_host_startup}.hf.space")
227
  else:
228
  print("ℹ️ SPACE_HOST environment variable not found (running locally?).")
229
+ if space_id_startup:
 
230
  print(f"✅ SPACE_ID found: {space_id_startup}")
231
  print(f" Repo URL: https://huggingface.co/spaces/{space_id_startup}")
232
  print(f" Repo Tree URL: https://huggingface.co/spaces/{space_id_startup}/tree/main")
233
  else:
234
  print("ℹ️ SPACE_ID environment variable not found (running locally?). Repo URL cannot be determined.")
 
235
  print("-"*(60 + len(" App Starting ")) + "\n")
236
+ print("Launching Gradio Interface for LangGraph ReAct Agent Evaluation...")
 
237
  demo.launch(debug=True, share=False)