Text Generation
Transformers
Safetensors
English
qwen2
chat
conversational
text-generation-inference
4-bit precision
awq
Instructions to use lindazeng979/iol-in-context-examples with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use lindazeng979/iol-in-context-examples with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("text-generation", model="lindazeng979/iol-in-context-examples") messages = [ {"role": "user", "content": "Who are you?"}, ] pipe(messages)# pip install -U transformers accelerate # Load model directly from transformers import AutoTokenizer, AutoModelForCausalLM tokenizer = AutoTokenizer.from_pretrained("lindazeng979/iol-in-context-examples") model = AutoModelForCausalLM.from_pretrained("lindazeng979/iol-in-context-examples", device_map="auto") messages = [ {"role": "user", "content": "Who are you?"}, ] inputs = tokenizer.apply_chat_template( messages, add_generation_prompt=True, tokenize=True, return_dict=True, return_tensors="pt", ).to(model.device) outputs = model.generate(**inputs, max_new_tokens=256) print(tokenizer.decode(outputs[0][inputs["input_ids"].shape[-1]:])) - Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- vLLM
How to use lindazeng979/iol-in-context-examples with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "lindazeng979/iol-in-context-examples" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "lindazeng979/iol-in-context-examples", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }'Use Docker
docker model run hf.co/lindazeng979/iol-in-context-examples
- SGLang
How to use lindazeng979/iol-in-context-examples with SGLang:
Install from pip and serve model
# Install SGLang from pip: pip install sglang # Start the SGLang server: python3 -m sglang.launch_server \ --model-path "lindazeng979/iol-in-context-examples" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "lindazeng979/iol-in-context-examples", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }'Use Docker images
docker run --gpus all \ --shm-size 32g \ -p 30000:30000 \ -v ~/.cache/huggingface:/root/.cache/huggingface \ --env "HF_TOKEN=<secret>" \ --ipc=host \ lmsysorg/sglang:latest \ python3 -m sglang.launch_server \ --model-path "lindazeng979/iol-in-context-examples" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "lindazeng979/iol-in-context-examples", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }' - Docker Model Runner
How to use lindazeng979/iol-in-context-examples with Docker Model Runner:
docker model run hf.co/lindazeng979/iol-in-context-examples
Download script.py from lindazeng979/iol-in-context-examples: direct link, hf CLI and curl.
- Browser
- Download file 7.52 kB
-
https://huggingface.co/lindazeng979/iol-in-context-examples/resolve/main/script.py
- Command line
-
hf download hf://lindazeng979/iol-in-context-examples/script.py
-
curl -L -o script.py https://huggingface.co/lindazeng979/iol-in-context-examples/resolve/main/script.py
7.52 kB
| #import subprocess, sys | |
| #subprocess.run( | |
| # [ | |
| # sys.executable, | |
| # "-m", | |
| # "pip", | |
| # "install", | |
| # "-q", | |
| # "transformers>=4.43", | |
| # "accelerate>=0.30", | |
| # "torch>=2.2", | |
| # "pandas", | |
| # ], | |
| # check=True, | |
| #) | |
| import os | |
| import re | |
| import json | |
| import pandas as pd | |
| import torch | |
| from transformers import AutoTokenizer, AutoModelForCausalLM | |
| from incontext_examples import INCONTEXT_EXAMPLES | |
| # The repository is the working directory at runtime. | |
| # The evaluation sandbox has no internet access. | |
| os.environ["HF_HUB_OFFLINE"] = "1" | |
| os.environ["TRANSFORMERS_OFFLINE"] = "1" | |
| MODEL_ID = "." | |
| TEST_PATH = "/tmp/data/test.csv" | |
| OUTPUT_PATH = "submission.csv" | |
| MAX_NEW_TOKENS = 1536 | |
| tokenizer = AutoTokenizer.from_pretrained(MODEL_ID) | |
| if tokenizer.pad_token_id is None: | |
| tokenizer.pad_token = tokenizer.eos_token | |
| model = AutoModelForCausalLM.from_pretrained( | |
| MODEL_ID, | |
| torch_dtype=torch.float16, | |
| device_map="auto", | |
| ).eval() | |
| df = pd.read_csv( | |
| TEST_PATH, | |
| dtype=str, | |
| ).fillna("") | |
| required_columns = { | |
| "id", | |
| "context", | |
| "query", | |
| "task_type", | |
| } | |
| missing_columns = required_columns - set(df.columns) | |
| if missing_columns: | |
| raise ValueError( | |
| "The test CSV is missing required columns: " | |
| f"{sorted(missing_columns)}" | |
| ) | |
| SYSTEM = ( | |
| "You solve International Linguistics Olympiad problems by reasoning from the " | |
| "data you are given. You may meet a task type you have never seen: read the " | |
| "instruction and the examples, and answer in the same form they use. " | |
| "Common task types and what to give -- " | |
| "translation: the translated form only, in the language the task asks for; " | |
| "fill_blanks: only the missing form for each blank; " | |
| "match_letters: only the option letter, for example A, B, or C; " | |
| "text_to_num: the number in digits; " | |
| "num_to_text: the number written out in words, in the language asked; " | |
| "any other type: give exactly what the instruction asks, nothing else. " | |
| "Reason step by step first. Then write a line that says exactly FINAL ANSWERS: " | |
| "and, below it, one answer per line in the order the items are asked. " | |
| "Each final answer must be bare: no numbering, no quotes, and no extra text." | |
| ) | |
| def normalize_task_type(task_type): | |
| """ | |
| Normalize task-type names so capitalization, spaces, and hyphens | |
| do not prevent a match. | |
| Examples: | |
| "Num To Text" -> "num_to_text" | |
| "fill-blanks" -> "fill_blanks" | |
| """ | |
| task_type = str(task_type).strip().lower() | |
| task_type = re.sub(r"[\s-]+", "_", task_type) | |
| return task_type | |
| def parse_answers(text): | |
| """ | |
| Keep only nonempty lines after the final FINAL ANSWERS marker. | |
| If the model adds numbering such as: | |
| 1. answer | |
| 2) answer | |
| the numbering is removed. | |
| """ | |
| markers = list( | |
| re.finditer( | |
| r"(?im)^\s*final answers?\s*:?\s*$", | |
| text, | |
| ) | |
| ) | |
| if markers: | |
| text = text[markers[-1].end():] | |
| answers = [] | |
| for line in text.splitlines(): | |
| line = re.sub( | |
| r"^\s*\d+[.)]\s*", | |
| "", | |
| line, | |
| ).strip() | |
| if line: | |
| answers.append(line) | |
| return answers | |
| def format_example_assistant_response(example): | |
| """ | |
| Format the stored reasoning trace and answers as an assistant message. | |
| """ | |
| reasoning = str( | |
| example.get("reasoning", "") | |
| ).strip() | |
| answers = example.get("answers", []) | |
| if isinstance(answers, str): | |
| answers = [ | |
| line.strip() | |
| for line in answers.splitlines() | |
| if line.strip() | |
| ] | |
| formatted_answers = "\n".join( | |
| str(answer).strip() | |
| for answer in answers | |
| ) | |
| if reasoning: | |
| return ( | |
| f"{reasoning}\n\n" | |
| f"FINAL ANSWERS:\n" | |
| f"{formatted_answers}" | |
| ) | |
| return ( | |
| f"FINAL ANSWERS:\n" | |
| f"{formatted_answers}" | |
| ) | |
| def get_incontext_example(task_type): | |
| """ | |
| Retrieve the demonstration matching the current problem's task type. | |
| Returns None when no demonstration exists for that task type. | |
| """ | |
| normalized_type = normalize_task_type(task_type) | |
| # Normalize the dictionary keys as well, so keys such as | |
| # "num-to-text" and "num_to_text" are treated equivalently. | |
| normalized_examples = { | |
| normalize_task_type(key): value | |
| for key, value in INCONTEXT_EXAMPLES.items() | |
| } | |
| return normalized_examples.get(normalized_type) | |
| def build_messages(row): | |
| """ | |
| Construct the conversation for one test problem. | |
| The conversation contains: | |
| 1. the system prompt; | |
| 2. a solved demonstration of the same task type, when available; | |
| 3. the actual test problem. | |
| """ | |
| task_type = normalize_task_type( | |
| row["task_type"] | |
| ) | |
| example = get_incontext_example( | |
| task_type | |
| ) | |
| messages = [ | |
| { | |
| "role": "system", | |
| "content": SYSTEM, | |
| } | |
| ] | |
| if example is not None: | |
| example_context = str( | |
| example.get("context", "") | |
| ).strip() | |
| example_query = str( | |
| example.get("query", "") | |
| ).strip() | |
| messages.extend( | |
| [ | |
| { | |
| "role": "user", | |
| "content": ( | |
| f"Task type: {task_type}\n\n" | |
| f"{example_context}\n\n" | |
| f"{example_query}" | |
| ), | |
| }, | |
| { | |
| "role": "assistant", | |
| "content": format_example_assistant_response( | |
| example | |
| ), | |
| }, | |
| ] | |
| ) | |
| else: | |
| print( | |
| "Warning: no in-context example found for " | |
| f"task_type={task_type!r}, id={row['id']!r}. " | |
| "Running the problem without a demonstration." | |
| ) | |
| problem_context = str( | |
| row["context"] | |
| ).strip() | |
| problem_query = str( | |
| row["query"] | |
| ).strip() | |
| messages.append( | |
| { | |
| "role": "user", | |
| "content": ( | |
| f"Task type: {task_type}\n\n" | |
| f"{problem_context}\n\n" | |
| f"{problem_query}" | |
| ), | |
| } | |
| ) | |
| return messages | |
| rows = [] | |
| for _, row in df.iterrows(): | |
| messages = build_messages( | |
| row | |
| ) | |
| inputs = tokenizer.apply_chat_template( | |
| messages, | |
| add_generation_prompt=True, | |
| return_tensors="pt", | |
| return_dict=True, | |
| ).to(model.device) | |
| input_length = inputs[ | |
| "input_ids" | |
| ].shape[-1] | |
| with torch.no_grad(): | |
| output = model.generate( | |
| **inputs, | |
| max_new_tokens=MAX_NEW_TOKENS, | |
| do_sample=False, | |
| pad_token_id=tokenizer.pad_token_id, | |
| eos_token_id=tokenizer.eos_token_id, | |
| ) | |
| generated_text = tokenizer.decode( | |
| output[0][input_length:], | |
| skip_special_tokens=True, | |
| ).strip() | |
| answers = parse_answers( | |
| generated_text | |
| ) | |
| rows.append( | |
| { | |
| "id": row["id"], | |
| "pred": json.dumps( | |
| answers, | |
| ensure_ascii=False, | |
| ), | |
| } | |
| ) | |
| submission_df = pd.DataFrame( | |
| rows | |
| ) | |
| submission_df.to_csv( | |
| OUTPUT_PATH, | |
| index=False, | |
| ) | |
| print( | |
| f"Saved {len(submission_df)} predictions to {OUTPUT_PATH}" | |
| ) |