Download tasks/bibliometric_lab/solution/reference/predict.py from Tokenwave-AI/REE: direct link, hf CLI and curl.
- Browser
- Download file 1.45 kB
-
https://huggingface.co/datasets/Tokenwave-AI/REE/resolve/main/tasks/bibliometric_lab/solution/reference/predict.py
- Command line
-
hf download hf://datasets/Tokenwave-AI/REE/tasks/bibliometric_lab/solution/reference/predict.py
-
curl -L -o predict.py https://huggingface.co/datasets/Tokenwave-AI/REE/resolve/main/tasks/bibliometric_lab/solution/reference/predict.py
1.45 kB
| """Offline prediction through run.sh DATA_DIR OUTPUT_DIR.""" | |
| import csv | |
| import json | |
| from pathlib import Path | |
| import sys | |
| import numpy as np | |
| from sklearn.feature_extraction.text import TfidfVectorizer | |
| from text_features import paper_text | |
| def main(): | |
| data, output = map(Path, sys.argv[1:]) | |
| method = Path(__file__).resolve().parent | |
| with (method / "vocabulary.json").open(encoding="utf-8") as stream: | |
| vocabulary = json.load(stream) | |
| with (data / "documents.jsonl").open(encoding="utf-8") as stream: | |
| documents = [json.loads(line) for line in stream if line.strip()] | |
| with np.load(method / "weights.npz", allow_pickle=False) as state: | |
| vectorizer = TfidfVectorizer(vocabulary=vocabulary) | |
| vectorizer.idf_ = state["idf"] | |
| features = vectorizer.transform([paper_text(row) for row in documents]) | |
| predictions = np.asarray(features @ state["coef"]).ravel() + state["intercept"][0] | |
| if not np.isfinite(predictions).all(): | |
| raise ValueError("Nonfinite prediction") | |
| output.mkdir(parents=True, exist_ok=True) | |
| with (output / "predictions.csv").open("w", encoding="utf-8", newline="") as stream: | |
| writer = csv.writer(stream) | |
| writer.writerow(["paper_id", "prediction"]) | |
| writer.writerows((row["doc_id"], format(float(prediction), ".17g")) | |
| for row, prediction in zip(documents, predictions)) | |
| if __name__ == "__main__": | |
| main() | |