Update readme with example usage
Browse files
README.md
CHANGED
|
@@ -8,4 +8,79 @@ tags:
|
|
| 8 |
- event-data
|
| 9 |
- political-science
|
| 10 |
- computational-social-science
|
| 11 |
-
---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
- event-data
|
| 9 |
- political-science
|
| 10 |
- computational-social-science
|
| 11 |
+
---
|
| 12 |
+
|
| 13 |
+
Recommended usage is with vLLM.
|
| 14 |
+
|
| 15 |
+
## Load the model and tokenizer
|
| 16 |
+
|
| 17 |
+
```
|
| 18 |
+
from vllm import LLM, SamplingParams
|
| 19 |
+
from transformers import AutoTokenizer
|
| 20 |
+
|
| 21 |
+
model = LLM(model="ahalt/event-attribute-extractor",
|
| 22 |
+
enable_prefix_caching=True,
|
| 23 |
+
max_model_len=8000,
|
| 24 |
+
gpu_memory_utilization=0.80)
|
| 25 |
+
|
| 26 |
+
tokenizer = AutoTokenizer.from_pretrained("Qwen/Qwen3-0.6B")
|
| 27 |
+
```
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
## Prompt setup
|
| 31 |
+
|
| 32 |
+
```
|
| 33 |
+
system_content_short = """Extract political events as JSON.
|
| 34 |
+
|
| 35 |
+
OUTPUT FORMAT:
|
| 36 |
+
[
|
| 37 |
+
{
|
| 38 |
+
"event_type": "EVENT_TYPE",
|
| 39 |
+
"anchor_quote": "quote from text",
|
| 40 |
+
"actor": "who performed action OR N/A",
|
| 41 |
+
"recipient": "who was targeted OR N/A",
|
| 42 |
+
"date": "when occurred OR N/A",
|
| 43 |
+
"location": "where occurred OR N/A"
|
| 44 |
+
}
|
| 45 |
+
]
|
| 46 |
+
|
| 47 |
+
Return valid JSON only. Empty array [] if no events."""
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
def make_prompt(doc, event_type, tokenizer):
|
| 51 |
+
messages = [
|
| 52 |
+
{"role": "system", "content": system_content_short},
|
| 53 |
+
{"role": "user", "content": f"## Document: {doc}\n\n## Event Type: {event_type}"},
|
| 54 |
+
]
|
| 55 |
+
prompt = tokenizer.apply_chat_template(
|
| 56 |
+
messages,
|
| 57 |
+
tokenize=False,
|
| 58 |
+
add_generation_prompt=True,
|
| 59 |
+
enable_thinking=False
|
| 60 |
+
)
|
| 61 |
+
return prompt
|
| 62 |
+
```
|
| 63 |
+
|
| 64 |
+
|
| 65 |
+
## Example usage
|
| 66 |
+
|
| 67 |
+
```
|
| 68 |
+
text = """KYIV, Ukraine (AP) — Ukraine’s anti-corruption agencies said they had uncovered a major graft scheme involving inflated military procurement contracts, just two days after Ukraine’s parliament voted to restore the agencies’ independence.
|
| 69 |
+
|
| 70 |
+
In a joint statement published Saturday on social media, the National Anti-Corruption Bureau (NABU) and the Specialized Anti-Corruption Prosecutor’s Office (SAPO) said the suspects had taken bribes in a scheme that used state funds to buy drones and other military equipment at inflated prices.
|
| 71 |
+
|
| 72 |
+
“The essence of the scheme was to conclude state contracts with supplier companies at deliberately inflated prices,” the statement said, adding that offenders had received kickbacks of up to 30% of the contracts’ value.
|
| 73 |
+
|
| 74 |
+
event_type = "Investigate, charge, or prosecute"
|
| 75 |
+
|
| 76 |
+
prompt = make_prompt(text, event_type, tokenizer2)
|
| 77 |
+
output = model2.generate(prompt, sampling_params=sampling_params)
|
| 78 |
+
response = output[0].outputs[0].text.strip()
|
| 79 |
+
|
| 80 |
+
[{"event_type": "Investigate, charge, or prosecute",
|
| 81 |
+
"anchor_quote": "Ukraine\u2019s anti-corruption agencies said they had uncovered a major graft scheme involving inflated military procurement contracts",
|
| 82 |
+
"actor": "National Anti-Corruption Bureau (NABU); Specialized Anti-Corruption Prosecutor\u2019s Office (SAPO)",
|
| 83 |
+
"recipient": "suspects involved in the scheme",
|
| 84 |
+
"date": "Saturday",
|
| 85 |
+
"location": "Ukraine"}
|
| 86 |
+
```
|