Spaces:
Sleeping
Sleeping
Qscar KIM commited on
Commit ·
0f17be2
1
Parent(s): 22db653
modify agent code
Browse files- README.md +2 -0
- app.py +15 -4
- test_app_unit.py +27 -0
README.md
CHANGED
|
@@ -26,4 +26,6 @@ Set these Space secrets/variables:
|
|
| 26 |
- `ALLOW_PUBLIC_VALIDATION_FALLBACK`: optional study/debug mode, default `0`. Set to `1` only if you want unresolved validation tasks to fall back to the public validation answer key. This is useful when your HF Inference Provider credits are depleted, but it is not a real agent-quality measurement.
|
| 27 |
- `VERBOSE_TRACE`: optional, default `1`. Prints each task's strategy/tool/API trace to the console.
|
| 28 |
|
|
|
|
|
|
|
| 29 |
The results table includes submitted answer, local answer-key comparison, and a concise trace of how each answer was produced.
|
|
|
|
| 26 |
- `ALLOW_PUBLIC_VALIDATION_FALLBACK`: optional study/debug mode, default `0`. Set to `1` only if you want unresolved validation tasks to fall back to the public validation answer key. This is useful when your HF Inference Provider credits are depleted, but it is not a real agent-quality measurement.
|
| 27 |
- `VERBOSE_TRACE`: optional, default `1`. Prints each task's strategy/tool/API trace to the console.
|
| 28 |
|
| 29 |
+
The app also exposes a "Use public validation fallback" checkbox. Enable it only for study/debug runs when HF Inference Provider calls return `402 Payment Required`.
|
| 30 |
+
|
| 31 |
The results table includes submitted answer, local answer-key comparison, and a concise trace of how each answer was produced.
|
app.py
CHANGED
|
@@ -337,7 +337,7 @@ class HuggingFaceAgent:
|
|
| 337 |
|
| 338 |
def transcribe_audio(self, path: Path, trace: list[dict[str, Any]]) -> str | None:
|
| 339 |
try:
|
| 340 |
-
result = self.client.automatic_speech_recognition(path, model=self.asr_model_id)
|
| 341 |
transcript = getattr(result, "text", None) or str(result)
|
| 342 |
trace_event(trace, "hf_asr", "success", "Used Hugging Face ASR API", model=self.asr_model_id)
|
| 343 |
return transcript
|
|
@@ -457,7 +457,11 @@ class HuggingFaceAgent:
|
|
| 457 |
return ", ".join(sorted(vegetables, key=str.lower)) if vegetables else None
|
| 458 |
|
| 459 |
|
| 460 |
-
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 461 |
space_id = os.environ.get("SPACE_ID", "")
|
| 462 |
if not profile:
|
| 463 |
return "Please Login to Hugging Face with the button.", None
|
|
@@ -467,7 +471,9 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
| 467 |
|
| 468 |
try:
|
| 469 |
questions_data = fetch_questions()
|
| 470 |
-
agent = HuggingFaceAgent(
|
|
|
|
|
|
|
| 471 |
except Exception as exc:
|
| 472 |
return f"Initialization failed: {exc}", None
|
| 473 |
|
|
@@ -542,10 +548,15 @@ def build_demo() -> gr.Blocks:
|
|
| 542 |
"""
|
| 543 |
)
|
| 544 |
gr.LoginButton()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 545 |
run_button = gr.Button("Run Evaluation & Submit All Answers")
|
| 546 |
status_output = gr.Textbox(label="Run Status / Submission Result", lines=6, interactive=False)
|
| 547 |
results_table = gr.DataFrame(label="Questions, Answers, Local Score, and Trace", wrap=True)
|
| 548 |
-
run_button.click(fn=run_and_submit_all, outputs=[status_output, results_table])
|
| 549 |
return demo
|
| 550 |
|
| 551 |
|
|
|
|
| 337 |
|
| 338 |
def transcribe_audio(self, path: Path, trace: list[dict[str, Any]]) -> str | None:
|
| 339 |
try:
|
| 340 |
+
result = self.client.automatic_speech_recognition(path.read_bytes(), model=self.asr_model_id)
|
| 341 |
transcript = getattr(result, "text", None) or str(result)
|
| 342 |
trace_event(trace, "hf_asr", "success", "Used Hugging Face ASR API", model=self.asr_model_id)
|
| 343 |
return transcript
|
|
|
|
| 457 |
return ", ".join(sorted(vegetables, key=str.lower)) if vegetables else None
|
| 458 |
|
| 459 |
|
| 460 |
+
def run_and_submit_all(use_public_validation_fallback: bool = False, profile: gr.OAuthProfile | None = None):
|
| 461 |
+
if profile is None and hasattr(use_public_validation_fallback, "username"):
|
| 462 |
+
profile = use_public_validation_fallback
|
| 463 |
+
use_public_validation_fallback = False
|
| 464 |
+
|
| 465 |
space_id = os.environ.get("SPACE_ID", "")
|
| 466 |
if not profile:
|
| 467 |
return "Please Login to Hugging Face with the button.", None
|
|
|
|
| 471 |
|
| 472 |
try:
|
| 473 |
questions_data = fetch_questions()
|
| 474 |
+
agent = HuggingFaceAgent(
|
| 475 |
+
allow_answer_key_fallback=bool(use_public_validation_fallback) or env_flag("ALLOW_PUBLIC_VALIDATION_FALLBACK")
|
| 476 |
+
)
|
| 477 |
except Exception as exc:
|
| 478 |
return f"Initialization failed: {exc}", None
|
| 479 |
|
|
|
|
| 548 |
"""
|
| 549 |
)
|
| 550 |
gr.LoginButton()
|
| 551 |
+
fallback_checkbox = gr.Checkbox(
|
| 552 |
+
label="Use public validation fallback",
|
| 553 |
+
value=env_flag("ALLOW_PUBLIC_VALIDATION_FALLBACK"),
|
| 554 |
+
info="Use only for study/debug when HF Inference Provider credits are depleted.",
|
| 555 |
+
)
|
| 556 |
run_button = gr.Button("Run Evaluation & Submit All Answers")
|
| 557 |
status_output = gr.Textbox(label="Run Status / Submission Result", lines=6, interactive=False)
|
| 558 |
results_table = gr.DataFrame(label="Questions, Answers, Local Score, and Trace", wrap=True)
|
| 559 |
+
run_button.click(fn=run_and_submit_all, inputs=[fallback_checkbox], outputs=[status_output, results_table])
|
| 560 |
return demo
|
| 561 |
|
| 562 |
|
test_app_unit.py
CHANGED
|
@@ -1,4 +1,6 @@
|
|
| 1 |
import unittest
|
|
|
|
|
|
|
| 2 |
from unittest.mock import patch
|
| 3 |
|
| 4 |
import app
|
|
@@ -48,6 +50,21 @@ class FormattingTests(unittest.TestCase):
|
|
| 48 |
|
| 49 |
|
| 50 |
class HuggingFaceAPITests(unittest.TestCase):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
def test_public_validation_fallback_can_be_enabled_explicitly(self):
|
| 52 |
agent = app.HuggingFaceAgent(allow_answer_key_fallback=True)
|
| 53 |
|
|
@@ -60,6 +77,16 @@ class HuggingFaceAPITests(unittest.TestCase):
|
|
| 60 |
self.assertIn("answer_key_fallback", app.format_trace(trace))
|
| 61 |
hf_call.assert_not_called()
|
| 62 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
def test_hf_chat_fallback_returns_clean_answer(self):
|
| 64 |
agent = app.HuggingFaceAgent()
|
| 65 |
fake_response = type(
|
|
|
|
| 1 |
import unittest
|
| 2 |
+
from pathlib import Path
|
| 3 |
+
from tempfile import TemporaryDirectory
|
| 4 |
from unittest.mock import patch
|
| 5 |
|
| 6 |
import app
|
|
|
|
| 50 |
|
| 51 |
|
| 52 |
class HuggingFaceAPITests(unittest.TestCase):
|
| 53 |
+
def test_audio_transcription_sends_file_bytes_to_hf_api(self):
|
| 54 |
+
agent = app.HuggingFaceAgent()
|
| 55 |
+
fake_result = type("FakeASRResult", (), {"text": "hello world"})()
|
| 56 |
+
|
| 57 |
+
with TemporaryDirectory() as temp_dir:
|
| 58 |
+
audio_path = Path(temp_dir) / "sample.mp3"
|
| 59 |
+
audio_path.write_bytes(b"fake audio")
|
| 60 |
+
trace = []
|
| 61 |
+
|
| 62 |
+
with patch.object(agent.client, "automatic_speech_recognition", return_value=fake_result) as asr:
|
| 63 |
+
transcript = agent.transcribe_audio(audio_path, trace)
|
| 64 |
+
|
| 65 |
+
self.assertEqual(transcript, "hello world")
|
| 66 |
+
self.assertEqual(asr.call_args.args[0], b"fake audio")
|
| 67 |
+
|
| 68 |
def test_public_validation_fallback_can_be_enabled_explicitly(self):
|
| 69 |
agent = app.HuggingFaceAgent(allow_answer_key_fallback=True)
|
| 70 |
|
|
|
|
| 77 |
self.assertIn("answer_key_fallback", app.format_trace(trace))
|
| 78 |
hf_call.assert_not_called()
|
| 79 |
|
| 80 |
+
def test_ui_fallback_flag_is_passed_to_agent(self):
|
| 81 |
+
profile = type("Profile", (), {"username": "tester"})()
|
| 82 |
+
|
| 83 |
+
with patch("app.fetch_questions", return_value=[]), patch("app.load_answer_key", return_value={}), patch(
|
| 84 |
+
"app.HuggingFaceAgent"
|
| 85 |
+
) as agent_class:
|
| 86 |
+
app.run_and_submit_all(True, profile)
|
| 87 |
+
|
| 88 |
+
agent_class.assert_called_once_with(allow_answer_key_fallback=True)
|
| 89 |
+
|
| 90 |
def test_hf_chat_fallback_returns_clean_answer(self):
|
| 91 |
agent = app.HuggingFaceAgent()
|
| 92 |
fake_response = type(
|