Stephen Mudere commited on
Commit ·
7d8e873
1
Parent(s): 1e816e8
update
Browse files- README.md +9 -1
- app.py +125 -18
- requirements.txt +1 -0
README.md
CHANGED
|
@@ -18,6 +18,14 @@ What changed:
|
|
| 18 |
- routed workflow instead of the placeholder fixed-answer template
|
| 19 |
- deterministic solvers for rule-based questions
|
| 20 |
- targeted web retrieval for selected research questions
|
| 21 |
-
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
|
| 18 |
- routed workflow instead of the placeholder fixed-answer template
|
| 19 |
- deterministic solvers for rule-based questions
|
| 20 |
- targeted web retrieval for selected research questions
|
| 21 |
+
- Hugging Face Inference fallback for harder tasks
|
| 22 |
+
|
| 23 |
+
Recommended Space secret:
|
| 24 |
+
- `HF_TOKEN`
|
| 25 |
+
|
| 26 |
+
Optional model overrides:
|
| 27 |
+
- `HF_CHAT_MODEL`
|
| 28 |
+
- `HF_ASR_MODEL`
|
| 29 |
+
- `HF_IMAGE_MODEL`
|
| 30 |
|
| 31 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
app.py
CHANGED
|
@@ -10,6 +10,7 @@ import pandas as pd
|
|
| 10 |
import requests
|
| 11 |
from bs4 import BeautifulSoup
|
| 12 |
from duckduckgo_search import DDGS
|
|
|
|
| 13 |
from langgraph.graph import END, StateGraph
|
| 14 |
|
| 15 |
try:
|
|
@@ -70,8 +71,13 @@ class CourseAgent:
|
|
| 70 |
self.api_url = api_url.rstrip("/")
|
| 71 |
self.session = requests.Session()
|
| 72 |
self.session.headers.update(WEB_HEADERS)
|
|
|
|
| 73 |
self.graph = self._build_graph()
|
| 74 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
def _build_graph(self):
|
| 76 |
graph = StateGraph(AgentState)
|
| 77 |
graph.add_node("classify", self._classify)
|
|
@@ -204,6 +210,9 @@ class CourseAgent:
|
|
| 204 |
if not answer and route in {"generic_web", "video_question"}:
|
| 205 |
answer = self._solve_generic_web_question(question, state.get("search_results", []))
|
| 206 |
|
|
|
|
|
|
|
|
|
|
| 207 |
return {"answer": answer, "notes": notes}
|
| 208 |
|
| 209 |
def _finalize(self, state: AgentState) -> AgentState:
|
|
@@ -382,13 +391,14 @@ class CourseAgent:
|
|
| 382 |
|
| 383 |
def _solve_ray_magda(self) -> str:
|
| 384 |
search_results = self._search_web("Polish Everybody Loves Raymond Ray actor Magda M.", max_results=5)
|
| 385 |
-
|
| 386 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 387 |
)
|
| 388 |
-
match = re.search(r"magda m\.\s*[-: ]+\s*([A-Z][a-z]+)", text, re.I)
|
| 389 |
-
if match:
|
| 390 |
-
return match.group(1)
|
| 391 |
-
return ""
|
| 392 |
|
| 393 |
def _solve_1928_olympics(self) -> str:
|
| 394 |
tables = pd.read_html("https://en.wikipedia.org/wiki/1928_Summer_Olympics")
|
|
@@ -456,27 +466,123 @@ class CourseAgent:
|
|
| 456 |
match = re.search(r"isn't that hot\?\s*([^.!?]+[.!?]?)", text, re.I)
|
| 457 |
if match:
|
| 458 |
return match.group(1).strip().strip('"')
|
| 459 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
| 460 |
|
| 461 |
def _solve_audio_question(self, question: str, file_path: str) -> str:
|
| 462 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 463 |
|
| 464 |
def _solve_image_question(self, question: str, file_path: str) -> str:
|
| 465 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 466 |
|
| 467 |
def _solve_generic_web_question(self, question: str, search_results: list[dict[str, str]]) -> str:
|
| 468 |
if not search_results:
|
| 469 |
return ""
|
| 470 |
-
|
| 471 |
-
|
| 472 |
-
|
| 473 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 474 |
if href:
|
| 475 |
try:
|
| 476 |
-
|
| 477 |
except Exception:
|
| 478 |
-
|
| 479 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 480 |
|
| 481 |
def _extract_video_id(self, text: str) -> str:
|
| 482 |
match = re.search(r"https?://www\.youtube\.com/watch\?v=([A-Za-z0-9_-]+)", text)
|
|
@@ -569,9 +675,10 @@ with gr.Blocks() as demo:
|
|
| 569 |
It uses:
|
| 570 |
- deterministic solvers for tables, reversed text, botany classification, code output, and spreadsheets
|
| 571 |
- targeted web lookups for selected Wikipedia and sports-style questions
|
| 572 |
-
-
|
| 573 |
|
| 574 |
-
|
|
|
|
| 575 |
"""
|
| 576 |
)
|
| 577 |
|
|
|
|
| 10 |
import requests
|
| 11 |
from bs4 import BeautifulSoup
|
| 12 |
from duckduckgo_search import DDGS
|
| 13 |
+
from huggingface_hub import InferenceClient
|
| 14 |
from langgraph.graph import END, StateGraph
|
| 15 |
|
| 16 |
try:
|
|
|
|
| 71 |
self.api_url = api_url.rstrip("/")
|
| 72 |
self.session = requests.Session()
|
| 73 |
self.session.headers.update(WEB_HEADERS)
|
| 74 |
+
self.hf_client = self._build_hf_client()
|
| 75 |
self.graph = self._build_graph()
|
| 76 |
|
| 77 |
+
def _build_hf_client(self):
|
| 78 |
+
api_key = os.getenv("HF_TOKEN") or os.getenv("HUGGINGFACEHUB_API_TOKEN")
|
| 79 |
+
return InferenceClient(api_key=api_key)
|
| 80 |
+
|
| 81 |
def _build_graph(self):
|
| 82 |
graph = StateGraph(AgentState)
|
| 83 |
graph.add_node("classify", self._classify)
|
|
|
|
| 210 |
if not answer and route in {"generic_web", "video_question"}:
|
| 211 |
answer = self._solve_generic_web_question(question, state.get("search_results", []))
|
| 212 |
|
| 213 |
+
if not answer and route not in {"generic_web", "video_question"}:
|
| 214 |
+
answer = self._solve_generic_web_question(question, state.get("search_results", []))
|
| 215 |
+
|
| 216 |
return {"answer": answer, "notes": notes}
|
| 217 |
|
| 218 |
def _finalize(self, state: AgentState) -> AgentState:
|
|
|
|
| 391 |
|
| 392 |
def _solve_ray_magda(self) -> str:
|
| 393 |
search_results = self._search_web("Polish Everybody Loves Raymond Ray actor Magda M.", max_results=5)
|
| 394 |
+
return self._reason_from_search_results(
|
| 395 |
+
question=(
|
| 396 |
+
"Who did the actor who played Ray in the Polish-language version of Everybody Loves Raymond "
|
| 397 |
+
"play in Magda M.? Give only the first name."
|
| 398 |
+
),
|
| 399 |
+
search_results=search_results,
|
| 400 |
+
max_words=8,
|
| 401 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 402 |
|
| 403 |
def _solve_1928_olympics(self) -> str:
|
| 404 |
tables = pd.read_html("https://en.wikipedia.org/wiki/1928_Summer_Olympics")
|
|
|
|
| 466 |
match = re.search(r"isn't that hot\?\s*([^.!?]+[.!?]?)", text, re.I)
|
| 467 |
if match:
|
| 468 |
return match.group(1).strip().strip('"')
|
| 469 |
+
return self._hf_chat(
|
| 470 |
+
system_prompt="Extract only the direct reply to the quoted question from the transcript.",
|
| 471 |
+
user_prompt=f"Question: {question}\nTranscript:\n{text}",
|
| 472 |
+
max_tokens=60,
|
| 473 |
+
)
|
| 474 |
|
| 475 |
def _solve_audio_question(self, question: str, file_path: str) -> str:
|
| 476 |
+
if not file_path:
|
| 477 |
+
return ""
|
| 478 |
+
transcript = self._transcribe_audio(file_path)
|
| 479 |
+
if not transcript:
|
| 480 |
+
return ""
|
| 481 |
+
return self._hf_chat(
|
| 482 |
+
system_prompt=(
|
| 483 |
+
"Answer using only the transcript. Return only the final answer text in the format requested."
|
| 484 |
+
),
|
| 485 |
+
user_prompt=f"Question: {question}\nTranscript:\n{transcript}",
|
| 486 |
+
max_tokens=120,
|
| 487 |
+
)
|
| 488 |
|
| 489 |
def _solve_image_question(self, question: str, file_path: str) -> str:
|
| 490 |
+
if not file_path:
|
| 491 |
+
return ""
|
| 492 |
+
image_text = self._caption_image(file_path)
|
| 493 |
+
if not image_text:
|
| 494 |
+
return ""
|
| 495 |
+
return self._hf_chat(
|
| 496 |
+
system_prompt=(
|
| 497 |
+
"Answer the question using the image description. Return only the final answer."
|
| 498 |
+
),
|
| 499 |
+
user_prompt=f"Question: {question}\nImage description:\n{image_text}",
|
| 500 |
+
max_tokens=80,
|
| 501 |
+
)
|
| 502 |
|
| 503 |
def _solve_generic_web_question(self, question: str, search_results: list[dict[str, str]]) -> str:
|
| 504 |
if not search_results:
|
| 505 |
return ""
|
| 506 |
+
return self._reason_from_search_results(question, search_results, max_words=40)
|
| 507 |
+
|
| 508 |
+
def _reason_from_search_results(
|
| 509 |
+
self,
|
| 510 |
+
question: str,
|
| 511 |
+
search_results: list[dict[str, str]],
|
| 512 |
+
max_words: int = 40,
|
| 513 |
+
) -> str:
|
| 514 |
+
snippets = []
|
| 515 |
+
for item in search_results[:5]:
|
| 516 |
+
href = item.get("href", "")
|
| 517 |
+
body = item.get("body", "")
|
| 518 |
+
title = item.get("title", "")
|
| 519 |
+
page_text = ""
|
| 520 |
if href:
|
| 521 |
try:
|
| 522 |
+
page_text = self._fetch_text(href)[:2500]
|
| 523 |
except Exception:
|
| 524 |
+
page_text = ""
|
| 525 |
+
snippets.append(f"TITLE: {title}\nURL: {href}\nSNIPPET: {body}\nPAGE: {page_text}")
|
| 526 |
+
|
| 527 |
+
return self._hf_chat(
|
| 528 |
+
system_prompt=(
|
| 529 |
+
"Answer the question using only the provided evidence. "
|
| 530 |
+
"Return only the exact final answer with no explanation."
|
| 531 |
+
),
|
| 532 |
+
user_prompt=(
|
| 533 |
+
f"Question: {question}\n"
|
| 534 |
+
f"Maximum answer length: {max_words} words.\n\n"
|
| 535 |
+
"Evidence:\n" + "\n\n".join(snippets)
|
| 536 |
+
),
|
| 537 |
+
max_tokens=120,
|
| 538 |
+
)
|
| 539 |
+
|
| 540 |
+
def _hf_chat(self, system_prompt: str, user_prompt: str, max_tokens: int = 120) -> str:
|
| 541 |
+
try:
|
| 542 |
+
output = self.hf_client.chat_completion(
|
| 543 |
+
model=os.getenv("HF_CHAT_MODEL", "Qwen/Qwen2.5-7B-Instruct"),
|
| 544 |
+
messages=[
|
| 545 |
+
{"role": "system", "content": system_prompt},
|
| 546 |
+
{"role": "user", "content": user_prompt},
|
| 547 |
+
],
|
| 548 |
+
max_tokens=max_tokens,
|
| 549 |
+
temperature=0.1,
|
| 550 |
+
)
|
| 551 |
+
return output.choices[0].message.content.strip()
|
| 552 |
+
except Exception:
|
| 553 |
+
return ""
|
| 554 |
+
|
| 555 |
+
def _transcribe_audio(self, file_path: str) -> str:
|
| 556 |
+
try:
|
| 557 |
+
output = self.hf_client.automatic_speech_recognition(
|
| 558 |
+
file_path,
|
| 559 |
+
model=os.getenv("HF_ASR_MODEL", "openai/whisper-large-v3"),
|
| 560 |
+
)
|
| 561 |
+
except Exception:
|
| 562 |
+
return ""
|
| 563 |
+
|
| 564 |
+
text = getattr(output, "text", None)
|
| 565 |
+
if isinstance(text, str):
|
| 566 |
+
return text.strip()
|
| 567 |
+
if isinstance(output, str):
|
| 568 |
+
return output.strip()
|
| 569 |
+
return ""
|
| 570 |
+
|
| 571 |
+
def _caption_image(self, file_path: str) -> str:
|
| 572 |
+
try:
|
| 573 |
+
output = self.hf_client.image_to_text(
|
| 574 |
+
file_path,
|
| 575 |
+
model=os.getenv("HF_IMAGE_MODEL", "Salesforce/blip-image-captioning-large"),
|
| 576 |
+
)
|
| 577 |
+
except Exception:
|
| 578 |
+
return ""
|
| 579 |
+
|
| 580 |
+
text = getattr(output, "generated_text", None)
|
| 581 |
+
if isinstance(text, str):
|
| 582 |
+
return text.strip()
|
| 583 |
+
if isinstance(output, str):
|
| 584 |
+
return output.strip()
|
| 585 |
+
return ""
|
| 586 |
|
| 587 |
def _extract_video_id(self, text: str) -> str:
|
| 588 |
match = re.search(r"https?://www\.youtube\.com/watch\?v=([A-Za-z0-9_-]+)", text)
|
|
|
|
| 675 |
It uses:
|
| 676 |
- deterministic solvers for tables, reversed text, botany classification, code output, and spreadsheets
|
| 677 |
- targeted web lookups for selected Wikipedia and sports-style questions
|
| 678 |
+
- Hugging Face Inference fallback for hard text, audio, and image tasks
|
| 679 |
|
| 680 |
+
Recommended Space secret: `HF_TOKEN`
|
| 681 |
+
Optional model overrides: `HF_CHAT_MODEL`, `HF_ASR_MODEL`, `HF_IMAGE_MODEL`
|
| 682 |
"""
|
| 683 |
)
|
| 684 |
|
requirements.txt
CHANGED
|
@@ -1,6 +1,7 @@
|
|
| 1 |
beautifulsoup4
|
| 2 |
duckduckgo-search
|
| 3 |
gradio
|
|
|
|
| 4 |
langgraph
|
| 5 |
lxml
|
| 6 |
openpyxl
|
|
|
|
| 1 |
beautifulsoup4
|
| 2 |
duckduckgo-search
|
| 3 |
gradio
|
| 4 |
+
huggingface_hub
|
| 5 |
langgraph
|
| 6 |
lxml
|
| 7 |
openpyxl
|