C1W1 ungraded lab

Hi everyone,

I was able to go through the exercise and complete the challenge in part 2. But then I tried to see what happens if I change the prediction function a bit. I commented out everything to do with the image, got it to read in an integer and a .txt file instead of an image and then tried to see if I could just return the integer or have the function not return anything at all or have the function return some HTML content. The GET part works fine , the POST doesn’t for any of the returns I tried. It gives error 500. My prediction function:

def prediction(num: int, file: UploadFile = File(…)):
# 1. VALIDATE INPUT FILE
filename = file.filename
fileExtension = filename.split(“.”)[-1] in (“txt”)
if not fileExtension:
raise HTTPException(status_code=415, detail=“Unsupported file provided.”)

# 3. RUN OBJECT DETECTION MODEL

# Run object detection
test_out = num

html_content = """
<html>
    <head>
        <title>Some HTML in here</title>
    </head>
    <body>
        <h1>Look ma! HTML!</h1>
    </body>
</html>
"""
return test_out

#return HTMLResponse(content=html_content, status_code=200)


I’m afraid I’m new to all of this API, POST, GET, etc. stuff so am just trying things out with trial and error. Could anyone give me some hints perhaps? What if I don’t want to return an image but just return a string with the content/name of the uploaded file? what if I just want to return a random integer?

Cheers
Sheema

Before moving further, I recommend you start at mozilla developer network to understand what HTTP methods are.

After that, please ask specific questions regarding the lab.

As an example, save the following code in a python file and run it:

import uvicorn
from fastapi import FastAPI
from starlette.responses import HTMLResponse

app = FastAPI()


@app.get('/welcome/{name}')
def get_welcome_message(name):
    return HTMLResponse(
        f"""
        <html>
            <head>
                <body>
                    <h1>Welcome {name}<h1>
                </body>
            </head>
        </html>
        """
    )


uvicorn.run(app)

Open your browser and visit http://localhost:8000/welcome/YOUR_NAME to get a response.

1 Like

Thanks a lot. I will look into it. Though I am quite able to play around with the GET part it’s the POST I’m struggling with.