1. Homepage
  2. Programming
  3. Assignment #2: FastAPI and LeNet-5

Assignment #2: FastAPI and LeNet-5

Engage in a Conversation
PythonFastAPILeNet-5

Assignment #2: FastAPI and LeNet-5 CourseNana.COM


Description CourseNana.COM

You have just learned that some large neural models (e.g. GPT-4) cannot run on everyday computers. Usually, such models are accessible to users via online services. That is to say, users do not need to run the big models on their own computers; instead, the models are running on some GPU/TPU servers. A user just sends something to a service at some IP address, and the service program will return something required by the user. CourseNana.COM

Usually, the service does not need to show a webpage—or a Web User Interface (Web UI) in computer jargon—to a developer, because the developer may not want to interact with a webpage in their app. Consider this situation, you develop an app that can mark and display your current location on Google Map. Then it’s a bad idea to embed a web browser and visit map.google.com in your app; instead, you call Google Map’s Application Programming Interfaces (APIs), to complete the “mark and display” task. With the structural returned data, you as a developer have the maximum freedom to utilize them. CourseNana.COM

To prevent abuse of APIs, an online service usually applies some means of authorization. In simple words, the service would provide something similar to the “username and password” mechanism to a developer, then the developer will use the “username and password” mechanism to access the APIs. However, it is a very bad idea to save plaintext passwords on the server side, because once the server is hacked, the plaintext passwords will be exploited by hackers. A cleverer way to solve the problem is to save the hash values on the server side (Note that, it’s still not the best practice, but let’s just keep the solution simple). A hash function produces a fixed-length “fingerprint” to any input, and it’s difficult in the sense of computation to guess the original input from the fingerprint. SHA-1, for example, is a common hash function widely used for file comparison. That is to say, if CourseNana.COM

SHA1(file1) = cb9b33bdd36cfc2e42a71ef6c004b5c8bf19ef95 CourseNana.COM

SHA2(file2) = cb9b33bdd36cfc2e42a71ef6c004b5c8bf19ef95 CourseNana.COM

We’ll be very confident that file1 and file2 are the same file. And also, it’s nearly impossible for anyone to recover the input from cb9b33bdd36cfc2e42a71ef6c004b5c8bf19ef95. By saving the hash values on the server, we don’t need to worry too much if hackers get them. CourseNana.COM

Now, here comes the task— CourseNana.COM

Task (20 points) CourseNana.COM

NOTE As some students reported that they had little or no experience in programming, and even Assignment #1 was time-consuming, I decide to reduce the number of assignments. Before adjustment, the assignments are CourseNana.COM

10 points x 5 mini-projects and after adjustment, the assignments become CourseNana.COM

#1 (10 points) + #2 (20 points) + #3 (20 points) CourseNana.COM

I hope that you can learn something new and useful from the mini-projects, so the mini- projects are designed as some kind of guidance for you. With less assignments, we now have more time to finish them. That is to say, there’ll be two weeks for this assignment. Relax, take your time :) CourseNana.COM

Consider you’re providing an online service that allows a user to upload a picture of a digit, and the service can recognize the number and return the result to the user. FastAPI is a handy while commercial–level Python package that can help you. First, you may want to install the dependencies: CourseNana.COM

pip install -r requirements.txt
Second, we provide a file fastapi_demo.py for you, you may run it via command line under the CourseNana.COM

same path where fastapi_demo.py is located: CourseNana.COM

uvicorn fastapi_demo:app --host 0.0.0.0 --port 8000 --reload CourseNana.COM

If everything goes well, you may open a browser and visit CourseNana.COM

http://127.0.0.1:8000/docs#/ CourseNana.COM

And you’ll see a webpage that can help you debug: CourseNana.COM

Since the admin user is hard-coded in fastapi_demo.py (that’s a bad idea, but let’s keep it the simple way): CourseNana.COM

admin_users_db = { "admin": { CourseNana.COM

"username": "admin",
"hashed_password": '$2b$12$FLl.CBwUBtvIONHKEYn2J.exN01is9T154Mud7/lETlN2pHKmnUaq', "disabled": False,
CourseNana.COM

} } CourseNana.COM

... you can click the Authorize button on the webpage, and sign in with CourseNana.COM

Username: admin Password: icsslab307 CourseNana.COM

... and then, click the /upload frame and click “Try it out”: CourseNana.COM

Fill in the “path” a filename, e.g. CourseNana.COM

./t.pdf CourseNana.COM

choose a file, and click Execute: CourseNana.COM

If everything goes well, you’ll see t.pdf exists under the same path of fastapi_demo.py. Finally, you need to modify the following function: CourseNana.COM

@app.post("/upload") CourseNana.COM

async def upload_file(path: Path, overwrite: bool = False, file: UploadFile = File(...), u: User = Depends(get_current_active_user)): CourseNana.COM

try:
if path.is_file() and not overwrite:
CourseNana.COM

return {"status": "error", "return": "file already exists"} with path.open('wb') as buffer: CourseNana.COM

shutil.copyfileobj(file.file, buffer) except Exception as e: CourseNana.COM

return {"status": "error", "return": str(e)} finally: CourseNana.COM

file.file.close() return {"status": "ok"} CourseNana.COM

After the user successfully upload a picture, this function should use the LeNet-5 model to predict the number, and return {"status": "ok", "result": num} CourseNana.COM

As you can see on the webpage: CourseNana.COM

A user can construct a post request to do the same thing without visit this webpage. See test/ test.py for example. CourseNana.COM

Now congratulations! you’ve successfully provided an online service API! If you have a public IP on the Internet, the whole world can access your API via the public IP! CourseNana.COM

The submission should include:
The modified fastapi_demo.py
The corresponding LeNet-5 model file
(For the final 2 points) test2.py, read the scoring table at the end for details CourseNana.COM

CourseNana.COM

Notes CourseNana.COM

The TAs will evaluate your submission as follows: CourseNana.COM

  1. Use command uvicorn fastapi_demo:app --host 0.0.0.0 --port 8000 --reload to start the service. CourseNana.COM

  2. Run test/test.py to upload an image, e.g. 5.jpg in that folder. CourseNana.COM

  3. Check the return string. Without any modification of fastapi_demo.py, step 2 still works, it returns {'status': 'ok'}. After finishing your code in fastapi_demo.py, for this test case, it CourseNana.COM

    should return {'status': 'ok', 'result': 5} or {'status': 'ok', 'result': '5'}. CourseNana.COM

Please also test your code using test/test.py before submission. Note that, it doesn’t matter if the ‘result’ is wrong—that is to say, you don’t need to train a 100% correct LeNet-5, since the aim of this assignment is to learn FastAPI. CourseNana.COM

Score Completeness CourseNana.COM

Plagiarism CourseNana.COM

The uvicorn command succeeds (TAs will install required packages) CourseNana.COM

Indeed add required packages in fastapi_demo.py, and add some codes in /upload function CourseNana.COM

Test.py can retrieve the results, but obviously the LeNet-5 model doesn’t work. That is, we’ll upload 10 images from the training dataset to the service, but >= 4 are incorrectly recognized. CourseNana.COM

Test.py can retrieve correct results. That is, the accuracy should >= 90%. CourseNana.COM

Add another admin user in the database—
Username: bob
Plaintext Password: 123456
Hint: Read fastapi_demo.py carefully, find a way to generate a hashed_password for 123456, and then add an entry in admin_users_db
CourseNana.COM

Modify and submit test2.py and it can successfully retrieve {'username': 'bob', 'disabled': False}. Of course, you cannot get 2 points here by just modifying admin’s username to bob CourseNana.COM

Get in Touch with Our Experts

WeChat (微信) WeChat (微信)
Whatsapp WhatsApp
Python代写,FastAPI代写,LeNet-5代写,Python代编,FastAPI代编,LeNet-5代编,Python代考,FastAPI代考,LeNet-5代考,Pythonhelp,FastAPIhelp,LeNet-5help,Python作业代写,FastAPI作业代写,LeNet-5作业代写,Python编程代写,FastAPI编程代写,LeNet-5编程代写,Pythonprogramming help,FastAPIprogramming help,LeNet-5programming help,Pythonassignment help,FastAPIassignment help,LeNet-5assignment help,Pythonsolution,FastAPIsolution,LeNet-5solution,