question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How can i reload the change of DL Models on Fastapi?

See original GitHub issue

First Check

  • I added a very descriptive title to this issue.
  • I used the GitHub search to find a similar issue and didn’t find it.
  • I searched the FastAPI documentation, with the integrated search.
  • I already searched in Google “How to X in FastAPI” and didn’t find any information.
  • I already read and followed all the tutorial in the docs and didn’t find an answer.
  • I already checked if it is not related to FastAPI but to Pydantic.
  • I already checked if it is not related to FastAPI but to Swagger UI.
  • I already checked if it is not related to FastAPI but to ReDoc.

Commit to Help

  • I commit to help with one of those options 👆

Example Code

no code example

Description

When i change the files of models the models files does not seem to be reloaded and the old ones seems to be used , so how can i make the change in models appear automatically also i dont want to reload the whole app using reload=True is there a way to do this ?

Operating System

Linux

Operating System Details

No response

FastAPI Version

0.70.0

Python Version

3.7

Additional Context

No response

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:11 (4 by maintainers)

github_iconTop GitHub Comments

3reactions
insomnescommented, Dec 8, 2021

You can create asyncio task on startup (and cancel it on shutdown) which checks files hashsum and sleeps for some time, if hashsum has been changed you reload your model into memory.

import asyncio
from hashlib import md5
from fastapi import FastAPI

FILE_PATH = "test.txt"


class MemLoader:
    def __init__(self, file_path: str):
        self.file_path = file_path
        self.file_content, self.file_hash = self.load_file()

    def load_file(self):
        with open(self.file_path) as file:
            file_content = file.read()

        file_hash = md5(file_content.encode("UTF-8")).digest()

        return file_content, file_hash

    def check_file_changed(self):
        file_content, file_hash = self.load_file()
        if file_hash != self.file_hash:
            print("FILE CHANGED RELOAD")
            self.file_content = file_content
            self.file_hash = file_hash

    def get_content(self):
        return self.file_content


app = FastAPI()


async def check_file_changed(mem_loader: MemLoader):
    loop = asyncio.get_running_loop()
    print("STARTING FILE CHECK")
    while True:
        await asyncio.sleep(15)
        await loop.run_in_executor(
            executor=None, func=mem_loader.check_file_changed
        )


@app.on_event("startup")
async def load_mem_loader():
    print("LOADING MEM LOADER")
    app.mem_loader = MemLoader(FILE_PATH)
    app.mem_loader_task = asyncio.create_task(
        check_file_changed(app.mem_loader)
    )


@app.on_event("shutdown")
async def cancel_mem_loader():
    app.mem_loader_task.cancel()
    print("CHECK TASK CANCELED")



@app.get("/content")
async def get_content():
    return app.mem_loader.get_content()
1reaction
HGamalElDiincommented, Dec 9, 2021

@insomnes thanks for your help really, i did that and woks fine now

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to reload FastAPI app when a file, other than *.py files ...
I want my FastAPI app to reload when a .csv file in the same directory changes. I tried the following command, but it...
Read more >
How you can quickly deploy your ML models with FastAPI
We can run the FastAPI app using the following command. uvicorn main:app --reload. The command starts a local Uvicorn server and you should...
Read more >
Body - Updates - FastAPI
Create a copy of the stored model, updating it's attributes with the received partial updates (using the update parameter). Convert the copied model...
Read more >
Deploying ML Models as API using FastAPI - GeeksforGeeks
Python3 · basic-app refers to the name of the file we created our API in. · app refers to the FastAPI instance we...
Read more >
Machine Learning Model Deployment Using FastAPI - YouTube
In this video, we focus on deploying our model as API using the FastAPI library. This way, the solution can be consumed via...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found