DATAD2 commited on
Commit
369bfec
Β·
verified Β·
1 Parent(s): 81917a3

Upload 5 files

Browse files
Files changed (5) hide show
  1. agent.py +332 -0
  2. app.py +862 -186
  3. code_interpreter.py +281 -0
  4. evaluation_app.py +218 -0
  5. explore_metadata.ipynb +332 -0
agent.py ADDED
@@ -0,0 +1,332 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import json
2
+ import os
3
+ from typing import List, Optional
4
+
5
+ import pandas as pd
6
+ import pytesseract
7
+ import requests
8
+ from dotenv import load_dotenv
9
+ from langchain_community.document_loaders import WikipediaLoader
10
+ from langchain_community.tools.tavily_search import TavilySearchResults
11
+ from langchain_core.messages import HumanMessage, SystemMessage
12
+ from langchain_core.tools import tool
13
+ from langchain_groq import ChatGroq
14
+ from langgraph.graph import START, MessagesState, StateGraph
15
+ from langgraph.prebuilt import ToolNode, tools_condition
16
+ from PIL import Image
17
+
18
+ from code_interpreter import CodeInterpreter
19
+ from extract_answer import extract_final_answer
20
+ from files_util import format_user_message
21
+
22
+ load_dotenv()
23
+
24
+ ROOT_DIR = os.path.dirname(os.path.abspath(__file__))
25
+ PROMPT_PATH = os.path.join(ROOT_DIR, "system_prompt.txt")
26
+ MAX_TOOL_ROUNDS = 8
27
+ RECURSION_LIMIT = 20
28
+
29
+ _interpreter = None
30
+
31
+
32
+ def get_system_prompt() -> str:
33
+ with open(PROMPT_PATH, "r", encoding="utf-8") as handle:
34
+ return handle.read()
35
+
36
+
37
+ def get_interpreter() -> CodeInterpreter:
38
+ global _interpreter
39
+ if _interpreter is None:
40
+ _interpreter = CodeInterpreter()
41
+ return _interpreter
42
+
43
+
44
+ @tool
45
+ def wiki_search(query: str) -> str:
46
+ """Search Wikipedia and return up to 2 page excerpts.
47
+
48
+ Args:
49
+ query: Search query, preferably a proper name or article title.
50
+ """
51
+ docs = WikipediaLoader(query=query, load_max_docs=2).load()
52
+ if not docs:
53
+ return "No Wikipedia results."
54
+ chunks = []
55
+ for doc in docs:
56
+ source = doc.metadata.get("source", "")
57
+ chunks.append(f'<Document source="{source}">\n{doc.page_content}\n</Document>')
58
+ return "\n\n---\n\n".join(chunks)
59
+
60
+
61
+ @tool
62
+ def web_search(query: str) -> str:
63
+ """Search the public web via Tavily and return up to 3 results.
64
+
65
+ Args:
66
+ query: Search query. Be specific; include years, full names, and distinctive terms.
67
+ """
68
+ results = TavilySearchResults(max_results=3).invoke(query)
69
+ if not results:
70
+ return "No web results."
71
+ chunks = []
72
+ for doc in results:
73
+ chunks.append(
74
+ f'<Document source="{doc.get("url", "")}" title="{doc.get("title", "")}">\n'
75
+ f'{doc.get("content", "")}\n</Document>'
76
+ )
77
+ return "\n\n---\n\n".join(chunks)
78
+
79
+
80
+ @tool
81
+ def execute_python(code: str) -> str:
82
+ """Run Python for calculation, pandas/Excel/CSV analysis, dates, and string parsing.
83
+
84
+ Args:
85
+ code: Complete Python source. Print the values you need. Use absolute file paths from the question.
86
+ """
87
+ result = get_interpreter().execute_code(code, language="python")
88
+ lines = []
89
+ if result["status"] == "success":
90
+ lines.append("Python execution succeeded.")
91
+ if result.get("stdout"):
92
+ lines.append("stdout:\n" + result["stdout"].strip())
93
+ if result.get("stderr"):
94
+ lines.append("stderr:\n" + result["stderr"].strip())
95
+ if result.get("result") is not None:
96
+ lines.append("result:\n" + str(result["result"]).strip())
97
+ for df_info in result.get("dataframes") or []:
98
+ preview = pd.DataFrame(df_info["head"])
99
+ lines.append(f"DataFrame {df_info['name']} shape={df_info['shape']}\n{preview}")
100
+ else:
101
+ lines.append("Python execution failed.")
102
+ if result.get("stderr"):
103
+ lines.append(result["stderr"].strip())
104
+ return "\n\n".join(lines)[:12000]
105
+
106
+
107
+ @tool
108
+ def download_file_from_url(url: str, filename: Optional[str] = None) -> str:
109
+ """Download a file from an http(s) URL to a local path and return that path.
110
+
111
+ Args:
112
+ url: Direct download URL.
113
+ filename: Optional filename to use in the temp directory.
114
+ """
115
+ from urllib.parse import urlparse
116
+ import tempfile
117
+ import uuid
118
+
119
+ try:
120
+ if not filename:
121
+ filename = os.path.basename(urlparse(url).path) or f"download_{uuid.uuid4().hex[:8]}"
122
+ filepath = os.path.join(tempfile.gettempdir(), filename)
123
+ response = requests.get(url, timeout=60, stream=True)
124
+ response.raise_for_status()
125
+ with open(filepath, "wb") as handle:
126
+ for chunk in response.iter_content(chunk_size=8192):
127
+ handle.write(chunk)
128
+ return f"Downloaded to {filepath}"
129
+ except Exception as exc:
130
+ return f"Download failed: {exc}"
131
+
132
+
133
+ @tool
134
+ def read_file(file_path: str, max_chars: int = 8000) -> str:
135
+ """Read a local text, CSV, Excel, JSON, or PDF file and return a preview.
136
+
137
+ Args:
138
+ file_path: Absolute path on disk.
139
+ max_chars: Truncate text-like previews to this many characters.
140
+ """
141
+ if not os.path.exists(file_path):
142
+ return f"File not found: {file_path}"
143
+
144
+ ext = os.path.splitext(file_path)[1].lower()
145
+ try:
146
+ if ext in {".csv"}:
147
+ df = pd.read_csv(file_path)
148
+ return (
149
+ f"CSV rows={len(df)} cols={list(df.columns)}\n"
150
+ f"{df.head(20).to_string()}\n\n{df.describe(include='all')}"
151
+ )[:max_chars]
152
+ if ext in {".xlsx", ".xls"}:
153
+ df = pd.read_excel(file_path)
154
+ return (
155
+ f"Excel rows={len(df)} cols={list(df.columns)}\n"
156
+ f"{df.head(20).to_string()}\n\n{df.describe(include='all')}"
157
+ )[:max_chars]
158
+ if ext == ".pdf":
159
+ try:
160
+ import fitz
161
+ except ImportError:
162
+ return "PyMuPDF is not installed; use execute_python if available."
163
+ doc = fitz.open(file_path)
164
+ text = "\n".join(page.get_text() for page in doc)
165
+ doc.close()
166
+ return text[:max_chars] or "PDF had no extractable text."
167
+ with open(file_path, "r", encoding="utf-8", errors="replace") as handle:
168
+ return handle.read(max_chars)
169
+ except Exception as exc:
170
+ return f"Failed to read {file_path}: {exc}"
171
+
172
+
173
+ @tool
174
+ def extract_text_from_image(image_path: str) -> str:
175
+ """OCR text from a local image with Tesseract.
176
+
177
+ Args:
178
+ image_path: Absolute path to a jpg/png/webp/gif/bmp file.
179
+ """
180
+ try:
181
+ image = Image.open(image_path)
182
+ text = pytesseract.image_to_string(image)
183
+ return text.strip() or "OCR returned no text."
184
+ except Exception as exc:
185
+ return f"OCR failed: {exc}"
186
+
187
+
188
+ TOOLS = [
189
+ web_search,
190
+ wiki_search,
191
+ execute_python,
192
+ download_file_from_url,
193
+ read_file,
194
+ extract_text_from_image,
195
+ ]
196
+
197
+
198
+ def _tool_signature(message) -> Optional[str]:
199
+ calls = getattr(message, "tool_calls", None) or []
200
+ if not calls:
201
+ return None
202
+ parts = []
203
+ for call in calls:
204
+ name = call.get("name") if isinstance(call, dict) else getattr(call, "name", "")
205
+ args = call.get("args") if isinstance(call, dict) else getattr(call, "args", {})
206
+ parts.append(f"{name}:{json.dumps(args, sort_keys=True, default=str)}")
207
+ return "|".join(parts)
208
+
209
+
210
+ def _should_force_final(messages) -> bool:
211
+ tool_round_messages = [m for m in messages if getattr(m, "tool_calls", None)]
212
+ if len(tool_round_messages) >= MAX_TOOL_ROUNDS:
213
+ return True
214
+ if len(tool_round_messages) >= 2:
215
+ if _tool_signature(tool_round_messages[-1]) == _tool_signature(tool_round_messages[-2]):
216
+ return True
217
+ return False
218
+
219
+
220
+ def build_graph(provider: str = "groq", use_retriever: bool = False):
221
+ """Compile the tool-calling graph. Retriever is off unless explicitly enabled."""
222
+ if provider != "groq":
223
+ raise ValueError("Only provider='groq' is supported after the refactor.")
224
+
225
+ llm = ChatGroq(model="qwen/qwen3-32b", temperature=0)
226
+ llm_with_tools = llm.bind_tools(TOOLS)
227
+
228
+ retriever_store = _build_optional_retriever() if use_retriever else None
229
+
230
+ def maybe_example(state: MessagesState):
231
+ if retriever_store is None:
232
+ return {}
233
+ question = ""
234
+ for message in state["messages"]:
235
+ if isinstance(message, HumanMessage):
236
+ question = message.content
237
+ break
238
+ hits = retriever_store.similarity_search(question, k=1)
239
+ if not hits:
240
+ return {}
241
+ return {
242
+ "messages": [
243
+ HumanMessage(
244
+ content=(
245
+ "Optional similar example for format only. "
246
+ "Do not copy if it is a different question.\n\n"
247
+ f"{hits[0].page_content}"
248
+ )
249
+ )
250
+ ]
251
+ }
252
+
253
+ def assistant(state: MessagesState):
254
+ messages = state["messages"]
255
+ if _should_force_final(messages):
256
+ stop = SystemMessage(
257
+ content=(
258
+ "Stop calling tools. Using only the information already in this "
259
+ "conversation, reply with one line: FINAL ANSWER: <answer>"
260
+ )
261
+ )
262
+ return {"messages": [llm.invoke(messages + [stop])]}
263
+ return {"messages": [llm_with_tools.invoke(messages)]}
264
+
265
+ builder = StateGraph(MessagesState)
266
+ builder.add_node("maybe_example", maybe_example)
267
+ builder.add_node("assistant", assistant)
268
+ builder.add_node("tools", ToolNode(TOOLS))
269
+ builder.add_edge(START, "maybe_example")
270
+ builder.add_edge("maybe_example", "assistant")
271
+ builder.add_conditional_edges("assistant", tools_condition)
272
+ builder.add_edge("tools", "assistant")
273
+ return builder.compile()
274
+
275
+
276
+ def _build_optional_retriever():
277
+ url = os.environ.get("SUPABASE_URL")
278
+ key = os.environ.get("SUPABASE_SERVICE_ROLE_KEY")
279
+ if not url or not key:
280
+ print("Retriever requested but SUPABASE_* env vars are missing; skipping.")
281
+ return None
282
+ from langchain_community.vectorstores import SupabaseVectorStore
283
+ from langchain_huggingface import HuggingFaceEmbeddings
284
+ from supabase.client import create_client
285
+
286
+ embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-mpnet-base-v2")
287
+ client = create_client(url, key)
288
+ return SupabaseVectorStore(
289
+ client=client,
290
+ embedding=embeddings,
291
+ table_name="documents2",
292
+ query_name="match_documents_2",
293
+ )
294
+
295
+
296
+ def run_agent(
297
+ question: str,
298
+ file_paths: Optional[List[str]] = None,
299
+ graph=None,
300
+ use_retriever: bool = False,
301
+ ) -> dict:
302
+ """Run one question and return raw last message plus extracted FINAL ANSWER."""
303
+ if graph is None:
304
+ graph = build_graph(use_retriever=use_retriever)
305
+ payload = format_user_message(question, file_paths)
306
+ result = graph.invoke(
307
+ {
308
+ "messages": [
309
+ SystemMessage(content=get_system_prompt()),
310
+ HumanMessage(content=payload),
311
+ ]
312
+ },
313
+ {"recursion_limit": RECURSION_LIMIT},
314
+ )
315
+ last = result["messages"][-1]
316
+ raw = last.content if hasattr(last, "content") else str(last)
317
+ if isinstance(raw, list):
318
+ raw = "".join(
319
+ part.get("text", "") if isinstance(part, dict) else str(part) for part in raw
320
+ )
321
+ return {
322
+ "raw": raw,
323
+ "final": extract_final_answer(raw),
324
+ "messages": result["messages"],
325
+ }
326
+
327
+
328
+ if __name__ == "__main__":
329
+ demo = "When was a picture of St. Thomas Aquinas first added to the Wikipedia page on the Principle of double effect?"
330
+ output = run_agent(demo)
331
+ print("RAW:\n", output["raw"])
332
+ print("FINAL:", output["final"])
app.py CHANGED
@@ -1,196 +1,872 @@
1
- import os
2
  import gradio as gr
3
- import requests
4
- import inspect
5
- import pandas as pd
6
-
7
- # (Keep Constants as is)
8
- # --- Constants ---
9
- DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
10
 
11
- # --- Basic Agent Definition ---
12
- # ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
13
- class BasicAgent:
14
  def __init__(self):
15
- print("BasicAgent initialized.")
16
- def __call__(self, question: str) -> str:
17
- print(f"Agent received question (first 50 chars): {question[:50]}...")
18
- fixed_answer = "This is a default answer."
19
- print(f"Agent returning fixed answer: {fixed_answer}")
20
- return fixed_answer
21
-
22
- def run_and_submit_all( profile: gr.OAuthProfile | None):
23
- """
24
- Fetches all questions, runs the BasicAgent on them, submits all answers,
25
- and displays the results.
26
- """
27
- # --- Determine HF Space Runtime URL and Repo URL ---
28
- space_id = os.getenv("SPACE_ID") # Get the SPACE_ID for sending link to the code
29
-
30
- if profile:
31
- username= f"{profile.username}"
32
- print(f"User logged in: {username}")
33
- else:
34
- print("User not logged in.")
35
- return "Please Login to Hugging Face with the button.", None
36
-
37
- api_url = DEFAULT_API_URL
38
- questions_url = f"{api_url}/questions"
39
- submit_url = f"{api_url}/submit"
40
-
41
- # 1. Instantiate Agent ( modify this part to create your agent)
42
- try:
43
- agent = BasicAgent()
44
- except Exception as e:
45
- print(f"Error instantiating agent: {e}")
46
- return f"Error initializing agent: {e}", None
47
- # 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)
48
- agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
49
- print(agent_code)
50
-
51
- # 2. Fetch Questions
52
- print(f"Fetching questions from: {questions_url}")
53
- try:
54
- response = requests.get(questions_url, timeout=15)
55
- response.raise_for_status()
56
- questions_data = response.json()
57
- if not questions_data:
58
- print("Fetched questions list is empty.")
59
- return "Fetched questions list is empty or invalid format.", None
60
- print(f"Fetched {len(questions_data)} questions.")
61
- except requests.exceptions.RequestException as e:
62
- print(f"Error fetching questions: {e}")
63
- return f"Error fetching questions: {e}", None
64
- except requests.exceptions.JSONDecodeError as e:
65
- print(f"Error decoding JSON response from questions endpoint: {e}")
66
- print(f"Response text: {response.text[:500]}")
67
- return f"Error decoding server response for 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({"task_id": task_id, "submitted_answer": submitted_answer})
85
- results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
86
- except Exception as e:
87
- print(f"Error running agent on task {task_id}: {e}")
88
- results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"AGENT ERROR: {e}"})
 
 
 
89
 
90
- if not answers_payload:
91
- print("Agent did not produce any answers to submit.")
92
- return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
 
93
 
94
- # 4. Prepare Submission
95
- submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload}
96
- status_update = f"Agent finished. Submitting {len(answers_payload)} answers for user '{username}'..."
97
- print(status_update)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
98
 
99
- # 5. Submit
100
- print(f"Submitting {len(answers_payload)} answers to: {submit_url}")
101
- try:
102
- response = requests.post(submit_url, json=submission_data, timeout=60)
103
- response.raise_for_status()
104
- result_data = response.json()
105
- final_status = (
106
- f"Submission Successful!\n"
107
- f"User: {result_data.get('username')}\n"
108
- f"Overall Score: {result_data.get('score', 'N/A')}% "
109
- f"({result_data.get('correct_count', '?')}/{result_data.get('total_attempted', '?')} correct)\n"
110
- f"Message: {result_data.get('message', 'No message received.')}"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
111
  )
112
- print("Submission successful.")
113
- results_df = pd.DataFrame(results_log)
114
- return final_status, results_df
115
- except requests.exceptions.HTTPError as e:
116
- error_detail = f"Server responded with status {e.response.status_code}."
117
- try:
118
- error_json = e.response.json()
119
- error_detail += f" Detail: {error_json.get('detail', e.response.text)}"
120
- except requests.exceptions.JSONDecodeError:
121
- error_detail += f" Response: {e.response.text[:500]}"
122
- status_message = f"Submission Failed: {error_detail}"
123
- print(status_message)
124
- results_df = pd.DataFrame(results_log)
125
- return status_message, results_df
126
- except requests.exceptions.Timeout:
127
- status_message = "Submission Failed: The request timed out."
128
- print(status_message)
129
- results_df = pd.DataFrame(results_log)
130
- return status_message, results_df
131
- except requests.exceptions.RequestException as e:
132
- status_message = f"Submission Failed: Network error - {e}"
133
- print(status_message)
134
- results_df = pd.DataFrame(results_log)
135
- return status_message, results_df
136
- except Exception as e:
137
- status_message = f"An unexpected error occurred during submission: {e}"
138
- print(status_message)
139
- results_df = pd.DataFrame(results_log)
140
- return status_message, results_df
141
-
142
-
143
- # --- Build Gradio Interface using Blocks ---
144
- with gr.Blocks() as demo:
145
- gr.Markdown("# Basic Agent Evaluation Runner")
146
- gr.Markdown(
147
- """
148
- **Instructions:**
149
-
150
- 1. Please clone this space, then modify the code to define your agent's logic, the tools, the necessary packages, etc ...
151
- 2. Log in to your Hugging Face account using the button below. This uses your HF username for submission.
152
- 3. Click 'Run Evaluation & Submit All Answers' to fetch questions, run your agent, submit answers, and see the score.
153
-
154
- ---
155
- **Disclaimers:**
156
- 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).
157
- 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.
158
- """
159
- )
160
-
161
- gr.LoginButton()
162
-
163
- run_button = gr.Button("Run Evaluation & Submit All Answers")
164
-
165
- status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
166
- # Removed max_rows=10 from DataFrame constructor
167
- results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
168
-
169
- run_button.click(
170
- fn=run_and_submit_all,
171
- outputs=[status_output, results_table]
172
- )
173
 
174
  if __name__ == "__main__":
175
- print("\n" + "-"*30 + " App Starting " + "-"*30)
176
- # Check for SPACE_HOST and SPACE_ID at startup for information
177
- space_host_startup = os.getenv("SPACE_HOST")
178
- space_id_startup = os.getenv("SPACE_ID") # Get SPACE_ID at startup
179
-
180
- if space_host_startup:
181
- print(f"βœ… SPACE_HOST found: {space_host_startup}")
182
- print(f" Runtime URL should be: https://{space_host_startup}.hf.space")
183
- else:
184
- print("ℹ️ SPACE_HOST environment variable not found (running locally?).")
185
-
186
- if space_id_startup: # Print repo URLs if SPACE_ID is found
187
- print(f"βœ… SPACE_ID found: {space_id_startup}")
188
- print(f" Repo URL: https://huggingface.co/spaces/{space_id_startup}")
189
- print(f" Repo Tree URL: https://huggingface.co/spaces/{space_id_startup}/tree/main")
190
- else:
191
- print("ℹ️ SPACE_ID environment variable not found (running locally?). Repo URL cannot be determined.")
192
-
193
- print("-"*(60 + len(" App Starting ")) + "\n")
194
-
195
- print("Launching Gradio Interface for Basic Agent Evaluation...")
196
- demo.launch(debug=True, share=False)
 
 
1
  import gradio as gr
2
+ import os
3
+ from typing import List, Tuple, Optional
4
+ from agent import build_graph, run_agent
 
 
 
 
5
 
6
+ class QnAChatbot:
7
+ """A Q&A chatbot interface for the agent."""
8
+
9
  def __init__(self):
10
+ print("πŸ€– QnAChatbot initializing...")
11
+ print("πŸ”§ Building agent graph...")
12
+ self.graph = build_graph()
13
+ self.conversation_history = []
14
+ print("βœ… QnAChatbot initialized successfully")
15
+
16
+ def process_question(self, question: str, history: List[Tuple[str, str]], uploaded_files: Optional[List] = None) -> Tuple[str, List[Tuple[str, str]]]:
17
+ """Process a question and return the response with updated history."""
18
+ if not question.strip() and not uploaded_files:
19
+ print("⚠️ No question or files provided")
20
+ return "", history
21
+
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22
  try:
23
+ print(f"\n{'='*60}")
24
+ print(f"πŸ€– Processing new question...")
25
+ print(f"πŸ“ Question: {question[:100]}{'...' if len(question) > 100 else ''}")
26
+ print(f"πŸ“ Files uploaded: {len(uploaded_files) if uploaded_files else 0}")
27
+
28
+ file_paths = []
29
+ if uploaded_files:
30
+ print(f"πŸ“‚ Processing {len(uploaded_files)} uploaded file(s)...")
31
+ file_paths = self._collect_file_paths(uploaded_files)
32
 
33
+ print("πŸ”„ Invoking agent graph...")
34
+ result = run_agent(question, file_paths=file_paths, graph=self.graph)
35
+ print(f"πŸ“¨ Received {len(result['messages'])} message(s) from agent")
36
+ print(f"🏁 Extracted FINAL ANSWER: {result['final']}")
37
 
38
+ answer = result["raw"]
39
+ if result["final"] and result["final"] not in answer:
40
+ answer = f"{answer}\n\nExtracted: {result['final']}"
41
+
42
+ # Update conversation history
43
+ history.append((question, answer))
44
+ print(f"βœ… Question processed successfully")
45
+ print(f"πŸ“Š Response length: {len(answer)} characters")
46
+ print(f"πŸ’¬ Total conversation history: {len(history)} exchanges")
47
+ print(f"{'='*60}\n")
48
+
49
+ return "", history
50
+
51
+ except Exception as e:
52
+ error_msg = f"Error processing question: {str(e)}"
53
+ print(f"❌ {error_msg}")
54
+ print(f"πŸ” Exception details: {type(e).__name__}: {str(e)}")
55
+ import traceback
56
+ print(f"πŸ“‹ Traceback:\n{traceback.format_exc()}")
57
+ history.append((question, error_msg))
58
+ print(f"{'='*60}\n")
59
+ return "", history
60
+
61
+ def _collect_file_paths(self, uploaded_files: List) -> List[str]:
62
+ """Keep local paths only; do not inline base64 into the prompt."""
63
+ paths = []
64
+ for file_path in uploaded_files:
65
+ if not file_path or not os.path.exists(file_path):
66
+ print(f"⚠️ Skipping invalid file path: {file_path}")
67
+ continue
68
+ print(f"πŸ“„ File ready: {os.path.basename(file_path)} ({os.path.getsize(file_path)} bytes)")
69
+ paths.append(file_path)
70
+ return paths
71
+
72
+ def clear_history(self):
73
+ """Clear the conversation history."""
74
+ print("🧹 Clearing conversation history...")
75
+ self.conversation_history = []
76
+ print("βœ… Conversation history cleared")
77
+ return []
78
 
79
+ def create_qna_interface():
80
+ """Create the Q&A chatbot interface."""
81
+
82
+ print("πŸš€ Creating Q&A interface...")
83
+ # Initialize the chatbot
84
+ chatbot = QnAChatbot()
85
+ print("🎨 Setting up UI components...")
86
+
87
+ # Enhanced Custom CSS for modern, professional styling
88
+ custom_css = """
89
+ /* Import Google Fonts */
90
+ @import url('https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700;800&family=JetBrains+Mono:wght@400;500&display=swap');
91
+
92
+ /* CSS Variables for Theme Support */
93
+ :root {
94
+ --primary-gradient: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
95
+ --secondary-gradient: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
96
+ --success-gradient: linear-gradient(135deg, #4facfe 0%, #00f2fe 100%);
97
+ --warning-gradient: linear-gradient(135deg, #43e97b 0%, #38f9d7 100%);
98
+ --glass-bg: rgba(255, 255, 255, 0.95);
99
+ --glass-border: rgba(255, 255, 255, 0.2);
100
+ --text-primary: #2d3748;
101
+ --text-secondary: #4a5568;
102
+ --text-light: #718096;
103
+ --bg-light: #f7fafc;
104
+ --bg-card: #ffffff;
105
+ --shadow-sm: 0 2px 10px rgba(0, 0, 0, 0.05);
106
+ --shadow-md: 0 10px 30px rgba(0, 0, 0, 0.1);
107
+ --shadow-lg: 0 20px 40px rgba(0, 0, 0, 0.15);
108
+ --border-radius: 12px;
109
+ --border-radius-lg: 20px;
110
+ --transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
111
+ }
112
+
113
+ /* Global Styles */
114
+ * {
115
+ font-family: 'Inter', sans-serif !important;
116
+ box-sizing: border-box !important;
117
+ }
118
+
119
+ /* Main Container with Enhanced Background */
120
+ .gradio-container {
121
+ max-width: 1400px !important;
122
+ margin: 0 auto !important;
123
+ background:
124
+ radial-gradient(circle at 20% 50%, rgba(120, 119, 198, 0.3), transparent 50%),
125
+ radial-gradient(circle at 80% 20%, rgba(255, 119, 198, 0.3), transparent 50%),
126
+ radial-gradient(circle at 40% 80%, rgba(120, 219, 255, 0.3), transparent 50%),
127
+ var(--primary-gradient) !important;
128
+ min-height: 100vh !important;
129
+ padding: 20px !important;
130
+ position: relative !important;
131
+ overflow-x: hidden !important;
132
+ }
133
+
134
+ /* Animated Background Particles */
135
+ .gradio-container::before {
136
+ content: '' !important;
137
+ position: fixed !important;
138
+ top: 0 !important;
139
+ left: 0 !important;
140
+ width: 100% !important;
141
+ height: 100% !important;
142
+ background: url("data:image/svg+xml,%3Csvg width='60' height='60' viewBox='0 0 60 60' xmlns='http://www.w3.org/2000/svg'%3E%3Cg fill='none' fill-rule='evenodd'%3E%3Cg fill='%23ffffff' fill-opacity='0.05'%3E%3Ccircle cx='7' cy='7' r='1'/%3E%3Ccircle cx='53' cy='7' r='1'/%3E%3Ccircle cx='7' cy='53' r='1'/%3E%3Ccircle cx='53' cy='53' r='1'/%3E%3C/g%3E%3C/g%3E%3C/svg%3E") !important;
143
+ animation: float 20s ease-in-out infinite !important;
144
+ pointer-events: none !important;
145
+ z-index: 0 !important;
146
+ }
147
+
148
+ /* Main Content Area with Glass Effect */
149
+ .main-content {
150
+ background: var(--glass-bg) !important;
151
+ backdrop-filter: blur(20px) !important;
152
+ -webkit-backdrop-filter: blur(20px) !important;
153
+ border-radius: var(--border-radius-lg) !important;
154
+ box-shadow:
155
+ var(--shadow-lg),
156
+ inset 0 1px 0 var(--glass-border) !important;
157
+ padding: 40px !important;
158
+ margin: 20px 0 !important;
159
+ position: relative !important;
160
+ z-index: 1 !important;
161
+ border: 1px solid var(--glass-border) !important;
162
+ transition: var(--transition) !important;
163
+ }
164
+
165
+ .main-content:hover {
166
+ transform: translateY(-2px) !important;
167
+ box-shadow:
168
+ 0 25px 50px rgba(0, 0, 0, 0.15),
169
+ inset 0 1px 0 var(--glass-border) !important;
170
+ }
171
+
172
+ /* Enhanced Header with Animations */
173
+ .markdown h1 {
174
+ background: var(--primary-gradient) !important;
175
+ -webkit-background-clip: text !important;
176
+ -webkit-text-fill-color: transparent !important;
177
+ background-clip: text !important;
178
+ font-size: 3rem !important;
179
+ font-weight: 800 !important;
180
+ text-align: center !important;
181
+ margin-bottom: 1.5rem !important;
182
+ position: relative !important;
183
+ animation: titleGlow 3s ease-in-out infinite alternate !important;
184
+ }
185
+
186
+ .markdown h1::after {
187
+ content: '' !important;
188
+ position: absolute !important;
189
+ bottom: -10px !important;
190
+ left: 50% !important;
191
+ transform: translateX(-50%) !important;
192
+ width: 100px !important;
193
+ height: 4px !important;
194
+ background: var(--primary-gradient) !important;
195
+ border-radius: 2px !important;
196
+ animation: pulse 2s ease-in-out infinite !important;
197
+ }
198
+
199
+ /* Enhanced Chat Interface */
200
+ .chatbot {
201
+ border: none !important;
202
+ border-radius: var(--border-radius) !important;
203
+ box-shadow: var(--shadow-md) !important;
204
+ background: var(--bg-card) !important;
205
+ overflow: hidden !important;
206
+ position: relative !important;
207
+ }
208
+
209
+ /* Chat Messages with Better Styling */
210
+ .chatbot .message-wrap {
211
+ padding: 20px !important;
212
+ margin: 15px !important;
213
+ border-radius: 18px !important;
214
+ max-width: 85% !important;
215
+ animation: messageSlideIn 0.5s cubic-bezier(0.4, 0, 0.2, 1) !important;
216
+ position: relative !important;
217
+ word-wrap: break-word !important;
218
+ }
219
+
220
+ .chatbot .message.user {
221
+ background: var(--primary-gradient) !important;
222
+ color: white !important;
223
+ margin-left: auto !important;
224
+ margin-right: 15px !important;
225
+ box-shadow: 0 8px 25px rgba(102, 126, 234, 0.4) !important;
226
+ border-bottom-right-radius: 5px !important;
227
+ }
228
+
229
+ .chatbot .message.user::before {
230
+ content: 'πŸ§‘β€πŸ’»' !important;
231
+ position: absolute !important;
232
+ top: -25px !important;
233
+ right: 10px !important;
234
+ font-size: 16px !important;
235
+ background: var(--bg-card) !important;
236
+ padding: 5px 8px !important;
237
+ border-radius: 20px !important;
238
+ box-shadow: var(--shadow-sm) !important;
239
+ }
240
+
241
+ .chatbot .message.bot {
242
+ background: linear-gradient(135deg, #f8f9ff 0%, #e8eeff 100%) !important;
243
+ color: var(--text-primary) !important;
244
+ margin-right: auto !important;
245
+ margin-left: 15px !important;
246
+ border: 1px solid #e2e8f0 !important;
247
+ box-shadow: var(--shadow-sm) !important;
248
+ border-bottom-left-radius: 5px !important;
249
+ }
250
+
251
+ .chatbot .message.bot::before {
252
+ content: 'πŸ€–' !important;
253
+ position: absolute !important;
254
+ top: -25px !important;
255
+ left: 10px !important;
256
+ font-size: 16px !important;
257
+ background: var(--bg-card) !important;
258
+ padding: 5px 8px !important;
259
+ border-radius: 20px !important;
260
+ box-shadow: var(--shadow-sm) !important;
261
+ }
262
+
263
+ /* Typing Indicator */
264
+ .typing-indicator {
265
+ display: flex !important;
266
+ align-items: center !important;
267
+ padding: 15px 20px !important;
268
+ margin: 15px !important;
269
+ background: linear-gradient(135deg, #f8f9ff 0%, #e8eeff 100%) !important;
270
+ border-radius: 18px !important;
271
+ max-width: 85% !important;
272
+ margin-right: auto !important;
273
+ margin-left: 15px !important;
274
+ border: 1px solid #e2e8f0 !important;
275
+ animation: messageSlideIn 0.5s cubic-bezier(0.4, 0, 0.2, 1) !important;
276
+ }
277
+
278
+ .typing-dots {
279
+ display: flex !important;
280
+ align-items: center !important;
281
+ gap: 4px !important;
282
+ }
283
+
284
+ .typing-dots span {
285
+ width: 8px !important;
286
+ height: 8px !important;
287
+ border-radius: 50% !important;
288
+ background: var(--text-light) !important;
289
+ animation: typingDots 1.4s ease-in-out infinite both !important;
290
+ }
291
+
292
+ .typing-dots span:nth-child(1) { animation-delay: -0.32s !important; }
293
+ .typing-dots span:nth-child(2) { animation-delay: -0.16s !important; }
294
+ .typing-dots span:nth-child(3) { animation-delay: 0s !important; }
295
+
296
+ /* Enhanced Input Areas */
297
+ .textbox input, .textbox textarea {
298
+ border: 2px solid #e2e8f0 !important;
299
+ border-radius: var(--border-radius) !important;
300
+ padding: 18px 24px !important;
301
+ font-size: 16px !important;
302
+ line-height: 1.5 !important;
303
+ transition: var(--transition) !important;
304
+ background: var(--bg-card) !important;
305
+ box-shadow: var(--shadow-sm) !important;
306
+ font-family: 'Inter', sans-serif !important;
307
+ }
308
+
309
+ .textbox input:focus, .textbox textarea:focus {
310
+ border-color: #667eea !important;
311
+ box-shadow:
312
+ 0 0 0 4px rgba(102, 126, 234, 0.1),
313
+ var(--shadow-md) !important;
314
+ outline: none !important;
315
+ transform: translateY(-1px) !important;
316
+ background: #ffffff !important;
317
+ }
318
+
319
+ /* Enhanced Buttons with Micro-interactions */
320
+ .btn {
321
+ border-radius: var(--border-radius) !important;
322
+ font-weight: 600 !important;
323
+ text-transform: none !important;
324
+ letter-spacing: 0.3px !important;
325
+ transition: var(--transition) !important;
326
+ border: none !important;
327
+ box-shadow: var(--shadow-sm) !important;
328
+ padding: 14px 28px !important;
329
+ font-size: 16px !important;
330
+ position: relative !important;
331
+ overflow: hidden !important;
332
+ cursor: pointer !important;
333
+ }
334
+
335
+ .btn::before {
336
+ content: '' !important;
337
+ position: absolute !important;
338
+ top: 0 !important;
339
+ left: -100% !important;
340
+ width: 100% !important;
341
+ height: 100% !important;
342
+ background: linear-gradient(90deg, transparent, rgba(255,255,255,0.3), transparent) !important;
343
+ transition: left 0.5s !important;
344
+ }
345
+
346
+ .btn:hover::before {
347
+ left: 100% !important;
348
+ }
349
+
350
+ .btn-primary {
351
+ background: var(--primary-gradient) !important;
352
+ color: white !important;
353
+ box-shadow: 0 8px 25px rgba(102, 126, 234, 0.3) !important;
354
+ }
355
+
356
+ .btn-primary:hover {
357
+ transform: translateY(-3px) scale(1.02) !important;
358
+ box-shadow: 0 12px 35px rgba(102, 126, 234, 0.4) !important;
359
+ }
360
+
361
+ .btn-primary:active {
362
+ transform: translateY(-1px) scale(0.98) !important;
363
+ }
364
+
365
+ .btn-secondary {
366
+ background: linear-gradient(135deg, #f7fafc 0%, #edf2f7 100%) !important;
367
+ color: var(--text-secondary) !important;
368
+ border: 1px solid #e2e8f0 !important;
369
+ }
370
+
371
+ .btn-secondary:hover {
372
+ transform: translateY(-2px) !important;
373
+ background: linear-gradient(135deg, #edf2f7 0%, #e2e8f0 100%) !important;
374
+ box-shadow: var(--shadow-md) !important;
375
+ }
376
+
377
+ /* Enhanced File Upload with Drag & Drop Animation */
378
+ .file-upload {
379
+ border: 3px dashed #cbd5e0 !important;
380
+ border-radius: var(--border-radius) !important;
381
+ background: linear-gradient(135deg, #f7fafc 0%, #edf2f7 100%) !important;
382
+ padding: 40px !important;
383
+ text-align: center !important;
384
+ transition: var(--transition) !important;
385
+ position: relative !important;
386
+ overflow: hidden !important;
387
+ cursor: pointer !important;
388
+ }
389
+
390
+ .file-upload:hover {
391
+ border-color: #667eea !important;
392
+ background: linear-gradient(135deg, #edf2f7 0%, #e2e8f0 100%) !important;
393
+ transform: translateY(-2px) scale(1.01) !important;
394
+ box-shadow: var(--shadow-md) !important;
395
+ }
396
+
397
+ .file-upload::before {
398
+ content: 'πŸ“' !important;
399
+ font-size: 4rem !important;
400
+ display: block !important;
401
+ margin-bottom: 15px !important;
402
+ animation: fileFloat 3s ease-in-out infinite !important;
403
+ }
404
+
405
+ .file-upload::after {
406
+ content: 'Drag files here or click to browse' !important;
407
+ position: absolute !important;
408
+ bottom: 15px !important;
409
+ left: 50% !important;
410
+ transform: translateX(-50%) !important;
411
+ font-size: 14px !important;
412
+ color: var(--text-light) !important;
413
+ font-weight: 500 !important;
414
+ }
415
+
416
+ /* Enhanced Examples Section */
417
+ .examples {
418
+ background: linear-gradient(135deg, #f8f9ff 0%, #e8eeff 100%) !important;
419
+ border-radius: var(--border-radius) !important;
420
+ padding: 30px !important;
421
+ margin: 25px 0 !important;
422
+ border: 1px solid #e2e8f0 !important;
423
+ box-shadow: var(--shadow-sm) !important;
424
+ position: relative !important;
425
+ }
426
+
427
+ .examples::before {
428
+ content: 'πŸ’‘' !important;
429
+ position: absolute !important;
430
+ top: -15px !important;
431
+ left: 30px !important;
432
+ background: var(--bg-card) !important;
433
+ padding: 10px !important;
434
+ border-radius: 50% !important;
435
+ font-size: 20px !important;
436
+ box-shadow: var(--shadow-sm) !important;
437
+ }
438
+
439
+ .examples h3 {
440
+ color: #667eea !important;
441
+ font-weight: 700 !important;
442
+ margin-bottom: 20px !important;
443
+ font-size: 1.3rem !important;
444
+ margin-left: 20px !important;
445
+ }
446
+
447
+ /* Feature Cards with Hover Effects */
448
+ .feature-card {
449
+ background: var(--bg-card) !important;
450
+ border-radius: var(--border-radius) !important;
451
+ padding: 25px !important;
452
+ margin: 15px 0 !important;
453
+ box-shadow: var(--shadow-sm) !important;
454
+ border-left: 4px solid transparent !important;
455
+ transition: var(--transition) !important;
456
+ cursor: pointer !important;
457
+ }
458
+
459
+ .feature-card:hover {
460
+ transform: translateY(-5px) translateX(5px) !important;
461
+ box-shadow: var(--shadow-md) !important;
462
+ }
463
+
464
+ .feature-card.research {
465
+ border-left-color: #667eea !important;
466
+ }
467
+
468
+ .feature-card.code {
469
+ border-left-color: #48bb78 !important;
470
+ }
471
+
472
+ .feature-card.data {
473
+ border-left-color: #ed8936 !important;
474
+ }
475
+
476
+ .feature-card.image {
477
+ border-left-color: #dd6b20 !important;
478
+ }
479
+
480
+ /* Status Indicator with Pulse Animation */
481
+ .status-indicator {
482
+ display: inline-block !important;
483
+ width: 12px !important;
484
+ height: 12px !important;
485
+ border-radius: 50% !important;
486
+ background: radial-gradient(circle, #48bb78, #38a169) !important;
487
+ margin-right: 12px !important;
488
+ animation: statusPulse 2s ease-in-out infinite !important;
489
+ box-shadow: 0 0 0 0 rgba(72, 187, 120, 0.7) !important;
490
+ }
491
+
492
+ /* Enhanced Footer */
493
+ .footer {
494
+ background: var(--glass-bg) !important;
495
+ backdrop-filter: blur(10px) !important;
496
+ border-radius: var(--border-radius) !important;
497
+ padding: 30px !important;
498
+ margin-top: 40px !important;
499
+ text-align: center !important;
500
+ border: 1px solid var(--glass-border) !important;
501
+ box-shadow: var(--shadow-sm) !important;
502
+ }
503
+
504
+ /* Advanced Animations */
505
+ @keyframes titleGlow {
506
+ 0%, 100% { text-shadow: 0 0 20px rgba(102, 126, 234, 0.5); }
507
+ 50% { text-shadow: 0 0 30px rgba(102, 126, 234, 0.8), 0 0 40px rgba(118, 75, 162, 0.6); }
508
+ }
509
+
510
+ @keyframes messageSlideIn {
511
+ from {
512
+ opacity: 0;
513
+ transform: translateY(30px) scale(0.95);
514
+ }
515
+ to {
516
+ opacity: 1;
517
+ transform: translateY(0) scale(1);
518
+ }
519
+ }
520
+
521
+ @keyframes typingDots {
522
+ 0%, 80%, 100% { transform: scale(0.8); opacity: 0.5; }
523
+ 40% { transform: scale(1.2); opacity: 1; }
524
+ }
525
+
526
+ @keyframes fileFloat {
527
+ 0%, 100% { transform: translateY(0px); }
528
+ 50% { transform: translateY(-10px); }
529
+ }
530
+
531
+ @keyframes statusPulse {
532
+ 0% {
533
+ transform: scale(0.95);
534
+ box-shadow: 0 0 0 0 rgba(72, 187, 120, 0.7);
535
+ }
536
+ 70% {
537
+ transform: scale(1);
538
+ box-shadow: 0 0 0 10px rgba(72, 187, 120, 0);
539
+ }
540
+ 100% {
541
+ transform: scale(0.95);
542
+ box-shadow: 0 0 0 0 rgba(72, 187, 120, 0);
543
+ }
544
+ }
545
+
546
+ @keyframes float {
547
+ 0%, 100% { transform: translateY(0px) rotate(0deg); }
548
+ 33% { transform: translateY(-10px) rotate(1deg); }
549
+ 66% { transform: translateY(-5px) rotate(-1deg); }
550
+ }
551
+
552
+ @keyframes pulse {
553
+ 0%, 100% { transform: scale(1) scaleX(1); }
554
+ 50% { transform: scale(1.05) scaleX(1.1); }
555
+ }
556
+
557
+ /* Responsive Design Enhancements */
558
+ @media (max-width: 1024px) {
559
+ .gradio-container { padding: 15px !important; }
560
+ .main-content { padding: 25px !important; margin: 15px 0 !important; }
561
+ .markdown h1 { font-size: 2.5rem !important; }
562
+ }
563
+
564
+ @media (max-width: 768px) {
565
+ .gradio-container { padding: 10px !important; }
566
+ .main-content { padding: 20px !important; margin: 10px 0 !important; }
567
+ .chatbot .message-wrap { max-width: 90% !important; margin: 10px !important; padding: 15px !important; }
568
+ .markdown h1 { font-size: 2rem !important; }
569
+ .btn { padding: 12px 20px !important; font-size: 14px !important; }
570
+ .file-upload { padding: 30px 20px !important; }
571
+ }
572
+
573
+ @media (max-width: 480px) {
574
+ .markdown h1 { font-size: 1.8rem !important; }
575
+ .chatbot .message-wrap { margin: 8px !important; padding: 12px !important; }
576
+ .main-content { padding: 15px !important; }
577
+ }
578
+
579
+ /* Custom Scrollbar Enhancement */
580
+ ::-webkit-scrollbar { width: 12px; }
581
+ ::-webkit-scrollbar-track {
582
+ background: rgba(241, 241, 241, 0.5);
583
+ border-radius: 6px;
584
+ }
585
+ ::-webkit-scrollbar-thumb {
586
+ background: var(--primary-gradient);
587
+ border-radius: 6px;
588
+ border: 2px solid rgba(255, 255, 255, 0.2);
589
+ }
590
+ ::-webkit-scrollbar-thumb:hover {
591
+ background: linear-gradient(135deg, #5a6fd8 0%, #6a4190 100%);
592
+ }
593
+
594
+ /* Loading States */
595
+ .loading { animation: pulse 2s infinite !important; }
596
+ .processing {
597
+ position: relative !important;
598
+ overflow: hidden !important;
599
+ }
600
+ .processing::after {
601
+ content: '' !important;
602
+ position: absolute !important;
603
+ top: 0 !important;
604
+ left: -100% !important;
605
+ width: 100% !important;
606
+ height: 100% !important;
607
+ background: linear-gradient(90deg, transparent, rgba(255,255,255,0.4), transparent) !important;
608
+ animation: shimmer 2s infinite !important;
609
+ }
610
+
611
+ @keyframes shimmer {
612
+ 0% { left: -100%; }
613
+ 100% { left: 100%; }
614
+ }
615
+
616
+ /* Dark mode support (future enhancement) */
617
+ @media (prefers-color-scheme: dark) {
618
+ :root {
619
+ --glass-bg: rgba(26, 32, 44, 0.95);
620
+ --text-primary: #e2e8f0;
621
+ --text-secondary: #cbd5e0;
622
+ --text-light: #a0aec0;
623
+ --bg-card: #2d3748;
624
+ --bg-light: #1a202c;
625
+ }
626
+ }
627
+ """
628
+
629
+ with gr.Blocks(css=custom_css, title="GAIA Lite", theme=gr.themes.Soft()) as demo:
630
+ # Header with enhanced styling
631
+ with gr.Row(elem_classes="main-content"):
632
+ gr.Markdown(
633
+ """
634
+ <h1 align="center">πŸ€– GAIA Lite</h1>
635
+
636
+ Welcome to **GAIA Lite**. Ask a question; the agent will search, run Python, or read files, then return a short answer.
637
+
638
+ ### 🌟 **What I can do:**
639
+
640
+ <div style="display: grid; grid-template-columns: repeat(auto-fit, minmax(300px, 1fr)); gap: 20px; margin: 20px 0;">
641
+ <div class="feature-card research" style="background: linear-gradient(135deg, #f8f9ff 0%, #e8eeff 100%); padding: 25px; border-radius: 12px; border-left: 4px solid #667eea; cursor: pointer; transition: all 0.3s ease;">
642
+ <div style="display: flex; align-items: center; margin-bottom: 10px;">
643
+ <span style="font-size: 2rem; margin-right: 15px;">πŸ”</span>
644
+ <strong style="font-size: 1.1rem; color: #667eea;">Research & Search</strong>
645
+ </div>
646
+ <p style="margin: 0; color: #718096; font-size: 14px; line-height: 1.5;">Web search, Wikipedia, academic papers, arXiv research</p>
647
+ </div>
648
+ <div class="feature-card code" style="background: linear-gradient(135deg, #f0fff4 0%, #c6f6d5 100%); padding: 25px; border-radius: 12px; border-left: 4px solid #48bb78; cursor: pointer; transition: all 0.3s ease;">
649
+ <div style="display: flex; align-items: center; margin-bottom: 10px;">
650
+ <span style="font-size: 2rem; margin-right: 15px;">πŸ’»</span>
651
+ <strong style="font-size: 1.1rem; color: #48bb78;">Code Execution</strong>
652
+ </div>
653
+ <p style="margin: 0; color: #718096; font-size: 14px; line-height: 1.5;">Python, Bash, SQL, C, Java with real-time results</p>
654
+ </div>
655
+ <div class="feature-card data" style="background: linear-gradient(135deg, #fffaf0 0%, #fbd38d 100%); padding: 25px; border-radius: 12px; border-left: 4px solid #ed8936; cursor: pointer; transition: all 0.3s ease;">
656
+ <div style="display: flex; align-items: center; margin-bottom: 10px;">
657
+ <span style="font-size: 2rem; margin-right: 15px;">πŸ“Š</span>
658
+ <strong style="font-size: 1.1rem; color: #ed8936;">Data Analysis</strong>
659
+ </div>
660
+ <p style="margin: 0; color: #718096; font-size: 14px; line-height: 1.5;">CSV, Excel, visualizations, statistical analysis</p>
661
+ </div>
662
+ <div class="feature-card image" style="background: linear-gradient(135deg, #fef5e7 0%, #f6ad55 100%); padding: 25px; border-radius: 12px; border-left: 4px solid #dd6b20; cursor: pointer; transition: all 0.3s ease;">
663
+ <div style="display: flex; align-items: center; margin-bottom: 10px;">
664
+ <span style="font-size: 2rem; margin-right: 15px;">πŸ–ΌοΈ</span>
665
+ <strong style="font-size: 1.1rem; color: #dd6b20;">Image Processing</strong>
666
+ </div>
667
+ <p style="margin: 0; color: #718096; font-size: 14px; line-height: 1.5;">Analysis, OCR, transformations, generation</p>
668
+ </div>
669
+ </div>
670
+
671
+ ---
672
+ """
673
+ )
674
+
675
+ # Chat interface with enhanced styling
676
+ with gr.Row(elem_classes="main-content"):
677
+ with gr.Column(scale=1):
678
+ chatbot_interface = gr.Chatbot(
679
+ label="πŸ’¬ Conversation",
680
+ height=600,
681
+ show_label=True,
682
+ container=True,
683
+ bubble_full_width=False,
684
+ elem_classes="chatbot"
685
+ )
686
+
687
+ # File upload section with enhanced styling
688
+ with gr.Row(elem_classes="main-content"):
689
+ with gr.Column():
690
+ file_upload = gr.File(
691
+ label="πŸ“ Upload Files - Drag & drop or click to upload images, documents, CSV, Excel files, etc.",
692
+ file_count="multiple",
693
+ file_types=[
694
+ ".jpg", ".jpeg", ".png", ".gif", ".bmp", ".webp", # Images
695
+ ".txt", ".md", ".py", ".js", ".html", ".css", ".json", ".xml", # Text files
696
+ ".csv", ".xlsx", ".xls", # Data files
697
+ ".pdf", ".doc", ".docx" # Documents
698
+ ],
699
+ height=120,
700
+ elem_classes="file-upload"
701
+ )
702
+
703
+ # Input and buttons with enhanced styling
704
+ with gr.Row(elem_classes="main-content"):
705
+ with gr.Column(scale=8):
706
+ question_input = gr.Textbox(
707
+ label="πŸ’­ Ask a question",
708
+ placeholder="Type your question here or upload files above... (e.g., 'What is the capital of France?', 'Analyze this image', 'Summarize this document')",
709
+ lines=3,
710
+ max_lines=5,
711
+ elem_classes="textbox"
712
+ )
713
+ with gr.Column(scale=2, min_width=120):
714
+ submit_btn = gr.Button("πŸš€ Send", variant="primary", size="lg", elem_classes="btn btn-primary")
715
+
716
+ with gr.Row(elem_classes="main-content"):
717
+ with gr.Column(scale=1):
718
+ clear_btn = gr.Button("🧹 Clear History", variant="secondary", elem_classes="btn btn-secondary")
719
+ with gr.Column(scale=1):
720
+ clear_files_btn = gr.Button("πŸ—‘οΈ Clear Files", variant="secondary", elem_classes="btn btn-secondary")
721
+ with gr.Column(scale=1):
722
+ export_btn = gr.Button("πŸ’Ύ Export Chat", variant="secondary", elem_classes="btn btn-secondary")
723
+
724
+ # Hidden download component for chat export
725
+ download_file = gr.File(visible=False)
726
+
727
+ with gr.Row(elem_classes="main-content"):
728
+ with gr.Column():
729
+ gr.Examples(
730
+ examples=[
731
+ "What is the current population of Tokyo?",
732
+ "Calculate the square root of 144",
733
+ "Write a Python function to sort a list",
734
+ "What are the latest developments in AI?",
735
+ "Explain quantum computing in simple terms",
736
+ ],
737
+ inputs=question_input,
738
+ label="🌐 General Questions"
739
+ )
740
+ with gr.Column():
741
+ gr.Examples(
742
+ examples=[
743
+ "Search for recent papers on machine learning",
744
+ "What is the weather like today?",
745
+ "Create a simple bar chart using Python",
746
+ "Convert 100 USD to EUR",
747
+ "What are the benefits of renewable energy?",
748
+ ],
749
+ inputs=question_input,
750
+ label="πŸ”¬ Research & Analysis"
751
+ )
752
+ with gr.Column():
753
+ gr.Examples(
754
+ examples=[
755
+ "Analyze this image and describe what you see",
756
+ "Extract text from this image using OCR",
757
+ "Summarize the content of this document",
758
+ "Analyze the data in this CSV file",
759
+ "What insights can you find in this Excel file?",
760
+ ],
761
+ inputs=question_input,
762
+ label="πŸ“ File Analysis"
763
+ )
764
+
765
+ # Event handlers
766
+ def submit_question(question, history, files):
767
+ print(f"🎯 UI: Submit button clicked")
768
+ print(f"πŸ“ UI: Question length: {len(question) if question else 0}")
769
+ print(f"πŸ“ UI: Files count: {len(files) if files else 0}")
770
+ result_question, result_history = chatbot.process_question(question, history, files)
771
+ print(f"πŸ”„ UI: Returning results and clearing files")
772
+ return result_question, result_history, None # Clear files after processing
773
+
774
+ def clear_conversation():
775
+ print("🧹 UI: Clear conversation button clicked")
776
+ return chatbot.clear_history()
777
+
778
+ def clear_files():
779
+ print("πŸ—‘οΈ UI: Clear files button clicked")
780
+ return None
781
+
782
+ def export_conversation(history):
783
+ """Export conversation history to a text file"""
784
+ print("πŸ’Ύ UI: Export conversation button clicked")
785
+ if not history:
786
+ print("⚠️ No conversation to export")
787
+ return None
788
+
789
+ try:
790
+ import tempfile
791
+ import datetime
792
+
793
+ # Create export content
794
+ timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
795
+ export_content = f"# GAIA Lite Conversation Export\n"
796
+ export_content += f"Export Date: {datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')}\n"
797
+ export_content += f"Total Messages: {len(history)}\n\n"
798
+ export_content += "=" * 50 + "\n\n"
799
+
800
+ for i, (user_msg, bot_msg) in enumerate(history, 1):
801
+ export_content += f"## Message {i}\n\n"
802
+ export_content += f"**User:** {user_msg}\n\n"
803
+ export_content += f"**Assistant:** {bot_msg}\n\n"
804
+ export_content += "-" * 30 + "\n\n"
805
+
806
+ # Save to temporary file
807
+ temp_file = tempfile.NamedTemporaryFile(
808
+ mode='w',
809
+ suffix=f'_gaia_lite_chat_export_{timestamp}.md',
810
+ delete=False,
811
+ encoding='utf-8'
812
+ )
813
+ temp_file.write(export_content)
814
+ temp_file.close()
815
+
816
+ print(f"πŸ“„ Conversation exported to: {temp_file.name}")
817
+ return temp_file.name
818
+
819
+ except Exception as e:
820
+ print(f"❌ Error exporting conversation: {e}")
821
+ return None
822
+
823
+ # Connect the events
824
+ submit_btn.click(
825
+ fn=submit_question,
826
+ inputs=[question_input, chatbot_interface, file_upload],
827
+ outputs=[question_input, chatbot_interface, file_upload],
828
+ show_progress=True
829
  )
830
+
831
+ question_input.submit(
832
+ fn=submit_question,
833
+ inputs=[question_input, chatbot_interface, file_upload],
834
+ outputs=[question_input, chatbot_interface, file_upload],
835
+ show_progress=True
836
+ )
837
+
838
+ clear_btn.click(
839
+ fn=clear_conversation,
840
+ outputs=[chatbot_interface],
841
+ show_progress=False
842
+ )
843
+
844
+ clear_files_btn.click(
845
+ fn=clear_files,
846
+ outputs=[file_upload],
847
+ show_progress=False
848
+ )
849
+
850
+ export_btn.click(
851
+ fn=export_conversation,
852
+ inputs=[chatbot_interface],
853
+ outputs=[download_file],
854
+ show_progress=True
855
+ )
856
+
857
+ return demo
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
858
 
859
  if __name__ == "__main__":
860
+ print("\n" + "-"*50)
861
+ print("πŸš€ Starting GAIA Lite...")
862
+ print("-"*50 + "\n")
863
+
864
+ # Create and launch the interface
865
+ demo = create_qna_interface()
866
+ demo.launch(
867
+ debug=True,
868
+ share=False,
869
+ server_name="0.0.0.0",
870
+ server_port=7860,
871
+ show_error=True
872
+ )
 
 
 
 
 
 
 
 
 
code_interpreter.py ADDED
@@ -0,0 +1,281 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import io
3
+ import sys
4
+ import uuid
5
+ import base64
6
+ import traceback
7
+ import contextlib
8
+ import tempfile
9
+ import subprocess
10
+ import sqlite3
11
+ from typing import Dict, List, Any, Optional, Union
12
+ import numpy as np
13
+ import pandas as pd
14
+ import matplotlib.pyplot as plt
15
+ from PIL import Image
16
+
17
+ class CodeInterpreter:
18
+ def __init__(self, allowed_modules=None, max_execution_time=30, working_directory=None):
19
+ """Initialize the code interpreter with safety measures."""
20
+ self.allowed_modules = allowed_modules or [
21
+ "numpy", "pandas", "matplotlib", "scipy", "sklearn",
22
+ "math", "random", "statistics", "datetime", "collections",
23
+ "itertools", "functools", "operator", "re", "json",
24
+ "sympy", "networkx", "nltk", "PIL", "pytesseract",
25
+ "cmath", "uuid", "tempfile", "requests", "urllib"
26
+ ]
27
+ self.max_execution_time = max_execution_time
28
+ self.working_directory = working_directory or os.path.join(os.getcwd())
29
+ if not os.path.exists(self.working_directory):
30
+ os.makedirs(self.working_directory)
31
+
32
+ self.globals = {
33
+ "__builtins__": __builtins__,
34
+ "np": np,
35
+ "pd": pd,
36
+ "plt": plt,
37
+ "Image": Image,
38
+ }
39
+ self.temp_sqlite_db = os.path.join(tempfile.gettempdir(), "code_exec.db")
40
+
41
+ def execute_code(self, code: str, language: str = "python") -> Dict[str, Any]:
42
+ """Execute the provided code in the selected programming language."""
43
+ language = language.lower()
44
+ execution_id = str(uuid.uuid4())
45
+
46
+ result = {
47
+ "execution_id": execution_id,
48
+ "status": "error",
49
+ "stdout": "",
50
+ "stderr": "",
51
+ "result": None,
52
+ "plots": [],
53
+ "dataframes": []
54
+ }
55
+
56
+ try:
57
+ if language == "python":
58
+ return self._execute_python(code, execution_id)
59
+ elif language == "bash":
60
+ return self._execute_bash(code, execution_id)
61
+ elif language == "sql":
62
+ return self._execute_sql(code, execution_id)
63
+ elif language == "c":
64
+ return self._execute_c(code, execution_id)
65
+ elif language == "java":
66
+ return self._execute_java(code, execution_id)
67
+ else:
68
+ result["stderr"] = f"Unsupported language: {language}"
69
+ except Exception as e:
70
+ result["stderr"] = str(e)
71
+
72
+ return result
73
+
74
+ def _execute_python(self, code: str, execution_id: str) -> dict:
75
+ output_buffer = io.StringIO()
76
+ error_buffer = io.StringIO()
77
+ result = {
78
+ "execution_id": execution_id,
79
+ "status": "error",
80
+ "stdout": "",
81
+ "stderr": "",
82
+ "result": None,
83
+ "plots": [],
84
+ "dataframes": []
85
+ }
86
+
87
+ try:
88
+ exec_dir = os.path.join(self.working_directory, execution_id)
89
+ os.makedirs(exec_dir, exist_ok=True)
90
+ plt.switch_backend('Agg')
91
+
92
+ with contextlib.redirect_stdout(output_buffer), contextlib.redirect_stderr(error_buffer):
93
+ exec_result = exec(code, self.globals)
94
+
95
+ if plt.get_fignums():
96
+ for i, fig_num in enumerate(plt.get_fignums()):
97
+ fig = plt.figure(fig_num)
98
+ img_path = os.path.join(exec_dir, f"plot_{i}.png")
99
+ fig.savefig(img_path)
100
+ with open(img_path, "rb") as img_file:
101
+ img_data = base64.b64encode(img_file.read()).decode('utf-8')
102
+ result["plots"].append({
103
+ "figure_number": fig_num,
104
+ "data": img_data
105
+ })
106
+
107
+ for var_name, var_value in self.globals.items():
108
+ if isinstance(var_value, pd.DataFrame) and len(var_value) > 0:
109
+ result["dataframes"].append({
110
+ "name": var_name,
111
+ "head": var_value.head().to_dict(),
112
+ "shape": var_value.shape,
113
+ "dtypes": str(var_value.dtypes)
114
+ })
115
+
116
+ result["status"] = "success"
117
+ result["stdout"] = output_buffer.getvalue()
118
+ result["result"] = exec_result
119
+
120
+ except Exception as e:
121
+ result["status"] = "error"
122
+ result["stderr"] = f"{error_buffer.getvalue()}\n{traceback.format_exc()}"
123
+
124
+ return result
125
+
126
+ def _execute_bash(self, code: str, execution_id: str) -> dict:
127
+ try:
128
+ completed = subprocess.run(
129
+ code, shell=True, capture_output=True, text=True, timeout=self.max_execution_time
130
+ )
131
+ return {
132
+ "execution_id": execution_id,
133
+ "status": "success" if completed.returncode == 0 else "error",
134
+ "stdout": completed.stdout,
135
+ "stderr": completed.stderr,
136
+ "result": None,
137
+ "plots": [],
138
+ "dataframes": []
139
+ }
140
+ except subprocess.TimeoutExpired:
141
+ return {
142
+ "execution_id": execution_id,
143
+ "status": "error",
144
+ "stdout": "",
145
+ "stderr": "Execution timed out.",
146
+ "result": None,
147
+ "plots": [],
148
+ "dataframes": []
149
+ }
150
+
151
+ def _execute_sql(self, code: str, execution_id: str) -> dict:
152
+ result = {
153
+ "execution_id": execution_id,
154
+ "status": "error",
155
+ "stdout": "",
156
+ "stderr": "",
157
+ "result": None,
158
+ "plots": [],
159
+ "dataframes": []
160
+ }
161
+ try:
162
+ conn = sqlite3.connect(self.temp_sqlite_db)
163
+ cur = conn.cursor()
164
+ cur.execute(code)
165
+ if code.strip().lower().startswith("select"):
166
+ columns = [description[0] for description in cur.description]
167
+ rows = cur.fetchall()
168
+ df = pd.DataFrame(rows, columns=columns)
169
+ result["dataframes"].append({
170
+ "name": "query_result",
171
+ "head": df.head().to_dict(),
172
+ "shape": df.shape,
173
+ "dtypes": str(df.dtypes)
174
+ })
175
+ else:
176
+ conn.commit()
177
+
178
+ result["status"] = "success"
179
+ result["stdout"] = "Query executed successfully."
180
+
181
+ except Exception as e:
182
+ result["stderr"] = str(e)
183
+ finally:
184
+ conn.close()
185
+
186
+ return result
187
+
188
+ def _execute_c(self, code: str, execution_id: str) -> dict:
189
+ temp_dir = tempfile.mkdtemp()
190
+ source_path = os.path.join(temp_dir, "program.c")
191
+ binary_path = os.path.join(temp_dir, "program")
192
+
193
+ try:
194
+ with open(source_path, "w") as f:
195
+ f.write(code)
196
+
197
+ compile_proc = subprocess.run(
198
+ ["gcc", source_path, "-o", binary_path],
199
+ capture_output=True, text=True, timeout=self.max_execution_time
200
+ )
201
+ if compile_proc.returncode != 0:
202
+ return {
203
+ "execution_id": execution_id,
204
+ "status": "error",
205
+ "stdout": compile_proc.stdout,
206
+ "stderr": compile_proc.stderr,
207
+ "result": None,
208
+ "plots": [],
209
+ "dataframes": []
210
+ }
211
+
212
+ run_proc = subprocess.run(
213
+ [binary_path],
214
+ capture_output=True, text=True, timeout=self.max_execution_time
215
+ )
216
+ return {
217
+ "execution_id": execution_id,
218
+ "status": "success" if run_proc.returncode == 0 else "error",
219
+ "stdout": run_proc.stdout,
220
+ "stderr": run_proc.stderr,
221
+ "result": None,
222
+ "plots": [],
223
+ "dataframes": []
224
+ }
225
+ except Exception as e:
226
+ return {
227
+ "execution_id": execution_id,
228
+ "status": "error",
229
+ "stdout": "",
230
+ "stderr": str(e),
231
+ "result": None,
232
+ "plots": [],
233
+ "dataframes": []
234
+ }
235
+
236
+ def _execute_java(self, code: str, execution_id: str) -> dict:
237
+ temp_dir = tempfile.mkdtemp()
238
+ source_path = os.path.join(temp_dir, "Main.java")
239
+
240
+ try:
241
+ with open(source_path, "w") as f:
242
+ f.write(code)
243
+
244
+ compile_proc = subprocess.run(
245
+ ["javac", source_path],
246
+ capture_output=True, text=True, timeout=self.max_execution_time
247
+ )
248
+ if compile_proc.returncode != 0:
249
+ return {
250
+ "execution_id": execution_id,
251
+ "status": "error",
252
+ "stdout": compile_proc.stdout,
253
+ "stderr": compile_proc.stderr,
254
+ "result": None,
255
+ "plots": [],
256
+ "dataframes": []
257
+ }
258
+
259
+ run_proc = subprocess.run(
260
+ ["java", "-cp", temp_dir, "Main"],
261
+ capture_output=True, text=True, timeout=self.max_execution_time
262
+ )
263
+ return {
264
+ "execution_id": execution_id,
265
+ "status": "success" if run_proc.returncode == 0 else "error",
266
+ "stdout": run_proc.stdout,
267
+ "stderr": run_proc.stderr,
268
+ "result": None,
269
+ "plots": [],
270
+ "dataframes": []
271
+ }
272
+ except Exception as e:
273
+ return {
274
+ "execution_id": execution_id,
275
+ "status": "error",
276
+ "stdout": "",
277
+ "stderr": str(e),
278
+ "result": None,
279
+ "plots": [],
280
+ "dataframes": []
281
+ }
evaluation_app.py ADDED
@@ -0,0 +1,218 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """GAIA Lite evaluation runner with answer cache, attachment download, and split submit."""
2
+ import json
3
+ import os
4
+ import tempfile
5
+ import time
6
+
7
+ import gradio as gr
8
+ import pandas as pd
9
+ import requests
10
+
11
+ from agent import run_agent, build_graph
12
+ from files_util import DEFAULT_API_URL, download_scoring_file
13
+
14
+ CACHE_PATH = os.getenv("GAIA_LITE_ANSWERS_CACHE", os.path.join(os.path.dirname(__file__), "answers_cache.json"))
15
+ MAX_RETRIES = 3
16
+
17
+
18
+ def load_cache() -> dict:
19
+ if not os.path.exists(CACHE_PATH):
20
+ return {}
21
+ try:
22
+ with open(CACHE_PATH, "r", encoding="utf-8") as handle:
23
+ return json.load(handle)
24
+ except json.JSONDecodeError:
25
+ return {}
26
+
27
+
28
+ def save_cache(cache: dict) -> None:
29
+ with open(CACHE_PATH, "w", encoding="utf-8") as handle:
30
+ json.dump(cache, handle, ensure_ascii=False, indent=2)
31
+
32
+
33
+ class BasicAgent:
34
+ def __init__(self):
35
+ print("BasicAgent initialized.")
36
+ self.graph = build_graph(use_retriever=False)
37
+
38
+ def __call__(self, question: str, file_paths=None) -> str:
39
+ print(f"Agent received question (first 80 chars): {question[:80]}...")
40
+ result = run_agent(question, file_paths=file_paths, graph=self.graph)
41
+ print(f"Extracted FINAL ANSWER: {result['final']}")
42
+ return result["final"]
43
+
44
+
45
+ def fetch_questions(api_url: str) -> list:
46
+ response = requests.get(f"{api_url}/questions", timeout=30)
47
+ response.raise_for_status()
48
+ data = response.json()
49
+ if not data:
50
+ raise ValueError("Fetched questions list is empty.")
51
+ return data
52
+
53
+
54
+ def run_one_question(agent: BasicAgent, item: dict, work_dir: str, api_url: str) -> str:
55
+ question_text = item.get("question") or ""
56
+ task_id = item["task_id"]
57
+ file_name = item.get("file_name") or ""
58
+ file_paths = []
59
+ if file_name:
60
+ saved = download_scoring_file(task_id, file_name, dest_dir=work_dir, api_url=api_url)
61
+ if saved:
62
+ file_paths.append(saved)
63
+ else:
64
+ question_text += (
65
+ f"\n\nNote: an attachment named {file_name} was expected but could not be downloaded."
66
+ )
67
+ return agent(question_text, file_paths=file_paths)
68
+
69
+
70
+ def invoke_with_retry(agent: BasicAgent, item: dict, work_dir: str, api_url: str) -> str:
71
+ delay = 5
72
+ last_error = None
73
+ for attempt in range(1, MAX_RETRIES + 1):
74
+ try:
75
+ return run_one_question(agent, item, work_dir, api_url)
76
+ except Exception as exc:
77
+ last_error = exc
78
+ print(f"Attempt {attempt}/{MAX_RETRIES} failed for {item.get('task_id')}: {exc}")
79
+ if attempt < MAX_RETRIES:
80
+ time.sleep(delay)
81
+ delay = min(delay * 2, 60)
82
+ return f"AGENT ERROR: {last_error}"
83
+
84
+
85
+ def results_dataframe(questions_data: list, cache: dict) -> pd.DataFrame:
86
+ rows = []
87
+ for item in questions_data:
88
+ task_id = item.get("task_id")
89
+ cached = cache.get(task_id, {})
90
+ rows.append(
91
+ {
92
+ "Task ID": task_id,
93
+ "Question": item.get("question"),
94
+ "File": item.get("file_name") or "",
95
+ "Submitted Answer": cached.get("submitted_answer", ""),
96
+ }
97
+ )
98
+ return pd.DataFrame(rows)
99
+
100
+
101
+ def run_evaluation(profile: gr.OAuthProfile | None):
102
+ if not profile:
103
+ return "Please log in to Hugging Face first.", None
104
+
105
+ api_url = DEFAULT_API_URL
106
+ try:
107
+ questions_data = fetch_questions(api_url)
108
+ except Exception as exc:
109
+ return f"Error fetching questions: {exc}", None
110
+
111
+ try:
112
+ agent = BasicAgent()
113
+ except Exception as exc:
114
+ return f"Error initializing agent: {exc}", None
115
+
116
+ cache = load_cache()
117
+ work_dir = tempfile.mkdtemp(prefix="gaia_lite_files_")
118
+ print(f"Running agent on {len(questions_data)} questions; cache={CACHE_PATH}")
119
+
120
+ for item in questions_data:
121
+ task_id = item.get("task_id")
122
+ if not task_id or item.get("question") is None:
123
+ continue
124
+ if task_id in cache and not str(cache[task_id].get("submitted_answer", "")).startswith("AGENT ERROR"):
125
+ print(f"Skip cached {task_id}")
126
+ continue
127
+ answer = invoke_with_retry(agent, item, work_dir, api_url)
128
+ cache[task_id] = {
129
+ "question": item.get("question"),
130
+ "file_name": item.get("file_name") or "",
131
+ "submitted_answer": answer,
132
+ }
133
+ save_cache(cache)
134
+
135
+ df = results_dataframe(questions_data, cache)
136
+ done = sum(1 for item in questions_data if item.get("task_id") in cache)
137
+ return f"Evaluation finished. Cached {done}/{len(questions_data)} answers at {CACHE_PATH}", df
138
+
139
+
140
+ def submit_cached(profile: gr.OAuthProfile | None):
141
+ if not profile:
142
+ return "Please log in to Hugging Face first.", None
143
+
144
+ username = profile.username
145
+ space_id = os.getenv("SPACE_ID")
146
+ agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
147
+ api_url = DEFAULT_API_URL
148
+
149
+ try:
150
+ questions_data = fetch_questions(api_url)
151
+ except Exception as exc:
152
+ return f"Error fetching questions: {exc}", None
153
+
154
+ cache = load_cache()
155
+ answers_payload = []
156
+ for item in questions_data:
157
+ task_id = item.get("task_id")
158
+ cached = cache.get(task_id)
159
+ if not cached:
160
+ continue
161
+ answer = cached.get("submitted_answer", "")
162
+ if str(answer).startswith("AGENT ERROR"):
163
+ continue
164
+ answers_payload.append({"task_id": task_id, "submitted_answer": answer})
165
+
166
+ if not answers_payload:
167
+ return "No cached answers to submit. Run evaluation first.", results_dataframe(questions_data, cache)
168
+
169
+ submission_data = {
170
+ "username": username.strip(),
171
+ "agent_code": agent_code,
172
+ "answers": answers_payload,
173
+ }
174
+ try:
175
+ response = requests.post(f"{api_url}/submit", json=submission_data, timeout=60)
176
+ response.raise_for_status()
177
+ result_data = response.json()
178
+ status = (
179
+ f"Submission successful.\n"
180
+ f"User: {result_data.get('username')}\n"
181
+ f"Overall Score: {result_data.get('score', 'N/A')}% "
182
+ f"({result_data.get('correct_count', '?')}/{result_data.get('total_attempted', '?')} correct)\n"
183
+ f"Submitted {len(answers_payload)} answers.\n"
184
+ f"Message: {result_data.get('message', '')}"
185
+ )
186
+ return status, results_dataframe(questions_data, cache)
187
+ except requests.HTTPError as exc:
188
+ detail = exc.response.text[:500] if exc.response is not None else str(exc)
189
+ return f"Submission failed: {detail}", results_dataframe(questions_data, cache)
190
+ except Exception as exc:
191
+ return f"Submission failed: {exc}", results_dataframe(questions_data, cache)
192
+
193
+
194
+ with gr.Blocks() as demo:
195
+ gr.Markdown(
196
+ """
197
+ # GAIA Lite Evaluation
198
+
199
+ 1. Log in with Hugging Face.
200
+ 2. **Run evaluation** answers every question (skips successful cache entries, writes `answers_cache.json`).
201
+ 3. **Submit cached answers** posts the cache to the scoring API.
202
+
203
+ Attachments are downloaded from `/files/{task_id}` when `file_name` is present.
204
+ """
205
+ )
206
+ gr.LoginButton()
207
+ with gr.Row():
208
+ run_button = gr.Button("Run evaluation (no submit)", variant="primary")
209
+ submit_button = gr.Button("Submit cached answers")
210
+ status_output = gr.Textbox(label="Status", lines=6, interactive=False)
211
+ results_table = gr.DataFrame(label="Questions and answers", wrap=True)
212
+ run_button.click(fn=run_evaluation, outputs=[status_output, results_table])
213
+ submit_button.click(fn=submit_cached, outputs=[status_output, results_table])
214
+
215
+
216
+ if __name__ == "__main__":
217
+ print("Launching GAIA Lite evaluation UI...")
218
+ demo.launch(debug=True, share=False)
explore_metadata.ipynb ADDED
@@ -0,0 +1,332 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "code",
5
+ "execution_count": 9,
6
+ "id": "a600d7fc",
7
+ "metadata": {},
8
+ "outputs": [],
9
+ "source": [
10
+ "import json \n",
11
+ "with open('metadata.jsonl', 'r') as f: \n",
12
+ " json_list = list(f)\n",
13
+ "\n",
14
+ "json_QA = []\n",
15
+ "for json_str in json_list: \n",
16
+ " json_data = json.loads(json_str)\n",
17
+ " json_QA.append(json_data)"
18
+ ]
19
+ },
20
+ {
21
+ "cell_type": "code",
22
+ "execution_count": 10,
23
+ "id": "fa5d8eb8",
24
+ "metadata": {},
25
+ "outputs": [
26
+ {
27
+ "name": "stdout",
28
+ "output_type": "stream",
29
+ "text": [
30
+ "==================================================\n",
31
+ "Task ID: d1af70ea-a9a4-421a-b9cc-94b5e02f1788\n",
32
+ "Question: As of the 2020 census, what was the population difference between the largest county seat and smallest county seat, by land area of the county seat, in Washington state? For population figures, please use the official data from data.census.gov. Please report the integer difference.\n",
33
+ "Level: 2\n",
34
+ "Final Answer: 736455\n",
35
+ "Annotator Metadata: \n",
36
+ " β”œβ”€β”€ Steps: \n",
37
+ " β”‚ β”œβ”€β”€ Step 1: Using a web browser, access a search engine and conduct a search, \"Washington cities by area\"\n",
38
+ " β”‚ β”œβ”€β”€ Step 2: Navigate to the second search result, https://en.wikipedia.org/wiki/List_of_municipalities_in_Washington\n",
39
+ " β”‚ β”œβ”€β”€ Step 3: Evaluate the page contents, finding the largest and smallest county seats by land area, Seattle and Cathlamet\n",
40
+ " β”‚ β”œβ”€β”€ Step 4: Using a web browser, navigate to https://data.census.gov/\n",
41
+ " β”‚ β”œβ”€β”€ Step 5: Using the website's search area, conduct a search, Seattle, Washington\n",
42
+ " β”‚ β”œβ”€β”€ Step 6: Record the reported 2020 Decennial Census population of Seattle, Washington, 737,015\n",
43
+ " β”‚ β”œβ”€β”€ Step 7: Using the website's search area, conduct a search, Cathlamet, Washington\n",
44
+ " β”‚ β”œβ”€β”€ Step 8: Record the reported 2020 Decennial Census population of Cathlamet, Washington, 560\n",
45
+ " β”‚ β”œβ”€β”€ Step 9: Using a calculator, find the difference in populations,\n",
46
+ " β”‚ β”œβ”€β”€ \n",
47
+ " β”‚ β”œβ”€β”€ 737,015 - 560\n",
48
+ " β”‚ β”œβ”€β”€ 736,455\n",
49
+ " β”‚ β”œβ”€β”€ Step 10: Report the correct answer to my user in the requested format, \"736,455\"\n",
50
+ " β”œβ”€β”€ Number of steps: 10\n",
51
+ " β”œβ”€β”€ How long did this take?: 5 minutes\n",
52
+ " β”œβ”€β”€ Tools:\n",
53
+ " β”‚ β”œβ”€β”€ 1. A web browser\n",
54
+ " β”‚ β”œβ”€β”€ 2. A search engine\n",
55
+ " β”‚ β”œβ”€β”€ 3. A calculator\n",
56
+ " └── Number of tools: 3\n",
57
+ "==================================================\n"
58
+ ]
59
+ }
60
+ ],
61
+ "source": [
62
+ "import random\n",
63
+ "random_samples = random.sample(json_QA, 1)\n",
64
+ "for sample in random_samples:\n",
65
+ " print(\"=\" * 50)\n",
66
+ " print(f\"Task ID: {sample['task_id']}\")\n",
67
+ " print(f\"Question: {sample['Question']}\")\n",
68
+ " print(f\"Level: {sample['Level']}\")\n",
69
+ " print(f\"Final Answer: {sample['Final answer']}\")\n",
70
+ " print(f\"Annotator Metadata: \")\n",
71
+ " print(f\" β”œβ”€β”€ Steps: \")\n",
72
+ " for step in sample['Annotator Metadata']['Steps'].split('\\n'):\n",
73
+ " print(f\" β”‚ β”œβ”€β”€ {step}\")\n",
74
+ " print(f\" β”œβ”€β”€ Number of steps: {sample['Annotator Metadata']['Number of steps']}\")\n",
75
+ " print(f\" β”œβ”€β”€ How long did this take?: {sample['Annotator Metadata']['How long did this take?']}\")\n",
76
+ " print(f\" β”œβ”€β”€ Tools:\")\n",
77
+ " for tool in sample['Annotator Metadata']['Tools'].split('\\n'):\n",
78
+ " print(f\" β”‚ β”œβ”€β”€ {tool}\")\n",
79
+ " print(f\" └── Number of tools: {sample['Annotator Metadata']['Number of tools']}\")\n",
80
+ "print(\"=\" * 50)"
81
+ ]
82
+ },
83
+ {
84
+ "cell_type": "code",
85
+ "execution_count": 11,
86
+ "id": "05076516",
87
+ "metadata": {},
88
+ "outputs": [],
89
+ "source": [
90
+ "import os\n",
91
+ "from dotenv import load_dotenv\n",
92
+ "from langchain_huggingface import HuggingFaceEmbeddings\n",
93
+ "from langchain_community.vectorstores import SupabaseVectorStore\n",
94
+ "from supabase.client import Client, create_client\n",
95
+ "\n",
96
+ "\n",
97
+ "load_dotenv()\n",
98
+ "embeddings = HuggingFaceEmbeddings(model_name=\"sentence-transformers/all-mpnet-base-v2\") # dim=768\n",
99
+ "\n",
100
+ "supabase_url = os.environ.get(\"SUPABASE_URL\")\n",
101
+ "supabase_key = os.environ.get(\"SUPABASE_SERVICE_ROLE_KEY\")\n",
102
+ "supabase: Client = create_client(supabase_url, supabase_key)"
103
+ ]
104
+ },
105
+ {
106
+ "cell_type": "code",
107
+ "execution_count": 20,
108
+ "id": "aa1402e3",
109
+ "metadata": {},
110
+ "outputs": [],
111
+ "source": [
112
+ "from langchain.schema import Document\n",
113
+ "docs = []\n",
114
+ "cnt = 0 \n",
115
+ "for sample in json_QA:\n",
116
+ " content = f\"Question : {sample['Question']}\\n\\nFinal answer : {sample['Final answer']}\"\n",
117
+ " doc = {\n",
118
+ " \"id\" : cnt,\n",
119
+ " \"content\" : content,\n",
120
+ " \"metadata\" : {\n",
121
+ " \"source\" : sample['task_id']\n",
122
+ " },\n",
123
+ " \"embedding\" : embeddings.embed_query(content),\n",
124
+ " }\n",
125
+ " docs.append(doc)\n",
126
+ " cnt += 1\n",
127
+ "\n",
128
+ "# upload the documents to the vector database\n",
129
+ "try:\n",
130
+ " response = (\n",
131
+ " supabase.table(\"documents2\")\n",
132
+ " .insert(docs)\n",
133
+ " .execute()\n",
134
+ " )\n",
135
+ "except Exception as exception:\n",
136
+ " print(\"Error inserting data into Supabase:\", exception)\n",
137
+ "\n",
138
+ "# # Save the documents (a list of dict) into a csv file, and manually upload it to Supabase\n",
139
+ "# import pandas as pd\n",
140
+ "# df = pd.DataFrame(docs)\n",
141
+ "# df.to_csv('supabase_docs.csv',index=False)"
142
+ ]
143
+ },
144
+ {
145
+ "cell_type": "code",
146
+ "execution_count": 41,
147
+ "id": "9aa7eb5e",
148
+ "metadata": {},
149
+ "outputs": [],
150
+ "source": [
151
+ "# add items to vector database\n",
152
+ "vector_store = SupabaseVectorStore(\n",
153
+ " client=supabase,\n",
154
+ " embedding= embeddings,\n",
155
+ " table_name=\"documents2\",\n",
156
+ " query_name=\"match_documents_2\",\n",
157
+ ")\n",
158
+ "retriever = vector_store.as_retriever()"
159
+ ]
160
+ },
161
+ {
162
+ "cell_type": "code",
163
+ "execution_count": 42,
164
+ "id": "9eecafd1",
165
+ "metadata": {},
166
+ "outputs": [],
167
+ "source": [
168
+ "query = \"On June 6, 2023, an article by Carolyn Collins Petersen was published in Universe Today. This article mentions a team that produced a paper about their observations, linked at the bottom of the article. Find this paper. Under what NASA award number was the work performed by R. G. Arendt supported by?\"\n",
169
+ "# matched_docs = vector_store.similarity_search(query, k=2)\n",
170
+ "docs = retriever.invoke(query)"
171
+ ]
172
+ },
173
+ {
174
+ "cell_type": "code",
175
+ "execution_count": 43,
176
+ "id": "ff917840",
177
+ "metadata": {},
178
+ "outputs": [
179
+ {
180
+ "data": {
181
+ "text/plain": [
182
+ "Document(metadata={'source': '840bfca7-4f7b-481a-8794-c560c340185d'}, page_content='Question : On June 6, 2023, an article by Carolyn Collins Petersen was published in Universe Today. This article mentions a team that produced a paper about their observations, linked at the bottom of the article. Find this paper. Under what NASA award number was the work performed by R. G. Arendt supported by?\\n\\nFinal answer : 80GSFC21M0002')"
183
+ ]
184
+ },
185
+ "execution_count": 43,
186
+ "metadata": {},
187
+ "output_type": "execute_result"
188
+ }
189
+ ],
190
+ "source": [
191
+ "docs[0]"
192
+ ]
193
+ },
194
+ {
195
+ "cell_type": "code",
196
+ "execution_count": 44,
197
+ "id": "01c8f337",
198
+ "metadata": {},
199
+ "outputs": [
200
+ {
201
+ "name": "stdout",
202
+ "output_type": "stream",
203
+ "text": [
204
+ "List of tools used in all samples:\n",
205
+ "Total number of tools used: 83\n",
206
+ " β”œβ”€β”€ web browser: 107\n",
207
+ " β”œβ”€β”€ image recognition tools (to identify and parse a figure with three axes): 1\n",
208
+ " β”œβ”€β”€ search engine: 101\n",
209
+ " β”œβ”€β”€ calculator: 34\n",
210
+ " β”œβ”€β”€ unlambda compiler (optional): 1\n",
211
+ " β”œβ”€β”€ a web browser.: 2\n",
212
+ " β”œβ”€β”€ a search engine.: 2\n",
213
+ " β”œβ”€β”€ a calculator.: 1\n",
214
+ " β”œβ”€β”€ microsoft excel: 5\n",
215
+ " β”œβ”€β”€ google search: 1\n",
216
+ " β”œβ”€β”€ ne: 9\n",
217
+ " β”œβ”€β”€ pdf access: 7\n",
218
+ " β”œβ”€β”€ file handling: 2\n",
219
+ " β”œβ”€β”€ python: 3\n",
220
+ " β”œβ”€β”€ image recognition tools: 12\n",
221
+ " β”œβ”€β”€ jsonld file access: 1\n",
222
+ " β”œβ”€β”€ video parsing: 1\n",
223
+ " β”œβ”€β”€ python compiler: 1\n",
224
+ " β”œβ”€β”€ video recognition tools: 3\n",
225
+ " β”œβ”€β”€ pdf viewer: 7\n",
226
+ " β”œβ”€β”€ microsoft excel / google sheets: 3\n",
227
+ " β”œβ”€β”€ word document access: 1\n",
228
+ " β”œβ”€β”€ tool to extract text from images: 1\n",
229
+ " β”œβ”€β”€ a word reversal tool / script: 1\n",
230
+ " β”œβ”€β”€ counter: 1\n",
231
+ " β”œβ”€β”€ excel: 3\n",
232
+ " β”œβ”€β”€ image recognition: 5\n",
233
+ " β”œβ”€β”€ color recognition: 3\n",
234
+ " β”œβ”€β”€ excel file access: 3\n",
235
+ " β”œβ”€β”€ xml file access: 1\n",
236
+ " β”œβ”€β”€ access to the internet archive, web.archive.org: 1\n",
237
+ " β”œβ”€β”€ text processing/diff tool: 1\n",
238
+ " β”œβ”€β”€ gif parsing tools: 1\n",
239
+ " β”œβ”€β”€ a web browser: 7\n",
240
+ " β”œβ”€β”€ a search engine: 7\n",
241
+ " β”œβ”€β”€ a speech-to-text tool: 2\n",
242
+ " β”œβ”€β”€ code/data analysis tools: 1\n",
243
+ " β”œβ”€β”€ audio capability: 2\n",
244
+ " β”œβ”€β”€ pdf reader: 1\n",
245
+ " β”œβ”€β”€ markdown: 1\n",
246
+ " β”œβ”€β”€ a calculator: 5\n",
247
+ " β”œβ”€β”€ access to wikipedia: 3\n",
248
+ " β”œβ”€β”€ image recognition/ocr: 3\n",
249
+ " β”œβ”€β”€ google translate access: 1\n",
250
+ " β”œβ”€β”€ ocr: 4\n",
251
+ " β”œβ”€β”€ bass note data: 1\n",
252
+ " β”œβ”€β”€ text editor: 1\n",
253
+ " β”œβ”€β”€ xlsx file access: 1\n",
254
+ " β”œβ”€β”€ powerpoint viewer: 1\n",
255
+ " β”œβ”€β”€ csv file access: 1\n",
256
+ " β”œβ”€β”€ calculator (or use excel): 1\n",
257
+ " β”œβ”€β”€ computer algebra system: 1\n",
258
+ " β”œβ”€β”€ video processing software: 1\n",
259
+ " β”œβ”€β”€ audio processing software: 1\n",
260
+ " β”œβ”€β”€ computer vision: 1\n",
261
+ " β”œβ”€β”€ google maps: 1\n",
262
+ " β”œβ”€β”€ access to excel files: 1\n",
263
+ " β”œβ”€β”€ calculator (or ability to count): 1\n",
264
+ " β”œβ”€β”€ a file interface: 3\n",
265
+ " β”œβ”€β”€ a python ide: 1\n",
266
+ " β”œβ”€β”€ spreadsheet editor: 1\n",
267
+ " β”œβ”€β”€ tools required: 1\n",
268
+ " β”œβ”€β”€ b browser: 1\n",
269
+ " β”œβ”€β”€ image recognition and processing tools: 1\n",
270
+ " β”œβ”€β”€ computer vision or ocr: 1\n",
271
+ " β”œβ”€β”€ c++ compiler: 1\n",
272
+ " β”œβ”€β”€ access to google maps: 1\n",
273
+ " β”œβ”€β”€ youtube player: 1\n",
274
+ " β”œβ”€β”€ natural language processor: 1\n",
275
+ " β”œβ”€β”€ graph interaction tools: 1\n",
276
+ " β”œβ”€β”€ bablyonian cuniform -> arabic legend: 1\n",
277
+ " β”œβ”€β”€ access to youtube: 1\n",
278
+ " β”œβ”€β”€ image search tools: 1\n",
279
+ " β”œβ”€β”€ calculator or counting function: 1\n",
280
+ " β”œβ”€β”€ a speech-to-text audio processing tool: 1\n",
281
+ " β”œβ”€β”€ access to academic journal websites: 1\n",
282
+ " β”œβ”€β”€ pdf reader/extracter: 1\n",
283
+ " β”œβ”€β”€ rubik's cube model: 1\n",
284
+ " β”œβ”€β”€ wikipedia: 1\n",
285
+ " β”œβ”€β”€ video capability: 1\n",
286
+ " β”œβ”€β”€ image processing tools: 1\n",
287
+ " β”œβ”€β”€ age recognition software: 1\n",
288
+ " β”œβ”€β”€ youtube: 1\n"
289
+ ]
290
+ }
291
+ ],
292
+ "source": [
293
+ "# list of the tools used in all the samples\n",
294
+ "from collections import Counter, OrderedDict\n",
295
+ "\n",
296
+ "tools = []\n",
297
+ "for sample in json_QA:\n",
298
+ " for tool in sample['Annotator Metadata']['Tools'].split('\\n'):\n",
299
+ " tool = tool[2:].strip().lower()\n",
300
+ " if tool.startswith(\"(\"):\n",
301
+ " tool = tool[11:].strip()\n",
302
+ " tools.append(tool)\n",
303
+ "tools_counter = OrderedDict(Counter(tools))\n",
304
+ "print(\"List of tools used in all samples:\")\n",
305
+ "print(\"Total number of tools used:\", len(tools_counter))\n",
306
+ "for tool, count in tools_counter.items():\n",
307
+ " print(f\" β”œβ”€β”€ {tool}: {count}\")"
308
+ ]
309
+ }
310
+ ],
311
+ "metadata": {
312
+ "kernelspec": {
313
+ "display_name": "env",
314
+ "language": "python",
315
+ "name": "python3"
316
+ },
317
+ "language_info": {
318
+ "codemirror_mode": {
319
+ "name": "ipython",
320
+ "version": 3
321
+ },
322
+ "file_extension": ".py",
323
+ "mimetype": "text/x-python",
324
+ "name": "python",
325
+ "nbconvert_exporter": "python",
326
+ "pygments_lexer": "ipython3",
327
+ "version": "3.11.9"
328
+ }
329
+ },
330
+ "nbformat": 4,
331
+ "nbformat_minor": 5
332
+ }