Update app.py
#527
by Shrutipanchal086 - opened
app.py
CHANGED
|
@@ -1,196 +1,625 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
import requests
|
| 4 |
-
import inspect
|
| 5 |
import pandas as pd
|
| 6 |
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
-
# --- Basic Agent Definition ---
|
| 12 |
-
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
| 13 |
class BasicAgent:
|
|
|
|
| 14 |
def __init__(self):
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
"""
|
| 24 |
-
|
| 25 |
-
|
|
|
|
|
|
|
| 26 |
"""
|
| 27 |
-
|
| 28 |
-
|
|
|
|
|
|
|
| 29 |
|
| 30 |
if profile:
|
| 31 |
-
|
|
|
|
|
|
|
| 32 |
print(f"User logged in: {username}")
|
|
|
|
| 33 |
else:
|
|
|
|
| 34 |
print("User not logged in.")
|
| 35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
api_url = DEFAULT_API_URL
|
|
|
|
| 38 |
questions_url = f"{api_url}/questions"
|
|
|
|
| 39 |
submit_url = f"{api_url}/submit"
|
| 40 |
|
| 41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
-
|
| 52 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
try:
|
| 54 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
response.raise_for_status()
|
|
|
|
| 56 |
questions_data = response.json()
|
|
|
|
| 57 |
if not questions_data:
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
results_log = []
|
|
|
|
| 74 |
answers_payload = []
|
| 75 |
-
|
| 76 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
task_id = item.get("task_id")
|
|
|
|
| 78 |
question_text = item.get("question")
|
|
|
|
| 79 |
if not task_id or question_text is None:
|
| 80 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
continue
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
try:
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
except Exception as e:
|
| 87 |
-
|
| 88 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
-
|
| 95 |
-
|
| 96 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 97 |
print(status_update)
|
| 98 |
|
| 99 |
-
|
| 100 |
-
|
|
|
|
|
|
|
|
|
|
| 101 |
try:
|
| 102 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 103 |
response.raise_for_status()
|
|
|
|
| 104 |
result_data = response.json()
|
|
|
|
|
|
|
| 105 |
final_status = (
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
f"
|
| 110 |
-
f"
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 115 |
except requests.exceptions.HTTPError as e:
|
| 116 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 117 |
try:
|
|
|
|
| 118 |
error_json = e.response.json()
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 126 |
except requests.exceptions.Timeout:
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
|
|
|
|
|
|
|
|
|
| 131 |
except requests.exceptions.RequestException as e:
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
|
|
|
|
|
|
|
|
|
| 136 |
except Exception as e:
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
|
|
|
| 141 |
|
| 142 |
|
| 143 |
-
#
|
|
|
|
|
|
|
|
|
|
| 144 |
with gr.Blocks() as demo:
|
| 145 |
-
|
| 146 |
gr.Markdown(
|
| 147 |
-
""
|
| 148 |
-
|
| 149 |
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
|
|
|
| 158 |
"""
|
| 159 |
)
|
| 160 |
|
| 161 |
gr.LoginButton()
|
| 162 |
|
| 163 |
-
run_button = gr.Button("Run Evaluation & Submit All Answers")
|
| 164 |
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 168 |
|
| 169 |
run_button.click(
|
| 170 |
fn=run_and_submit_all,
|
| 171 |
-
outputs=[
|
|
|
|
|
|
|
|
|
|
| 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 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 192 |
|
| 193 |
-
print("-"*(60 + len(" App Starting ")) + "\n")
|
| 194 |
|
| 195 |
-
|
| 196 |
-
|
|
|
|
|
|
|
|
|
| 1 |
+
# ============================================================
|
| 2 |
+
# INSTALL REQUIRED LIBRARIES
|
| 3 |
+
# ============================================================
|
| 4 |
+
|
| 5 |
+
import subprocess
|
| 6 |
+
import sys
|
| 7 |
+
|
| 8 |
+
subprocess.check_call([
|
| 9 |
+
sys.executable, "-m", "pip", "install", "-q",
|
| 10 |
+
"smolagents[toolkit]",
|
| 11 |
+
"gradio",
|
| 12 |
+
"requests",
|
| 13 |
+
"pandas"
|
| 14 |
+
])
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
# ============================================================
|
| 18 |
+
# IMPORT LIBRARIES
|
| 19 |
+
# ============================================================
|
| 20 |
+
|
| 21 |
import os
|
| 22 |
import gradio as gr
|
| 23 |
import requests
|
|
|
|
| 24 |
import pandas as pd
|
| 25 |
|
| 26 |
+
from smolagents import (
|
| 27 |
+
CodeAgent,
|
| 28 |
+
InferenceClientModel,
|
| 29 |
+
DuckDuckGoSearchTool,
|
| 30 |
+
PythonInterpreterTool
|
| 31 |
+
)
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
# ============================================================
|
| 35 |
+
# CONSTANTS
|
| 36 |
+
# ============================================================
|
| 37 |
+
|
| 38 |
+
DEFAULT_API_URL = "https://huggingface.co/Shrutipanchal086/structrural_AI_agent"
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
# ============================================================
|
| 42 |
+
# STRUCTURALGPT / GAIA AGENT
|
| 43 |
+
# ============================================================
|
| 44 |
|
|
|
|
|
|
|
| 45 |
class BasicAgent:
|
| 46 |
+
|
| 47 |
def __init__(self):
|
| 48 |
+
|
| 49 |
+
print("Initializing StructuralGPT GAIA Agent...")
|
| 50 |
+
|
| 51 |
+
# ----------------------------------------------------
|
| 52 |
+
# Hugging Face token
|
| 53 |
+
# Add HF_TOKEN in Space Settings -> Secrets
|
| 54 |
+
# ----------------------------------------------------
|
| 55 |
+
|
| 56 |
+
hf_token = os.getenv("hf_token")
|
| 57 |
+
|
| 58 |
+
if not hf_token:
|
| 59 |
+
raise ValueError(
|
| 60 |
+
"HF_TOKEN not found. "
|
| 61 |
+
"Please add HF_TOKEN in Space Settings -> Secrets."
|
| 62 |
+
)
|
| 63 |
+
|
| 64 |
+
# ----------------------------------------------------
|
| 65 |
+
# Hugging Face model
|
| 66 |
+
# ----------------------------------------------------
|
| 67 |
+
|
| 68 |
+
self.model = InferenceClientModel(
|
| 69 |
+
model_id="Qwen/Qwen3-Next-80B-A3B-Thinking",
|
| 70 |
+
token=hf_token,
|
| 71 |
+
max_tokens=3000
|
| 72 |
+
)
|
| 73 |
+
|
| 74 |
+
# ----------------------------------------------------
|
| 75 |
+
# Web search
|
| 76 |
+
# ----------------------------------------------------
|
| 77 |
+
|
| 78 |
+
self.search_tool = DuckDuckGoSearchTool(
|
| 79 |
+
max_results=8
|
| 80 |
+
)
|
| 81 |
+
|
| 82 |
+
# ----------------------------------------------------
|
| 83 |
+
# Python calculator / reasoning tool
|
| 84 |
+
# ----------------------------------------------------
|
| 85 |
+
|
| 86 |
+
self.python_tool = PythonInterpreterTool(
|
| 87 |
+
authorized_imports=[
|
| 88 |
+
"math",
|
| 89 |
+
"statistics",
|
| 90 |
+
"datetime",
|
| 91 |
+
"json",
|
| 92 |
+
"re"
|
| 93 |
+
]
|
| 94 |
+
)
|
| 95 |
+
|
| 96 |
+
# ----------------------------------------------------
|
| 97 |
+
# Agent
|
| 98 |
+
# ----------------------------------------------------
|
| 99 |
+
|
| 100 |
+
self.agent = CodeAgent(
|
| 101 |
+
model=self.model,
|
| 102 |
+
tools=[
|
| 103 |
+
self.search_tool,
|
| 104 |
+
self.python_tool
|
| 105 |
+
],
|
| 106 |
+
max_steps=10
|
| 107 |
+
)
|
| 108 |
+
|
| 109 |
+
print("StructuralGPT GAIA Agent initialized successfully.")
|
| 110 |
+
|
| 111 |
+
|
| 112 |
def __call__(self, question: str) -> str:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 113 |
|
| 114 |
+
print("\n" + "=" * 60)
|
| 115 |
+
print("QUESTION:")
|
| 116 |
+
print(question)
|
| 117 |
+
print("=" * 60)
|
| 118 |
+
|
| 119 |
+
prompt = f"""
|
| 120 |
+
You are a powerful general-purpose AI agent participating
|
| 121 |
+
in the GAIA benchmark.
|
| 122 |
+
|
| 123 |
+
Your job is to solve the user's question accurately.
|
| 124 |
+
|
| 125 |
+
IMPORTANT RULES:
|
| 126 |
+
|
| 127 |
+
1. Understand the question completely before answering.
|
| 128 |
+
|
| 129 |
+
2. If the question requires current, factual, or external
|
| 130 |
+
information, use the web search tool.
|
| 131 |
+
|
| 132 |
+
3. If calculations are required, use the Python tool.
|
| 133 |
+
Do not rely on mental arithmetic for complicated calculations.
|
| 134 |
+
|
| 135 |
+
4. Break difficult problems into smaller steps.
|
| 136 |
+
|
| 137 |
+
5. Verify important calculations and facts before producing
|
| 138 |
+
the final answer.
|
| 139 |
+
|
| 140 |
+
6. If multiple pieces of information are required, collect
|
| 141 |
+
all necessary information before answering.
|
| 142 |
+
|
| 143 |
+
7. Do not invent facts, sources, numbers, or results.
|
| 144 |
+
|
| 145 |
+
8. Give ONLY the final answer required by the question.
|
| 146 |
+
Do not unnecessarily explain your internal reasoning.
|
| 147 |
+
|
| 148 |
+
9. Pay very close attention to:
|
| 149 |
+
- units
|
| 150 |
+
- dates
|
| 151 |
+
- names
|
| 152 |
+
- numerical values
|
| 153 |
+
- percentages
|
| 154 |
+
- requested formats
|
| 155 |
+
|
| 156 |
+
10. If the question asks for a specific format, follow that
|
| 157 |
+
format exactly.
|
| 158 |
+
|
| 159 |
+
You are also knowledgeable in civil and structural engineering,
|
| 160 |
+
including RCC design, steel design, structural analysis,
|
| 161 |
+
foundation engineering, transportation engineering,
|
| 162 |
+
water resources engineering, and construction management.
|
| 163 |
+
|
| 164 |
+
For engineering questions, use Indian Standards when relevant,
|
| 165 |
+
including IS 456, IS 875, IS 1893, IS 800 and IS 13920.
|
| 166 |
+
|
| 167 |
+
USER QUESTION:
|
| 168 |
+
{question}
|
| 169 |
+
"""
|
| 170 |
+
|
| 171 |
+
try:
|
| 172 |
+
|
| 173 |
+
result = self.agent.run(prompt)
|
| 174 |
+
|
| 175 |
+
answer = str(result).strip()
|
| 176 |
+
|
| 177 |
+
print("\nFINAL ANSWER:")
|
| 178 |
+
print(answer)
|
| 179 |
+
|
| 180 |
+
return answer
|
| 181 |
+
|
| 182 |
+
except Exception as e:
|
| 183 |
+
|
| 184 |
+
print("Agent error:", e)
|
| 185 |
+
|
| 186 |
+
return f"Unable to solve the question because of an agent error: {e}"
|
| 187 |
+
|
| 188 |
+
|
| 189 |
+
# ============================================================
|
| 190 |
+
# RUN AND SUBMIT ALL
|
| 191 |
+
# ============================================================
|
| 192 |
+
|
| 193 |
+
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
| 194 |
+
|
| 195 |
"""
|
| 196 |
+
Fetch all GAIA questions,
|
| 197 |
+
run the agent,
|
| 198 |
+
submit answers,
|
| 199 |
+
and display results.
|
| 200 |
"""
|
| 201 |
+
|
| 202 |
+
# --------------------------------------------------------
|
| 203 |
+
# Check login
|
| 204 |
+
# --------------------------------------------------------
|
| 205 |
|
| 206 |
if profile:
|
| 207 |
+
|
| 208 |
+
username = profile.username
|
| 209 |
+
|
| 210 |
print(f"User logged in: {username}")
|
| 211 |
+
|
| 212 |
else:
|
| 213 |
+
|
| 214 |
print("User not logged in.")
|
| 215 |
+
|
| 216 |
+
return (
|
| 217 |
+
"Please login to Hugging Face using the Login button.",
|
| 218 |
+
None
|
| 219 |
+
)
|
| 220 |
+
|
| 221 |
+
|
| 222 |
+
# --------------------------------------------------------
|
| 223 |
+
# API URLs
|
| 224 |
+
# --------------------------------------------------------
|
| 225 |
|
| 226 |
api_url = DEFAULT_API_URL
|
| 227 |
+
|
| 228 |
questions_url = f"{api_url}/questions"
|
| 229 |
+
|
| 230 |
submit_url = f"{api_url}/submit"
|
| 231 |
|
| 232 |
+
|
| 233 |
+
# --------------------------------------------------------
|
| 234 |
+
# Space information
|
| 235 |
+
# --------------------------------------------------------
|
| 236 |
+
|
| 237 |
+
space_id = os.getenv("SPACE_ID")
|
| 238 |
+
|
| 239 |
+
if space_id:
|
| 240 |
+
|
| 241 |
+
agent_code = (
|
| 242 |
+
f"https://huggingface.co/spaces/"
|
| 243 |
+
f"{space_id}/tree/main"
|
| 244 |
+
)
|
| 245 |
+
|
| 246 |
+
else:
|
| 247 |
+
|
| 248 |
+
agent_code = "Local/Unknown-Space"
|
| 249 |
+
|
| 250 |
+
|
| 251 |
+
print("Agent code URL:")
|
| 252 |
+
print(agent_code)
|
| 253 |
+
|
| 254 |
+
|
| 255 |
+
# ========================================================
|
| 256 |
+
# 1. CREATE AGENT
|
| 257 |
+
# ========================================================
|
| 258 |
+
|
| 259 |
try:
|
| 260 |
+
|
| 261 |
agent = BasicAgent()
|
| 262 |
+
|
| 263 |
except Exception as e:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 264 |
|
| 265 |
+
print("Error creating agent:", e)
|
| 266 |
+
|
| 267 |
+
return (
|
| 268 |
+
f"Error initializing agent: {e}",
|
| 269 |
+
None
|
| 270 |
+
)
|
| 271 |
+
|
| 272 |
+
|
| 273 |
+
# ========================================================
|
| 274 |
+
# 2. FETCH QUESTIONS
|
| 275 |
+
# ========================================================
|
| 276 |
+
|
| 277 |
+
print("\nFetching GAIA questions...")
|
| 278 |
+
|
| 279 |
try:
|
| 280 |
+
|
| 281 |
+
response = requests.get(
|
| 282 |
+
questions_url,
|
| 283 |
+
timeout=30
|
| 284 |
+
)
|
| 285 |
+
|
| 286 |
response.raise_for_status()
|
| 287 |
+
|
| 288 |
questions_data = response.json()
|
| 289 |
+
|
| 290 |
if not questions_data:
|
| 291 |
+
|
| 292 |
+
return (
|
| 293 |
+
"No questions were received.",
|
| 294 |
+
None
|
| 295 |
+
)
|
| 296 |
+
|
| 297 |
+
print(
|
| 298 |
+
f"Fetched {len(questions_data)} questions."
|
| 299 |
+
)
|
| 300 |
+
|
| 301 |
except Exception as e:
|
|
|
|
|
|
|
| 302 |
|
| 303 |
+
print("Error fetching questions:", e)
|
| 304 |
+
|
| 305 |
+
return (
|
| 306 |
+
f"Error fetching questions: {e}",
|
| 307 |
+
None
|
| 308 |
+
)
|
| 309 |
+
|
| 310 |
+
|
| 311 |
+
# ========================================================
|
| 312 |
+
# 3. RUN AGENT
|
| 313 |
+
# ========================================================
|
| 314 |
+
|
| 315 |
results_log = []
|
| 316 |
+
|
| 317 |
answers_payload = []
|
| 318 |
+
|
| 319 |
+
print("\nRunning agent...")
|
| 320 |
+
|
| 321 |
+
for number, item in enumerate(
|
| 322 |
+
questions_data,
|
| 323 |
+
start=1
|
| 324 |
+
):
|
| 325 |
+
|
| 326 |
task_id = item.get("task_id")
|
| 327 |
+
|
| 328 |
question_text = item.get("question")
|
| 329 |
+
|
| 330 |
if not task_id or question_text is None:
|
| 331 |
+
|
| 332 |
+
print(
|
| 333 |
+
"Skipping invalid question:",
|
| 334 |
+
item
|
| 335 |
+
)
|
| 336 |
+
|
| 337 |
continue
|
| 338 |
+
|
| 339 |
+
|
| 340 |
+
print(
|
| 341 |
+
f"\nProcessing question "
|
| 342 |
+
f"{number}/{len(questions_data)}"
|
| 343 |
+
)
|
| 344 |
+
|
| 345 |
+
|
| 346 |
try:
|
| 347 |
+
|
| 348 |
+
submitted_answer = agent(
|
| 349 |
+
question_text
|
| 350 |
+
)
|
| 351 |
+
|
| 352 |
+
answers_payload.append(
|
| 353 |
+
{
|
| 354 |
+
"task_id": task_id,
|
| 355 |
+
"submitted_answer": submitted_answer
|
| 356 |
+
}
|
| 357 |
+
)
|
| 358 |
+
|
| 359 |
+
results_log.append(
|
| 360 |
+
{
|
| 361 |
+
"Task ID": task_id,
|
| 362 |
+
"Question": question_text,
|
| 363 |
+
"Submitted Answer": submitted_answer
|
| 364 |
+
}
|
| 365 |
+
)
|
| 366 |
+
|
| 367 |
except Exception as e:
|
| 368 |
+
|
| 369 |
+
print(
|
| 370 |
+
f"Agent error on task {task_id}: {e}"
|
| 371 |
+
)
|
| 372 |
+
|
| 373 |
+
results_log.append(
|
| 374 |
+
{
|
| 375 |
+
"Task ID": task_id,
|
| 376 |
+
"Question": question_text,
|
| 377 |
+
"Submitted Answer":
|
| 378 |
+
f"AGENT ERROR: {e}"
|
| 379 |
+
}
|
| 380 |
+
)
|
| 381 |
+
|
| 382 |
+
|
| 383 |
+
# ========================================================
|
| 384 |
+
# 4. CHECK ANSWERS
|
| 385 |
+
# ========================================================
|
| 386 |
|
| 387 |
if not answers_payload:
|
|
|
|
|
|
|
| 388 |
|
| 389 |
+
return (
|
| 390 |
+
"Agent did not produce any answers.",
|
| 391 |
+
pd.DataFrame(results_log)
|
| 392 |
+
)
|
| 393 |
+
|
| 394 |
+
|
| 395 |
+
# ========================================================
|
| 396 |
+
# 5. PREPARE SUBMISSION
|
| 397 |
+
# ========================================================
|
| 398 |
+
|
| 399 |
+
submission_data = {
|
| 400 |
+
|
| 401 |
+
"username":
|
| 402 |
+
username.strip(),
|
| 403 |
+
|
| 404 |
+
"agent_code":
|
| 405 |
+
agent_code,
|
| 406 |
+
|
| 407 |
+
"answers":
|
| 408 |
+
answers_payload
|
| 409 |
+
}
|
| 410 |
+
|
| 411 |
+
|
| 412 |
+
status_update = (
|
| 413 |
+
f"Agent finished. "
|
| 414 |
+
f"Submitting {len(answers_payload)} answers..."
|
| 415 |
+
)
|
| 416 |
+
|
| 417 |
print(status_update)
|
| 418 |
|
| 419 |
+
|
| 420 |
+
# ========================================================
|
| 421 |
+
# 6. SUBMIT TO GAIA
|
| 422 |
+
# ========================================================
|
| 423 |
+
|
| 424 |
try:
|
| 425 |
+
|
| 426 |
+
response = requests.post(
|
| 427 |
+
submit_url,
|
| 428 |
+
json=submission_data,
|
| 429 |
+
timeout=120
|
| 430 |
+
)
|
| 431 |
+
|
| 432 |
response.raise_for_status()
|
| 433 |
+
|
| 434 |
result_data = response.json()
|
| 435 |
+
|
| 436 |
+
|
| 437 |
final_status = (
|
| 438 |
+
|
| 439 |
+
"Submission Successful!\n\n"
|
| 440 |
+
|
| 441 |
+
f"User: "
|
| 442 |
+
f"{result_data.get('username')}\n"
|
| 443 |
+
|
| 444 |
+
f"Overall Score: "
|
| 445 |
+
f"{result_data.get('score', 'N/A')}%\n"
|
| 446 |
+
|
| 447 |
+
f"Correct: "
|
| 448 |
+
f"{result_data.get('correct_count', '?')}/"
|
| 449 |
+
f"{result_data.get('total_attempted', '?')}\n\n"
|
| 450 |
+
|
| 451 |
+
f"Message: "
|
| 452 |
+
f"{result_data.get('message', '')}"
|
| 453 |
+
)
|
| 454 |
+
|
| 455 |
+
|
| 456 |
+
results_df = pd.DataFrame(
|
| 457 |
+
results_log
|
| 458 |
+
)
|
| 459 |
+
|
| 460 |
+
return (
|
| 461 |
+
final_status,
|
| 462 |
+
results_df
|
| 463 |
+
)
|
| 464 |
+
|
| 465 |
+
|
| 466 |
except requests.exceptions.HTTPError as e:
|
| 467 |
+
|
| 468 |
+
error_detail = (
|
| 469 |
+
f"Server responded with "
|
| 470 |
+
f"status {e.response.status_code}."
|
| 471 |
+
)
|
| 472 |
+
|
| 473 |
try:
|
| 474 |
+
|
| 475 |
error_json = e.response.json()
|
| 476 |
+
|
| 477 |
+
error_detail += (
|
| 478 |
+
f" Detail: "
|
| 479 |
+
f"{error_json.get('detail', '')}"
|
| 480 |
+
)
|
| 481 |
+
|
| 482 |
+
except Exception:
|
| 483 |
+
|
| 484 |
+
error_detail += (
|
| 485 |
+
f" Response: "
|
| 486 |
+
f"{e.response.text[:500]}"
|
| 487 |
+
)
|
| 488 |
+
|
| 489 |
+
|
| 490 |
+
return (
|
| 491 |
+
f"Submission Failed: {error_detail}",
|
| 492 |
+
pd.DataFrame(results_log)
|
| 493 |
+
)
|
| 494 |
+
|
| 495 |
+
|
| 496 |
except requests.exceptions.Timeout:
|
| 497 |
+
|
| 498 |
+
return (
|
| 499 |
+
"Submission Failed: Request timed out.",
|
| 500 |
+
pd.DataFrame(results_log)
|
| 501 |
+
)
|
| 502 |
+
|
| 503 |
+
|
| 504 |
except requests.exceptions.RequestException as e:
|
| 505 |
+
|
| 506 |
+
return (
|
| 507 |
+
f"Submission Failed: Network error - {e}",
|
| 508 |
+
pd.DataFrame(results_log)
|
| 509 |
+
)
|
| 510 |
+
|
| 511 |
+
|
| 512 |
except Exception as e:
|
| 513 |
+
|
| 514 |
+
return (
|
| 515 |
+
f"Unexpected submission error: {e}",
|
| 516 |
+
pd.DataFrame(results_log)
|
| 517 |
+
)
|
| 518 |
|
| 519 |
|
| 520 |
+
# ============================================================
|
| 521 |
+
# GRADIO INTERFACE
|
| 522 |
+
# ============================================================
|
| 523 |
+
|
| 524 |
with gr.Blocks() as demo:
|
| 525 |
+
|
| 526 |
gr.Markdown(
|
| 527 |
+
"# 🚀 StructuralGPT - GAIA Agent"
|
| 528 |
+
)
|
| 529 |
|
| 530 |
+
gr.Markdown(
|
| 531 |
+
"""
|
| 532 |
+
### Instructions
|
| 533 |
|
| 534 |
+
1. Login to Hugging Face.
|
| 535 |
+
2. The agent uses Qwen through Hugging Face.
|
| 536 |
+
3. The agent can search the web.
|
| 537 |
+
4. The agent can perform calculations using Python.
|
| 538 |
+
5. Click **Run Evaluation & Submit All Answers**.
|
| 539 |
"""
|
| 540 |
)
|
| 541 |
|
| 542 |
gr.LoginButton()
|
| 543 |
|
|
|
|
| 544 |
|
| 545 |
+
run_button = gr.Button(
|
| 546 |
+
"Run Evaluation & Submit All Answers"
|
| 547 |
+
)
|
| 548 |
+
|
| 549 |
+
|
| 550 |
+
status_output = gr.Textbox(
|
| 551 |
+
label="Run Status / Submission Result",
|
| 552 |
+
lines=8,
|
| 553 |
+
interactive=False
|
| 554 |
+
)
|
| 555 |
+
|
| 556 |
+
|
| 557 |
+
results_table = gr.DataFrame(
|
| 558 |
+
label="Questions and Agent Answers",
|
| 559 |
+
wrap=True
|
| 560 |
+
)
|
| 561 |
+
|
| 562 |
|
| 563 |
run_button.click(
|
| 564 |
fn=run_and_submit_all,
|
| 565 |
+
outputs=[
|
| 566 |
+
status_output,
|
| 567 |
+
results_table
|
| 568 |
+
]
|
| 569 |
)
|
| 570 |
|
| 571 |
+
|
| 572 |
+
# ============================================================
|
| 573 |
+
# START APPLICATION
|
| 574 |
+
# ============================================================
|
| 575 |
+
|
| 576 |
if __name__ == "__main__":
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 577 |
|
| 578 |
+
print(
|
| 579 |
+
"\n" +
|
| 580 |
+
"-" * 30 +
|
| 581 |
+
" App Starting " +
|
| 582 |
+
"-" * 30
|
| 583 |
+
)
|
| 584 |
+
|
| 585 |
+
|
| 586 |
+
space_host = os.getenv(
|
| 587 |
+
"SPACE_HOST"
|
| 588 |
+
)
|
| 589 |
+
|
| 590 |
+
space_id = os.getenv(
|
| 591 |
+
"SPACE_ID"
|
| 592 |
+
)
|
| 593 |
+
|
| 594 |
+
|
| 595 |
+
if space_host:
|
| 596 |
+
|
| 597 |
+
print(
|
| 598 |
+
f"SPACE_HOST: {space_host}"
|
| 599 |
+
)
|
| 600 |
+
|
| 601 |
+
if space_id:
|
| 602 |
+
|
| 603 |
+
print(
|
| 604 |
+
f"SPACE_ID: {space_id}"
|
| 605 |
+
|
| 606 |
+
)
|
| 607 |
+
|
| 608 |
+
print(
|
| 609 |
+
"Repository:"
|
| 610 |
+
)
|
| 611 |
+
|
| 612 |
+
print(
|
| 613 |
+
f"https://huggingface.co/spaces/{space_id}"
|
| 614 |
+
)
|
| 615 |
+
|
| 616 |
+
|
| 617 |
+
print(
|
| 618 |
+
"\nLaunching StructuralGPT..."
|
| 619 |
+
)
|
| 620 |
|
|
|
|
| 621 |
|
| 622 |
+
demo.launch(
|
| 623 |
+
debug=True,
|
| 624 |
+
share=False
|
| 625 |
+
)
|