app.py
Browse filesGAIA certification code
app.py
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import json
|
| 3 |
+
import os
|
| 4 |
+
from smolagents import CodeAgent, HfApiModel, DuckDuckGoSearchTool, VisitWebpageTool, Tool
|
| 5 |
+
from PIL import Image
|
| 6 |
+
from transformers import pipeline
|
| 7 |
+
|
| 8 |
+
#custom tool
|
| 9 |
+
|
| 10 |
+
class TranscriptionTool(Tool):
|
| 11 |
+
name = "transcriber"
|
| 12 |
+
description = "Converts audio files (.mp3, .wav) to text. Use this for the recipe and homework tasks."
|
| 13 |
+
inputs = {"audio_path": {"type": "string", "description": "Local path to the audio file."}}
|
| 14 |
+
output_type = "string"
|
| 15 |
+
|
| 16 |
+
def forward(self, audio_path: str) -> str:
|
| 17 |
+
pipe = pipeline("automatic-speech-recognition", model="openai/whisper-tiny")
|
| 18 |
+
return pipe(audio_path)["text"]
|
| 19 |
+
|
| 20 |
+
class VisionTool(Tool):
|
| 21 |
+
name = "image_analyzer"
|
| 22 |
+
description = "Analyzes images and chess positions. Use this for the chess and arXiv figure tasks."
|
| 23 |
+
inputs = {
|
| 24 |
+
"image_path": {"type": "string", "description": "Local path to the image."},
|
| 25 |
+
"question": {"type": "string", "description": "The specific question about the image."}
|
| 26 |
+
}
|
| 27 |
+
output_type = "string"
|
| 28 |
+
|
| 29 |
+
def forward(self, image_path: str, question: str) -> str:
|
| 30 |
+
model = pipeline("visual-question-answering", model="dandelin/vilt-b32-finetuned-vqa")
|
| 31 |
+
return str(model(image=image_path, question=question))
|
| 32 |
+
|
| 33 |
+
# Agent Initialization
|
| 34 |
+
|
| 35 |
+
hf_token = os.getenv("HF_TOKEN")
|
| 36 |
+
model = HfApiModel(model_id="Qwen/Qwen2.5-Coder-32B-Instruct",token=hf_token)
|
| 37 |
+
|
| 38 |
+
agent = CodeAgent(
|
| 39 |
+
tools=[DuckDuckGoSearchTool(), VisitWebpageTool(), TranscriptionTool(), VisionTool()],
|
| 40 |
+
model=model,
|
| 41 |
+
add_base_tools=True, # Essential for Excel (pandas) and Python file execution
|
| 42 |
+
max_steps=15
|
| 43 |
+
)
|
| 44 |
+
|
| 45 |
+
# The Processing Logic
|
| 46 |
+
|
| 47 |
+
def run_gaia_tasks(json_input):
|
| 48 |
+
try:
|
| 49 |
+
tasks = json.loads(json_input)
|
| 50 |
+
except:
|
| 51 |
+
return "Invalid JSON input.", None
|
| 52 |
+
|
| 53 |
+
results = []
|
| 54 |
+
|
| 55 |
+
for task in tasks:
|
| 56 |
+
# Construct the prompt with strict formatting rules
|
| 57 |
+
prompt = f"""
|
| 58 |
+
TASK: {task['question']}
|
| 59 |
+
FILE: {task.get('file_name', 'None')}
|
| 60 |
+
|
| 61 |
+
INSTRUCTIONS:
|
| 62 |
+
1. If a file is mentioned, use Python to open/analyze it.
|
| 63 |
+
2. YOUR FINAL ANSWER MUST BE THE RAW VALUE ONLY.
|
| 64 |
+
3. No explanations, no units, no 'The answer is'.
|
| 65 |
+
Example: If the answer is 5, output '5'.
|
| 66 |
+
"""
|
| 67 |
+
|
| 68 |
+
try:
|
| 69 |
+
answer = agent.run(prompt)
|
| 70 |
+
results.append({"task_id": task["task_id"], "answer": str(answer).strip()})
|
| 71 |
+
except Exception as e:
|
| 72 |
+
results.append({"task_id": task["task_id"], "answer": f"Error: {str(e)}"})
|
| 73 |
+
|
| 74 |
+
# Save to a file for download
|
| 75 |
+
output_file = "submission.json"
|
| 76 |
+
with open(output_file, "w") as f:
|
| 77 |
+
json.dump(results, f, indent=4)
|
| 78 |
+
|
| 79 |
+
return json.dumps(results, indent=4), output_file
|
| 80 |
+
|
| 81 |
+
# Gradio Interface
|
| 82 |
+
|
| 83 |
+
with gr.Blocks(title="GAIA Certification Agent") as demo:
|
| 84 |
+
gr.Markdown("# 🤖 GAIA Agent Solver")
|
| 85 |
+
gr.Markdown("Paste your task JSON below. Ensure all referenced files (.mp3, .png, .xlsx) are uploaded to the Space.")
|
| 86 |
+
|
| 87 |
+
input_json = gr.Textbox(label="Input Tasks (JSON)", lines=10)
|
| 88 |
+
run_btn = gr.Button("Run Agent", variant="primary")
|
| 89 |
+
|
| 90 |
+
with gr.Row():
|
| 91 |
+
output_text = gr.Code(label="Agent Results", language="json")
|
| 92 |
+
output_file = gr.File(label="Download submission.json")
|
| 93 |
+
|
| 94 |
+
run_btn.click(fn=run_gaia_tasks, inputs=input_json, outputs=[output_text, output_file])
|
| 95 |
+
|
| 96 |
+
if __name__ == "__main__":
|
| 97 |
+
demo.launch()
|