mr-kush commited on
Commit
5fb75e8
·
verified ·
1 Parent(s): 76ceff2

update the app.py to handle the clear cache logic + show

Browse files
Files changed (1) hide show
  1. app.py +48 -1
app.py CHANGED
@@ -4,12 +4,44 @@ from typing import Union, List
4
  from predict_dept_model import DepartmentPredictor
5
  from contextlib import asynccontextmanager
6
  from response_schema import ClassificationOutput, TextInput
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7
 
8
 
9
 
10
  # Setting up startup and shutdown logic
11
  @asynccontextmanager
12
  async def lifespan(app: FastAPI):
 
 
 
 
 
13
  global predictor
14
  predictor = DepartmentPredictor()
15
  yield
@@ -39,7 +71,22 @@ def predict_department(input_data: TextInput):
39
 
40
  @app.get("/")
41
  def root():
42
- return {"message": "Sambodhan Department Classification API is running."}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
43
 
44
  # if __name__ == "__main__":
45
  # # Important for Hugging Face Spaces (port detection)
 
4
  from predict_dept_model import DepartmentPredictor
5
  from contextlib import asynccontextmanager
6
  from response_schema import ClassificationOutput, TextInput
7
+ from huggingface_hub import scan_cache_dir, HfApi
8
+
9
+ # hf api
10
+ api = HfApi()
11
+
12
+ def clear_hf_cache(model_repo: str) -> None:
13
+ """
14
+ Clears the Hugging Face model cache for the specified repository.
15
+
16
+ Args:
17
+ model_repo (str): The model repository identifier (e.g., 'mr-kush/sambodhan-department-classification-model').
18
+ """
19
+ # Scan the cache directory
20
+ cache_info = scan_cache_dir()
21
+
22
+ # Find the repository in the cache
23
+ repo_info = next((repo for repo in cache_info.cached_repos if repo.repo_id == model_repo), None)
24
+
25
+ if repo_info:
26
+ # Delete all revisions of the repository
27
+ delete_strategy = cache_info.delete_revisions(*repo_info.revisions)
28
+ print(f"Will free {delete_strategy.expected_freed_size_str}")
29
+ delete_strategy.execute()
30
+ print("Cache deletion done.")
31
+ else:
32
+ print(f"Repository '{model_repo}' not found in cache.")
33
+
34
 
35
 
36
 
37
  # Setting up startup and shutdown logic
38
  @asynccontextmanager
39
  async def lifespan(app: FastAPI):
40
+
41
+ # Clear the Hugging Face model cache
42
+ clear_hf_cache("mr-kush/sambodhan-department-classification-model")
43
+
44
+ # Load the model
45
  global predictor
46
  predictor = DepartmentPredictor()
47
  yield
 
71
 
72
  @app.get("/")
73
  def root():
74
+ # Define the model repository ID
75
+ model_repo = "mr-kush/sambodhan-department-classification-model"
76
+
77
+
78
+ # Fetch the latest commit hash (revision) from the model repository
79
+ latest_tag = api.list_repo_refs(repo_id=model_repo, repo_type="model").tags[0].name
80
+
81
+ return {
82
+ "message": "Sambodhan Department Classification API is running.",
83
+ "status": "Active" if predictor else "Inactive"
84
+ "model_version": latest_tag
85
+ }
86
+
87
+
88
+
89
+
90
 
91
  # if __name__ == "__main__":
92
  # # Important for Hugging Face Spaces (port detection)