JuliaKreutzerCohere commited on
Commit
bdcf184
·
verified ·
1 Parent(s): afb76d1

Upload script.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. script.py +29 -5
script.py CHANGED
@@ -1,7 +1,27 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import os
2
  import re
 
3
  import json
4
- import pandas as pd
5
  import torch
6
  from transformers import AutoTokenizer, AutoModelForCausalLM
7
 
@@ -46,10 +66,11 @@ model = AutoModelForCausalLM.from_pretrained(
46
  MODEL_ID, torch_dtype=torch.float16, device_map="auto"
47
  ).eval()
48
 
49
- df = pd.read_csv("/tmp/data/test.csv", dtype=str).fillna("")
 
50
 
51
  outputs_queries_types = []
52
- for _, r in df.iterrows():
53
 
54
  # Create the prompt.
55
  messages = [
@@ -81,7 +102,7 @@ for _, r in df.iterrows():
81
  done = True
82
 
83
  outputs_queries_types.append((text, r['id'], r['query'], r['task_type']))
84
- print(f"{len(outputs_queries_types)}/{len(df)} done", flush=True)
85
 
86
  # Postprocess and store the answers.
87
  def expected_answer_count(query: str, task_type: str) -> int:
@@ -200,5 +221,8 @@ rows = []
200
  for answer, row_id, query, task_type in outputs_queries_types:
201
  answers = postprocess_answer(answer, query, task_type)
202
  rows.append({"id": row_id, "pred": json.dumps(answers, ensure_ascii=False)})
203
- pd.DataFrame(rows).to_csv("submission.csv", index=False)
 
 
 
204
  print("wrote submission.csv", flush=True)
 
1
+ import subprocess
2
+ import sys
3
+
4
+ # Eval sandbox ships older transformers/tokenizers; tiny-aya needs recent versions.
5
+ # Pip has network even though model loading is offline.
6
+ subprocess.run(
7
+ [
8
+ sys.executable,
9
+ "-m",
10
+ "pip",
11
+ "install",
12
+ "-q",
13
+ "transformers>=4.56",
14
+ "tokenizers>=0.21.1",
15
+ "accelerate>=0.30",
16
+ "torch>=2.2",
17
+ ],
18
+ check=True,
19
+ )
20
+
21
  import os
22
  import re
23
+ import csv
24
  import json
 
25
  import torch
26
  from transformers import AutoTokenizer, AutoModelForCausalLM
27
 
 
66
  MODEL_ID, torch_dtype=torch.float16, device_map="auto"
67
  ).eval()
68
 
69
+ with open("/tmp/data/test.csv", encoding="utf-8", newline="") as f:
70
+ test_rows = list(csv.DictReader(f))
71
 
72
  outputs_queries_types = []
73
+ for r in test_rows:
74
 
75
  # Create the prompt.
76
  messages = [
 
102
  done = True
103
 
104
  outputs_queries_types.append((text, r['id'], r['query'], r['task_type']))
105
+ print(f"{len(outputs_queries_types)}/{len(test_rows)} done", flush=True)
106
 
107
  # Postprocess and store the answers.
108
  def expected_answer_count(query: str, task_type: str) -> int:
 
221
  for answer, row_id, query, task_type in outputs_queries_types:
222
  answers = postprocess_answer(answer, query, task_type)
223
  rows.append({"id": row_id, "pred": json.dumps(answers, ensure_ascii=False)})
224
+ with open("submission.csv", "w", encoding="utf-8", newline="") as f:
225
+ writer = csv.DictWriter(f, fieldnames=["id", "pred"])
226
+ writer.writeheader()
227
+ writer.writerows(rows)
228
  print("wrote submission.csv", flush=True)