forked from mhezarei/ai-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeepmine.py
66 lines (51 loc) · 1.6 KB
/
deepmine.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Dec 21 10:55:00 2020
@author: mahsa
"""
import datetime
import json
import requests as rq
class Deepmine():
def __init__(self):
self.start = datetime.datetime.now()
self.token = self.get_token()
def get_token(self):
data = {
'grant_type': '',
'username': 'aibot',
'password': '[email protected]'
}
try:
response = rq.post('https://deepmine.ir:8095/token', data=data, verify=False)
except Exception:
return 0
token = json.loads(response.content)['access_token']
return token
def refresh_token(self):
if (datetime.datetime.now()-self.start).seconds > 3599 :
self.start = datetime.datetime.now()
self.token = self.get_token()
def get_text(self,file_path):
self.refresh_token()
if self.token == 0 :
return 0,"Error in get_token from Deepmine"
headers = {
'Authorization': 'Bearer '+self.token
}
params = (
('nbest', '1'),
)
files = {
'file': (file_path, open(file_path, 'rb')),
}
response = rq.post('https://deepmine.ir:8095/file2text', headers=headers, params=params, files=files, verify=False)
if response.status_code == 200:
try:
text = json.loads(response.content)['data'][0]['text']
return 1,text
except Exception as e:
return 0,e
else:
return 0,response.content