Upload Files to Your AI App with FastAPI
Job to be done: Implement file upload functionality in a FastAPI application for AI tools
🇳🇬 Ways to use this in Nigeria
Ideas to get you started, adapt to your situation.
- Student
Build a web app to upload lecture notes for AI analysis, like summarization or quiz generation.
- 9-5 employee
Create a tool to upload daily reports for automated data extraction and summary generation.
- Entrepreneur
Develop a platform where users can upload design drafts for AI-powered feedback and iteration.
What you’ll get
You will learn how to create a simple web application using FastAPI that can accept file uploads from users. This is a fundamental step for many AI applications that need to process user-provided data like documents or images. This approach works because FastAPI is designed to handle web requests efficiently, and it integrates seamlessly with libraries that manage file uploads.
Tools you need
- FastAPI (free): A modern, fast (high-performance) web framework for building APIs with Python.
- python-multipart (free): A library that FastAPI uses to handle file uploads.
- Python (free): The programming language used to build the application. You’ll need Python installed on your computer.
- A code editor (free): Such as VS Code, to write and manage your code.
- A terminal or command prompt (free): To install packages and run your application.
Steps
-
Install FastAPI and python-multipart: Open your terminal and run the following command to install the necessary libraries:
pip install fastapi python-multipart uvicorn[standard]You should see messages indicating that the packages have been successfully installed.
-
Create your FastAPI application file: In your code editor, create a new Python file (e.g.,
main.py) and add the following code to set up a basic FastAPI application:from fastapi import FastAPI, File, UploadFile import shutil app = FastAPI() @app.post("/uploadfile/") async def create_upload_file(file: UploadFile = File(...)): return {"filename": file.filename}This code defines an API endpoint
/uploadfile/that expects a file to be uploaded. It will return the filename of the uploaded file. -
Run the FastAPI application: Open your terminal, navigate to the directory where you saved
main.py, and run the following command:uvicorn main:app --reloadYou should see output indicating that the server is running, usually at
http://127.0.0.1:8000. -
Test the file upload: Open your web browser and go to
http://127.0.0.1:8000/docs. This will open the interactive API documentation (Swagger UI). Find the/uploadfile/endpoint, click on it, then click the “Try it out” button. You will see an option to upload a file. Click “Choose File”, select a file from your computer, and then click the “Execute” button. You should see a response like{"filename": "your_file_name.ext"}. -
Modify the endpoint to save the file: To actually save the uploaded file on your server, replace the content of
main.pywith the following code:from fastapi import FastAPI, File, UploadFile import shutil app = FastAPI() @app.post("/uploadfile/") async def create_upload_file(file: UploadFile = File(...)): try: with open(file.filename, "wb") as buffer: shutil.copyfileobj(file.file, buffer) finally: await file.close() return {"message": f"File {file.filename} uploaded successfully and saved.", "filename": file.filename}This code now saves the uploaded file to the same directory where your script is running. You will need to restart your
uvicornserver for these changes to take effect (the--reloadflag usually handles this automatically). -
Test saving the file: Go back to
http://127.0.0.1:8000/docs, click “Try it out” for/uploadfile/, upload a file, and click “Execute”. You should now see a success message, and the file will appear in your project folder.
Original source
This workflow is adapted from a guide by zeroshotanu on the DEV Community platform, focusing on how to implement file uploads within FastAPI applications as part of a series for AI engineers.
Notes & variations
- Free tier alternative: FastAPI and python-multipart are open-source and free to use. The main cost is your time and the computing resources to run the Python application.
- Common mistake: Forgetting to close the
UploadFileobject after reading or saving its contents can lead to resource leaks. Thefinally: await file.close()block in the updated code handles this. - Tip for better results: For AI applications, you’ll often want to process the file’s content immediately after saving or reading it. You can add your AI processing logic within the same endpoint function after the file has been saved or read using
await file.read().