初始化项目,由ModelHub XC社区提供模型
Model: Luimas/claim-extractor-detective-qwen3b Source: Original Platform
This commit is contained in:
26
scripts/evaluate.py
Normal file
26
scripts/evaluate.py
Normal file
@@ -0,0 +1,26 @@
|
||||
#!/usr/bin/env python
|
||||
# Quick self-contained eval: runs the GGUF (grammar-constrained) on built-in test passages and
|
||||
# reports JSON-validity, sarcasm handling, and contradiction detection. pip install llama-cpp-python
|
||||
import glob, json, os
|
||||
from llama_cpp import Llama, LlamaGrammar
|
||||
HERE = os.path.dirname(os.path.abspath(__file__)); ROOT = os.path.dirname(HERE)
|
||||
GGUF = (glob.glob(os.path.join(ROOT, "*.gguf")) + glob.glob(os.path.join(HERE, "*.gguf")))[0]
|
||||
PROMPT = open(os.path.join(ROOT, "prompt.txt"), encoding="utf-8").read()
|
||||
GRAMMAR = LlamaGrammar.from_string(open(os.path.join(ROOT, "claim.gbnf"), encoding="utf-8").read())
|
||||
CASES = [
|
||||
("The mayor said crime fell last year. Hours later the police chief said it actually rose.", "contradiction"),
|
||||
("What an incredible update, my phone now dies by noon. The engineers really outdid themselves.", "sarcasm"),
|
||||
("On 2024-03-12 the central bank cut rates to 4.5%, citing easing inflation.", "fact"),
|
||||
]
|
||||
llm = Llama(model_path=GGUF, n_ctx=4096, n_gpu_layers=-1, verbose=False)
|
||||
ok = con = sar = nclaims = 0
|
||||
for text, tag in CASES:
|
||||
o = llm.create_chat_completion(messages=[{"role": "user", "content": PROMPT + text}],
|
||||
grammar=GRAMMAR, temperature=0.0, max_tokens=768)
|
||||
try:
|
||||
d = json.loads(o["choices"][0]["message"]["content"]); ok += 1; nclaims += len(d.get("claims", []))
|
||||
if tag == "contradiction" and d.get("contradictions"): con += 1
|
||||
if tag == "sarcasm" and any(c.get("stance") == "ironic" for c in d.get("claims", [])): sar += 1
|
||||
except Exception:
|
||||
pass
|
||||
print(f"valid JSON: {ok}/{len(CASES)} | avg claims: {nclaims/max(1,ok):.1f} | contradiction: {con}/1 | sarcasm: {sar}/1")
|
||||
24
scripts/inference.py
Normal file
24
scripts/inference.py
Normal file
@@ -0,0 +1,24 @@
|
||||
#!/usr/bin/env python
|
||||
# Local grammar-constrained inference (always-valid JSON). pip install llama-cpp-python
|
||||
import argparse, glob, json, os, sys
|
||||
from llama_cpp import Llama, LlamaGrammar
|
||||
HERE = os.path.dirname(os.path.abspath(__file__)); ROOT = os.path.dirname(HERE)
|
||||
def _find(name):
|
||||
for d in (ROOT, HERE):
|
||||
hits = glob.glob(os.path.join(d, name))
|
||||
if hits: return hits[0]
|
||||
raise FileNotFoundError(name)
|
||||
GGUF = _find("*.gguf")
|
||||
PROMPT = open(_find("prompt.txt"), encoding="utf-8").read()
|
||||
GRAMMAR = LlamaGrammar.from_string(open(_find("claim.gbnf"), encoding="utf-8").read())
|
||||
def main():
|
||||
ap = argparse.ArgumentParser()
|
||||
ap.add_argument("--text"); ap.add_argument("--file"); ap.add_argument("--n_gpu_layers", type=int, default=-1)
|
||||
a = ap.parse_args()
|
||||
text = open(a.file, encoding="utf-8").read() if a.file else (a.text or sys.stdin.read())
|
||||
llm = Llama(model_path=GGUF, n_ctx=4096, n_gpu_layers=a.n_gpu_layers, verbose=False)
|
||||
out = llm.create_chat_completion(messages=[{"role": "user", "content": PROMPT + text[:4000]}],
|
||||
grammar=GRAMMAR, temperature=0.0, max_tokens=768)
|
||||
print(json.dumps(json.loads(out["choices"][0]["message"]["content"]), indent=2, ensure_ascii=False))
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
23
scripts/inference_hf.py
Normal file
23
scripts/inference_hf.py
Normal file
@@ -0,0 +1,23 @@
|
||||
#!/usr/bin/env python
|
||||
# Inference via the merged fp16 model (Transformers). pip install transformers accelerate torch
|
||||
import argparse, json, os, sys
|
||||
from transformers import AutoModelForCausalLM, AutoTokenizer
|
||||
HERE = os.path.dirname(os.path.abspath(__file__)); ROOT = os.path.dirname(HERE)
|
||||
PROMPT = open(os.path.join(ROOT, "prompt.txt"), encoding="utf-8").read()
|
||||
def main():
|
||||
ap = argparse.ArgumentParser()
|
||||
ap.add_argument("--text"); ap.add_argument("--file"); ap.add_argument("--model", default=ROOT)
|
||||
a = ap.parse_args()
|
||||
text = open(a.file, encoding="utf-8").read() if a.file else (a.text or sys.stdin.read())
|
||||
tok = AutoTokenizer.from_pretrained(a.model)
|
||||
model = AutoModelForCausalLM.from_pretrained(a.model, torch_dtype="auto", device_map="auto")
|
||||
ids = tok.apply_chat_template([{"role": "user", "content": PROMPT + text[:4000]}],
|
||||
return_tensors="pt", add_generation_prompt=True).to(model.device)
|
||||
out = model.generate(ids, max_new_tokens=768, do_sample=False, pad_token_id=tok.eos_token_id)
|
||||
raw = tok.decode(out[0, ids.shape[1]:], skip_special_tokens=True)
|
||||
try:
|
||||
print(json.dumps(json.loads(raw[raw.find("{"):raw.rfind("}") + 1]), indent=2, ensure_ascii=False))
|
||||
except Exception:
|
||||
print(raw)
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user