Sobit commited on
Commit
3aaed2f
โ€ข
1 Parent(s): a3496fe

Upload 3 files

Browse files
Files changed (3) hide show
  1. app.py +65 -0
  2. requirements.txt +56 -0
  3. vgg.h5 +3 -0
app.py ADDED
@@ -0,0 +1,65 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI, UploadFile, File
2
+ import uvicorn
3
+ import tensorflow as tf
4
+ from tensorflow import keras
5
+ from keras import models
6
+ from PIL import Image
7
+ from io import BytesIO
8
+ import numpy as np
9
+ import cv2
10
+
11
+ # Some constants to be used in the program
12
+ IMG_SIZE = (32,32)
13
+ APP_HOST = '127.0.1.1'
14
+ APP_PORT = '5000'
15
+
16
+ # Character mapping for the character prediction
17
+ char_map = {
18
+ 0:'๐‘‘(0)', 1:'๐‘‘‘(1)', 2:'๐‘‘’(2)', 3:'๐‘‘“(3)', 4: '๐‘‘”(4)', 5: '๐‘‘•(5)', 6: '๐‘‘–(6)', 7: '๐‘‘—(7)',
19
+ 8:'๐‘‘˜(8)', 9:'๐‘‘™(9)', 10:'๐‘‘‰(OM)', 11:'๐‘€(A)', 12: '๐‘(AA)', 13: '๐‘€๐‘‘…(AH)', 14: '๐‘‚(I)',
20
+ 15:'๐‘ƒ(II)',16:'๐‘„(U)', 17:'๐‘…(UU)', 18:'๐‘†(R)', 19: '๐‘†๐‘บ(RR)', 20: '๐‘Š(E)', 21: '๐‘‹(AI)', 22: '๐‘Œ(O)',
21
+ 23:'๐‘(AU)', 24:'๐‘ˆ(L)', 25:'๐‘‰(LL)', 26:'๐‘Ž(KA)', 27: '๐‘Ž๐‘‘‚๐‘ณ(KSA)', 28: '๐‘(KHA)',29: '๐‘(GA)', 30: '๐‘‘(GHA)',
22
+ 31:'๐‘’(NGA)',32:'๐‘”(CA)', 33:'๐‘•(CHA)', 34:'๐‘–(JA)', 35: '๐‘–๐‘‘‚๐‘˜(JรฑA)', 36: '๐‘—(JHA)',37: '๐‘—(JHA-alt)',38: '๐‘˜(NYA)',
23
+ 39:'๐‘š(TA)', 40:'๐‘›(TTHA)', 41:'๐‘œ(DDA)', 42:'๐‘(DHA)', 43: '๐‘ž(NNA)', 44: '๐‘Ÿ(TA)', 45: '๐‘Ÿ๐‘‘‚๐‘ฌ(TRA)', 46: '๐‘ (THA)',
24
+ 47:'๐‘ก(DA)', 49:'๐‘ฃ(NA)', 50:'๐‘ฅ(PA)', 51:'๐‘ฆ(PHA)', 52: '๐‘ง(BA)', 53: '๐‘จ(BHA)', 54: '๐‘ฉ(MA)', 55: '๐‘ซ(YA)',
25
+ 56:'๐‘ฌ(RA)', 57: '๐‘ฎ(LA)', 58:'๐‘ฐ(WA)', 59:'๐‘ฑ(SHA)', 60: '๐‘ฑ(SHA-alt)', 61: '๐‘ฒ(SSA)', 62: '๐‘ณ(SA)', 63: '๐‘ด(HA)'
26
+ }
27
+
28
+
29
+
30
+ # Importing the model
31
+ model = models.load_model('vgg.h5')
32
+
33
+
34
+ # Defining the FastAPI instance here
35
+ app = FastAPI()
36
+
37
+ # Function for reading image
38
+ def file_to_array(data) -> np.ndarray:
39
+ image = np.array(Image.open(BytesIO(data)))
40
+
41
+ return image
42
+
43
+
44
+ @app.get('/')
45
+ async def root_func():
46
+ return {'message': 'this is the root function'}
47
+
48
+
49
+ @app.post('/predict_image')
50
+ async def upload_image(file: UploadFile = File(...)):
51
+ image = Image.open(BytesIO(await file.read()))
52
+
53
+ image = cv2.resize(np.array(image), IMG_SIZE)
54
+ image = image.astype('float32')
55
+ image = np.expand_dims(image, axis=0)
56
+
57
+ output = model.predict(image)
58
+ result = char_map[np.argmax(output)]
59
+
60
+ return {'prediction': result}
61
+
62
+
63
+
64
+ if __name__ == "__main__":
65
+ uvicorn.run(app, host=APP_HOST, port=APP_PORT)
requirements.txt ADDED
@@ -0,0 +1,56 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ absl-py==1.4.0
2
+ anyio==3.6.2
3
+ astunparse==1.6.3
4
+ cachetools==5.3.0
5
+ certifi==2022.12.7
6
+ charset-normalizer==3.0.1
7
+ click==8.1.3
8
+ fastapi==0.92.0
9
+ flatbuffers==23.1.21
10
+ gast==0.4.0
11
+ google-auth==2.16.1
12
+ google-auth-oauthlib==0.4.6
13
+ google-pasta==0.2.0
14
+ grpcio==1.51.3
15
+ gunicorn==20.1.0
16
+ h11==0.14.0
17
+ h5py==3.8.0
18
+ httptools==0.5.0
19
+ idna==3.4
20
+ keras==2.11.0
21
+ libclang==15.0.6.1
22
+ Markdown==3.4.1
23
+ MarkupSafe==2.1.2
24
+ multipart==0.2.4
25
+ numpy==1.24.2
26
+ oauthlib==3.2.2
27
+ opencv-python==4.7.0.72
28
+ opt-einsum==3.3.0
29
+ packaging==23.0
30
+ protobuf==3.19.6
31
+ pyasn1==0.4.8
32
+ pyasn1-modules==0.2.8
33
+ pydantic==1.10.5
34
+ python-dotenv==1.0.0
35
+ PyYAML==6.0
36
+ requests==2.28.2
37
+ requests-oauthlib==1.3.1
38
+ rsa==4.9
39
+ six==1.16.0
40
+ sniffio==1.3.0
41
+ starlette==0.25.0
42
+ tensorboard==2.11.2
43
+ tensorboard-data-server==0.6.1
44
+ tensorboard-plugin-wit==1.8.1
45
+ tensorflow==2.11.0
46
+ tensorflow-estimator==2.11.0
47
+ tensorflow-io-gcs-filesystem==0.31.0
48
+ termcolor==2.2.0
49
+ typing_extensions==4.5.0
50
+ urllib3==1.26.14
51
+ uvicorn==0.20.0
52
+ uvloop==0.17.0
53
+ watchfiles==0.18.1
54
+ websockets==10.4
55
+ Werkzeug==2.2.3
56
+ wrapt==1.15.0
vgg.h5 ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:3da1c33e2d6095b0705c84e44993387e92a4b9e8b3d5e753c31f44b3c96ef97c
3
+ size 16506024