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.

Not able to train a spacy model using FASTAPI + UVICORN setup

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

from fastpi import FastAPI, Body
from spacy.cli.train import train

app = FastAPI()


@app.post("/training")
def root(key = Body(embed = True)):
    train("scratch_config.cfg", output_path="this_day", overrides={"paths.train": "train.spacy", "paths.dev": "valid.spacy"}) # this is the function used for training a spacy model

Description

Command to start the server:

uvicorn spacy_train:app --workers 4

Actual behaviour:

[2022-09-23 15:21:22 +0530] [24893] [ERROR] Exception in ASGI application                                                                                                                                  
Traceback (most recent call last):                                                                                                                                                                         
  File "/home/local/ZOHOCORP/ravi-9151/anaconda3/lib/python3.8/site-packages/uvicorn/protocols/http/h11_impl.py", line 404, in run_asgi                                                                    
    result = await app(  # type: ignore[func-returns-value]                                                                                                                                                
  File "/home/local/ZOHOCORP/ravi-9151/anaconda3/lib/python3.8/site-packages/uvicorn/middleware/proxy_headers.py", line 78, in __call__                                                                    
    return await self.app(scope, receive, send)                                                                                                                                                            
  File "/home/local/ZOHOCORP/ravi-9151/anaconda3/lib/python3.8/site-packages/fastapi/applications.py", line 269, in __call__
    await super().__call__(scope, receive, send)                                                                                                                                                           
  File "/home/local/ZOHOCORP/ravi-9151/anaconda3/lib/python3.8/site-packages/starlette/applications.py", line 124, in __call__                                                                             
    await self.middleware_stack(scope, receive, send)                                                                                                                                                      
  File "/home/local/ZOHOCORP/ravi-9151/anaconda3/lib/python3.8/site-packages/starlette/middleware/errors.py", line 184, in __call__                                                                        
    raise exc                                                                                                                                                                                              
  File "/home/local/ZOHOCORP/ravi-9151/anaconda3/lib/python3.8/site-packages/starlette/middleware/errors.py", line 162, in __call__                                                                        
    await self.app(scope, receive, _send)                                                                                                                                                                  
  File "/home/local/ZOHOCORP/ravi-9151/anaconda3/lib/python3.8/site-packages/starlette/exceptions.py", line 93, in __call__                                                                                
    raise exc                                                                                                                                                                                              
  File "/home/local/ZOHOCORP/ravi-9151/anaconda3/lib/python3.8/site-packages/starlette/exceptions.py", line 82, in __call__                                                                                
    await self.app(scope, receive, sender)                                                                                                                                                                 
  File "/home/local/ZOHOCORP/ravi-9151/anaconda3/lib/python3.8/site-packages/fastapi/middleware/asyncexitstack.py", line 21, in __call__
    raise e                                                                                                                                                                                                
  File "/home/local/ZOHOCORP/ravi-9151/anaconda3/lib/python3.8/site-packages/fastapi/middleware/asyncexitstack.py", line 18, in __call__
    await self.app(scope, receive, send)                                                             
  File "/home/local/ZOHOCORP/ravi-9151/anaconda3/lib/python3.8/site-packages/starlette/routing.py", line 670, in __call__
    await route.handle(scope, receive, send)                                                                                                                                                               
  File "/home/local/ZOHOCORP/ravi-9151/anaconda3/lib/python3.8/site-packages/starlette/routing.py", line 266, in handle                                                                                    
    await self.app(scope, receive, send)
  File "/home/local/ZOHOCORP/ravi-9151/anaconda3/lib/python3.8/site-packages/starlette/routing.py", line 65, in app
    response = await func(request)
  File "/home/local/ZOHOCORP/ravi-9151/anaconda3/lib/python3.8/site-packages/fastapi/routing.py", line 231, in app
    raw_response = await run_endpoint_function(
  File "/home/local/ZOHOCORP/ravi-9151/anaconda3/lib/python3.8/site-packages/fastapi/routing.py", line 162, in run_endpoint_function
    return await run_in_threadpool(dependant.call, **values)
  File "/home/local/ZOHOCORP/ravi-9151/anaconda3/lib/python3.8/site-packages/starlette/concurrency.py", line 41, in run_in_threadpool
    return await anyio.to_thread.run_sync(func, *args)
  File "/home/local/ZOHOCORP/ravi-9151/anaconda3/lib/python3.8/site-packages/anyio/to_thread.py", line 31, in run_sync
    return await get_asynclib().run_sync_in_worker_thread(
  File "/home/local/ZOHOCORP/ravi-9151/anaconda3/lib/python3.8/site-packages/anyio/_backends/_asyncio.py", line 937, in run_sync_in_worker_thread
    return await future
  File "/home/local/ZOHOCORP/ravi-9151/anaconda3/lib/python3.8/site-packages/anyio/_backends/_asyncio.py", line 867, in run
    result = context.run(func, *args)
  File "/home/local/ZOHOCORP/ravi-9151/Downloads/custom_ner/streamlit_pipeline/fast_api.py", line 119, in root
    train("scratch_config.cfg")
TypeError: 'list' object is not callable

Expected Behaviour:

_ Created output directory: this_day
_ Saving to output directory: this_day
_ Using CPU

=========================== Initializing pipeline ===========================
_ Initialized pipeline

============================= Training pipeline =============================
_ Pipeline: ['tok2vec', 'textcat']
_ Initial learn rate: 0.001
E    #       LOSS TOK2VEC  LOSS TEXTCAT  CATS_SCORE  ENTS_F  ENTS_P  ENTS_R  TAG_ACC  POS_ACC  MORPH_ACC  LEMMA_ACC  DEP_UAS  DEP_LAS  SENTS_F  SCORE 
---  ------  ------------  ------------  ----------  ------  ------  ------  -------  -------  ---------  ---------  -------  -------  -------  ------
  0       0          0.00          0.25       50.00    0.00    0.00    0.00     0.00     0.00       0.00       0.00     0.00     0.00     0.00    0.25
 69     200          0.00          7.03       50.00    0.00    0.00    0.00     0.00     0.00       0.00       0.00     0.00     0.00     0.00    0.25
169     400          0.00          0.09       50.00    0.00    0.00    0.00     0.00     0.00       0.00       0.00     0.00     0.00     0.00    0.25
269     600          0.00          0.03       50.00    0.00    0.00    0.00     0.00     0.00       0.00       0.00     0.00     0.00     0.00    0.25
382     800          0.00          0.02       50.00    0.00    0.00    0.00     0.00     0.00       0.00       0.00     0.00     0.00     0.00    0.25
582    1000          0.00          0.01       50.00    0.00    0.00    0.00     0.00     0.00       0.00       0.00     0.00     0.00     0.00    0.25
782    1200          0.00          0.01       50.00    0.00    0.00    0.00     0.00     0.00       0.00       0.00     0.00     0.00     0.00    0.25
982    1400          0.00          0.00       50.00    0.00    0.00    0.00     0.00     0.00       0.00       0.00     0.00     0.00     0.00    0.25
1182    1600          0.00          0.00       50.00    0.00    0.00    0.00     0.00     0.00       0.00       0.00     0.00     0.00     0.00    0.25
_ Saved pipeline to output directory
this_day/model-last

Operating System

Linux

Operating System Details

Ubuntu 20.04.1 LTS

FastAPI Version

0.82.0

Python Version

3.8.3

Additional Context

train("scratch_config.cfg", output_path="this_day", overrides={"paths.train": "train.spacy", "paths.dev": "valid.spacy"})

The above call works properly when i add it in a python file and execute it using the command python spacy_train.py

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
rkoystartcommented, Sep 27, 2022

Do you have any variable named train? As of your app implementation everything seems fine

Yes. Got the reason for the error. I had a list named train which was causing the error. Changing the list name to training resolved the issue. Thanks @iudeen

0reactions
tiangolocommented, Nov 14, 2022

Thanks for the help here @iudeen ! 👏 🙇

Thanks for reporting back and closing the issue @rkoystart 👍

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to Deploy an NLP Model with FastAPI - freeCodeCamp
First, we need to import some Python packages to load the data, clean the data, create a machine learning model (classifier), and save...
Read more >
Production-Ready Machine Learning NLP API with FastAPI ...
Let me tell you why we made such a choice, and show you how to implement an API based on FastAPI and spaCy...
Read more >
Deploying Your First Machine Learning API | by Abid Ali Awan
In Jupyter notebook, you can run the app using 👇 or in the terminal just type uvicorn then the main file and with...
Read more >
Can we train multiple NER spacy model at the same time?
I have created an application using FastAPI and running from the VM server by exposing the IP using the Unicorn server command [uvicorn...
Read more >
Training Pipelines & Models · spaCy Usage Documentation
If you have hyperparameters or other settings used by multiple components, define them once and reference them as variables. Reproducibility with no hidden ......
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