update app.py
#319
by anikett7 - opened
app.py
CHANGED
|
@@ -1,25 +1,35 @@
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
import requests
|
| 4 |
-
import inspect
|
| 5 |
import pandas as pd
|
|
|
|
|
|
|
| 6 |
|
| 7 |
# (Keep Constants as is)
|
| 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("BasicAgent initialized.")
|
| 16 |
-
|
|
|
|
| 17 |
print(f"Agent received question (first 50 chars): {question[:50]}...")
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
-
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
| 23 |
"""
|
| 24 |
Fetches all questions, runs the BasicAgent on them, submits all answers,
|
| 25 |
and displays the results.
|
|
@@ -73,14 +83,26 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
| 73 |
results_log = []
|
| 74 |
answers_payload = []
|
| 75 |
print(f"Running agent on {len(questions_data)} questions...")
|
|
|
|
| 76 |
for item in questions_data:
|
| 77 |
task_id = item.get("task_id")
|
| 78 |
question_text = item.get("question")
|
|
|
|
| 79 |
if not task_id or question_text is None:
|
| 80 |
print(f"Skipping item with missing task_id or question: {item}")
|
| 81 |
continue
|
| 82 |
try:
|
| 83 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
| 85 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
| 86 |
except Exception as e:
|
|
@@ -139,7 +161,7 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
| 139 |
results_df = pd.DataFrame(results_log)
|
| 140 |
return status_message, results_df
|
| 141 |
|
| 142 |
-
|
| 143 |
# --- Build Gradio Interface using Blocks ---
|
| 144 |
with gr.Blocks() as demo:
|
| 145 |
gr.Markdown("# Basic Agent Evaluation Runner")
|
|
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
import requests
|
|
|
|
| 4 |
import pandas as pd
|
| 5 |
+
from langgraph.agent import langgraph_agent
|
| 6 |
+
from llamaindex.agent import llamaindex_agent
|
| 7 |
|
| 8 |
# (Keep Constants as is)
|
| 9 |
# --- Constants ---
|
| 10 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 11 |
+
FRAMEWORK = "LANGGRAPH"
|
| 12 |
+
# FRAMEWORK = "LLAMAINDEX"
|
| 13 |
# --- Basic Agent Definition ---
|
| 14 |
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
| 15 |
class BasicAgent:
|
| 16 |
def __init__(self):
|
| 17 |
+
if FRAMEWORK == "LANGGRAPH":
|
| 18 |
+
self.agent = langgraph_agent()
|
| 19 |
+
else:
|
| 20 |
+
self.agent = llamaindex_agent()
|
| 21 |
print("BasicAgent initialized.")
|
| 22 |
+
|
| 23 |
+
async def __call__(self, question: str) -> str:
|
| 24 |
print(f"Agent received question (first 50 chars): {question[:50]}...")
|
| 25 |
+
if FRAMEWORK == "LANGGRAPH":
|
| 26 |
+
answer = self.agent(question)
|
| 27 |
+
else:
|
| 28 |
+
answer = await self.agent(question)
|
| 29 |
+
|
| 30 |
+
return answer
|
| 31 |
|
| 32 |
+
async def run_and_submit_all( profile: gr.OAuthProfile | None):
|
| 33 |
"""
|
| 34 |
Fetches all questions, runs the BasicAgent on them, submits all answers,
|
| 35 |
and displays the results.
|
|
|
|
| 83 |
results_log = []
|
| 84 |
answers_payload = []
|
| 85 |
print(f"Running agent on {len(questions_data)} questions...")
|
| 86 |
+
|
| 87 |
for item in questions_data:
|
| 88 |
task_id = item.get("task_id")
|
| 89 |
question_text = item.get("question")
|
| 90 |
+
file_name = item.get("file_name")
|
| 91 |
if not task_id or question_text is None:
|
| 92 |
print(f"Skipping item with missing task_id or question: {item}")
|
| 93 |
continue
|
| 94 |
try:
|
| 95 |
+
if file_name:
|
| 96 |
+
# add the URL of the data source to the question (so that the agent can deal with it)
|
| 97 |
+
file_url = f"{DEFAULT_API_URL}/files/{task_id}"
|
| 98 |
+
question_text += f'\nFile URL: "{file_url}"'
|
| 99 |
+
# get the extension of the file to help the agent
|
| 100 |
+
try:
|
| 101 |
+
ext = file_name.split('.')[-1]
|
| 102 |
+
question_text += f" (.{ext} file)"
|
| 103 |
+
except:
|
| 104 |
+
pass
|
| 105 |
+
submitted_answer = await agent(question_text)
|
| 106 |
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
| 107 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
| 108 |
except Exception as e:
|
|
|
|
| 161 |
results_df = pd.DataFrame(results_log)
|
| 162 |
return status_message, results_df
|
| 163 |
|
| 164 |
+
# run_and_submit_all(None)
|
| 165 |
# --- Build Gradio Interface using Blocks ---
|
| 166 |
with gr.Blocks() as demo:
|
| 167 |
gr.Markdown("# Basic Agent Evaluation Runner")
|