Download src/api.py from Ukandu/webgenie: direct link, hf CLI and curl.
- Browser
- Download file 1.71 kB
-
https://huggingface.co/spaces/Ukandu/webgenie/resolve/main/src/api.py
- Command line
-
hf download hf://spaces/Ukandu/webgenie/src/api.py
-
curl -L -o api.py https://huggingface.co/spaces/Ukandu/webgenie/resolve/main/src/api.py
1.71 kB
| from fastapi import FastAPI, UploadFile, File | |
| from pydantic import BaseModel | |
| import uvicorn | |
| import os, uuid, pandas as pd, threading | |
| from runners.genie3 import run_genie3 | |
| from runners.grnboost2 import run_grnboost2 | |
| from runners.scns import run_scns | |
| app=FastAPI() | |
| DATA="data" | |
| INP=f"{DATA}/inputs" | |
| OUT=f"{DATA}/outputs" | |
| os.makedirs(INP,exist_ok=True) | |
| os.makedirs(OUT,exist_ok=True) | |
| class RunRequest(BaseModel): | |
| dataset_id:str | |
| algorithm:str | |
| params:dict | |
| jobs={} | |
| def run_job(job_id,req): | |
| infile=f"{INP}/{req.dataset_id}.csv" | |
| outfile=f"{OUT}/{job_id}_rankedEdges.csv" | |
| if req.algorithm=="GENIE3": | |
| run_genie3(infile,outfile) | |
| elif req.algorithm=="GRNBOOST2": | |
| run_grnboost2(infile,outfile) | |
| elif req.algorithm=="SCNS": | |
| run_scns(infile,outfile) | |
| jobs[job_id]["status"]="done" | |
| async def upload_dataset(file:UploadFile=File(...)): | |
| df=pd.read_csv(file.file) | |
| path=f"{INP}/{file.filename}" | |
| df.to_csv(path,index=False) | |
| return {"status":"ok","dataset":file.filename} | |
| def list_datasets(): | |
| return {"datasets":[f for f in os.listdir(INP) if f.endswith(".csv")]} | |
| def run(req:RunRequest): | |
| job_id=str(uuid.uuid4()) | |
| jobs[job_id]={"status":"running"} | |
| t=threading.Thread(target=run_job,args=(job_id,req)) | |
| t.start() | |
| return {"job_id":job_id} | |
| def job_status(job_id:str): | |
| return jobs.get(job_id,{"status":"unknown"}) | |
| def results(job_id:str): | |
| path=f"{OUT}/{job_id}_rankedEdges.csv" | |
| if not os.path.exists(path): | |
| return {"error":"not ready"} | |
| return {"file":path} | |