Shrutipanchal086's picture
Update app.py
63e18a9 verified
Raw
History Blame
13.7 kB
# ============================================================
# INSTALL REQUIRED LIBRARIES
# ============================================================
import subprocess
import sys
subprocess.check_call([
sys.executable, "-m", "pip", "install", "-q",
"smolagents[toolkit]",
"gradio",
"requests",
"pandas"
])
# ============================================================
# IMPORT LIBRARIES
# ============================================================
import os
import gradio as gr
import requests
import pandas as pd
from smolagents import (
CodeAgent,
InferenceClientModel,
DuckDuckGoSearchTool,
PythonInterpreterTool
)
# ============================================================
# CONSTANTS
# ============================================================
DEFAULT_API_URL = "https://huggingface.co/Shrutipanchal086/structrural_AI_agent"
# ============================================================
# STRUCTURALGPT / GAIA AGENT
# ============================================================
class BasicAgent:
def __init__(self):
print("Initializing StructuralGPT GAIA Agent...")
# ----------------------------------------------------
# Hugging Face token
# Add HF_TOKEN in Space Settings -> Secrets
# ----------------------------------------------------
hf_token = os.getenv("hf_token")
if not hf_token:
raise ValueError(
"HF_TOKEN not found. "
"Please add HF_TOKEN in Space Settings -> Secrets."
)
# ----------------------------------------------------
# Hugging Face model
# ----------------------------------------------------
self.model = InferenceClientModel(
model_id="Qwen/Qwen3-Next-80B-A3B-Thinking",
token=hf_token,
max_tokens=3000
)
# ----------------------------------------------------
# Web search
# ----------------------------------------------------
self.search_tool = DuckDuckGoSearchTool(
max_results=8
)
# ----------------------------------------------------
# Python calculator / reasoning tool
# ----------------------------------------------------
self.python_tool = PythonInterpreterTool(
authorized_imports=[
"math",
"statistics",
"datetime",
"json",
"re"
]
)
# ----------------------------------------------------
# Agent
# ----------------------------------------------------
self.agent = CodeAgent(
model=self.model,
tools=[
self.search_tool,
self.python_tool
],
max_steps=10
)
print("StructuralGPT GAIA Agent initialized successfully.")
def __call__(self, question: str) -> str:
print("\n" + "=" * 60)
print("QUESTION:")
print(question)
print("=" * 60)
prompt = f"""
You are a powerful general-purpose AI agent participating
in the GAIA benchmark.
Your job is to solve the user's question accurately.
IMPORTANT RULES:
1. Understand the question completely before answering.
2. If the question requires current, factual, or external
information, use the web search tool.
3. If calculations are required, use the Python tool.
Do not rely on mental arithmetic for complicated calculations.
4. Break difficult problems into smaller steps.
5. Verify important calculations and facts before producing
the final answer.
6. If multiple pieces of information are required, collect
all necessary information before answering.
7. Do not invent facts, sources, numbers, or results.
8. Give ONLY the final answer required by the question.
Do not unnecessarily explain your internal reasoning.
9. Pay very close attention to:
- units
- dates
- names
- numerical values
- percentages
- requested formats
10. If the question asks for a specific format, follow that
format exactly.
You are also knowledgeable in civil and structural engineering,
including RCC design, steel design, structural analysis,
foundation engineering, transportation engineering,
water resources engineering, and construction management.
For engineering questions, use Indian Standards when relevant,
including IS 456, IS 875, IS 1893, IS 800 and IS 13920.
USER QUESTION:
{question}
"""
try:
result = self.agent.run(prompt)
answer = str(result).strip()
print("\nFINAL ANSWER:")
print(answer)
return answer
except Exception as e:
print("Agent error:", e)
return f"Unable to solve the question because of an agent error: {e}"
# ============================================================
# RUN AND SUBMIT ALL
# ============================================================
def run_and_submit_all(profile: gr.OAuthProfile | None):
"""
Fetch all GAIA questions,
run the agent,
submit answers,
and display results.
"""
# --------------------------------------------------------
# Check login
# --------------------------------------------------------
if profile:
username = profile.username
print(f"User logged in: {username}")
else:
print("User not logged in.")
return (
"Please login to Hugging Face using the Login button.",
None
)
# --------------------------------------------------------
# API URLs
# --------------------------------------------------------
api_url = DEFAULT_API_URL
questions_url = f"{api_url}/questions"
submit_url = f"{api_url}/submit"
# --------------------------------------------------------
# Space information
# --------------------------------------------------------
space_id = os.getenv("SPACE_ID")
if space_id:
agent_code = (
f"https://huggingface.co/spaces/"
f"{space_id}/tree/main"
)
else:
agent_code = "Local/Unknown-Space"
print("Agent code URL:")
print(agent_code)
# ========================================================
# 1. CREATE AGENT
# ========================================================
try:
agent = BasicAgent()
except Exception as e:
print("Error creating agent:", e)
return (
f"Error initializing agent: {e}",
None
)
# ========================================================
# 2. FETCH QUESTIONS
# ========================================================
print("\nFetching GAIA questions...")
try:
response = requests.get(
questions_url,
timeout=30
)
response.raise_for_status()
questions_data = response.json()
if not questions_data:
return (
"No questions were received.",
None
)
print(
f"Fetched {len(questions_data)} questions."
)
except Exception as e:
print("Error fetching questions:", e)
return (
f"Error fetching questions: {e}",
None
)
# ========================================================
# 3. RUN AGENT
# ========================================================
results_log = []
answers_payload = []
print("\nRunning agent...")
for number, item in enumerate(
questions_data,
start=1
):
task_id = item.get("task_id")
question_text = item.get("question")
if not task_id or question_text is None:
print(
"Skipping invalid question:",
item
)
continue
print(
f"\nProcessing question "
f"{number}/{len(questions_data)}"
)
try:
submitted_answer = agent(
question_text
)
answers_payload.append(
{
"task_id": task_id,
"submitted_answer": submitted_answer
}
)
results_log.append(
{
"Task ID": task_id,
"Question": question_text,
"Submitted Answer": submitted_answer
}
)
except Exception as e:
print(
f"Agent error on task {task_id}: {e}"
)
results_log.append(
{
"Task ID": task_id,
"Question": question_text,
"Submitted Answer":
f"AGENT ERROR: {e}"
}
)
# ========================================================
# 4. CHECK ANSWERS
# ========================================================
if not answers_payload:
return (
"Agent did not produce any answers.",
pd.DataFrame(results_log)
)
# ========================================================
# 5. PREPARE SUBMISSION
# ========================================================
submission_data = {
"username":
username.strip(),
"agent_code":
agent_code,
"answers":
answers_payload
}
status_update = (
f"Agent finished. "
f"Submitting {len(answers_payload)} answers..."
)
print(status_update)
# ========================================================
# 6. SUBMIT TO GAIA
# ========================================================
try:
response = requests.post(
submit_url,
json=submission_data,
timeout=120
)
response.raise_for_status()
result_data = response.json()
final_status = (
"Submission Successful!\n\n"
f"User: "
f"{result_data.get('username')}\n"
f"Overall Score: "
f"{result_data.get('score', 'N/A')}%\n"
f"Correct: "
f"{result_data.get('correct_count', '?')}/"
f"{result_data.get('total_attempted', '?')}\n\n"
f"Message: "
f"{result_data.get('message', '')}"
)
results_df = pd.DataFrame(
results_log
)
return (
final_status,
results_df
)
except requests.exceptions.HTTPError as e:
error_detail = (
f"Server responded with "
f"status {e.response.status_code}."
)
try:
error_json = e.response.json()
error_detail += (
f" Detail: "
f"{error_json.get('detail', '')}"
)
except Exception:
error_detail += (
f" Response: "
f"{e.response.text[:500]}"
)
return (
f"Submission Failed: {error_detail}",
pd.DataFrame(results_log)
)
except requests.exceptions.Timeout:
return (
"Submission Failed: Request timed out.",
pd.DataFrame(results_log)
)
except requests.exceptions.RequestException as e:
return (
f"Submission Failed: Network error - {e}",
pd.DataFrame(results_log)
)
except Exception as e:
return (
f"Unexpected submission error: {e}",
pd.DataFrame(results_log)
)
# ============================================================
# GRADIO INTERFACE
# ============================================================
with gr.Blocks() as demo:
gr.Markdown(
"# 🚀 StructuralGPT - GAIA Agent"
)
gr.Markdown(
"""
### Instructions
1. Login to Hugging Face.
2. The agent uses Qwen through Hugging Face.
3. The agent can search the web.
4. The agent can perform calculations using Python.
5. Click **Run Evaluation & Submit All Answers**.
"""
)
gr.LoginButton()
run_button = gr.Button(
"Run Evaluation & Submit All Answers"
)
status_output = gr.Textbox(
label="Run Status / Submission Result",
lines=8,
interactive=False
)
results_table = gr.DataFrame(
label="Questions and Agent Answers",
wrap=True
)
run_button.click(
fn=run_and_submit_all,
outputs=[
status_output,
results_table
]
)
# ============================================================
# START APPLICATION
# ============================================================
if __name__ == "__main__":
print(
"\n" +
"-" * 30 +
" App Starting " +
"-" * 30
)
space_host = os.getenv(
"SPACE_HOST"
)
space_id = os.getenv(
"SPACE_ID"
)
if space_host:
print(
f"SPACE_HOST: {space_host}"
)
if space_id:
print(
f"SPACE_ID: {space_id}"
)
print(
"Repository:"
)
print(
f"https://huggingface.co/spaces/{space_id}"
)
print(
"\nLaunching StructuralGPT..."
)
demo.launch(
debug=True,
share=False
)