4 months ago
Hi, I am using NLTK packages in my fastapi app. I am getting NLTK data through a local folder (Not using ntlk.download inside the app.). I explicitly set NTLK folder path in my fastapi like this :
nltk.data.path.append(os.getenv("NLTK_DATA", "./nltk_data"))
And I also create an env variable in railway for this.
When I test this with my local enviroment it works fine but deployment does not work. I am not getting any error in the process but want to share logs anyway:
Database connection successful
NLTK data path: ['./nltk_data']
INFO: Started server process [1]
INFO: Waiting for application startup.
INFO: Application startup complete.
INFO: Uvicorn running on http://0.0.0.0:8000 (Press CTRL+C to quit)
Database connection successful
NLTK data path: ['./nltk_data']
INFO: Started server process [1]
INFO: Waiting for application startup.
INFO: Application startup complete.
INFO: Uvicorn running on http://0.0.0.0:8000 (Press CTRL+C to quit)
INFO: 100.64.0.2:47100 - "GET /api/sentences/soon?categories=news,science,psychology,encyclopedia&page=1 HTTP/1.1" 200 OK
Database connection successful
Realize that my database connection is somehow repeated. I am suspecting maybe this causes to problem but this is not happening in my local env. either.
I also getting this errors in website logs :
Access to XMLHttpRequest at 'https://articlewbackend-production.up.railway.app/api/wordInfo/soon' from origin 'https://learn-with-articles-next-lybkelxr7-kaya70875s-projects.vercel.app' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.Understand this errorAI
articlewbackend-production.up.railway.app/api/wordInfo/soon:1
Failed to load resource: net::ERR_FAILED
I set origins to access from anywhere so CORS policy probably not important here.
Here you can check my repo it is public.
https://github.com/kaya70875/articlewbackend
Also I am using python 3.11.9 for this deployment with a custom runtime.txt file.
Here my other configs which is default
{ "$schema": "https://railway.com/railway.schema.json", "build": { "builder": "NIXPACKS" }, "deploy": { "runtime": "V2", "numReplicas": 1, "startCommand": "python -m uvicorn app.main:app --host 0.0.0.0 --port 8000 --log-level debug", "sleepApplication": false, "multiRegionConfig": { "us-west2": { "numReplicas": 1 } }, "restartPolicyType": "ON_FAILURE", "restartPolicyMaxRetries": 10 } }
3 Replies
4 months ago
To address the issues you're facing with NLTK and CORS in your FastAPI app on Railway, here are some suggestions:
NLTK Data Path: Ensure that the
nltk_data
directory is included in your project and is being deployed correctly. You might want to check if the directory is being ignored by.gitignore
or not being included in the deployment package.CORS Policy: The CORS error indicates that your API is not allowing requests from your frontend's origin. You can resolve this by configuring CORS in your FastAPI app. Here's a quick way to set it up:
from fastapi.middleware.cors import CORSMiddleware
app.add_middleware(
CORSMiddleware,
allow_origins=["*"], # Adjust this to your specific frontend URL for better security
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
Database Connection Logs: The repeated database connection logs might indicate that your app is restarting. Check if there are any resource limits being hit or if the app is crashing and restarting. You can increase logging verbosity to get more insights.
Environment Variables: Double-check that your environment variables are correctly set in Railway. You can manage them through the Railway dashboard under the "Variables" section. For more details, refer to the Using Variables guide.
Python Version: Ensure that the Python version specified in your
runtime.txt
is supported by Railway. You can find more about supported versions in the Python deployment guide.
For more detailed guidance on deploying FastAPI apps, you can refer to the FastAPI deployment guide. If these steps don't resolve your issues, please provide more specific error messages or logs.
Status changed to Awaiting User Response railway[bot] • 4 months ago
4 months ago
Hello,
Your deployment has ran out of memory over and over -
You will need to upgrade to a paid plan in order to access more than 500MB of memory.
Best,
Brody
Attachments
4 months ago
I limited my memory and tested it in my local environment, and I can safely say the problem is due to limited memory. The NLTK process uses about 500MB of memory, but while my deployment does not crash, it is still failing to retrieve NLTK data. So, I'll upgrade my plan, I think.
Status changed to Awaiting Railway Response railway[bot] • 4 months ago
Status changed to Solved brody • 4 months ago