This error is being thrown in the GET and POST functions that are defined. I tried installing it several times using conda, cmd as well as in the jupyter notebook itself using ‘!pip install python-multipart’ but the same error is being shown again and again. If possible, please help.
This is the function I’m talking about:
# Assign an instance of the FastAPI class to the variable "app".
# You will interact with your api using this instance.
app = FastAPI(title='Deploying a ML Model with FastAPI')
# List available models using Enum for convenience. This is useful when the options are pre-defined.
class Model(str, Enum):
yolov3tiny = "yolov3-tiny"
yolov3 = "yolov3"
# By using @app.get("/") you are allowing the GET method to work for the / endpoint.
@app.get("/")
def home():
return "Congratulations! Your API is working as expected. Now head over to http://localhost:8000/docs."
# This endpoint handles all the logic necessary for the object detection to work.
# It requires the desired model and the image in which to perform object detection.
@app.post("/predict")
def prediction(model: Model, file: UploadFile = File(...)):
# 1. VALIDATE INPUT FILE
filename = file.filename
fileExtension = filename.split(".")[-1] in ("jpg", "jpeg", "png")
if not fileExtension:
raise HTTPException(status_code=415, detail="Unsupported file provided.")
# 2. TRANSFORM RAW IMAGE INTO CV2 image
# Read image as a stream of bytes
image_stream = io.BytesIO(file.file.read())
# Start the stream from the beginning (position zero)
image_stream.seek(0)
# Write the stream of bytes into a numpy array
file_bytes = np.asarray(bytearray(image_stream.read()), dtype=np.uint8)
# Decode the numpy array as an image
image = cv2.imdecode(file_bytes, cv2.IMREAD_COLOR)
# 3. RUN OBJECT DETECTION MODEL
# Run object detection
bbox, label, conf = cv.detect_common_objects(image, model=model)
# Create image that includes bounding boxes and labels
output_image = draw_bbox(image, bbox, label, conf)
# Save it in a folder within the server
cv2.imwrite(f'images_uploaded/{filename}', output_image)
# 4. STREAM THE RESPONSE BACK TO THE CLIENT
# Open the saved image for reading in binary mode
file_image = open(f'images_uploaded/{filename}', mode="rb")
# Return the image as a stream specifying media type
return StreamingResponse(file_image, media_type="image/jpeg")
And this is the error:
Form data requires "python-multipart" to be installed.
You can install "python-multipart" with:
pip install python-multipart
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
c:\users\sh1va\anaconda3\envs\mlep-w1-lab\lib\site-packages\fastapi\dependencies\utils.py in check_file_field(field)
92 # __version__ is available in both multiparts, and can be mocked
---> 93 from multipart import __version__ # type: ignore
94
ImportError: cannot import name '__version__' from 'multipart' (unknown location)
During handling of the above exception, another exception occurred:
RuntimeError Traceback (most recent call last)
<ipython-input-10-dab682a97309> in <module>
18 # It requires the desired model and the image in which to perform object detection.
19 @app.post("/predict")
---> 20 def prediction(model: Model, file: UploadFile = File(...)):
21
22 # 1. VALIDATE INPUT FILE
c:\users\sh1va\anaconda3\envs\mlep-w1-lab\lib\site-packages\fastapi\routing.py in decorator(func)
572 response_class=response_class,
573 name=name,
--> 574 callbacks=callbacks,
575 )
576 return func
c:\users\sh1va\anaconda3\envs\mlep-w1-lab\lib\site-packages\fastapi\routing.py in add_api_route(self, path, endpoint, response_model, status_code, tags, dependencies, summary, description, response_description, responses, deprecated, methods, operation_id, response_model_include, response_model_exclude, response_model_by_alias, response_model_exclude_unset, response_model_exclude_defaults, response_model_exclude_none, include_in_schema, response_class, name, route_class_override, callbacks)
518 name=name,
519 dependency_overrides_provider=self.dependency_overrides_provider,
--> 520 callbacks=current_callbacks,
521 )
522 self.routes.append(route)
c:\users\sh1va\anaconda3\envs\mlep-w1-lab\lib\site-packages\fastapi\routing.py in __init__(self, path, endpoint, response_model, status_code, tags, dependencies, summary, description, response_description, responses, deprecated, name, methods, operation_id, response_model_include, response_model_exclude, response_model_by_alias, response_model_exclude_unset, response_model_exclude_defaults, response_model_exclude_none, include_in_schema, response_class, dependency_overrides_provider, callbacks)
384 get_parameterless_sub_dependant(depends=depends, path=self.path_format),
385 )
--> 386 self.body_field = get_body_field(dependant=self.dependant, name=self.unique_id)
387 self.dependency_overrides_provider = dependency_overrides_provider
388 self.callbacks = callbacks
c:\users\sh1va\anaconda3\envs\mlep-w1-lab\lib\site-packages\fastapi\dependencies\utils.py in get_body_field(dependant, name)
780 field_info=BodyFieldInfo(**BodyFieldInfo_kwargs),
781 )
--> 782 check_file_field(final_field)
783 return final_field
c:\users\sh1va\anaconda3\envs\mlep-w1-lab\lib\site-packages\fastapi\dependencies\utils.py in check_file_field(field)
104 except ImportError:
105 logger.error(multipart_not_installed_error)
--> 106 raise RuntimeError(multipart_not_installed_error)
107
108
RuntimeError: Form data requires "python-multipart" to be installed.
You can install "python-multipart" with:
pip install python-multipart