File size: 1,893 Bytes
2c21cf7 d91b022 2c21cf7 d91b022 2c21cf7 d91b022 2c21cf7 d91b022 2c21cf7 566c57e 2c21cf7 |
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 |
import json
import numpy as np
import pandas as pd
import uvicorn
from fastapi import FastAPI, Request
from fastapi.middleware.cors import CORSMiddleware
from fastapi.middleware.gzip import GZipMiddleware
from fastapi.responses import JSONResponse
from fastapi.staticfiles import StaticFiles
from languages import languages
from models import models
from tables import aggregate, make_country_table, make_language_table, make_model_table
app = FastAPI()
app.add_middleware(CORSMiddleware, allow_origins=["*"])
app.add_middleware(GZipMiddleware, minimum_size=1000)
with open("results.json", "r") as f:
results = pd.DataFrame(json.load(f))
def serialize(df):
return df.replace({np.nan: None}).to_dict(orient="records")
@app.post("/api/data")
async def data(request: Request):
body = await request.body()
data = json.loads(body)
selected_languages = data.get("selectedLanguages", {})
df = results
_, lang_results, model_results, task_results = aggregate(df)
# lang_results = pd.merge(languages, lang_results, on="bcp_47", how="outer")
language_table = make_language_table(lang_results, languages)
datasets_df = pd.read_json("data/datasets.json")
countries = make_country_table(language_table)
if selected_languages:
# the filtering is only applied for the model table
df = df[df["bcp_47"].isin(lang["bcp_47"] for lang in selected_languages)]
model_table = make_model_table(model_results, models)
all_tables = {
"model_table": serialize(model_table),
"language_table": serialize(language_table),
"dataset_table": serialize(datasets_df),
"countries": serialize(countries),
}
return JSONResponse(content=all_tables)
app.mount("/", StaticFiles(directory="frontend/public", html=True), name="frontend")
if __name__ == "__main__":
uvicorn.run(app, host="0.0.0.0", port=8000)
|