Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,127 +1,59 @@
|
|
| 1 |
-
|
| 2 |
-
import gradio as gr
|
| 3 |
-
import requests
|
| 4 |
-
import pandas as pd
|
| 5 |
-
from dotenv import load_dotenv
|
| 6 |
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
from
|
| 10 |
-
from
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
-
|
| 13 |
-
|
|
|
|
| 14 |
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
# --- GAIA-compatible Agent Definition ---
|
| 18 |
class GaiaAgent:
|
| 19 |
def __init__(self):
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
self.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
-
# Define tools
|
| 25 |
self.tools = [
|
| 26 |
-
|
| 27 |
-
ArxivQueryRun(api_wrapper=ArxivAPIWrapper(top_k_results=1, doc_content_chars_max=250)),
|
| 28 |
-
WikipediaQueryRun(api_wrapper=WikipediaAPIWrapper(top_k_results=1, doc_content_chars_max=250))
|
| 29 |
]
|
| 30 |
|
| 31 |
-
self.agent =
|
| 32 |
-
|
| 33 |
-
self.llm,
|
| 34 |
-
agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION,
|
| 35 |
-
handle_parsing_errors=True
|
| 36 |
-
)
|
| 37 |
-
|
| 38 |
-
def __call__(self, question: str) -> str:
|
| 39 |
-
try:
|
| 40 |
-
return self.agent.run(question)
|
| 41 |
-
except Exception as e:
|
| 42 |
-
return f"[ERROR] {str(e)}"
|
| 43 |
-
|
| 44 |
-
# --- Evaluation Logic ---
|
| 45 |
-
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
| 46 |
-
space_id = os.getenv("SPACE_ID")
|
| 47 |
-
|
| 48 |
-
if profile:
|
| 49 |
-
username = f"{profile.username}"
|
| 50 |
-
print(f"User logged in: {username}")
|
| 51 |
-
else:
|
| 52 |
-
print("User not logged in.")
|
| 53 |
-
return "Please Login to Hugging Face with the button.", None
|
| 54 |
-
|
| 55 |
-
questions_url = f"{DEFAULT_API_URL}/questions"
|
| 56 |
-
submit_url = f"{DEFAULT_API_URL}/submit"
|
| 57 |
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
|
|
|
|
|
|
| 62 |
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
except Exception as e:
|
| 71 |
-
return f"Error fetching questions: {e}", None
|
| 72 |
-
|
| 73 |
-
results_log = []
|
| 74 |
-
answers_payload = []
|
| 75 |
-
for item in questions_data:
|
| 76 |
-
task_id = item.get("task_id")
|
| 77 |
-
question_text = item.get("question")
|
| 78 |
-
if not task_id or question_text is None:
|
| 79 |
-
continue
|
| 80 |
-
try:
|
| 81 |
-
submitted_answer = agent(question_text)
|
| 82 |
-
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
| 83 |
-
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
| 84 |
-
except Exception as e:
|
| 85 |
-
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"AGENT ERROR: {e}"})
|
| 86 |
-
|
| 87 |
-
if not answers_payload:
|
| 88 |
-
return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
|
| 89 |
-
|
| 90 |
-
submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload}
|
| 91 |
-
print(f"Submitting answers to: {submit_url}")
|
| 92 |
-
|
| 93 |
-
try:
|
| 94 |
-
response = requests.post(submit_url, json=submission_data, timeout=60)
|
| 95 |
-
response.raise_for_status()
|
| 96 |
-
result_data = response.json()
|
| 97 |
-
final_status = (
|
| 98 |
-
f"Submission Successful!\n"
|
| 99 |
-
f"User: {result_data.get('username')}\n"
|
| 100 |
-
f"Overall Score: {result_data.get('score', 'N/A')}% "
|
| 101 |
-
f"({result_data.get('correct_count', '?')}/{result_data.get('total_attempted', '?')} correct)\n"
|
| 102 |
-
f"Message: {result_data.get('message', 'No message received.')}"
|
| 103 |
-
)
|
| 104 |
-
return final_status, pd.DataFrame(results_log)
|
| 105 |
-
except Exception as e:
|
| 106 |
-
return f"Submission Failed: {e}", pd.DataFrame(results_log)
|
| 107 |
-
|
| 108 |
-
# --- Gradio UI ---
|
| 109 |
-
with gr.Blocks() as demo:
|
| 110 |
-
gr.Markdown("# Basic Agent Evaluation Runner")
|
| 111 |
-
gr.Markdown("""
|
| 112 |
-
**Instructions:**
|
| 113 |
-
1. Modify `GaiaAgent` to implement your logic.
|
| 114 |
-
2. Log in to your Hugging Face account below.
|
| 115 |
-
3. Click 'Run Evaluation & Submit All Answers'.
|
| 116 |
-
""")
|
| 117 |
-
|
| 118 |
-
gr.LoginButton()
|
| 119 |
-
run_button = gr.Button("Run Evaluation & Submit All Answers")
|
| 120 |
-
status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
|
| 121 |
-
results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
|
| 122 |
|
| 123 |
-
|
|
|
|
|
|
|
| 124 |
|
| 125 |
-
if __name__ == "__main__":
|
| 126 |
-
print("Launching GAIA-compatible agent...")
|
| 127 |
-
demo.launch(debug=True, share=False)
|
|
|
|
| 1 |
+
api_key = "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"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
+
import argparse
|
| 4 |
+
import streamlit as st
|
| 5 |
+
from langchain.agents import create_tool_calling_agent, AgentExecutor
|
| 6 |
+
from langchain_core.runnables import Runnable
|
| 7 |
+
from crewai_tools import ScrapeWebsiteTool
|
| 8 |
+
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
|
| 9 |
+
from langchain_community.chat_models import ChatLiteLLM
|
| 10 |
+
from litellm import completion
|
| 11 |
+
import importlib
|
| 12 |
+
|
| 13 |
+
# Define your custom LLM wrapper class
|
| 14 |
+
class CustomLLM(ChatLiteLLM):
|
| 15 |
+
def __init__(self):
|
| 16 |
+
super().__init__(model="gpt-4")
|
| 17 |
|
| 18 |
+
def _call(self, prompt: str, stop=None):
|
| 19 |
+
response = completion(model="gpt-4", messages=[{"role": "user", "content": prompt}])
|
| 20 |
+
return response.choices[0].message["content"]
|
| 21 |
|
| 22 |
+
# Define your agent class
|
|
|
|
|
|
|
| 23 |
class GaiaAgent:
|
| 24 |
def __init__(self):
|
| 25 |
+
self.llm = CustomLLM()
|
| 26 |
+
|
| 27 |
+
self.prompt = ChatPromptTemplate.from_messages([
|
| 28 |
+
("system", "You are a helpful assistant."),
|
| 29 |
+
MessagesPlaceholder(variable_name="chat_history"),
|
| 30 |
+
("human", "{input}"),
|
| 31 |
+
MessagesPlaceholder(variable_name="agent_scratchpad"),
|
| 32 |
+
])
|
| 33 |
|
|
|
|
| 34 |
self.tools = [
|
| 35 |
+
ScrapeWebsiteTool()
|
|
|
|
|
|
|
| 36 |
]
|
| 37 |
|
| 38 |
+
self.agent: Runnable = create_tool_calling_agent(self.llm, self.tools, self.prompt)
|
| 39 |
+
self.agent_executor: AgentExecutor = AgentExecutor(agent=self.agent, tools=self.tools, verbose=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
+
def run(self):
|
| 42 |
+
st.title("🧠 GAIA-compatible Agent")
|
| 43 |
+
user_input = st.text_input("Enter your query")
|
| 44 |
+
if user_input:
|
| 45 |
+
response = self.agent_executor.invoke({"input": user_input})
|
| 46 |
+
st.write("Response:", response)
|
| 47 |
|
| 48 |
+
# Main CLI-compatible entry point
|
| 49 |
+
if __name__ == "__main__":
|
| 50 |
+
parser = argparse.ArgumentParser()
|
| 51 |
+
parser.add_argument("--import", type=str, required=False, help="Module to import (ignored for static agent)")
|
| 52 |
+
parser.add_argument("--class", type=str, required=False, help="Class name to instantiate (ignored for static agent)")
|
| 53 |
+
parser.add_argument("--device", type=str, default="cpu", help="Device type (not used in this agent)")
|
| 54 |
+
args = parser.parse_args()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
|
| 56 |
+
# Directly instantiate and run the predefined GaiaAgent class
|
| 57 |
+
agent = GaiaAgent()
|
| 58 |
+
agent.run()
|
| 59 |
|
|
|
|
|
|
|
|
|