Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,15 +6,12 @@ import pandas as pd
|
|
| 6 |
import tempfile
|
| 7 |
import shutil
|
| 8 |
from pathlib import Path
|
| 9 |
-
import re
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
import base64 # For encoding images for multimodal models
|
| 14 |
-
import logging # For better debugging
|
| 15 |
-
import subprocess # To check for stockfish/ffmpeg maybe
|
| 16 |
from openai import OpenAI
|
| 17 |
-
import time
|
| 18 |
|
| 19 |
# Langchain specific imports
|
| 20 |
from langchain_openai import ChatOpenAI, OpenAIEmbeddings
|
|
@@ -36,17 +33,11 @@ logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(
|
|
| 36 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 37 |
# STOCKFISH_PATH = os.getenv("STOCKFISH_PATH", "stockfish") # No longer needed
|
| 38 |
|
| 39 |
-
# --- !!! SUBMISSION FLAG !!! ---
|
| 40 |
-
# Change this to True to enable submitting results to the scoring server.
|
| 41 |
-
# Keep it False to only run the agent and display answers locally.
|
| 42 |
ENABLE_SUBMISSION = False
|
| 43 |
-
# --- !!! SUBMISSION FLAG !!! ---
|
| 44 |
-
|
| 45 |
|
| 46 |
# --- Helper Functions ---
|
| 47 |
|
| 48 |
def download_file(url: str, destination_folder: str, task_id: str) -> Path | None:
|
| 49 |
-
# (Keep existing download_file function as is)
|
| 50 |
try:
|
| 51 |
response = requests.get(url, stream=True, timeout=30)
|
| 52 |
response.raise_for_status()
|
|
@@ -74,7 +65,6 @@ def download_file(url: str, destination_folder: str, task_id: str) -> Path | Non
|
|
| 74 |
# --- Custom Tools / Analysis Functions ---
|
| 75 |
|
| 76 |
def transcribe_audio(file_path: str) -> str:
|
| 77 |
-
# (Keep existing transcribe_audio function as is - uses direct OpenAI client)
|
| 78 |
if not Path(file_path).is_file(): return f"ERROR: Audio file not found at {file_path}"
|
| 79 |
try:
|
| 80 |
logging.info(f"Transcribing audio file: {file_path}")
|
|
@@ -93,7 +83,6 @@ def transcribe_audio(file_path: str) -> str:
|
|
| 93 |
|
| 94 |
|
| 95 |
def analyze_excel(file_path: str, question: str) -> str:
|
| 96 |
-
# (Keep existing analyze_excel function as is)
|
| 97 |
if not Path(file_path).is_file(): return f"ERROR: Excel file not found at {file_path}"
|
| 98 |
try:
|
| 99 |
logging.info(f"Analyzing Excel file: {file_path} for question: {question[:50]}...")
|
|
@@ -118,7 +107,6 @@ def analyze_excel(file_path: str, question: str) -> str:
|
|
| 118 |
|
| 119 |
|
| 120 |
def analyze_chess_image_gpt4o(file_path: str) -> str: # Renamed from analyze_chess_image
|
| 121 |
-
# (Keep existing analyze_chess_image_gpt4o function as is - uses GPT-4o directly)
|
| 122 |
if not Path(file_path).is_file(): return f"ERROR: Chess image file not found at {file_path}"
|
| 123 |
try:
|
| 124 |
logging.info(f"Analyzing chess image using GPT-4o: {file_path}")
|
|
@@ -147,14 +135,12 @@ def analyze_chess_image_gpt4o(file_path: str) -> str: # Renamed from analyze_che
|
|
| 147 |
|
| 148 |
|
| 149 |
def analyze_video_birds(file_path: str) -> str:
|
| 150 |
-
# (Keep existing analyze_video_birds function - returns standard error)
|
| 151 |
logging.warning(f"Video analysis (Q2 Birds) requested for {file_path}. Not supported.")
|
| 152 |
return "ERROR: Video analysis for simultaneous bird species count is currently not supported by this agent."
|
| 153 |
|
| 154 |
|
| 155 |
# --- Agent Definition ---
|
| 156 |
class GaiaAgent:
|
| 157 |
-
# (Keep __init__ as is, including tools and agent executor setup without early_stopping_method)
|
| 158 |
def __init__(self, api_url: str):
|
| 159 |
self.api_url = api_url
|
| 160 |
self.temp_dir = tempfile.mkdtemp()
|
|
@@ -194,7 +180,6 @@ class GaiaAgent:
|
|
| 194 |
max_iterations=8
|
| 195 |
)
|
| 196 |
|
| 197 |
-
# (Keep __call__ method largely as is, ensuring Q5 logic is included)
|
| 198 |
def __call__(self, question: str, task_id: str) -> str:
|
| 199 |
logging.info(f"Agent received question (task {task_id}): {question[:100]}...")
|
| 200 |
file_path = None
|
|
@@ -309,7 +294,6 @@ class GaiaAgent:
|
|
| 309 |
final_answer = f"ERROR: Agent execution failed. Details: {str(e)}"
|
| 310 |
|
| 311 |
# --- Post-processing and Cleanup ---
|
| 312 |
-
# (Keep existing post-processing logic for Q3, Q6, Q9, Q19)
|
| 313 |
prefixes = ["the answer is ", "here is the answer:", "the final answer is:", "answer:"]
|
| 314 |
final_answer_lower = final_answer.lower().strip()
|
| 315 |
for prefix in prefixes:
|
|
|
|
| 6 |
import tempfile
|
| 7 |
import shutil
|
| 8 |
from pathlib import Path
|
| 9 |
+
import re
|
| 10 |
+
import base64
|
| 11 |
+
import logging
|
| 12 |
+
import subprocess
|
|
|
|
|
|
|
|
|
|
| 13 |
from openai import OpenAI
|
| 14 |
+
import time
|
| 15 |
|
| 16 |
# Langchain specific imports
|
| 17 |
from langchain_openai import ChatOpenAI, OpenAIEmbeddings
|
|
|
|
| 33 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 34 |
# STOCKFISH_PATH = os.getenv("STOCKFISH_PATH", "stockfish") # No longer needed
|
| 35 |
|
|
|
|
|
|
|
|
|
|
| 36 |
ENABLE_SUBMISSION = False
|
|
|
|
|
|
|
| 37 |
|
| 38 |
# --- Helper Functions ---
|
| 39 |
|
| 40 |
def download_file(url: str, destination_folder: str, task_id: str) -> Path | None:
|
|
|
|
| 41 |
try:
|
| 42 |
response = requests.get(url, stream=True, timeout=30)
|
| 43 |
response.raise_for_status()
|
|
|
|
| 65 |
# --- Custom Tools / Analysis Functions ---
|
| 66 |
|
| 67 |
def transcribe_audio(file_path: str) -> str:
|
|
|
|
| 68 |
if not Path(file_path).is_file(): return f"ERROR: Audio file not found at {file_path}"
|
| 69 |
try:
|
| 70 |
logging.info(f"Transcribing audio file: {file_path}")
|
|
|
|
| 83 |
|
| 84 |
|
| 85 |
def analyze_excel(file_path: str, question: str) -> str:
|
|
|
|
| 86 |
if not Path(file_path).is_file(): return f"ERROR: Excel file not found at {file_path}"
|
| 87 |
try:
|
| 88 |
logging.info(f"Analyzing Excel file: {file_path} for question: {question[:50]}...")
|
|
|
|
| 107 |
|
| 108 |
|
| 109 |
def analyze_chess_image_gpt4o(file_path: str) -> str: # Renamed from analyze_chess_image
|
|
|
|
| 110 |
if not Path(file_path).is_file(): return f"ERROR: Chess image file not found at {file_path}"
|
| 111 |
try:
|
| 112 |
logging.info(f"Analyzing chess image using GPT-4o: {file_path}")
|
|
|
|
| 135 |
|
| 136 |
|
| 137 |
def analyze_video_birds(file_path: str) -> str:
|
|
|
|
| 138 |
logging.warning(f"Video analysis (Q2 Birds) requested for {file_path}. Not supported.")
|
| 139 |
return "ERROR: Video analysis for simultaneous bird species count is currently not supported by this agent."
|
| 140 |
|
| 141 |
|
| 142 |
# --- Agent Definition ---
|
| 143 |
class GaiaAgent:
|
|
|
|
| 144 |
def __init__(self, api_url: str):
|
| 145 |
self.api_url = api_url
|
| 146 |
self.temp_dir = tempfile.mkdtemp()
|
|
|
|
| 180 |
max_iterations=8
|
| 181 |
)
|
| 182 |
|
|
|
|
| 183 |
def __call__(self, question: str, task_id: str) -> str:
|
| 184 |
logging.info(f"Agent received question (task {task_id}): {question[:100]}...")
|
| 185 |
file_path = None
|
|
|
|
| 294 |
final_answer = f"ERROR: Agent execution failed. Details: {str(e)}"
|
| 295 |
|
| 296 |
# --- Post-processing and Cleanup ---
|
|
|
|
| 297 |
prefixes = ["the answer is ", "here is the answer:", "the final answer is:", "answer:"]
|
| 298 |
final_answer_lower = final_answer.lower().strip()
|
| 299 |
for prefix in prefixes:
|