Spaces:
Configuration error
Configuration error
Upload src/veris_classifier/classifier.py with huggingface_hub
Browse files
src/veris_classifier/classifier.py
CHANGED
|
@@ -7,6 +7,7 @@ Supports two backends:
|
|
| 7 |
|
| 8 |
import json
|
| 9 |
import logging
|
|
|
|
| 10 |
|
| 11 |
logger = logging.getLogger(__name__)
|
| 12 |
|
|
@@ -26,7 +27,10 @@ QA_SYSTEM_PROMPT = (
|
|
| 26 |
"You are a VERIS (Vocabulary for Event Recording and Incident Sharing) expert. "
|
| 27 |
"Answer questions about the VERIS framework accurately and thoroughly. "
|
| 28 |
"Reference specific VERIS terminology, enumeration values, and concepts. "
|
| 29 |
-
"Be helpful and educational."
|
|
|
|
|
|
|
|
|
|
| 30 |
)
|
| 31 |
|
| 32 |
# ββ HF Model Backend βββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
|
@@ -168,6 +172,15 @@ def _parse_json_response(raw: str) -> dict:
|
|
| 168 |
raise json.JSONDecodeError("No JSON object found in model output", text, 0)
|
| 169 |
|
| 170 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 171 |
# ββ Public API ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 172 |
|
| 173 |
|
|
@@ -228,10 +241,16 @@ def answer_question(
|
|
| 228 |
]
|
| 229 |
|
| 230 |
if use_hf:
|
| 231 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 232 |
else:
|
| 233 |
if client is None:
|
| 234 |
raise ValueError("OpenAI client required when use_hf=False")
|
| 235 |
-
|
| 236 |
client, messages, model=model, temperature=0.3, max_tokens=800
|
| 237 |
)
|
|
|
|
|
|
| 7 |
|
| 8 |
import json
|
| 9 |
import logging
|
| 10 |
+
import re
|
| 11 |
|
| 12 |
logger = logging.getLogger(__name__)
|
| 13 |
|
|
|
|
| 27 |
"You are a VERIS (Vocabulary for Event Recording and Incident Sharing) expert. "
|
| 28 |
"Answer questions about the VERIS framework accurately and thoroughly. "
|
| 29 |
"Reference specific VERIS terminology, enumeration values, and concepts. "
|
| 30 |
+
"Be helpful and educational. "
|
| 31 |
+
"Answer only the user's question. "
|
| 32 |
+
"Do not ask follow-up questions. "
|
| 33 |
+
"Do not append additional Q&A prompts."
|
| 34 |
)
|
| 35 |
|
| 36 |
# ββ HF Model Backend βββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
|
|
|
| 172 |
raise json.JSONDecodeError("No JSON object found in model output", text, 0)
|
| 173 |
|
| 174 |
|
| 175 |
+
def _clean_qa_response(answer: str) -> str:
|
| 176 |
+
"""Remove model-appended follow-up question chains from QA output."""
|
| 177 |
+
text = answer.strip()
|
| 178 |
+
match = re.search(r"(?:\n|[.!?]\s+)(What|How|Why|When|Where|Who)\b", text)
|
| 179 |
+
if match and match.start() > 0:
|
| 180 |
+
text = text[: match.start()].rstrip()
|
| 181 |
+
return text
|
| 182 |
+
|
| 183 |
+
|
| 184 |
# ββ Public API ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 185 |
|
| 186 |
|
|
|
|
| 241 |
]
|
| 242 |
|
| 243 |
if use_hf:
|
| 244 |
+
raw = _generate_hf_with_options(
|
| 245 |
+
messages,
|
| 246 |
+
max_new_tokens=320,
|
| 247 |
+
do_sample=False,
|
| 248 |
+
)
|
| 249 |
+
return _clean_qa_response(raw)
|
| 250 |
else:
|
| 251 |
if client is None:
|
| 252 |
raise ValueError("OpenAI client required when use_hf=False")
|
| 253 |
+
raw = _generate_openai(
|
| 254 |
client, messages, model=model, temperature=0.3, max_tokens=800
|
| 255 |
)
|
| 256 |
+
return _clean_qa_response(raw)
|