SamSwift commited on
Commit
ab17927
·
1 Parent(s): 564de44

Upload inference.py

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