Spaces:
Sleeping
Sleeping
Ankit Anand commited on
Commit ·
8f6e3b4
1
Parent(s): e8d474f
Final Agent Code
Browse files
app.py
CHANGED
|
@@ -1,15 +1,10 @@
|
|
| 1 |
import os
|
| 2 |
import json
|
| 3 |
-
import requests
|
| 4 |
import pandas as pd
|
| 5 |
from typing import Optional, List, Dict
|
| 6 |
|
| 7 |
import gradio as gr
|
| 8 |
|
| 9 |
-
GAIA_BASE_URL = "https://huggingface.co/datasets/gaia-benchmark/GAIA/resolve/main"
|
| 10 |
-
GAIA_LEVELS = ["level1", "level2", "level3"]
|
| 11 |
-
GAIA_SPLITS = ["dev", "test"]
|
| 12 |
-
|
| 13 |
# Optional: Use transformers/torchvision for image analysis if available
|
| 14 |
try:
|
| 15 |
from PIL import Image
|
|
@@ -20,11 +15,13 @@ try:
|
|
| 20 |
except ImportError:
|
| 21 |
HAS_VISION = False
|
| 22 |
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
|
|
|
|
|
|
| 28 |
|
| 29 |
class GeneralAIAssistant:
|
| 30 |
def __init__(self):
|
|
@@ -35,49 +32,35 @@ class GeneralAIAssistant:
|
|
| 35 |
self.vision_processor = None
|
| 36 |
self.vision_model = None
|
| 37 |
|
| 38 |
-
def __call__(self, question: str,
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
"
|
| 46 |
-
"results": results,
|
| 47 |
-
"final_answer": results[-1] if results else "unknown"
|
| 48 |
-
}
|
| 49 |
-
return json.dumps(structured_response, indent=2)
|
| 50 |
-
|
| 51 |
-
def plan_steps(self, question, file_url):
|
| 52 |
-
steps = []
|
| 53 |
-
if file_url:
|
| 54 |
-
steps.append({"action": "analyze_file", "file_url": file_url})
|
| 55 |
-
steps.append({"action": "answer_question", "question": question})
|
| 56 |
-
return steps
|
| 57 |
-
|
| 58 |
-
def execute_step(self, step):
|
| 59 |
-
if step["action"] == "analyze_file":
|
| 60 |
-
return self.analyze_file(step["file_url"])
|
| 61 |
-
elif step["action"] == "answer_question":
|
| 62 |
-
return self.answer_question(step["question"])
|
| 63 |
-
return "unknown"
|
| 64 |
|
| 65 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
if not HAS_VISION:
|
| 67 |
return "Image analysis unavailable (missing dependencies)."
|
| 68 |
try:
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
except Exception as e:
|
| 76 |
return f"File analysis failed: {e}"
|
| 77 |
|
| 78 |
def answer_question(self, question):
|
| 79 |
q = question.lower()
|
| 80 |
-
# Example: simple logic for some questions
|
| 81 |
if "capital of france" in q:
|
| 82 |
return "Paris"
|
| 83 |
if "12 * 8" in q or "12 times 8" in q:
|
|
@@ -88,55 +71,53 @@ class GeneralAIAssistant:
|
|
| 88 |
return "100"
|
| 89 |
return "unknown"
|
| 90 |
|
| 91 |
-
def run_and_submit_all(username
|
| 92 |
if not username or not username.strip():
|
| 93 |
return "Please enter your username.", None
|
|
|
|
| 94 |
try:
|
| 95 |
-
questions =
|
| 96 |
except Exception as e:
|
| 97 |
-
return f"Failed to
|
| 98 |
|
| 99 |
agent = GeneralAIAssistant()
|
| 100 |
-
|
| 101 |
for item in questions:
|
| 102 |
q = item.get("question")
|
| 103 |
-
|
|
|
|
| 104 |
if "file_name" in item and item["file_name"]:
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
"
|
| 109 |
-
"
|
| 110 |
-
"
|
| 111 |
-
}
|
| 112 |
-
|
| 113 |
-
|
|
|
|
| 114 |
|
| 115 |
with gr.Blocks() as demo:
|
| 116 |
gr.Markdown("# GAIA Multimodal Agent Evaluation Runner")
|
| 117 |
gr.Markdown(
|
| 118 |
"""
|
| 119 |
**Instructions:**
|
| 120 |
-
1.
|
| 121 |
-
2.
|
| 122 |
-
3.
|
| 123 |
-
|
| 124 |
-
---
|
| 125 |
-
This agent is a template. Expand the agent logic for better performance and multimodal reasoning.
|
| 126 |
"""
|
| 127 |
)
|
| 128 |
|
| 129 |
username_input = gr.Textbox(label="Username", placeholder="Enter your username here")
|
| 130 |
-
level_dropdown = gr.Dropdown(choices=GAIA_LEVELS, label="GAIA Level", value="level1")
|
| 131 |
-
split_dropdown = gr.Dropdown(choices=GAIA_SPLITS, label="GAIA Split", value="dev")
|
| 132 |
run_button = gr.Button("Run Evaluation & Submit All Answers")
|
| 133 |
status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
|
| 134 |
-
|
| 135 |
|
| 136 |
run_button.click(
|
| 137 |
fn=run_and_submit_all,
|
| 138 |
-
inputs=[username_input
|
| 139 |
-
outputs=[status_output,
|
| 140 |
)
|
| 141 |
|
| 142 |
if __name__ == "__main__":
|
|
|
|
| 1 |
import os
|
| 2 |
import json
|
|
|
|
| 3 |
import pandas as pd
|
| 4 |
from typing import Optional, List, Dict
|
| 5 |
|
| 6 |
import gradio as gr
|
| 7 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
# Optional: Use transformers/torchvision for image analysis if available
|
| 9 |
try:
|
| 10 |
from PIL import Image
|
|
|
|
| 15 |
except ImportError:
|
| 16 |
HAS_VISION = False
|
| 17 |
|
| 18 |
+
# Path to local GAIA metadata
|
| 19 |
+
GAIA_METADATA_PATH = "GAIA/2023/validation/metadata.jsonl"
|
| 20 |
+
GAIA_FILES_BASE = "GAIA/2023/validation/"
|
| 21 |
+
|
| 22 |
+
def load_gaia_metadata() -> List[Dict]:
|
| 23 |
+
with open(GAIA_METADATA_PATH, "r", encoding="utf-8") as f:
|
| 24 |
+
return [json.loads(line) for line in f]
|
| 25 |
|
| 26 |
class GeneralAIAssistant:
|
| 27 |
def __init__(self):
|
|
|
|
| 32 |
self.vision_processor = None
|
| 33 |
self.vision_model = None
|
| 34 |
|
| 35 |
+
def __call__(self, question: str, file_path: Optional[str] = None) -> (str, str):
|
| 36 |
+
reasoning = []
|
| 37 |
+
answer = None
|
| 38 |
+
|
| 39 |
+
if file_path:
|
| 40 |
+
reasoning.append(f"File provided: {file_path}")
|
| 41 |
+
file_analysis = self.analyze_file(file_path)
|
| 42 |
+
reasoning.append(f"File analysis: {file_analysis}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
|
| 44 |
+
answer = self.answer_question(question)
|
| 45 |
+
reasoning.append(f"Answered: {answer}")
|
| 46 |
+
|
| 47 |
+
return answer, " | ".join(reasoning)
|
| 48 |
+
|
| 49 |
+
def analyze_file(self, file_path):
|
| 50 |
if not HAS_VISION:
|
| 51 |
return "Image analysis unavailable (missing dependencies)."
|
| 52 |
try:
|
| 53 |
+
with open(file_path, "rb") as f:
|
| 54 |
+
img = Image.open(f).convert("RGB")
|
| 55 |
+
inputs = self.vision_processor(images=img, return_tensors="pt")
|
| 56 |
+
out = self.vision_model.generate(**inputs)
|
| 57 |
+
caption = self.vision_processor.decode(out[0], skip_special_tokens=True)
|
| 58 |
+
return f"Image caption: {caption}"
|
| 59 |
except Exception as e:
|
| 60 |
return f"File analysis failed: {e}"
|
| 61 |
|
| 62 |
def answer_question(self, question):
|
| 63 |
q = question.lower()
|
|
|
|
| 64 |
if "capital of france" in q:
|
| 65 |
return "Paris"
|
| 66 |
if "12 * 8" in q or "12 times 8" in q:
|
|
|
|
| 71 |
return "100"
|
| 72 |
return "unknown"
|
| 73 |
|
| 74 |
+
def run_and_submit_all(username):
|
| 75 |
if not username or not username.strip():
|
| 76 |
return "Please enter your username.", None
|
| 77 |
+
|
| 78 |
try:
|
| 79 |
+
questions = load_gaia_metadata()
|
| 80 |
except Exception as e:
|
| 81 |
+
return f"Failed to load GAIA metadata: {e}", None
|
| 82 |
|
| 83 |
agent = GeneralAIAssistant()
|
| 84 |
+
output_lines = []
|
| 85 |
for item in questions:
|
| 86 |
q = item.get("question")
|
| 87 |
+
task_id = item.get("task_id")
|
| 88 |
+
file_path = None
|
| 89 |
if "file_name" in item and item["file_name"]:
|
| 90 |
+
file_path = os.path.join(GAIA_FILES_BASE, item["file_name"])
|
| 91 |
+
model_answer, reasoning_trace = agent(q, file_path)
|
| 92 |
+
output_obj = {
|
| 93 |
+
"task_id": task_id,
|
| 94 |
+
"model_answer": model_answer,
|
| 95 |
+
"reasoning_trace": reasoning_trace
|
| 96 |
+
}
|
| 97 |
+
output_lines.append(json.dumps(output_obj, ensure_ascii=False))
|
| 98 |
+
# For Gradio, show as a single text output
|
| 99 |
+
return f"Processed {len(output_lines)} questions for {username}.", "\n".join(output_lines)
|
| 100 |
|
| 101 |
with gr.Blocks() as demo:
|
| 102 |
gr.Markdown("# GAIA Multimodal Agent Evaluation Runner")
|
| 103 |
gr.Markdown(
|
| 104 |
"""
|
| 105 |
**Instructions:**
|
| 106 |
+
1. Enter your username.
|
| 107 |
+
2. Click 'Run Evaluation & Submit All Answers' to process all questions and see the results.
|
| 108 |
+
3. Output is in the required JSON-lines format.
|
|
|
|
|
|
|
|
|
|
| 109 |
"""
|
| 110 |
)
|
| 111 |
|
| 112 |
username_input = gr.Textbox(label="Username", placeholder="Enter your username here")
|
|
|
|
|
|
|
| 113 |
run_button = gr.Button("Run Evaluation & Submit All Answers")
|
| 114 |
status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
|
| 115 |
+
results_output = gr.Textbox(label="JSONL Output", lines=20, interactive=False)
|
| 116 |
|
| 117 |
run_button.click(
|
| 118 |
fn=run_and_submit_all,
|
| 119 |
+
inputs=[username_input],
|
| 120 |
+
outputs=[status_output, results_output]
|
| 121 |
)
|
| 122 |
|
| 123 |
if __name__ == "__main__":
|