ecemumutlu commited on
Commit
249c88f
·
1 Parent(s): 73bf490

Add fastapi endpoint that runs lm-harness

Browse files
Files changed (5) hide show
  1. README.md +5 -0
  2. app.py +21 -3
  3. requirements.txt +2 -1
  4. svc/router.py +32 -0
  5. svc/schemas.py +16 -0
README.md CHANGED
@@ -8,3 +8,8 @@ pinned: false
8
  ---
9
 
10
  Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
 
 
 
 
8
  ---
9
 
10
  Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
11
+
12
+ ```shell
13
+ conda activate model-eval-be
14
+ pip install -r requirements.txt
15
+ ```
app.py CHANGED
@@ -1,7 +1,25 @@
1
  from fastapi import FastAPI
 
 
2
 
3
- app = FastAPI()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4
 
5
  @app.get("/")
6
- def greet_json():
7
- return {"Hello": "World!"}
 
1
  from fastapi import FastAPI
2
+ from fastapi.middleware.cors import CORSMiddleware
3
+ from svc.router import router
4
 
5
+ app = FastAPI(
6
+ title="Resume Generator API",
7
+ description="API for converting audio/text to structured resume with PDF generation",
8
+ version="1.0.0"
9
+ )
10
+
11
+ # Add CORS middleware
12
+ app.add_middleware(
13
+ CORSMiddleware,
14
+ allow_origins=["*"], # Modify this in production
15
+ allow_credentials=True,
16
+ allow_methods=["*"],
17
+ allow_headers=["*"],
18
+ )
19
+
20
+ # Include our router
21
+ app.include_router(router, prefix="/api")
22
 
23
  @app.get("/")
24
+ async def health_check():
25
+ return {"status": "healthy"}
requirements.txt CHANGED
@@ -1,2 +1,3 @@
1
  fastapi
2
- uvicorn[standard]
 
 
1
  fastapi
2
+ uvicorn[standard]
3
+ lm_eval
svc/router.py ADDED
@@ -0,0 +1,32 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import APIRouter, HTTPException
2
+ import logging
3
+
4
+ from lm_eval import evaluator, utils
5
+ from svc.schemas import LMHarnessTaskRequest, LMHarnessTaskResponse
6
+
7
+ router = APIRouter()
8
+
9
+ logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s")
10
+ logger = logging.getLogger(__name__)
11
+
12
+
13
+
14
+ @router.post("/chat", response_model=LMHarnessTaskResponse)
15
+ def inference_model(request: LMHarnessTaskRequest):
16
+ try:
17
+ results = evaluator.simple_evaluate(
18
+ model=request.model,
19
+ model_args=request.model_args,
20
+ tasks=request.tasks,
21
+ num_fewshot=request.num_fewshot,
22
+ batch_size=request.batch_size,
23
+ device=request.device,
24
+ limit=request.limit,
25
+ write_out=request.write_out # Whether to write out an example document and model input, for checking task integrity
26
+ )
27
+ except Exception as e:
28
+ raise HTTPException(status_code=500, detail=f"lm-harness task execution failed for model: {request.model_args}")
29
+
30
+ logger.info(results)
31
+ return LMHarnessTaskResponse(results=results)
32
+
svc/schemas.py ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from pydantic import BaseModel
2
+ from typing import List, Optional, Union, Any
3
+
4
+ class LMHarnessTaskRequest(BaseModel):
5
+ model: str
6
+ model_args: Optional[Union[str, dict]] = None
7
+ tasks: Optional[List[Union[str, dict, object]]] = None
8
+ num_fewshot: Optional[int] = None
9
+ batch_size: Optional[int] = None
10
+ device: Optional[str] = None
11
+ limit: Optional[Union[int, float]] = None
12
+ write_out: bool = False # Whether to write out an example document and model input, for checking task integrity
13
+
14
+
15
+ class LMHarnessTaskResponse(BaseModel):
16
+ results: dict[Any]