SamSwift commited on
Commit
173f590
·
1 Parent(s): 85adc84

Upload inference.py

Browse files
Files changed (1) hide show
  1. inference.py +65 -0
inference.py ADDED
@@ -0,0 +1,65 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import json
2
+ import httpx
3
+
4
+
5
+ API_TOKEN = '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'
6
+ headers = {"Authorization": f"Bearer {API_TOKEN}"}
7
+
8
+
9
+ def query(filename, lang):
10
+ """
11
+ Function to get inference for three languages
12
+
13
+ """
14
+
15
+
16
+ if (lang == 'ha') and (filename is not None):
17
+ API_URL = "https://api-inference.huggingface.co/models/Tiamz/hausa-4-ha-wa2vec-data-aug-xls-r-300m"
18
+ with open(filename, "rb") as f:
19
+ data = f.read()
20
+ try:
21
+ response = httpx.request("POST", API_URL, headers=headers, data=data)
22
+ command = json.loads(response.content.decode("utf-8"))
23
+ command = command['text']
24
+ if command != '':
25
+ print(command)
26
+ return command
27
+ else:
28
+ print('No response yet')
29
+ except KeyError as e:
30
+ print('Model is still loading, please wait!')
31
+
32
+ elif (lang == 'yo') and (filename is not None):
33
+ API_URL = "https://api-inference.huggingface.co/models/Ayoola/cdial-yoruba-test"
34
+ with open(filename, "rb") as f:
35
+ data = f.read()
36
+
37
+ try:
38
+ response = httpx.request("POST", API_URL, headers=headers, data=data)
39
+ command = json.loads(response.content.decode("utf-8"))
40
+ command = command['text']
41
+ if command != '':
42
+ print(command)
43
+ return command
44
+ else:
45
+ pass
46
+ except KeyError as e:
47
+ print('model is still loading, please wait!')
48
+
49
+ elif (lang == 'en') and (filename is not None):
50
+ API_URL = "https://api-inference.huggingface.co/models/facebook/wav2vec2-base-960h"
51
+ with open(filename, "rb") as f:
52
+ data = f.read()
53
+ try:
54
+ response = httpx.request("POST", API_URL, headers=headers, data=data)
55
+ command = json.loads(response.content.decode("utf-8"))
56
+ command = command['text']
57
+ if command != '':
58
+ print(command)
59
+ return command.lower()
60
+ else:
61
+ print('No response yet')
62
+ except KeyError as e:
63
+ print('model is still loading..., please wait!')
64
+ else:
65
+ pass