Skip to content

Commit

Permalink
add fastapi server to upload doc
Browse files Browse the repository at this point in the history
  • Loading branch information
jbcodeforce committed May 1, 2024
1 parent 3f02bb5 commit e7395ab
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 1 deletion.
30 changes: 29 additions & 1 deletion docs/fastapi/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,29 @@

[FastAPI](https://fastapi.tiangolo.com/) helps to build backend REST api in python.

To start an app use uvicorn
## Boilerplate

```python

```

## Running

To start an app, use uvicorn in a shell script

```sh
uvicorn orchestrator_api:app --host 0.0.0.0 --port 8000 --reload
```

Or add this in the main server python code:

```python
import uvicorn

if __name__ == "__main__":
uvicorn.run(app, host="0.0.0.0", port=8000)
```


* Some URLs

Expand All @@ -16,4 +33,15 @@ http://127.0.0.1:8000/docs
http://127.0.0.1:8000/redoc
```


## How tos

* [A server with a websocket listener](https://github.com/jbcodeforce/python-code/tree/master/web_server/websocket_server)
* [Test to upload file and pydantic object in the same URL](https://github.com/jbcodeforce/python-code/tree/master/web_server/file_upload)
* [Expose async function to stream content with a generator](https://github.com/jbcodeforce/python-code/tree/master/web_server/api_stream). It uses yield and asyncio


## Some content to read

* [See full tutorial](https://fastapi.tiangolo.com/tutorial/)

14 changes: 14 additions & 0 deletions web_server/file_upload/client_upload_file.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import requests
from server import FileDescription

url="http://localhost:8000/upload/"
files = {'aFile': ("ibu-claims-complaint-rules.pdf", open('./ibu-claims-complaint-rules.pdf', 'rb'), "application/pdf")}

fd= FileDescription(name="claim_complaint_rules",
description="a set of rules to manage complaints",
type="pdf",
file_name="ibu-claims-complaint-rules.pdf")

response = requests.post(url, params=fd , files=files)

print(response.text.encode("utf-8"))
Binary file not shown.
33 changes: 33 additions & 0 deletions web_server/file_upload/server.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
from fastapi import FastAPI, UploadFile, File, Depends
from pypdf import PdfReader
from pydantic import BaseModel
from typing import Optional
import uvicorn

app = FastAPI()
class FileDescription(BaseModel):
name: Optional[str] = ''
description: Optional[str] = ''
type: str= "md"
file_name: Optional[str] = ''


@app.post("/upload")
async def upload_file(fd: FileDescription = Depends(), aFile: UploadFile = File(...)):
print(fd)
contents = await aFile.read()
f = open("./file.pdf", "wb")
f.write(contents)
f.close()
reader = PdfReader("./file.pdf")
txt = ""
for page in range(len(reader.pages)):
pageObj = reader.pages[page]
txt+= pageObj.extract_text(extraction_mode="layout")
rettxt=txt.encode("utf-8")
return rettxt



if __name__ == "__main__":
uvicorn.run(app, host="0.0.0.0", port=8000)

0 comments on commit e7395ab

Please sign in to comment.