File tree 4 files changed +0
-159
lines changed
create_fastapi_project/templates/full/backend/app/app 4 files changed +0
-159
lines changed Original file line number Diff line number Diff line change 6
6
from app .models .hero_model import Hero
7
7
from app .db .session import SessionLocal
8
8
from asyncer import runnify
9
- import logging
10
- from celery import Task
11
- from transformers import pipeline
12
-
13
-
14
- class PredictTransformersPipelineTask (Task ):
15
- """
16
- Abstraction of Celery's Task class to support loading transformers model.
17
- """
18
-
19
- task_name = ""
20
- model_name = ""
21
- abstract = True
22
-
23
- def __init__ (self ):
24
- super ().__init__ ()
25
- self .pipeline
8000
= None
26
-
27
- def __call__ (self , * args , ** kwargs ):
28
- """
29
- Load pipeline on first call (i.e. first task processed)
30
- Avoids the need to load pipeline on each task request
31
- """
32
- if not self .pipeline :
33
- logging .info ("Loading pipeline..." )
34
- self .pipeline = pipeline (self .task_name , model = self .model_name )
35
- logging .info ("Pipeline loaded" )
36
- return self .run (* args , ** kwargs )
37
-
38
-
39
- @celery .task (
40
- ignore_result = False ,
41
- bind = True ,
42
- base = PredictTransformersPipelineTask ,
43
- task_name = "text-generation" ,
44
- model_name = "gpt2" ,
45
- name = "tasks.predict_transformers_pipeline" ,
46
- )
47
- def predict_transformers_pipeline (self , prompt : str ):
48
- """
49
- Essentially the run method of PredictTask
50
- """
51
- result = self .pipeline (prompt )
52
- return result
53
9
54
10
55
11
@celery .task (name = "tasks.increment" )
Original file line number Diff line number Diff line change 1
1
from fastapi import APIRouter
2
2
from app .api .v1 .endpoints import (
3
- natural_language ,
4
3
user ,
5
4
hero ,
6
5
team ,
23
22
api_router .include_router (cache .router , prefix = "/cache" , tags = ["cache" ])
24
23
api_router .include_router (weather .router , prefix = "/weather" , tags = ["weather" ])
25
24
api_router .include_router (report .router , prefix = "/report" , tags = ["report" ])
26
- api_router .include_router (
27
- natural_language .router , prefix = "/natural_language" , tags = ["natural_language" ]
28
- )
29
25
api_router .include_router (
30
26
periodic_tasks .router , prefix = "/periodic_tasks" , tags = ["periodic_tasks" ]
31
27
)
Load Diff This file was deleted.
Original file line number Diff line number Diff line change @@ -76,21 +76,11 @@ async def lifespan(app: FastAPI):
76
76
FastAPICache .init (RedisBackend (redis_client ), prefix = "fastapi-cache" )
77
77
await FastAPILimiter .init (redis_client , identifier = user_id_identifier )
78
78
79
- # Load a pre-trained sentiment analysis model as a dictionary to an easy cleanup
80
- models : dict [str , Any ] = {
81
- "sentiment_model" : pipeline (
82
- "sentiment-analysis" ,
83
- model = "distilbert-base-uncased-finetuned-sst-2-english" ,
84
- ),
85
- }
86
- g .set_default ("sentiment_model" , models ["sentiment_model" ])
87
79
print ("startup fastapi" )
88
80
yield
89
81
# shutdown
90
82
await FastAPICache .clear ()
91
83
await FastAPILimiter .close ()
92
- models .clear ()
93
- g .cleanup ()
94
84
gc .collect ()
95
85
96
86
You can’t perform that action at this time.
0 commit comments