Spaces:
Sleeping
Sleeping
Thomas Bartlett
commited on
Commit
·
ba7371b
1
Parent(s):
81917a3
GAIA Agent first commit
Browse files- app.py +67 -9
- requirements.txt +6 -1
app.py
CHANGED
|
@@ -1,23 +1,81 @@
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
import requests
|
| 4 |
-
import inspect
|
| 5 |
import pandas as pd
|
| 6 |
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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("
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
def __call__(self, question: str) -> str:
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
return fixed_answer
|
| 21 |
|
| 22 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
| 23 |
"""
|
|
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
import requests
|
|
|
|
| 4 |
import pandas as pd
|
| 5 |
|
| 6 |
+
from langchain_huggingface import ChatHuggingFace, HuggingFaceEndpoint
|
| 7 |
+
|
| 8 |
+
from langchain.tools import PythonREPLTool, RequestsGetTool
|
| 9 |
+
from langchain_community.tools import DuckDuckGoSearchRun
|
| 10 |
+
from langchain.agents import initialize_agent, AgentType
|
| 11 |
+
from langchain_core.prompts import ChatPromptTemplate
|
| 12 |
+
|
| 13 |
# --- Constants ---
|
| 14 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 15 |
+
SYSTEM_PROMPT = """
|
| 16 |
+
You are GAIA-Bot, a fully-autonomous problem-solver.
|
| 17 |
+
|
| 18 |
+
• For each question, THINK step-by-step before answering.
|
| 19 |
+
• When outside knowledge or calculations are needed, CALL an appropriate tool.
|
| 20 |
+
• Explain your reasoning only to yourself (in “Thought” messages); do NOT reveal it to the user.
|
| 21 |
+
• After finishing all reasoning, RESPOND with **only** the final answer string—no pre-amble, no labels, no extra text.
|
| 22 |
+
|
| 23 |
+
You have access to these tools:
|
| 24 |
+
- python_repl – run Python for maths, JSON/CSV/PDF parsing, etc.
|
| 25 |
+
- requests_get – fetch web pages or files via HTTP GET.
|
| 26 |
+
- duckduckgo_search – look up current facts on the public web.
|
| 27 |
+
|
| 28 |
+
If you require a file attached to the current task, download it with
|
| 29 |
+
`requests_get("https://agents-course-unit4-scoring.hf.space/files/{task_id}")`
|
| 30 |
+
and parse it inside Python.
|
| 31 |
+
|
| 32 |
+
Adhere strictly to these rules:
|
| 33 |
+
1. Use tools whenever they can reduce hallucination.
|
| 34 |
+
2. Never mention tool names or JSON in your final answer.
|
| 35 |
+
3. If you cannot find an answer, return “unknown”.
|
| 36 |
+
|
| 37 |
+
Begin.
|
| 38 |
+
"""
|
| 39 |
+
|
| 40 |
|
|
|
|
|
|
|
| 41 |
class BasicAgent:
|
| 42 |
def __init__(self):
|
| 43 |
+
print("LangChain-HF agent initialising…")
|
| 44 |
+
|
| 45 |
+
self.llm = ChatHuggingFace(
|
| 46 |
+
llm=HuggingFaceEndpoint(
|
| 47 |
+
repo_id="meta-llama/Meta-Llama-3-8B-Instruct",
|
| 48 |
+
task="text-generation",
|
| 49 |
+
max_new_tokens=512,
|
| 50 |
+
temperature=0.0,
|
| 51 |
+
do_sample=False,
|
| 52 |
+
)
|
| 53 |
+
)
|
| 54 |
+
|
| 55 |
+
tools = [
|
| 56 |
+
PythonREPLTool(),
|
| 57 |
+
RequestsGetTool(),
|
| 58 |
+
DuckDuckGoSearchRun(),
|
| 59 |
+
]
|
| 60 |
+
|
| 61 |
+
prompt = ChatPromptTemplate.from_messages([
|
| 62 |
+
("system", SYSTEM_PROMPT),
|
| 63 |
+
("user", "{input}")
|
| 64 |
+
])
|
| 65 |
+
|
| 66 |
+
self.agent = initialize_agent(
|
| 67 |
+
tools=tools,
|
| 68 |
+
llm=self.llm,
|
| 69 |
+
agent=AgentType.OPENAI_MULTI_FUNCTIONS,
|
| 70 |
+
prompt=prompt,
|
| 71 |
+
max_iterations=5,
|
| 72 |
+
verbose=False,
|
| 73 |
+
)
|
| 74 |
+
|
| 75 |
def __call__(self, question: str) -> str:
|
| 76 |
+
return self.agent.run(question)
|
| 77 |
+
|
| 78 |
+
|
|
|
|
| 79 |
|
| 80 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
| 81 |
"""
|
requirements.txt
CHANGED
|
@@ -1,2 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
gradio
|
| 2 |
-
|
|
|
|
|
|
| 1 |
+
langchain
|
| 2 |
+
langchain-huggingface
|
| 3 |
+
huggingface-hub
|
| 4 |
+
duckduckgo-search
|
| 5 |
gradio
|
| 6 |
+
pandas
|
| 7 |
+
requests
|