Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -48,7 +48,10 @@ from tenacity import (
|
|
| 48 |
)
|
| 49 |
import httpx
|
| 50 |
|
| 51 |
-
from scimago_index import
|
|
|
|
|
|
|
|
|
|
| 52 |
import openalex
|
| 53 |
from prompts import (
|
| 54 |
JOURNAL_SYSTEM_PROMPT, LICENSE_SYSTEM_PROMPT, REPO_SYSTEM_PROMPT,
|
|
@@ -1413,6 +1416,38 @@ async def browse_subject_debug(req: SubjectRequest):
|
|
| 1413 |
return {"ok": False, "results": results}
|
| 1414 |
|
| 1415 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1416 |
@app.post("/api/browse-subject")
|
| 1417 |
async def browse_subject(req: SubjectRequest, request: Request):
|
| 1418 |
# Guard: ensure SCImago index is loaded (cold-start protection)
|
|
@@ -1425,9 +1460,9 @@ async def browse_subject(req: SubjectRequest, request: Request):
|
|
| 1425 |
try:
|
| 1426 |
parsed = await call_openai(
|
| 1427 |
client,
|
| 1428 |
-
|
| 1429 |
f"{lang_instruction(req.language)}Subject input: {req.subject.strip()}",
|
| 1430 |
-
max_tokens=
|
| 1431 |
model=MODEL_LIGHT,
|
| 1432 |
)
|
| 1433 |
except RateLimitError:
|
|
@@ -1456,11 +1491,43 @@ async def browse_subject(req: SubjectRequest, request: Request):
|
|
| 1456 |
"journals": [],
|
| 1457 |
}}
|
| 1458 |
|
| 1459 |
-
|
| 1460 |
-
|
| 1461 |
-
|
| 1462 |
-
|
| 1463 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1464 |
|
| 1465 |
if not journals:
|
| 1466 |
# A recognised subject with nothing behind it in SCImago. This is the
|
|
@@ -1493,8 +1560,13 @@ async def browse_subject(req: SubjectRequest, request: Request):
|
|
| 1493 |
})
|
| 1494 |
|
| 1495 |
msg = user_message
|
| 1496 |
-
if fallback_used
|
| 1497 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1498 |
|
| 1499 |
return {"result": {
|
| 1500 |
"confidence": confidence,
|
|
|
|
| 48 |
)
|
| 49 |
import httpx
|
| 50 |
|
| 51 |
+
from scimago_index import (
|
| 52 |
+
lookup_any, load_scimago, search_by_subject, load_subject_index,
|
| 53 |
+
subject_vocabulary, is_real_category, is_real_area, suggest_categories,
|
| 54 |
+
)
|
| 55 |
import openalex
|
| 56 |
from prompts import (
|
| 57 |
JOURNAL_SYSTEM_PROMPT, LICENSE_SYSTEM_PROMPT, REPO_SYSTEM_PROMPT,
|
|
|
|
| 1416 |
return {"ok": False, "results": results}
|
| 1417 |
|
| 1418 |
|
| 1419 |
+
# ββ Subject vocabulary injection βββββββββββββββββββββββββββββββββββββββββββ
|
| 1420 |
+
#
|
| 1421 |
+
# The LLM used to be asked for an "Exact SCImago category name" without being
|
| 1422 |
+
# shown what the exact names are, so it generated plausible ones. "Crime and
|
| 1423 |
+
# Society" reads like a SCImago category; SCImago has no category containing
|
| 1424 |
+
# the word "crime". The invented name matched nothing, the search fell through
|
| 1425 |
+
# to a substring test on the area, and a criminology query returned the
|
| 1426 |
+
# Astrophysical Journal.
|
| 1427 |
+
#
|
| 1428 |
+
# Same pattern as the repository finder: the model chooses from a closed list
|
| 1429 |
+
# and the server owns the facts. 310 names is roughly 2.5k tokens on
|
| 1430 |
+
# gpt-4.1-nano β about $0.000025 a call, cheap enough not to think about.
|
| 1431 |
+
|
| 1432 |
+
_SUBJECT_VOCAB_BLOCK: Optional[str] = None
|
| 1433 |
+
|
| 1434 |
+
|
| 1435 |
+
def subject_prompt_with_vocabulary() -> str:
|
| 1436 |
+
"""SUBJECT_NORMALISE_PROMPT plus the real category and area names."""
|
| 1437 |
+
global _SUBJECT_VOCAB_BLOCK
|
| 1438 |
+
if _SUBJECT_VOCAB_BLOCK is None:
|
| 1439 |
+
v = subject_vocabulary()
|
| 1440 |
+
cats = sorted(v["categories"])
|
| 1441 |
+
areas = sorted(v["areas"])
|
| 1442 |
+
_SUBJECT_VOCAB_BLOCK = (
|
| 1443 |
+
"\n\nVALID AREAS (" + str(len(areas)) + "):\n"
|
| 1444 |
+
+ "\n".join("- " + a.title() for a in areas)
|
| 1445 |
+
+ "\n\nVALID CATEGORIES (" + str(len(cats)) + "):\n"
|
| 1446 |
+
+ "\n".join("- " + c.title() for c in cats)
|
| 1447 |
+
)
|
| 1448 |
+
return SUBJECT_NORMALISE_PROMPT + _SUBJECT_VOCAB_BLOCK
|
| 1449 |
+
|
| 1450 |
+
|
| 1451 |
@app.post("/api/browse-subject")
|
| 1452 |
async def browse_subject(req: SubjectRequest, request: Request):
|
| 1453 |
# Guard: ensure SCImago index is loaded (cold-start protection)
|
|
|
|
| 1460 |
try:
|
| 1461 |
parsed = await call_openai(
|
| 1462 |
client,
|
| 1463 |
+
subject_prompt_with_vocabulary(),
|
| 1464 |
f"{lang_instruction(req.language)}Subject input: {req.subject.strip()}",
|
| 1465 |
+
max_tokens=300,
|
| 1466 |
model=MODEL_LIGHT,
|
| 1467 |
)
|
| 1468 |
except RateLimitError:
|
|
|
|
| 1491 |
"journals": [],
|
| 1492 |
}}
|
| 1493 |
|
| 1494 |
+
# Hard-filter against the real vocabulary. A name the model invented is
|
| 1495 |
+
# discarded here rather than being handed to the search, where it would
|
| 1496 |
+
# fail the category test and fall through to a broad area match that looks
|
| 1497 |
+
# like a real answer.
|
| 1498 |
+
bad_category = bool(category) and not is_real_category(category)
|
| 1499 |
+
bad_area = bool(area) and not is_real_area(area)
|
| 1500 |
+
if bad_category:
|
| 1501 |
+
logger.warning("[BrowseSubject] model returned unlisted category %r for %r",
|
| 1502 |
+
category, req.subject)
|
| 1503 |
+
category = ""
|
| 1504 |
+
if bad_area:
|
| 1505 |
+
logger.warning("[BrowseSubject] model returned unlisted area %r for %r",
|
| 1506 |
+
area, req.subject)
|
| 1507 |
+
area = ""
|
| 1508 |
+
|
| 1509 |
+
if not category and not area:
|
| 1510 |
+
usage.usage.record_gap("journal_subject", "subject_unrecognised")
|
| 1511 |
+
request.state.usage_recorded = True
|
| 1512 |
+
near = suggest_categories(req.subject.strip(), limit=6)
|
| 1513 |
+
hint = (" Closest SCImago categories: "
|
| 1514 |
+
+ ", ".join(n.title() for n in near) + ".") if near else ""
|
| 1515 |
+
return {"result": {
|
| 1516 |
+
"confidence": "not_found", "normalised": normalised,
|
| 1517 |
+
"user_message": (user_message or
|
| 1518 |
+
f"SCImago has no subject category matching '{req.subject.strip()}'."
|
| 1519 |
+
+ hint),
|
| 1520 |
+
"journals": [],
|
| 1521 |
+
}}
|
| 1522 |
+
|
| 1523 |
+
journals, match_level = search_by_subject(category, area, top_n=30, with_level=True)
|
| 1524 |
+
|
| 1525 |
+
# match_level 1 means nothing matched the category and these journals were
|
| 1526 |
+
# selected by AREA alone β thousands of them, cut to 30 by h-index. That is
|
| 1527 |
+
# a browsing aid, not an answer, and it must be labelled as one. The old
|
| 1528 |
+
# `fallback_used` flag never fired for this case because the first search
|
| 1529 |
+
# already returned a full page.
|
| 1530 |
+
fallback_used = (match_level == 1)
|
| 1531 |
|
| 1532 |
if not journals:
|
| 1533 |
# A recognised subject with nothing behind it in SCImago. This is the
|
|
|
|
| 1560 |
})
|
| 1561 |
|
| 1562 |
msg = user_message
|
| 1563 |
+
if fallback_used:
|
| 1564 |
+
warn = (f"No SCImago category matched '{req.subject.strip()}'. "
|
| 1565 |
+
f"These are the highest-ranked journals across the whole "
|
| 1566 |
+
f"'{area.title()}' area, not a subject-specific list.")
|
| 1567 |
+
msg = f"{msg} {warn}" if msg else warn
|
| 1568 |
+
usage.usage.record_gap("journal_subject", "area_fallback")
|
| 1569 |
+
request.state.usage_recorded = True
|
| 1570 |
|
| 1571 |
return {"result": {
|
| 1572 |
"confidence": confidence,
|