Commit ·
a0b1b1d
1
Parent(s): 832bd05
minor restructuring and logging improvements
Browse files- app.py +43 -35
- requirements.txt +1 -0
app.py
CHANGED
|
@@ -4,6 +4,7 @@ import argparse
|
|
| 4 |
import requests
|
| 5 |
import pandas as pd
|
| 6 |
import json
|
|
|
|
| 7 |
|
| 8 |
# Import agent-related code from agents module
|
| 9 |
from agents import MyLangGraphAgent
|
|
@@ -16,6 +17,11 @@ from scorer import question_scorer
|
|
| 16 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 17 |
AGENT_TIMEOUT_SECONDS = 180 # 3 minutes max per question (enforced by agent's internal limits)
|
| 18 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
def FetchQuestions(api_url: str):
|
| 21 |
"""Fetch questions from the given API URL."""
|
|
@@ -322,9 +328,7 @@ def run_test_code(filter=None):
|
|
| 322 |
questions_data = get_questions(test_mode=True)
|
| 323 |
|
| 324 |
if not isinstance(questions_data, list):
|
| 325 |
-
|
| 326 |
-
print(error_msg)
|
| 327 |
-
return error_msg
|
| 328 |
|
| 329 |
# Apply filter or use all questions
|
| 330 |
if filter is not None:
|
|
@@ -354,52 +358,56 @@ if __name__ == "__main__":
|
|
| 354 |
args = parser.parse_args()
|
| 355 |
|
| 356 |
print("\n" + "-"*30 + " App Starting " + "-"*30)
|
| 357 |
-
# Check for SPACE_HOST and SPACE_ID at startup for information
|
| 358 |
-
space_host_startup = os.getenv("SPACE_HOST")
|
| 359 |
-
space_id_startup = os.getenv("SPACE_ID") # Get SPACE_ID at startup
|
| 360 |
|
| 361 |
-
|
| 362 |
-
|
| 363 |
-
print(f" Runtime URL should be: https://{space_host_startup}.hf.space")
|
| 364 |
-
else:
|
| 365 |
-
print("[INFO] SPACE_HOST environment variable not found (running locally?).")
|
| 366 |
|
| 367 |
-
|
| 368 |
-
|
| 369 |
-
|
| 370 |
-
|
| 371 |
-
|
| 372 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 373 |
|
| 374 |
print("-"*(60 + len(" App Starting ")) + "\n")
|
| 375 |
|
| 376 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 377 |
if args.test:
|
| 378 |
-
|
| 379 |
-
# Specify question indices to test, or None for all questions
|
| 380 |
# Examples:
|
| 381 |
# - (0, 1, 3, 4, 5, 9, 11, 13, 14, 17, 18) - All 11 incorrect questions
|
| 382 |
# - (0, 1, 4, 5, 14, 17) - All 6 incorrect except ones with files
|
| 383 |
-
# - None - Test all 20 questions
|
| 384 |
test_filter = (4, 7, 15) # Testing Q5, Q8, Q16
|
| 385 |
-
|
| 386 |
-
|
| 387 |
-
print("Running test code on ALL questions (CLI mode)...")
|
| 388 |
-
result = run_test_code(filter=None) # Test all questions
|
| 389 |
|
| 390 |
-
|
|
|
|
|
|
|
|
|
|
| 391 |
if isinstance(result, pd.DataFrame):
|
| 392 |
-
# Print DataFrame content without truncation
|
| 393 |
pd.set_option('display.max_colwidth', None)
|
| 394 |
pd.set_option('display.max_rows', None)
|
| 395 |
-
# Iterate and print each row's content to ensure clean text output
|
| 396 |
for col in result.columns:
|
| 397 |
-
|
| 398 |
-
|
| 399 |
else:
|
| 400 |
print(result)
|
| 401 |
-
sys.exit(0)
|
| 402 |
-
|
| 403 |
-
print("Launching Gradio Interface for Basic Agent Evaluation...")
|
| 404 |
-
grTestApp = create_ui(run_and_submit_all, run_test_code)
|
| 405 |
-
grTestApp.launch()
|
|
|
|
| 4 |
import requests
|
| 5 |
import pandas as pd
|
| 6 |
import json
|
| 7 |
+
from enum import Enum
|
| 8 |
|
| 9 |
# Import agent-related code from agents module
|
| 10 |
from agents import MyLangGraphAgent
|
|
|
|
| 17 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 18 |
AGENT_TIMEOUT_SECONDS = 180 # 3 minutes max per question (enforced by agent's internal limits)
|
| 19 |
|
| 20 |
+
# --- Run Modes ---
|
| 21 |
+
class RunMode(Enum):
|
| 22 |
+
UI = "ui" # Gradio UI mode
|
| 23 |
+
CLI = "cli" # Command-line test mode
|
| 24 |
+
|
| 25 |
|
| 26 |
def FetchQuestions(api_url: str):
|
| 27 |
"""Fetch questions from the given API URL."""
|
|
|
|
| 328 |
questions_data = get_questions(test_mode=True)
|
| 329 |
|
| 330 |
if not isinstance(questions_data, list):
|
| 331 |
+
return f"Failed to load questions: {questions_data}"
|
|
|
|
|
|
|
| 332 |
|
| 333 |
# Apply filter or use all questions
|
| 334 |
if filter is not None:
|
|
|
|
| 358 |
args = parser.parse_args()
|
| 359 |
|
| 360 |
print("\n" + "-"*30 + " App Starting " + "-"*30)
|
|
|
|
|
|
|
|
|
|
| 361 |
|
| 362 |
+
# Determine run mode
|
| 363 |
+
run_mode = RunMode.CLI if (args.test or args.testall) else RunMode.UI
|
|
|
|
|
|
|
|
|
|
| 364 |
|
| 365 |
+
# Print environment info only in UI mode
|
| 366 |
+
if run_mode == RunMode.UI:
|
| 367 |
+
space_host = os.getenv("SPACE_HOST")
|
| 368 |
+
space_id = os.getenv("SPACE_ID")
|
| 369 |
+
|
| 370 |
+
if space_host:
|
| 371 |
+
print(f"[OK] SPACE_HOST found: {space_host}")
|
| 372 |
+
print(f" Runtime URL should be: https://{space_host}.hf.space")
|
| 373 |
+
else:
|
| 374 |
+
print("[INFO] SPACE_HOST environment variable not found (running locally?).")
|
| 375 |
+
|
| 376 |
+
if space_id:
|
| 377 |
+
print(f"[OK] SPACE_ID found: {space_id}")
|
| 378 |
+
print(f" Repo URL: https://huggingface.co/spaces/{space_id}")
|
| 379 |
+
print(f" Repo Tree URL: https://huggingface.co/spaces/{space_id}/tree/main")
|
| 380 |
+
else:
|
| 381 |
+
print("[INFO] SPACE_ID environment variable not found (running locally?). Repo URL cannot be determined.")
|
| 382 |
|
| 383 |
print("-"*(60 + len(" App Starting ")) + "\n")
|
| 384 |
|
| 385 |
+
# Execute based on run mode
|
| 386 |
+
if run_mode == RunMode.UI: # Launch Gradio UI
|
| 387 |
+
print("Launching Gradio Interface for Basic Agent Evaluation...")
|
| 388 |
+
grTestApp = create_ui(run_and_submit_all, run_test_code)
|
| 389 |
+
grTestApp.launch()
|
| 390 |
+
|
| 391 |
+
else # run_mode == RunMode.CLI:
|
| 392 |
+
# Determine test filter based on which CLI flag was used
|
| 393 |
if args.test:
|
| 394 |
+
# Specify question indices to test
|
|
|
|
| 395 |
# Examples:
|
| 396 |
# - (0, 1, 3, 4, 5, 9, 11, 13, 14, 17, 18) - All 11 incorrect questions
|
| 397 |
# - (0, 1, 4, 5, 14, 17) - All 6 incorrect except ones with files
|
|
|
|
| 398 |
test_filter = (4, 7, 15) # Testing Q5, Q8, Q16
|
| 399 |
+
else: # args.testall
|
| 400 |
+
test_filter = None # Test all questions
|
|
|
|
|
|
|
| 401 |
|
| 402 |
+
print(f"Running test code on {len(test_filter) if test_filter else 'ALL'} questions (CLI mode)...")
|
| 403 |
+
result = run_test_code(filter=test_filter)
|
| 404 |
+
|
| 405 |
+
# Print results
|
| 406 |
if isinstance(result, pd.DataFrame):
|
|
|
|
| 407 |
pd.set_option('display.max_colwidth', None)
|
| 408 |
pd.set_option('display.max_rows', None)
|
|
|
|
| 409 |
for col in result.columns:
|
| 410 |
+
for val in result[col]:
|
| 411 |
+
print(val)
|
| 412 |
else:
|
| 413 |
print(result)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
requirements.txt
CHANGED
|
@@ -17,6 +17,7 @@ pytube
|
|
| 17 |
pymupdf
|
| 18 |
nest_asyncio
|
| 19 |
speechrecognition
|
|
|
|
| 20 |
markdownify
|
| 21 |
numpy
|
| 22 |
pandas
|
|
|
|
| 17 |
pymupdf
|
| 18 |
nest_asyncio
|
| 19 |
speechrecognition
|
| 20 |
+
pydub
|
| 21 |
markdownify
|
| 22 |
numpy
|
| 23 |
pandas
|