|
3 | 3 | import io
|
4 | 4 | import json
|
5 | 5 | import multiprocessing
|
| 6 | +import os |
6 | 7 | import queue
|
7 | 8 | from multiprocessing.queues import Queue
|
8 | 9 | from multiprocessing.synchronize import Event
|
@@ -746,19 +747,28 @@ async def predict(model_id: str, text: str) -> dict:
|
746 | 747 | """
|
747 | 748 | try:
|
748 | 749 | # First try loading as a registered model
|
749 |
| - try: |
750 |
| - loaded_model = mlflow.pyfunc.load_model(f"models:/{model_id}/latest") |
751 |
| - except mlflow.exceptions.MlflowException: |
752 |
| - # If not found as registered model, try loading as a run |
| 750 | + dst_path = f"model_cache/{model_id}" |
| 751 | + if model_id in os.listdir("model_cache"): |
| 752 | + loaded_model = mlflow.pyfunc.load_model(dst_path) |
| 753 | + else: |
| 754 | + os.makedirs(dst_path, exist_ok=True) |
753 | 755 | try:
|
754 | 756 | loaded_model = mlflow.pyfunc.load_model(
|
755 |
| - f"runs:/{model_id}/intent_model" |
| 757 | + f"models:/{model_id}/latest", |
| 758 | + dst_path=dst_path, |
756 | 759 | )
|
757 | 760 | except mlflow.exceptions.MlflowException:
|
758 |
| - raise HTTPException( |
759 |
| - status_code=404, |
760 |
| - detail=f"No model found with ID {model_id} (tried both registered models and runs)", |
761 |
| - ) |
| 761 | + # If not found as registered model, try loading as a run |
| 762 | + try: |
| 763 | + loaded_model = mlflow.pyfunc.load_model( |
| 764 | + f"runs:/{model_id}/intent_model", |
| 765 | + dst_path=dst_path, |
| 766 | + ) |
| 767 | + except mlflow.exceptions.MlflowException: |
| 768 | + raise HTTPException( |
| 769 | + status_code=404, |
| 770 | + detail=f"No model found with ID {model_id} (tried both registered models and runs)", |
| 771 | + ) |
762 | 772 |
|
763 | 773 | # Create a pandas DataFrame with the input text
|
764 | 774 | test_data = pd.DataFrame({"text": [text]})
|
|
0 commit comments