Update app.py
Browse files
app.py
CHANGED
|
@@ -19,100 +19,133 @@ import json
|
|
| 19 |
import re
|
| 20 |
from datetime import datetime
|
| 21 |
|
| 22 |
-
|
| 23 |
-
|
| 24 |
custom_role_conversions = {"tool-call": "assistant", "tool-response": "user"}
|
| 25 |
|
| 26 |
from linkup import LinkupClient
|
| 27 |
|
| 28 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
|
| 31 |
class BasicAgent:
|
| 32 |
def __init__(self):
|
| 33 |
"""
|
| 34 |
Initialize the GAIA dataset agent with SmoLagents.
|
| 35 |
-
|
| 36 |
Args:
|
| 37 |
api_key: API key for the LLM provider
|
| 38 |
model_name: Name of the LLM model to use
|
| 39 |
"""
|
| 40 |
print("BasicAgent initialized.")
|
| 41 |
-
|
| 42 |
# Initialize the agent
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
|
|
|
|
|
|
|
|
|
| 46 |
"""
|
| 47 |
Process a question and return an answer.
|
| 48 |
-
|
| 49 |
Args:
|
| 50 |
question: The question to answer
|
| 51 |
-
|
| 52 |
Returns:
|
| 53 |
The answer to the question
|
| 54 |
"""
|
| 55 |
-
|
| 56 |
-
|
|
|
|
|
|
|
| 57 |
# Create a prompt for the agent
|
| 58 |
-
|
| 59 |
-
You
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
try:
|
| 78 |
# Run the agent
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
res = client.search(
|
| 83 |
-
query=full_prompt,
|
| 84 |
-
depth="deep",
|
| 85 |
-
output_type="sourcedAnswer",
|
| 86 |
-
include_images=False,
|
| 87 |
-
)
|
| 88 |
-
response = getattr(res, "answer", "No answer provided.")
|
| 89 |
-
|
| 90 |
-
# print(response)
|
| 91 |
-
|
| 92 |
# Clean up the response
|
| 93 |
# Remove any system-prompt-like text at the beginning
|
| 94 |
cleaned_response = re.sub(r'^.*?Answer:', '', response, flags=re.DOTALL).strip()
|
| 95 |
-
|
| 96 |
if not cleaned_response:
|
| 97 |
cleaned_response = response # Fallback to original if cleaning removes everything
|
| 98 |
-
|
| 99 |
-
|
|
|
|
| 100 |
return cleaned_response
|
| 101 |
except Exception as e:
|
| 102 |
error_msg = f"Error processing question: {str(e)}"
|
| 103 |
print(error_msg)
|
| 104 |
return error_msg
|
| 105 |
|
| 106 |
-
|
|
|
|
| 107 |
"""
|
| 108 |
Fetches all questions, runs the BasicAgent on them, submits all answers,
|
| 109 |
and displays the results.
|
| 110 |
"""
|
| 111 |
# --- Determine HF Space Runtime URL and Repo URL ---
|
| 112 |
-
space_id = os.getenv("SPACE_ID")
|
| 113 |
|
| 114 |
if profile:
|
| 115 |
-
username= f"{profile.username}"
|
| 116 |
print(f"User logged in: {username}")
|
| 117 |
else:
|
| 118 |
print("User not logged in.")
|
|
@@ -128,7 +161,8 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
| 128 |
except Exception as e:
|
| 129 |
print(f"Error instantiating agent: {e}")
|
| 130 |
return f"Error initializing agent: {e}", None
|
| 131 |
-
# In the case of an app running as a hugging Face space, this link points toward your
|
|
|
|
| 132 |
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
|
| 133 |
print(agent_code)
|
| 134 |
|
|
@@ -139,16 +173,16 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
| 139 |
response.raise_for_status()
|
| 140 |
questions_data = response.json()
|
| 141 |
if not questions_data:
|
| 142 |
-
|
| 143 |
-
|
| 144 |
print(f"Fetched {len(questions_data)} questions.")
|
| 145 |
except requests.exceptions.RequestException as e:
|
| 146 |
print(f"Error fetching questions: {e}")
|
| 147 |
return f"Error fetching questions: {e}", None
|
| 148 |
except requests.exceptions.JSONDecodeError as e:
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
except Exception as e:
|
| 153 |
print(f"An unexpected error occurred fetching questions: {e}")
|
| 154 |
return f"An unexpected error occurred fetching questions: {e}", None
|
|
@@ -158,27 +192,31 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
| 158 |
answers_payload = []
|
| 159 |
print(f"Running agent on {len(questions_data)} questions...")
|
| 160 |
for idx, item in enumerate(questions_data):
|
| 161 |
-
print(f"Question {idx+1}: {item}")
|
| 162 |
task_id = item.get("task_id")
|
| 163 |
question_text = item.get("question")
|
| 164 |
if not task_id or question_text is None:
|
| 165 |
print(f"Skipping item with missing task_id or question: {item}")
|
| 166 |
continue
|
| 167 |
try:
|
| 168 |
-
submitted_answer = "hello"#agent(question_text)
|
| 169 |
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
| 170 |
-
results_log.append({"Task ID": task_id, "Question": question_text,
|
|
|
|
| 171 |
except Exception as e:
|
| 172 |
-
|
| 173 |
-
|
|
|
|
| 174 |
|
| 175 |
if not answers_payload:
|
| 176 |
print("Agent did not produce any answers to submit.")
|
| 177 |
return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
|
| 178 |
|
| 179 |
-
# 4. Prepare Submission
|
| 180 |
-
submission_data = {"username": username.strip(), "agent_code": agent_code,
|
| 181 |
-
|
|
|
|
|
|
|
| 182 |
print(status_update)
|
| 183 |
|
| 184 |
# 5. Submit
|
|
@@ -191,7 +229,8 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
| 191 |
f"Submission Successful!\n"
|
| 192 |
f"User: {result_data.get('username')}\n"
|
| 193 |
f"Overall Score: {result_data.get('score', 'N/A')}% "
|
| 194 |
-
f"({result_data.get('correct_count', '?')}/{result_data.get('total_attempted',
|
|
|
|
| 195 |
f"Message: {result_data.get('message', 'No message received.')}"
|
| 196 |
)
|
| 197 |
print("Submission successful.")
|
|
@@ -232,14 +271,21 @@ with gr.Blocks() as demo:
|
|
| 232 |
"""
|
| 233 |
**Instructions:**
|
| 234 |
|
| 235 |
-
1. Please clone this space, then modify the code to define your agent's logic,
|
| 236 |
-
|
| 237 |
-
|
|
|
|
|
|
|
|
|
|
| 238 |
|
| 239 |
---
|
| 240 |
**Disclaimers:**
|
| 241 |
-
Once clicking on the "submit button, it can take quite some time ( this is the time for
|
| 242 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 243 |
"""
|
| 244 |
)
|
| 245 |
|
|
@@ -257,10 +303,10 @@ with gr.Blocks() as demo:
|
|
| 257 |
)
|
| 258 |
|
| 259 |
if __name__ == "__main__":
|
| 260 |
-
print("\n" + "-"*30 + " App Starting " + "-"*30)
|
| 261 |
# Check for SPACE_HOST and SPACE_ID at startup for information
|
| 262 |
space_host_startup = os.getenv("SPACE_HOST")
|
| 263 |
-
space_id_startup = os.getenv("SPACE_ID")
|
| 264 |
|
| 265 |
if space_host_startup:
|
| 266 |
print(f"✅ SPACE_HOST found: {space_host_startup}")
|
|
@@ -268,14 +314,16 @@ if __name__ == "__main__":
|
|
| 268 |
else:
|
| 269 |
print("ℹ️ SPACE_HOST environment variable not found (running locally?).")
|
| 270 |
|
| 271 |
-
if space_id_startup:
|
| 272 |
print(f"✅ SPACE_ID found: {space_id_startup}")
|
| 273 |
print(f" Repo URL: https://huggingface.co/spaces/{space_id_startup}")
|
| 274 |
print(f" Repo Tree URL: https://huggingface.co/spaces/{space_id_startup}/tree/main")
|
| 275 |
else:
|
| 276 |
-
print(
|
|
|
|
|
|
|
| 277 |
|
| 278 |
-
print("-"*(60 + len(" App Starting ")) + "\n")
|
| 279 |
|
| 280 |
print("Launching Gradio Interface for Basic Agent Evaluation...")
|
| 281 |
-
demo.launch(debug=True, share=False)
|
|
|
|
| 19 |
import re
|
| 20 |
from datetime import datetime
|
| 21 |
|
|
|
|
|
|
|
| 22 |
custom_role_conversions = {"tool-call": "assistant", "tool-response": "user"}
|
| 23 |
|
| 24 |
from linkup import LinkupClient
|
| 25 |
|
| 26 |
|
| 27 |
+
def get_image_description(file_name: str, question: str, visual_inspection_tool) -> str:
|
| 28 |
+
prompt = f"""Write a caption of 5 sentences for this image. Pay special attention to any
|
| 29 |
+
details that might be useful for someone answering the following question:
|
| 30 |
+
{question}. But do not try to answer the question directly!
|
| 31 |
+
Do not add any information that is not present in the image."""
|
| 32 |
+
return visual_inspection_tool(image_path=file_name, question=prompt)
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
def get_document_description(file_path: str, question: str, document_inspection_tool) -> str:
|
| 36 |
+
prompt = f"""Write a caption of 5 sentences for this document. Pay special attention to any
|
| 37 |
+
details that might be useful for someone answering the following question:
|
| 38 |
+
{question}. But do not try to answer the question directly!
|
| 39 |
+
Do not add any information that is not present in the document."""
|
| 40 |
+
return document_inspection_tool.forward_initial_exam_mode(file_path=file_path, question=prompt)
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
def get_single_file_description(file_path: str, question: str, visual_inspection_tool,
|
| 44 |
+
document_inspection_tool):
|
| 45 |
+
file_extension = file_path.split(".")[-1]
|
| 46 |
+
if file_extension in ["png", "jpg", "jpeg"]:
|
| 47 |
+
file_description = f" - Attached image: {file_path}"
|
| 48 |
+
file_description += (
|
| 49 |
+
f"\n -> Image description: "
|
| 50 |
+
f"{get_image_description(file_path, question, visual_inspection_tool)}"
|
| 51 |
+
)
|
| 52 |
+
return file_description
|
| 53 |
+
elif file_extension in ["pdf", "xls", "xlsx", "docx", "doc", "xml"]:
|
| 54 |
+
file_description = f" - Attached document: {file_path}"
|
| 55 |
+
image_path = file_path.split(".")[0] + ".png"
|
| 56 |
+
if os.path.exists(image_path):
|
| 57 |
+
description = get_image_description(image_path, question, visual_inspection_tool)
|
| 58 |
+
else:
|
| 59 |
+
description = get_document_description(file_path, question, document_inspection_tool)
|
| 60 |
+
file_description += f"\n -> File description: {description}"
|
| 61 |
+
return file_description
|
| 62 |
+
elif file_extension in ["mp3", "m4a", "wav"]:
|
| 63 |
+
return f" - Attached audio: {file_path}"
|
| 64 |
+
else:
|
| 65 |
+
return f" - Attached file: {file_path}"
|
| 66 |
|
| 67 |
|
| 68 |
class BasicAgent:
|
| 69 |
def __init__(self):
|
| 70 |
"""
|
| 71 |
Initialize the GAIA dataset agent with SmoLagents.
|
| 72 |
+
|
| 73 |
Args:
|
| 74 |
api_key: API key for the LLM provider
|
| 75 |
model_name: Name of the LLM model to use
|
| 76 |
"""
|
| 77 |
print("BasicAgent initialized.")
|
| 78 |
+
|
| 79 |
# Initialize the agent
|
| 80 |
+
agent_assets = create_agent()
|
| 81 |
+
self.agent = agent_assets["agent"]
|
| 82 |
+
self.visual_inspection_tool = agent_assets["visualizer"]
|
| 83 |
+
self.document_inspection_tool = agent_assets["text_inspection_tool"]
|
| 84 |
+
|
| 85 |
+
def __call__(self, question: str, file_name: str = None) -> str:
|
| 86 |
"""
|
| 87 |
Process a question and return an answer.
|
| 88 |
+
|
| 89 |
Args:
|
| 90 |
question: The question to answer
|
| 91 |
+
|
| 92 |
Returns:
|
| 93 |
The answer to the question
|
| 94 |
"""
|
| 95 |
+
words = question.split()
|
| 96 |
+
joined_words = " ".join(words[:20])
|
| 97 |
+
print(f"Agent received question (first 20 words): {joined_words}...")
|
| 98 |
+
|
| 99 |
# Create a prompt for the agent
|
| 100 |
+
|
| 101 |
+
full_prompt = """You have one question to answer. It is paramount that you provide a
|
| 102 |
+
correct answer.
|
| 103 |
+
Give it all you can: I know for a fact that you have access to all the relevant tools to
|
| 104 |
+
solve it and find the correct answer (the answer does exist). Failure or 'I cannot
|
| 105 |
+
answer' or 'None found' will not be tolerated, success will be rewarded.
|
| 106 |
+
Run verification steps if that's needed, you must make sure you find the correct answer!
|
| 107 |
+
Here is the task:
|
| 108 |
+
""" + question
|
| 109 |
+
|
| 110 |
+
if file_name:
|
| 111 |
+
prompt_use_files = ("\n\nTo solve the task above, you will have to use this attached "
|
| 112 |
+
"file:")
|
| 113 |
+
prompt_use_files += get_single_file_description(
|
| 114 |
+
file_name, question, self.visual_inspection_tool,
|
| 115 |
+
self.document_inspection_tool
|
| 116 |
+
)
|
| 117 |
+
full_prompt += prompt_use_files
|
| 118 |
+
|
| 119 |
try:
|
| 120 |
# Run the agent
|
| 121 |
+
response = self.agent.run(full_prompt)
|
| 122 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 123 |
# Clean up the response
|
| 124 |
# Remove any system-prompt-like text at the beginning
|
| 125 |
cleaned_response = re.sub(r'^.*?Answer:', '', response, flags=re.DOTALL).strip()
|
| 126 |
+
|
| 127 |
if not cleaned_response:
|
| 128 |
cleaned_response = response # Fallback to original if cleaning removes everything
|
| 129 |
+
words = cleaned_response.split()
|
| 130 |
+
joined_words = " ".join(words[:20])
|
| 131 |
+
print(f"Agent returning answer (first 20 words): {joined_words}...")
|
| 132 |
return cleaned_response
|
| 133 |
except Exception as e:
|
| 134 |
error_msg = f"Error processing question: {str(e)}"
|
| 135 |
print(error_msg)
|
| 136 |
return error_msg
|
| 137 |
|
| 138 |
+
|
| 139 |
+
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
| 140 |
"""
|
| 141 |
Fetches all questions, runs the BasicAgent on them, submits all answers,
|
| 142 |
and displays the results.
|
| 143 |
"""
|
| 144 |
# --- Determine HF Space Runtime URL and Repo URL ---
|
| 145 |
+
space_id = os.getenv("SPACE_ID") # Get the SPACE_ID for sending link to the code
|
| 146 |
|
| 147 |
if profile:
|
| 148 |
+
username = f"{profile.username}"
|
| 149 |
print(f"User logged in: {username}")
|
| 150 |
else:
|
| 151 |
print("User not logged in.")
|
|
|
|
| 161 |
except Exception as e:
|
| 162 |
print(f"Error instantiating agent: {e}")
|
| 163 |
return f"Error initializing agent: {e}", None
|
| 164 |
+
# In the case of an app running as a hugging Face space, this link points toward your
|
| 165 |
+
# codebase ( usefull for others so please keep it public)
|
| 166 |
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
|
| 167 |
print(agent_code)
|
| 168 |
|
|
|
|
| 173 |
response.raise_for_status()
|
| 174 |
questions_data = response.json()
|
| 175 |
if not questions_data:
|
| 176 |
+
print("Fetched questions list is empty.")
|
| 177 |
+
return "Fetched questions list is empty or invalid format.", None
|
| 178 |
print(f"Fetched {len(questions_data)} questions.")
|
| 179 |
except requests.exceptions.RequestException as e:
|
| 180 |
print(f"Error fetching questions: {e}")
|
| 181 |
return f"Error fetching questions: {e}", None
|
| 182 |
except requests.exceptions.JSONDecodeError as e:
|
| 183 |
+
print(f"Error decoding JSON response from questions endpoint: {e}")
|
| 184 |
+
print(f"Response text: {response.text[:500]}")
|
| 185 |
+
return f"Error decoding server response for questions: {e}", None
|
| 186 |
except Exception as e:
|
| 187 |
print(f"An unexpected error occurred fetching questions: {e}")
|
| 188 |
return f"An unexpected error occurred fetching questions: {e}", None
|
|
|
|
| 192 |
answers_payload = []
|
| 193 |
print(f"Running agent on {len(questions_data)} questions...")
|
| 194 |
for idx, item in enumerate(questions_data):
|
| 195 |
+
print(f"Question {idx + 1}: {item}")
|
| 196 |
task_id = item.get("task_id")
|
| 197 |
question_text = item.get("question")
|
| 198 |
if not task_id or question_text is None:
|
| 199 |
print(f"Skipping item with missing task_id or question: {item}")
|
| 200 |
continue
|
| 201 |
try:
|
| 202 |
+
submitted_answer = "hello" # agent(question_text)
|
| 203 |
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
| 204 |
+
results_log.append({"Task ID": task_id, "Question": question_text,
|
| 205 |
+
"Submitted Answer": submitted_answer})
|
| 206 |
except Exception as e:
|
| 207 |
+
print(f"Error running agent on task {task_id}: {e}")
|
| 208 |
+
results_log.append({"Task ID": task_id, "Question": question_text,
|
| 209 |
+
"Submitted Answer": f"AGENT ERROR: {e}"})
|
| 210 |
|
| 211 |
if not answers_payload:
|
| 212 |
print("Agent did not produce any answers to submit.")
|
| 213 |
return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
|
| 214 |
|
| 215 |
+
# 4. Prepare Submission
|
| 216 |
+
submission_data = {"username": username.strip(), "agent_code": agent_code,
|
| 217 |
+
"answers": answers_payload}
|
| 218 |
+
status_update = (f"Agent finished. Submitting {len(answers_payload)} answers for user '"
|
| 219 |
+
f"{username}'...")
|
| 220 |
print(status_update)
|
| 221 |
|
| 222 |
# 5. Submit
|
|
|
|
| 229 |
f"Submission Successful!\n"
|
| 230 |
f"User: {result_data.get('username')}\n"
|
| 231 |
f"Overall Score: {result_data.get('score', 'N/A')}% "
|
| 232 |
+
f"({result_data.get('correct_count', '?')}/{result_data.get('total_attempted',
|
| 233 |
+
'?')} correct)\n"
|
| 234 |
f"Message: {result_data.get('message', 'No message received.')}"
|
| 235 |
)
|
| 236 |
print("Submission successful.")
|
|
|
|
| 271 |
"""
|
| 272 |
**Instructions:**
|
| 273 |
|
| 274 |
+
1. Please clone this space, then modify the code to define your agent's logic,
|
| 275 |
+
the tools, the necessary packages, etc ...
|
| 276 |
+
2. Log in to your Hugging Face account using the button below. This uses your HF
|
| 277 |
+
username for submission.
|
| 278 |
+
3. Click 'Run Evaluation & Submit All Answers' to fetch questions, run your agent,
|
| 279 |
+
submit answers, and see the score.
|
| 280 |
|
| 281 |
---
|
| 282 |
**Disclaimers:**
|
| 283 |
+
Once clicking on the "submit button, it can take quite some time ( this is the time for
|
| 284 |
+
the agent to go through all the questions).
|
| 285 |
+
This space provides a basic setup and is intentionally sub-optimal to encourage you to
|
| 286 |
+
develop your own, more robust solution. For instance for the delay process of the submit
|
| 287 |
+
button, a solution could be to cache the answers and submit in a seperate action or even
|
| 288 |
+
to answer the questions in async.
|
| 289 |
"""
|
| 290 |
)
|
| 291 |
|
|
|
|
| 303 |
)
|
| 304 |
|
| 305 |
if __name__ == "__main__":
|
| 306 |
+
print("\n" + "-" * 30 + " App Starting " + "-" * 30)
|
| 307 |
# Check for SPACE_HOST and SPACE_ID at startup for information
|
| 308 |
space_host_startup = os.getenv("SPACE_HOST")
|
| 309 |
+
space_id_startup = os.getenv("SPACE_ID") # Get SPACE_ID at startup
|
| 310 |
|
| 311 |
if space_host_startup:
|
| 312 |
print(f"✅ SPACE_HOST found: {space_host_startup}")
|
|
|
|
| 314 |
else:
|
| 315 |
print("ℹ️ SPACE_HOST environment variable not found (running locally?).")
|
| 316 |
|
| 317 |
+
if space_id_startup: # Print repo URLs if SPACE_ID is found
|
| 318 |
print(f"✅ SPACE_ID found: {space_id_startup}")
|
| 319 |
print(f" Repo URL: https://huggingface.co/spaces/{space_id_startup}")
|
| 320 |
print(f" Repo Tree URL: https://huggingface.co/spaces/{space_id_startup}/tree/main")
|
| 321 |
else:
|
| 322 |
+
print(
|
| 323 |
+
"ℹ️ SPACE_ID environment variable not found (running locally?). Repo URL cannot be "
|
| 324 |
+
"determined.")
|
| 325 |
|
| 326 |
+
print("-" * (60 + len(" App Starting ")) + "\n")
|
| 327 |
|
| 328 |
print("Launching Gradio Interface for Basic Agent Evaluation...")
|
| 329 |
+
demo.launch(debug=True, share=False)
|