1

I am writing an API for an AI model. This AI model gets images from a folder named input, does the process on the image or images and outputs the result in another folder named output in CSV format, which is then converted into JSON and returned to the user. This API is going to be used in a website, which will be used by many users in the future.

This is the code that I wrote so far and I know it's not a good practice, for example what if 100 users want to use this feature concurrently?

The AI model is a script in main.py file.

from fastapi import FastAPI, File, UploadFile
import pandas as pd
import csv 
import json 



IMAGEDIR = "inputs/"
app = FastAPI()


@app.post("/upload_image/")
async def upload_image(img: UploadFile = File(...)):
    img.filename = f"new.jpg"
    contents = await img.read()

    with open(f"{IMAGEDIR}{img.filename}", "wb") as f:
        f.write(contents)
    
    result = {}
    try:
        import main
        result = csv_to_json('outputs/Results/Results_1.csv')
        return result
    except:
        raise('Server Error')
        result = {'error': 'server error'}
    


def csv_to_json(csvFilePath):
    jsonArray = []
    #read csv file
    with open(csvFilePath, encoding='utf-8') as csvf: 
        #load csv file data using csv library's dictionary reader
        csvReader = csv.DictReader(csvf) 

        #convert each csv row into python dict
        for row in csvReader: 
            #add this python dict to json array
            jsonArray.append(row)


    return jsonArray

What is the correct way to solve this problem?

Nima
  • 41
  • 1
  • 2
  • 7
  • I might be missing something, but what problem? Doesn't your code work? – Yevhen Kuzmovych Feb 20 '23 at 11:18
  • It works just fine. but I don't know how it handles multiple users requests. I mean how to make such API work on larger scale. This code gets an image and name it new.jpg. what will happen if for example 3 users use this API at the same time. I don't know about these scenarios. or if fastapi handles it – Nima Feb 20 '23 at 11:50
  • Does this answer your question? [How to upload a CSV file in FastAPI and convert it into JSON?](https://stackoverflow.com/questions/70617121/how-to-upload-a-csv-file-in-fastapi-and-convert-it-into-json) – Chris Feb 20 '23 at 11:51
  • unfortunately no – Nima Feb 20 '23 at 11:57
  • 1
    [Don't optimize prematuraly](https://stackify.com/premature-optimization-evil/). But if you want to test it, read about load testing. – Yevhen Kuzmovych Feb 20 '23 at 12:46
  • 1
    In addition to [this answer](https://stackoverflow.com/a/70655118/17865804), please have a look at [this answer](https://stackoverflow.com/a/71517830/17865804) as well, which explains the difference between using `async def` and `def` endpoints, as well as provides solutions when you need to run synchronous blocking operations inside `async def` endpoints. – Chris Feb 22 '23 at 16:41
  • Please have a look at related answers [here](https://stackoverflow.com/a/70667530/17865804) and [here](https://stackoverflow.com/a/73443824/17865804) as well. – Chris Apr 22 '23 at 11:58

1 Answers1

1

You are using blocking operations inside an async function, which causes the event loop (created by FastApi) get blocked and be unable to handle more requests. Using libraries like aiofile might improve the performance; Although I'm not sure how much improvement you can get, since the Disk i/o operations might be the bottleneck then.

mehran si
  • 26
  • 1