OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

I cannot run my model that I saved with Tensorflow with FastApi

  • Thread starter Thread starter Quark
  • Start date Start date
Q

Quark

Guest
Code:
from fastapi import FastAPI, File, UploadFile
from fastapi.middleware.cors import CORSMiddleware
import uvicorn
import numpy as np
from io import BytesIO
from PIL import Image
from tensorflow.keras.layers import TFSMLayer
import tensorflow as tf

app = FastAPI()

origins = [
    "http://localhost",
    "http://localhost:3000",
]
app.add_middleware(
    CORSMiddleware,
    allow_origins=origins,
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)



#MODEL = tf.keras.models.load_model(r"D:\Derin_Ogrenme\doma\saved_models\models\2")


MODEL = TFSMLayer(r"D:\Derin_Ogrenme\doma\saved_models\models\2", call_endpoint='serving_default')


CLASS_NAMES = ["Early Blight", "Late Blight", "Healthy"]

@app.get("/ping")
async def ping():
    return "Hello, I am alive"

def read_file_as_image(data) -> np.ndarray:
    image = np.array(Image.open(BytesIO(data)))
    return image

@app.post("/predict")
async def predict(
    file: UploadFile = File(...)
):
    image = read_file_as_image(await file.read())
    img_batch = np.expand_dims(image, 0)
    
    predictions = MODEL.predict(img_batch)

    predicted_class = CLASS_NAMES[np.argmax(predictions[0])]
    confidence = np.max(predictions[0])
    return {
        'class': predicted_class,
        'confidence': float(confidence)
    }

if __name__ == "__main__":
    uvicorn.run(app, host='localhost', port=8000)

I'm trying to run this code but this line is not working

#MODEL = tf.keras.models.load_model(r"D:\\Derin_Ogrenme\\doma\\saved_models\\models\\2")

I asked chatgpt about this and he gave me this line of code.

MODEL = TFSMLayer(r"D:\\Derin_Ogrenme\\doma\\saved_models\\models\\2", call_endpoint='serving_default')

When running gpt's line of code an error occurs at terminal here is the error.

Code:
TypeError: '_UserObject' object is not callable
INFO:     ::1:51237 - "POST /predict HTTP/1.1" 500 Internal Server Error
ERROR:    Exception in ASGI application
Traceback (most recent call last):
  File "C:\Users\cagla\anaconda3\Lib\site-packages\uvicorn\protocols\http\httptools_impl.py", line 399, in run_asgi
    result = await app(  # type: ignore[func-returns-value]
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\cagla\anaconda3\Lib\site-packages\uvicorn\middleware\proxy_headers.py", line 70, in __call__
    return await self.app(scope, receive, send)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\cagla\anaconda3\Lib\site-packages\fastapi\applications.py", line 1054, in __call__
    await super().__call__(scope, receive, send)
  File "C:\Users\cagla\anaconda3\Lib\site-packages\starlette\applications.py", line 123, in __call__
    await self.middleware_stack(scope, receive, send)
  File "C:\Users\cagla\anaconda3\Lib\site-packages\starlette\middleware\errors.py", line 186, in __call__
    raise exc
  File "C:\Users\cagla\anaconda3\Lib\site-packages\starlette\middleware\errors.py", line 164, in __call__
    await self.app(scope, receive, _send)
  File "C:\Users\cagla\anaconda3\Lib\site-packages\starlette\middleware\cors.py", line 93, in __call__
    await self.simple_response(scope, receive, send, request_headers=headers)
  File "C:\Users\cagla\anaconda3\Lib\site-packages\starlette\middleware\cors.py", line 148, in simple_response
    await self.app(scope, receive, send)
  File "C:\Users\cagla\anaconda3\Lib\site-packages\starlette\middleware\exceptions.py", line 65, in __call__
    await wrap_app_handling_exceptions(self.app, conn)(scope, receive, send)
  File "C:\Users\cagla\anaconda3\Lib\site-packages\starlette\_exception_handler.py", line 64, in wrapped_app
    raise exc
  File "C:\Users\cagla\anaconda3\Lib\site-packages\starlette\_exception_handler.py", line 53, in wrapped_app
    await app(scope, receive, sender)
  File "C:\Users\cagla\anaconda3\Lib\site-packages\starlette\routing.py", line 756, in __call__
    await self.middleware_stack(scope, receive, send)
  File "C:\Users\cagla\anaconda3\Lib\site-packages\starlette\routing.py", line 776, in app
    await route.handle(scope, receive, send)
  File "C:\Users\cagla\anaconda3\Lib\site-packages\starlette\routing.py", line 297, in handle
    await self.app(scope, receive, send)
  File "C:\Users\cagla\anaconda3\Lib\site-packages\starlette\routing.py", line 77, in app
    await wrap_app_handling_exceptions(app, request)(scope, receive, send)
  File "C:\Users\cagla\anaconda3\Lib\site-packages\starlette\_exception_handler.py", line 64, in wrapped_app
    raise exc
  File "C:\Users\cagla\anaconda3\Lib\site-packages\starlette\_exception_handler.py", line 53, in wrapped_app
    await app(scope, receive, sender)
  File "C:\Users\cagla\anaconda3\Lib\site-packages\starlette\routing.py", line 72, in app
    response = await func(request)
               ^^^^^^^^^^^^^^^^^^^
  File "C:\Users\cagla\anaconda3\Lib\site-packages\fastapi\routing.py", line 278, in app
    raw_response = await run_endpoint_function(
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\cagla\anaconda3\Lib\site-packages\fastapi\routing.py", line 191, in run_endpoint_function
    return await dependant.call(**values)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "d:\Derin_Ogrenme\doma\api\main.py", line 46, in predict
    predictions = MODEL(img_batch)
                  ^^^^^^^^^^^^^^^^
TypeError: '_UserObject' object is not callable

When I load plant growth with FastApi, it is supposed to make a prediction( it has to classify leaf pics. but as you can see in the photo, it results in an error.
V0avFFct.png

<pre><code>from fastapi import FastAPI, File, UploadFile
from fastapi.middleware.cors import CORSMiddleware
import uvicorn
import numpy as np
from io import BytesIO
from PIL import Image
from tensorflow.keras.layers import TFSMLayer
import tensorflow as tf

app = FastAPI()

origins = [
"http://localhost",
"http://localhost:3000",
]
app.add_middleware(
CORSMiddleware,
allow_origins=origins,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)



#MODEL = tf.keras.models.load_model(r"D:\Derin_Ogrenme\doma\saved_models\models\2")


MODEL = TFSMLayer(r"D:\Derin_Ogrenme\doma\saved_models\models\2", call_endpoint='serving_default')


CLASS_NAMES = ["Early Blight", "Late Blight", "Healthy"]

@app.get("/ping")
async def ping():
return "Hello, I am alive"

def read_file_as_image(data) -> np.ndarray:
image = np.array(Image.open(BytesIO(data)))
return image

@app.post("/predict")
async def predict(
file: UploadFile = File(...)
):
image = read_file_as_image(await file.read())
img_batch = np.expand_dims(image, 0)

predictions = MODEL.predict(img_batch)

predicted_class = CLASS_NAMES[np.argmax(predictions[0])]
confidence = np.max(predictions[0])
return {
'class': predicted_class,
'confidence': float(confidence)
}

if __name__ == "__main__":
uvicorn.run(app, host='localhost', port=8000)

</code></pre>
<p>I'm trying to run this code but this line is not working</p>
<p><code>#MODEL = tf.keras.models.load_model(r"D:\\Derin_Ogrenme\\doma\\saved_models\\models\\2")</code></p>
<p>I asked chatgpt about this and he gave me this line of code.</p>
<p><code>MODEL = TFSMLayer(r"D:\\Derin_Ogrenme\\doma\\saved_models\\models\\2", call_endpoint='serving_default')</code></p>
<p>When running gpt's line of code an error occurs at terminal
here is the error.</p>
<pre><code>TypeError: '_UserObject' object is not callable
INFO: ::1:51237 - "POST /predict HTTP/1.1" 500 Internal Server Error
ERROR: Exception in ASGI application
Traceback (most recent call last):
File "C:\Users\cagla\anaconda3\Lib\site-packages\uvicorn\protocols\http\httptools_impl.py", line 399, in run_asgi
result = await app( # type: ignore[func-returns-value]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\cagla\anaconda3\Lib\site-packages\uvicorn\middleware\proxy_headers.py", line 70, in __call__
return await self.app(scope, receive, send)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\cagla\anaconda3\Lib\site-packages\fastapi\applications.py", line 1054, in __call__
await super().__call__(scope, receive, send)
File "C:\Users\cagla\anaconda3\Lib\site-packages\starlette\applications.py", line 123, in __call__
await self.middleware_stack(scope, receive, send)
File "C:\Users\cagla\anaconda3\Lib\site-packages\starlette\middleware\errors.py", line 186, in __call__
raise exc
File "C:\Users\cagla\anaconda3\Lib\site-packages\starlette\middleware\errors.py", line 164, in __call__
await self.app(scope, receive, _send)
File "C:\Users\cagla\anaconda3\Lib\site-packages\starlette\middleware\cors.py", line 93, in __call__
await self.simple_response(scope, receive, send, request_headers=headers)
File "C:\Users\cagla\anaconda3\Lib\site-packages\starlette\middleware\cors.py", line 148, in simple_response
await self.app(scope, receive, send)
File "C:\Users\cagla\anaconda3\Lib\site-packages\starlette\middleware\exceptions.py", line 65, in __call__
await wrap_app_handling_exceptions(self.app, conn)(scope, receive, send)
File "C:\Users\cagla\anaconda3\Lib\site-packages\starlette\_exception_handler.py", line 64, in wrapped_app
raise exc
File "C:\Users\cagla\anaconda3\Lib\site-packages\starlette\_exception_handler.py", line 53, in wrapped_app
await app(scope, receive, sender)
File "C:\Users\cagla\anaconda3\Lib\site-packages\starlette\routing.py", line 756, in __call__
await self.middleware_stack(scope, receive, send)
File "C:\Users\cagla\anaconda3\Lib\site-packages\starlette\routing.py", line 776, in app
await route.handle(scope, receive, send)
File "C:\Users\cagla\anaconda3\Lib\site-packages\starlette\routing.py", line 297, in handle
await self.app(scope, receive, send)
File "C:\Users\cagla\anaconda3\Lib\site-packages\starlette\routing.py", line 77, in app
await wrap_app_handling_exceptions(app, request)(scope, receive, send)
File "C:\Users\cagla\anaconda3\Lib\site-packages\starlette\_exception_handler.py", line 64, in wrapped_app
raise exc
File "C:\Users\cagla\anaconda3\Lib\site-packages\starlette\_exception_handler.py", line 53, in wrapped_app
await app(scope, receive, sender)
File "C:\Users\cagla\anaconda3\Lib\site-packages\starlette\routing.py", line 72, in app
response = await func(request)
^^^^^^^^^^^^^^^^^^^
File "C:\Users\cagla\anaconda3\Lib\site-packages\fastapi\routing.py", line 278, in app
raw_response = await run_endpoint_function(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\cagla\anaconda3\Lib\site-packages\fastapi\routing.py", line 191, in run_endpoint_function
return await dependant.call(**values)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "d:\Derin_Ogrenme\doma\api\main.py", line 46, in predict
predictions = MODEL(img_batch)
^^^^^^^^^^^^^^^^
TypeError: '_UserObject' object is not callable

</code></pre>
<p>When I load plant growth with FastApi, it is supposed to make a prediction( it has to classify leaf pics. but as you can see in the photo, it results in an error.
<img src="https://i.sstatic.net/V0avFFct.png" alt="" /></p>
 

Latest posts

Online statistics

Members online
0
Guests online
5
Total visitors
5
Top