Michal Tichý commited on
Commit
0488394
1 Parent(s): 68d4e93
Files changed (3) hide show
  1. app.py +27 -0
  2. requirements.txt +2 -0
  3. utils.py +37 -0
app.py ADDED
@@ -0,0 +1,27 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from utils import PingApi
3
+
4
+
5
+ def inference(img):
6
+ papi = PingApi()
7
+ rsp = papi.ping_api(img, 'vin-mining')
8
+
9
+ gathered_data = rsp['pages'][0]['gathered_data']
10
+ parsed_rsp = {
11
+ gathered_data['fields']['VIN']['texts'][0]['text']: gathered_data['fields']['VIN']['texts'][0]['confidence']
12
+ }
13
+
14
+ return parsed_rsp
15
+
16
+
17
+ title = "Car VIN mining"
18
+ description = "Extract VIN code of vehicle, if present on the image"
19
+ examples = [['./examples/ex1.jpg'], ['./examples/ex2.jpg'], ['./examples/ex3.jpg']]
20
+ gr.Interface(
21
+ inference,
22
+ inputs=gr.Image(type="filepath"),
23
+ outputs=[gr.Label(num_top_classes=1, label='Output')],
24
+ title=title,
25
+ description=description,
26
+ examples=examples
27
+ ).launch()
requirements.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ requests
2
+ gradio
utils.py ADDED
@@ -0,0 +1,37 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import requests
3
+
4
+
5
+ class PingApi():
6
+ def __init__(self):
7
+ self.url = os.getenv('API_URL')
8
+ self.auth_url = os.getenv('AUTH_URL')
9
+ client_id = os.getenv('CLIENT_ID')
10
+ client_secret = os.getenv('CLIENT_SECRET')
11
+ self.auth_payload = f'grant_type=client_credentials&client_id={client_id}&client_secret={client_secret}&scope=categorization'
12
+ self.access_token = self.authorize()
13
+
14
+ def authorize(self):
15
+ headers = {'Content-Type': 'application/x-www-form-urlencoded'}
16
+ response = requests.request("POST", self.auth_url, headers=headers, data=self.auth_payload)
17
+ try:
18
+ access_token = response.json()['access_token']
19
+ except Exception as e:
20
+ print(response.text)
21
+ raise e
22
+ return access_token
23
+
24
+ def ping_api(self, f_path, endpoint):
25
+ payload = {}
26
+ headers = {
27
+ 'Authorization': f"Bearer {self.access_token}"
28
+ }
29
+ files = [('file', (os.path.basename(f_path), open(f_path, 'rb'), 'image/jpeg'))]
30
+ response = requests.request("POST", self.url + '/' + endpoint, headers=headers, data=payload, files=files)
31
+ try:
32
+ rsp_json = response.json()
33
+ except Exception as e:
34
+ print(response.text)
35
+ raise e
36
+
37
+ return rsp_json