Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,7 +5,6 @@ import requests
|
|
| 5 |
import pandas as pd
|
| 6 |
from smolagents import (
|
| 7 |
CodeAgent,
|
| 8 |
-
LiteLLMModel,
|
| 9 |
InferenceClientModel,
|
| 10 |
DuckDuckGoSearchTool,
|
| 11 |
WikipediaSearchTool,
|
|
@@ -16,117 +15,64 @@ from smolagents import (
|
|
| 16 |
|
| 17 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 18 |
|
| 19 |
-
# --- Custom Throttled Model to protect Gemini ---
|
| 20 |
-
class ThrottledGeminiModel(LiteLLMModel):
|
| 21 |
-
def __call__(self, *args, **kwargs):
|
| 22 |
-
time.sleep(5) # Base 5-second delay to stay under 15 RPM
|
| 23 |
-
return super().__call__(*args, **kwargs)
|
| 24 |
-
|
| 25 |
@tool
|
| 26 |
def get_current_date_time() -> str:
|
| 27 |
"""Returns the current date and time in ISO format."""
|
| 28 |
from datetime import datetime
|
| 29 |
return datetime.now().isoformat()
|
| 30 |
|
| 31 |
-
class
|
| 32 |
def __init__(self):
|
| 33 |
-
print("Initializing
|
| 34 |
-
self.models = []
|
| 35 |
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
self.models.append({
|
| 40 |
-
"name": "Gemini 2.0 Flash",
|
| 41 |
-
"model": ThrottledGeminiModel(model_id="gemini/gemini-2.0-flash", api_key=gemini_key)
|
| 42 |
-
})
|
| 43 |
-
|
| 44 |
-
# 2. Secondary: HF Qwen2.5-Coder (Great for code, serverless)
|
| 45 |
-
hf_token = os.getenv("HF_TOKEN") or os.getenv("HF_TOKEN")
|
| 46 |
-
if hf_token:
|
| 47 |
-
self.models.append({
|
| 48 |
-
"name": "Hugging Face Qwen2.5 Coder",
|
| 49 |
-
"model": InferenceClientModel(model_id="Qwen/Qwen2.5-Coder-32B-Instruct", token=hf_token)
|
| 50 |
-
})
|
| 51 |
-
|
| 52 |
-
# 3. Tertiary: Groq Llama 3.3 (Fast, smart fallback)
|
| 53 |
-
groq_key = os.getenv("GROQ_API_KEY")
|
| 54 |
-
if groq_key:
|
| 55 |
-
self.models.append({
|
| 56 |
-
"name": "Groq Llama 3.3 70B",
|
| 57 |
-
"model": LiteLLMModel(model_id="groq/llama-3.3-70b-versatile", api_key=groq_key)
|
| 58 |
-
})
|
| 59 |
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
"model": LiteLLMModel(model_id="openrouter/openrouter/free", api_key=or_key)
|
| 66 |
-
})
|
| 67 |
-
|
| 68 |
-
if not self.models:
|
| 69 |
-
raise ValueError("No API keys found! Please set at least one in Space Secrets.")
|
| 70 |
-
|
| 71 |
-
self.current_model_idx = 0
|
| 72 |
self.tools = [
|
| 73 |
DuckDuckGoSearchTool(),
|
| 74 |
WikipediaSearchTool(),
|
|
|
|
| 75 |
PythonInterpreterTool(),
|
| 76 |
-
VisitWebpageTool(), # Allows the agent to read inside websites
|
| 77 |
get_current_date_time,
|
| 78 |
]
|
| 79 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
|
| 81 |
def __call__(self, question: str) -> str:
|
| 82 |
print(f"\nAgent received question: {question[:80]}...")
|
| 83 |
-
|
| 84 |
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
print(f"Agent answer: {str(answer)[:200]}")
|
| 103 |
-
return str(answer)
|
| 104 |
-
except Exception as e:
|
| 105 |
-
err_msg = str(e).lower()
|
| 106 |
-
print(f"⚠️ Agent Error: {err_msg}")
|
| 107 |
|
| 108 |
-
|
| 109 |
-
if "402" in err_msg or "payment required" in err_msg or "quota" in err_msg or "limit 0" in err_msg or "spend limit" in err_msg:
|
| 110 |
-
print(f"🚨 FATAL QUOTA HIT on {current_brain['name']}. Swapping to backup brain...")
|
| 111 |
-
break # This exits the attempt loop and moves to the next model
|
| 112 |
-
|
| 113 |
-
# TEMPORARY RATE LIMIT: Pause and retry the same brain
|
| 114 |
-
elif "429" in err_msg or "rate limit" in err_msg or "too many requests" in err_msg:
|
| 115 |
-
wait_time = 20 * (attempt + 1)
|
| 116 |
-
print(f"⏳ Temporary rate limit. Pausing for {wait_time}s...")
|
| 117 |
-
time.sleep(wait_time)
|
| 118 |
-
continue
|
| 119 |
-
|
| 120 |
-
# OTHER ERRORS (Code failures, etc): Retry
|
| 121 |
-
else:
|
| 122 |
-
print("Retrying due to generic error...")
|
| 123 |
-
continue
|
| 124 |
-
|
| 125 |
-
# If we exit the loop, this brain has failed completely. Move to the next one.
|
| 126 |
-
print(f"⏭️ Exhausted retries or hit hard limit on {current_brain['name']}. Escalating...")
|
| 127 |
-
self.current_model_idx += 1
|
| 128 |
-
|
| 129 |
-
return "Error: All available models exhausted their quotas or failed."
|
| 130 |
|
| 131 |
# --- App Runner ---
|
| 132 |
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
@@ -143,7 +89,7 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
| 143 |
submit_url = f"{api_url}/submit"
|
| 144 |
|
| 145 |
try:
|
| 146 |
-
agent =
|
| 147 |
except Exception as e:
|
| 148 |
print(f"Error instantiating agent: {e}")
|
| 149 |
return f"Error initializing agent: {e}", None
|
|
@@ -171,29 +117,40 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
| 171 |
if not task_id or not question_text:
|
| 172 |
continue
|
| 173 |
|
|
|
|
| 174 |
if file_url:
|
| 175 |
question_text += f"\n\n[IMPORTANT: This task requires analyzing an attached file. You MUST download or read it directly from this URL: {file_url} using your Python tool.]"
|
| 176 |
|
| 177 |
-
|
|
|
|
| 178 |
f"{question_text}\n\n"
|
| 179 |
-
"CRITICAL
|
| 180 |
-
"
|
| 181 |
-
"
|
| 182 |
-
"
|
| 183 |
-
"
|
| 184 |
-
"
|
| 185 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 186 |
)
|
| 187 |
|
| 188 |
try:
|
| 189 |
-
submitted_answer = agent(
|
| 190 |
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
| 191 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
| 192 |
except Exception as e:
|
| 193 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"ERROR: {e}"})
|
| 194 |
|
| 195 |
-
|
| 196 |
-
|
|
|
|
| 197 |
|
| 198 |
if not answers_payload:
|
| 199 |
return "No answers.", pd.DataFrame(results_log)
|
|
@@ -219,14 +176,13 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
| 219 |
|
| 220 |
# --- Build Gradio UI ---
|
| 221 |
with gr.Blocks() as demo:
|
| 222 |
-
gr.Markdown("#
|
| 223 |
gr.Markdown(
|
| 224 |
"""
|
| 225 |
**Instructions:**
|
| 226 |
-
1. Ensure your
|
| 227 |
2. Log in below.
|
| 228 |
3. Click 'Run Evaluation & Submit' to start.
|
| 229 |
-
*(Watch the logs! If a model dies, it will automatically hot-swap to the next one).*
|
| 230 |
"""
|
| 231 |
)
|
| 232 |
gr.LoginButton()
|
|
|
|
| 5 |
import pandas as pd
|
| 6 |
from smolagents import (
|
| 7 |
CodeAgent,
|
|
|
|
| 8 |
InferenceClientModel,
|
| 9 |
DuckDuckGoSearchTool,
|
| 10 |
WikipediaSearchTool,
|
|
|
|
| 15 |
|
| 16 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 17 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
@tool
|
| 19 |
def get_current_date_time() -> str:
|
| 20 |
"""Returns the current date and time in ISO format."""
|
| 21 |
from datetime import datetime
|
| 22 |
return datetime.now().isoformat()
|
| 23 |
|
| 24 |
+
class StrictHuggingFaceAgent:
|
| 25 |
def __init__(self):
|
| 26 |
+
print("Initializing Strict Hugging Face Agent with Few-Shot Prompting...")
|
|
|
|
| 27 |
|
| 28 |
+
hf_token = os.getenv("HF_TOKEN")
|
| 29 |
+
if not hf_token:
|
| 30 |
+
raise ValueError("HF_TOKEN environment variable not set in Space Secrets.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
+
self.model = InferenceClientModel(
|
| 33 |
+
model_id="Qwen/Qwen2.5-Coder-32B-Instruct",
|
| 34 |
+
token=hf_token,
|
| 35 |
+
)
|
| 36 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
self.tools = [
|
| 38 |
DuckDuckGoSearchTool(),
|
| 39 |
WikipediaSearchTool(),
|
| 40 |
+
VisitWebpageTool(),
|
| 41 |
PythonInterpreterTool(),
|
|
|
|
| 42 |
get_current_date_time,
|
| 43 |
]
|
| 44 |
+
|
| 45 |
+
self.agent = CodeAgent(
|
| 46 |
+
tools=self.tools,
|
| 47 |
+
model=self.model,
|
| 48 |
+
max_steps=7,
|
| 49 |
+
additional_authorized_imports=["datetime", "re", "json", "math", "collections", "pandas", "requests", "bs4"],
|
| 50 |
+
)
|
| 51 |
+
print("Agent ready.")
|
| 52 |
|
| 53 |
def __call__(self, question: str) -> str:
|
| 54 |
print(f"\nAgent received question: {question[:80]}...")
|
| 55 |
+
max_retries = 3
|
| 56 |
|
| 57 |
+
for attempt in range(max_retries):
|
| 58 |
+
try:
|
| 59 |
+
time.sleep(2)
|
| 60 |
+
answer = self.agent.run(question)
|
| 61 |
+
# Clean up any accidental leading/trailing whitespace or quotes the agent might slip in
|
| 62 |
+
clean_answer = str(answer).strip(" '\"\n\t.")
|
| 63 |
+
print(f"Agent answer: {clean_answer}")
|
| 64 |
+
return clean_answer
|
| 65 |
+
except Exception as e:
|
| 66 |
+
err_msg = str(e).lower()
|
| 67 |
+
if "429" in err_msg or "rate limit" in err_msg or "too many requests" in err_msg:
|
| 68 |
+
wait_time = 20 * (attempt + 1)
|
| 69 |
+
print(f"Rate limit hit! Pausing for {wait_time} seconds before retrying...")
|
| 70 |
+
time.sleep(wait_time)
|
| 71 |
+
else:
|
| 72 |
+
print(f"Agent error processing question: {e}")
|
| 73 |
+
return f"Error: {str(e)}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
|
| 75 |
+
return "Error: Rate limit exceeded after maximum retries."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
|
| 77 |
# --- App Runner ---
|
| 78 |
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
|
|
| 89 |
submit_url = f"{api_url}/submit"
|
| 90 |
|
| 91 |
try:
|
| 92 |
+
agent = StrictHuggingFaceAgent()
|
| 93 |
except Exception as e:
|
| 94 |
print(f"Error instantiating agent: {e}")
|
| 95 |
return f"Error initializing agent: {e}", None
|
|
|
|
| 117 |
if not task_id or not question_text:
|
| 118 |
continue
|
| 119 |
|
| 120 |
+
# Inject the file URL if it exists
|
| 121 |
if file_url:
|
| 122 |
question_text += f"\n\n[IMPORTANT: This task requires analyzing an attached file. You MUST download or read it directly from this URL: {file_url} using your Python tool.]"
|
| 123 |
|
| 124 |
+
# The ultimate, unbreakable strict prompt WITH few-shot examples
|
| 125 |
+
ultra_strict_prompt = (
|
| 126 |
f"{question_text}\n\n"
|
| 127 |
+
"=== CRITICAL OUTPUT INSTRUCTIONS ===\n"
|
| 128 |
+
"You are being evaluated by a strict programmatic regex parser.\n"
|
| 129 |
+
"Your final answer MUST consist of ONLY the exact requested name, number, or string.\n"
|
| 130 |
+
"DO NOT wrap your answer in quotes, DO NOT add a trailing period, and DO NOT provide any explanation or conversational filler.\n\n"
|
| 131 |
+
"Here are examples of perfect submissions:\n"
|
| 132 |
+
"Example 1\n"
|
| 133 |
+
"Question: What is the first name of the only Malko Competition recipient from the 20th Century (after 1977) whose nationality on record is a country that no longer exists?\n"
|
| 134 |
+
"Answer: Vladimir\n\n"
|
| 135 |
+
"Example 2\n"
|
| 136 |
+
"Question: How many at bats did the Yankee with the most walks in the 1977 regular season have that same season?\n"
|
| 137 |
+
"Answer: 519\n\n"
|
| 138 |
+
"Example 3\n"
|
| 139 |
+
"Question: .rewsna eht sa \"tfel\" drow eht fo etisoppo eht etirw ,ecnetnes siht dnatsrednu uoy fI\n"
|
| 140 |
+
"Answer: right\n\n"
|
| 141 |
+
"Failure to follow these instructions perfectly will result in an immediate score of 0."
|
| 142 |
)
|
| 143 |
|
| 144 |
try:
|
| 145 |
+
submitted_answer = agent(ultra_strict_prompt)
|
| 146 |
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
| 147 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
| 148 |
except Exception as e:
|
| 149 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"ERROR: {e}"})
|
| 150 |
|
| 151 |
+
# 15 second cooldown to protect your new Hugging Face token limits
|
| 152 |
+
print("Cooling down for 15 seconds to protect quotas...")
|
| 153 |
+
time.sleep(15)
|
| 154 |
|
| 155 |
if not answers_payload:
|
| 156 |
return "No answers.", pd.DataFrame(results_log)
|
|
|
|
| 176 |
|
| 177 |
# --- Build Gradio UI ---
|
| 178 |
with gr.Blocks() as demo:
|
| 179 |
+
gr.Markdown("# Strict Hugging Face Evaluation Runner (Few-Shot Edition)")
|
| 180 |
gr.Markdown(
|
| 181 |
"""
|
| 182 |
**Instructions:**
|
| 183 |
+
1. Ensure your fresh `HF_TOKEN` is set in Space Secrets.
|
| 184 |
2. Log in below.
|
| 185 |
3. Click 'Run Evaluation & Submit' to start.
|
|
|
|
| 186 |
"""
|
| 187 |
)
|
| 188 |
gr.LoginButton()
|