File size: 9,764 Bytes
fd21f2f
253bbca
ef19caa
5a41550
ef19caa
fd21f2f
 
253bbca
5a41550
253bbca
6d286c3
aec7db1
ef19caa
bc828c5
5a41550
 
d350b44
 
 
253bbca
76a283e
aec7db1
253bbca
 
 
f1bc1ad
253bbca
41869c7
 
253bbca
41869c7
253bbca
 
 
 
 
aec7db1
41869c7
253bbca
 
 
 
 
 
 
 
 
 
 
 
 
 
d8d7b29
 
253bbca
 
 
 
9d2346f
 
ad7b3f8
 
9d2346f
 
 
253bbca
 
aec7db1
4726829
aec7db1
4726829
 
 
 
 
 
 
 
253bbca
 
aec7db1
253bbca
 
 
 
 
 
aec7db1
253bbca
 
 
 
 
 
bc828c5
 
 
 
 
 
 
 
 
 
 
 
 
aec7db1
ef19caa
 
 
 
253bbca
 
 
 
 
 
 
 
 
 
 
ef19caa
bc828c5
 
253bbca
 
 
 
 
 
1b8ac52
 
41869c7
 
 
 
76a283e
 
 
41869c7
253bbca
 
 
41869c7
 
 
253bbca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bc828c5
 
 
 
 
 
 
 
 
 
 
a2d40e3
 
 
253bbca
 
 
 
 
 
 
 
 
 
 
 
bc828c5
 
 
 
a2d40e3
bc828c5
 
a2d40e3
 
253bbca
a2d40e3
 
 
 
 
 
 
 
 
253bbca
 
41869c7
 
 
 
1b8ac52
 
41869c7
76a283e
d350b44
aec7db1
1b8ac52
253bbca
 
1b8ac52
94ae8af
1b8ac52
ef19caa
f9987db
aec7db1
253bbca
 
aec7db1
 
 
253bbca
aec7db1
 
 
 
253bbca
aec7db1
 
253bbca
 
 
 
 
 
aec7db1
ef19caa
253bbca
aec7db1
253bbca
 
 
 
 
aec7db1
ef19caa
 
 
 
 
 
253bbca
 
f9987db
 
 
 
 
cee1d41
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
import logging
import os
import random
from datetime import timedelta
from statistics import mean
from typing import Annotated, Any, Iterator, Union

import fasttext
from cashews import cache
from dotenv import load_dotenv
from fastapi import FastAPI, Path, Query
from httpx import AsyncClient, Client, Timeout
from huggingface_hub import hf_hub_download
from iso639 import Lang
from starlette.responses import RedirectResponse
from toolz import concat, groupby, valmap

cache.setup("mem://")


logger = logging.getLogger(__name__)
app = FastAPI()
load_dotenv()
HF_TOKEN = os.getenv("HF_TOKEN")

os.environ["HF_HUB_ENABLE_HF_TRANSFER"] = "1"

FASTTEXT_PREFIX_LENGTH = 9  # fasttext labels are formatted like "__label__eng_Latn"

BASE_DATASETS_SERVER_URL = "https://datasets-server.huggingface.co"
DEFAULT_FAST_TEXT_MODEL = "facebook/fasttext-language-identification"
headers = {
    "authorization": f"Bearer ${HF_TOKEN}",
}
timeout = Timeout(60, read=120)
client = Client(headers=headers, timeout=timeout)
async_client = AsyncClient(headers=headers, timeout=timeout)

TARGET_COLUMN_NAMES = {
    "text",
    "input",
    "tokens",
    "prompt",
    "instruction",
    "sentence_1",
    "question",
    "sentence2",
    "answer",
    "sentence",
    "response",
    "context",
    "query",
    "chosen",
    "rejected",
}


def datasets_server_valid_rows(hub_id: str):
    try:
        resp = client.get(f"{BASE_DATASETS_SERVER_URL}/is-valid?dataset={hub_id}")
        data = resp.json()
        return True if data.get("viewer") else bool(data.get("preview"))
    except Exception as e:
        logger.error(f"Failed to get is-valid for {hub_id}: {e}")
        return False


async def get_first_config_and_split_name(hub_id: str):
    try:
        resp = await async_client.get(
            f"https://datasets-server.huggingface.co/splits?dataset={hub_id}"
        )

        data = resp.json()
        return data["splits"][0]["config"], data["splits"][0]["split"]
    except Exception as e:
        logger.error(f"Failed to get splits for {hub_id}: {e}")
        return None


async def get_dataset_info(hub_id: str, config: str | None = None):
    if config is None:
        config = get_first_config_and_split_name(hub_id)
        if config is None:
            return None
        else:
            config = config[0]
    resp = await async_client.get(
        f"{BASE_DATASETS_SERVER_URL}/info?dataset={hub_id}&config={config}"
    )
    resp.raise_for_status()
    return resp.json()


@cache(ttl=timedelta(minutes=5))
async def fetch_rows(url: str) -> list[dict]:
    response = await async_client.get(url)
    if response.status_code == 200:
        data = response.json()
        return data.get("rows")
    else:
        print(f"Failed to fetch data: {response.status_code}")
        print(url)
        return []


# Function to get random rows from the dataset
async def get_random_rows(
    hub_id: str,
    total_length: int,
    number_of_rows: int,
    max_request_calls: int,
    config="default",
    split="train",
):
    rows = []
    rows_per_call = min(
        number_of_rows // max_request_calls, total_length // max_request_calls
    )
    rows_per_call = min(rows_per_call, 100)  # Ensure rows_per_call is not more than 100
    for _ in range(min(max_request_calls, number_of_rows // rows_per_call)):
        offset = random.randint(0, total_length - rows_per_call)
        url = f"https://datasets-server.huggingface.co/rows?dataset={hub_id}&config={config}&split={split}&offset={offset}&length={rows_per_call}"
        logger.info(f"Fetching {url}")
        batch_rows = await fetch_rows(url)
        rows.extend(batch_rows)
        if len(rows) >= number_of_rows:
            break
    return [row.get("row") for row in rows]


def load_model(repo_id: str) -> fasttext.FastText._FastText:
    from pathlib import Path

    Path("code/models").mkdir(parents=True, exist_ok=True)
    model_path = hf_hub_download(
        repo_id,
        "model.bin",
        # cache_dir="code/models",
        # local_dir="code/models",
        # local_dir_use_symlinks=False,
    )
    return fasttext.load_model(model_path)


model = load_model(DEFAULT_FAST_TEXT_MODEL)


def yield_clean_rows(rows: Union[list[str], str], min_length: int = 3) -> Iterator[str]:
    for row in rows:
        if isinstance(row, str):
            # split on lines and remove empty lines
            line = row.split("\n")
            for line in line:
                if line:
                    yield line
        elif isinstance(row, list):
            try:
                line = " ".join(row)
                if len(line) < min_length:
                    continue
                else:
                    yield line
            except TypeError:
                continue


def model_predict(inputs: str, k=1) -> list[dict[str, float]]:
    predictions = model.predict(inputs, k=k)
    return [
        {"label": label[FASTTEXT_PREFIX_LENGTH:], "score": prob}
        for label, prob in zip(predictions[0], predictions[1])
    ]


def get_label(x):
    return x.get("label")


def get_mean_score(preds):
    return mean([pred.get("score") for pred in preds])


def filter_by_frequency(counts_dict: dict, threshold_percent: float = 0.2):
    """Filter a dict to include items whose value is above `threshold_percent`"""
    total = sum(counts_dict.values())
    threshold = total * threshold_percent
    return {k for k, v in counts_dict.items() if v >= threshold}


def try_parse_language(lang: str) -> str | None:
    try:
        split = lang.split("_")
        lang = split[0]
        lang = Lang(lang)
        return lang.pt1
    except Exception as e:
        logger.error(f"Failed to parse language {lang}: {e}")
        return None


def predict_rows(
    rows, target_column, language_threshold_percent=0.2, return_raw_predictions=False
):
    rows = (row.get(target_column) for row in rows)
    rows = (row for row in rows if row is not None)
    rows = list(yield_clean_rows(rows))
    predictions = [model_predict(row) for row in rows]
    predictions = [pred for pred in predictions if pred is not None]
    predictions = list(concat(predictions))
    predictions_by_lang = groupby(get_label, predictions)
    langues_counts = valmap(len, predictions_by_lang)
    keys_to_keep = filter_by_frequency(
        langues_counts, threshold_percent=language_threshold_percent
    )
    filtered_dict = {k: v for k, v in predictions_by_lang.items() if k in keys_to_keep}
    raw_model_prediction_summary = dict(valmap(get_mean_score, filtered_dict))
    parsed_langs = {
        try_parse_language(k): v for k, v in raw_model_prediction_summary.items()
    }
    default_data = {
        "language_prediction_summary": parsed_langs,
        "raw_model_prediction_summary": raw_model_prediction_summary,
        "hub_id": "hub_id",
        "config": "config",
    }
    if return_raw_predictions:
        default_data["raw_predictions"] = predictions
    return default_data


# @app.get("/", response_class=HTMLResponse)
# async def read_index():
#     html_content = Path("index.html").read_text()
#     return HTMLResponse(content=html_content)


@app.get("/", include_in_schema=False)
def root():
    return RedirectResponse(url="/docs")

    # item_id: Annotated[int, Path(title="The ID of the item to get", ge=1)], q: str


@app.get("/predict_dataset_language/{hub_id:path}")
@cache(ttl=timedelta(minutes=10))
async def predict_language(
    hub_id: Annotated[str, Path(title="The hub id of the dataset to predict")],
    config: str | None = None,
    split: str | None = None,
    max_request_calls: Annotated[
        int, Query(title="Max number of requests to datasets server", gt=0, le=50)
    ] = 10,
    number_of_rows: int = 1000,
    language_threshold_percent: float = 0.2,
) -> dict[Any, Any] | None:
    is_valid = datasets_server_valid_rows(hub_id)
    if not is_valid:
        logger.error(f"Dataset {hub_id} is not accessible via the datasets server.")
    if not config and not split:
        config, split = await get_first_config_and_split_name(hub_id)
    if not config:
        config, _ = await get_first_config_and_split_name(hub_id)
    if not split:
        _, split = await get_first_config_and_split_name(hub_id)
    info = await get_dataset_info(hub_id, config)
    if info is None:
        logger.error(f"Dataset {hub_id} is not accessible via the datasets server.")
        return None
    if dataset_info := info.get("dataset_info"):
        total_rows_for_split = dataset_info.get("splits").get(split).get("num_examples")
        features = dataset_info.get("features")
        column_names = set(features.keys())
        logger.info(f"Column names: {column_names}")
        if not set(column_names).intersection(TARGET_COLUMN_NAMES):
            logger.error(
                f"Dataset {hub_id} {column_names} is not in any of the target columns {TARGET_COLUMN_NAMES}"
            )
            return None
        for column in TARGET_COLUMN_NAMES:
            if column in column_names:
                target_column = column
                logger.info(f"Using column {target_column} for language detection")
                break
        random_rows = await get_random_rows(
            hub_id,
            total_rows_for_split,
            number_of_rows,
            max_request_calls,
            config,
            split,
        )
        logger.info(f"Predicting language for {len(random_rows)} rows")
        predictions = predict_rows(
            random_rows,
            target_column,
            language_threshold_percent=language_threshold_percent,
        )
        predictions["hub_id"] = hub_id
        predictions["config"] = config
        predictions["split"] = split
        return predictions