Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -7,22 +7,36 @@ import requests
|
|
| 7 |
import inspect
|
| 8 |
import pandas as pd
|
| 9 |
import spaces
|
| 10 |
-
from huggingface_hub import InferenceClient
|
| 11 |
from duckduckgo_search import DDGS
|
| 12 |
|
| 13 |
# (Keep Constants as is)
|
| 14 |
# --- Constants ---
|
| 15 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 16 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
# --- Basic Agent Definition ---
|
| 18 |
class BasicAgent:
|
| 19 |
def __init__(self):
|
| 20 |
print("BasicAgent initialized.")
|
| 21 |
self.api_url = DEFAULT_API_URL
|
| 22 |
-
self.
|
| 23 |
-
model="Qwen/Qwen2.5-72B-Instruct",
|
| 24 |
-
token=os.environ.get("HF_TOKEN")
|
| 25 |
-
)
|
| 26 |
|
| 27 |
# ---------- Utility: reversed text detection ----------
|
| 28 |
def is_reversed_text(self, text: str) -> bool:
|
|
@@ -49,17 +63,29 @@ class BasicAgent:
|
|
| 49 |
print(f"Search error: {e}")
|
| 50 |
return f"Search failed: {e}"
|
| 51 |
|
| 52 |
-
# ---------- Utility: LLM call with retry ----------
|
| 53 |
def call_llm(self, messages, max_tokens=512, retries=3):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
last_error = None
|
| 55 |
for attempt in range(retries):
|
| 56 |
try:
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
)
|
| 62 |
-
return response.choices[0].message.content.strip()
|
| 63 |
except Exception as e:
|
| 64 |
last_error = e
|
| 65 |
wait = 5 * (attempt + 1)
|
|
@@ -69,7 +95,12 @@ class BasicAgent:
|
|
| 69 |
|
| 70 |
# ---------- Utility: download attached file ----------
|
| 71 |
def fetch_file(self, task_id: str):
|
| 72 |
-
"""Downloads the file attached to a task, if any. Returns (bytes, None) or (None, error).
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
url = f"{self.api_url}/files/{task_id}"
|
| 74 |
try:
|
| 75 |
resp = requests.get(url, timeout=30)
|
|
@@ -79,16 +110,17 @@ class BasicAgent:
|
|
| 79 |
print(f"File fetch error for task {task_id}: {e}")
|
| 80 |
return None, str(e)
|
| 81 |
|
| 82 |
-
# ---------- Tool: transcribe audio ----------
|
| 83 |
-
def transcribe_audio(self, audio_bytes: bytes) -> str:
|
|
|
|
|
|
|
| 84 |
try:
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
)
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
return text.strip()
|
| 92 |
except Exception as e:
|
| 93 |
print(f"Transcription error: {e}")
|
| 94 |
return f"Transcription failed: {e}"
|
|
@@ -156,7 +188,7 @@ class BasicAgent:
|
|
| 156 |
if file_bytes:
|
| 157 |
lower_name = file_name.lower()
|
| 158 |
if lower_name.endswith((".mp3", ".wav", ".m4a")):
|
| 159 |
-
transcript = self.transcribe_audio(file_bytes)
|
| 160 |
file_context = f"Transcript of attached audio file '{file_name}':\n{transcript}"
|
| 161 |
elif lower_name.endswith((".xlsx", ".xls", ".csv")):
|
| 162 |
table_text = self.read_spreadsheet(file_bytes, file_name)
|
|
@@ -173,7 +205,9 @@ class BasicAgent:
|
|
| 173 |
else:
|
| 174 |
file_context = f"An attached file '{file_name}' was provided but its type is not supported by this agent."
|
| 175 |
else:
|
| 176 |
-
|
|
|
|
|
|
|
| 177 |
|
| 178 |
# Step 2: Web search for additional context
|
| 179 |
search_results = self.web_search(question)
|
|
@@ -186,7 +220,9 @@ class BasicAgent:
|
|
| 186 |
"Report your final answer as a concise string: a number, a short phrase, "
|
| 187 |
"or a comma separated list, with no explanation, no extra words, "
|
| 188 |
"no units unless explicitly asked for, and no leading phrases like 'The answer is'. "
|
| 189 |
-
"Answer with ONLY the final answer, nothing else."
|
|
|
|
|
|
|
| 190 |
)
|
| 191 |
|
| 192 |
user_prompt_parts = [f"Question: {question}"]
|
|
@@ -212,7 +248,10 @@ class BasicAgent:
|
|
| 212 |
return answer
|
| 213 |
|
| 214 |
|
| 215 |
-
@spaces.GPU
|
|
|
|
|
|
|
|
|
|
| 216 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
| 217 |
"""
|
| 218 |
Fetches all questions, runs the BasicAgent on them, submits all answers,
|
|
|
|
| 7 |
import inspect
|
| 8 |
import pandas as pd
|
| 9 |
import spaces
|
|
|
|
| 10 |
from duckduckgo_search import DDGS
|
| 11 |
|
| 12 |
# (Keep Constants as is)
|
| 13 |
# --- Constants ---
|
| 14 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 15 |
|
| 16 |
+
GROQ_API_URL = "https://api.groq.com/openai/v1/chat/completions"
|
| 17 |
+
GROQ_TRANSCRIBE_URL = "https://api.groq.com/openai/v1/audio/transcriptions"
|
| 18 |
+
GROQ_MODEL = "llama-3.3-70b-versatile"
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
# --- Dummy GPU function ---
|
| 22 |
+
# ZeroGPU requires at least one @spaces.GPU-decorated function to exist at
|
| 23 |
+
# startup. Our agent never actually needs a GPU (it just makes API calls),
|
| 24 |
+
# but the long evaluation loop must NOT be decorated with @spaces.GPU,
|
| 25 |
+
# because ZeroGPU enforces a short max duration per GPU call and the full
|
| 26 |
+
# 20-question loop takes much longer than that, causing it to be aborted.
|
| 27 |
+
# This tiny no-op function satisfies the startup check without limiting
|
| 28 |
+
# the runtime of the real evaluation loop below.
|
| 29 |
+
@spaces.GPU(duration=10)
|
| 30 |
+
def _zerogpu_warmup():
|
| 31 |
+
return "ok"
|
| 32 |
+
|
| 33 |
+
|
| 34 |
# --- Basic Agent Definition ---
|
| 35 |
class BasicAgent:
|
| 36 |
def __init__(self):
|
| 37 |
print("BasicAgent initialized.")
|
| 38 |
self.api_url = DEFAULT_API_URL
|
| 39 |
+
self.groq_api_key = os.environ.get("GROQ_API_KEY")
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
# ---------- Utility: reversed text detection ----------
|
| 42 |
def is_reversed_text(self, text: str) -> bool:
|
|
|
|
| 63 |
print(f"Search error: {e}")
|
| 64 |
return f"Search failed: {e}"
|
| 65 |
|
| 66 |
+
# ---------- Utility: LLM call via Groq, with retry ----------
|
| 67 |
def call_llm(self, messages, max_tokens=512, retries=3):
|
| 68 |
+
if not self.groq_api_key:
|
| 69 |
+
raise RuntimeError("GROQ_API_KEY is not set in this Space's secrets.")
|
| 70 |
+
|
| 71 |
+
headers = {
|
| 72 |
+
"Authorization": f"Bearer {self.groq_api_key}",
|
| 73 |
+
"Content-Type": "application/json",
|
| 74 |
+
}
|
| 75 |
+
payload = {
|
| 76 |
+
"model": GROQ_MODEL,
|
| 77 |
+
"messages": messages,
|
| 78 |
+
"max_tokens": max_tokens,
|
| 79 |
+
"temperature": 0,
|
| 80 |
+
}
|
| 81 |
+
|
| 82 |
last_error = None
|
| 83 |
for attempt in range(retries):
|
| 84 |
try:
|
| 85 |
+
resp = requests.post(GROQ_API_URL, headers=headers, json=payload, timeout=60)
|
| 86 |
+
resp.raise_for_status()
|
| 87 |
+
data = resp.json()
|
| 88 |
+
return data["choices"][0]["message"]["content"].strip()
|
|
|
|
|
|
|
| 89 |
except Exception as e:
|
| 90 |
last_error = e
|
| 91 |
wait = 5 * (attempt + 1)
|
|
|
|
| 95 |
|
| 96 |
# ---------- Utility: download attached file ----------
|
| 97 |
def fetch_file(self, task_id: str):
|
| 98 |
+
"""Downloads the file attached to a task, if any. Returns (bytes, None) or (None, error).
|
| 99 |
+
NOTE: as of this writing, the official course scoring Space's /files/{task_id}
|
| 100 |
+
endpoint is broken (returns 404 for every task) due to a known upstream issue,
|
| 101 |
+
so this will currently fail for all file-based questions. Left in place so it
|
| 102 |
+
will start working automatically once Hugging Face fixes the endpoint.
|
| 103 |
+
"""
|
| 104 |
url = f"{self.api_url}/files/{task_id}"
|
| 105 |
try:
|
| 106 |
resp = requests.get(url, timeout=30)
|
|
|
|
| 110 |
print(f"File fetch error for task {task_id}: {e}")
|
| 111 |
return None, str(e)
|
| 112 |
|
| 113 |
+
# ---------- Tool: transcribe audio via Groq Whisper ----------
|
| 114 |
+
def transcribe_audio(self, audio_bytes: bytes, file_name: str) -> str:
|
| 115 |
+
if not self.groq_api_key:
|
| 116 |
+
return "Transcription unavailable: GROQ_API_KEY not set."
|
| 117 |
try:
|
| 118 |
+
headers = {"Authorization": f"Bearer {self.groq_api_key}"}
|
| 119 |
+
files = {"file": (file_name, audio_bytes)}
|
| 120 |
+
data = {"model": "whisper-large-v3"}
|
| 121 |
+
resp = requests.post(GROQ_TRANSCRIBE_URL, headers=headers, files=files, data=data, timeout=60)
|
| 122 |
+
resp.raise_for_status()
|
| 123 |
+
return resp.json().get("text", "").strip()
|
|
|
|
| 124 |
except Exception as e:
|
| 125 |
print(f"Transcription error: {e}")
|
| 126 |
return f"Transcription failed: {e}"
|
|
|
|
| 188 |
if file_bytes:
|
| 189 |
lower_name = file_name.lower()
|
| 190 |
if lower_name.endswith((".mp3", ".wav", ".m4a")):
|
| 191 |
+
transcript = self.transcribe_audio(file_bytes, file_name)
|
| 192 |
file_context = f"Transcript of attached audio file '{file_name}':\n{transcript}"
|
| 193 |
elif lower_name.endswith((".xlsx", ".xls", ".csv")):
|
| 194 |
table_text = self.read_spreadsheet(file_bytes, file_name)
|
|
|
|
| 205 |
else:
|
| 206 |
file_context = f"An attached file '{file_name}' was provided but its type is not supported by this agent."
|
| 207 |
else:
|
| 208 |
+
# Known upstream issue: /files/{task_id} currently 404s for every
|
| 209 |
+
# task. Tell the LLM plainly rather than pretending we have context.
|
| 210 |
+
file_context = f"An attachment '{file_name}' was referenced but could not be downloaded (server error: {err})."
|
| 211 |
|
| 212 |
# Step 2: Web search for additional context
|
| 213 |
search_results = self.web_search(question)
|
|
|
|
| 220 |
"Report your final answer as a concise string: a number, a short phrase, "
|
| 221 |
"or a comma separated list, with no explanation, no extra words, "
|
| 222 |
"no units unless explicitly asked for, and no leading phrases like 'The answer is'. "
|
| 223 |
+
"Answer with ONLY the final answer, nothing else. "
|
| 224 |
+
"If you genuinely cannot determine the answer from the given context or your own knowledge, "
|
| 225 |
+
"answer with your single best guess rather than refusing."
|
| 226 |
)
|
| 227 |
|
| 228 |
user_prompt_parts = [f"Question: {question}"]
|
|
|
|
| 248 |
return answer
|
| 249 |
|
| 250 |
|
| 251 |
+
# NOTE: no @spaces.GPU here on purpose. The full evaluation loop can take
|
| 252 |
+
# several minutes (20 questions x search + LLM + retries), which exceeds
|
| 253 |
+
# ZeroGPU's allowed duration for a single GPU call and causes it to be
|
| 254 |
+
# aborted. Running this as plain CPU code avoids that entirely.
|
| 255 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
| 256 |
"""
|
| 257 |
Fetches all questions, runs the BasicAgent on them, submits all answers,
|