Home_Automation / inference.py
SamSwift's picture
Update inference.py
8413b4b
import json
import requests
import httpx
API_TOKEN = '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'
headers = {"Authorization": f"Bearer {API_TOKEN}"}
def query(filename, lang):
"""
Function to get inference for three languages
"""
if (lang == 'ha') and (filename is not None):
API_URL = "https://api-inference.huggingface.co/models/Tiamz/hausa-4-ha-wa2vec-data-aug-xls-r-300m"
with open(filename, "rb") as f:
data = f.read()
try:
response = requests.request("POST", API_URL, headers=headers, data=data)
command = json.loads(response.content.decode("utf-8"))
command = command['text']
if command != '':
#print(command)
return command
else:
print('No response yet')
except KeyError as e:
pass
#return 'Model is still loading..., please wait!'
elif (lang == 'yo') and (filename is not None):
API_URL = "https://api-inference.huggingface.co/models/Ayoola/cdial-yoruba-test"
with open(filename, "rb") as f:
data = f.read()
try:
response = requests.request("POST", API_URL, headers=headers, data=data)
command = json.loads(response.content.decode("utf-8"))
command = command['text']
if command != '':
#print(command)
return command
else:
pass
except KeyError as e:
pass
#return 'Model is still loading..., please wait!'
elif (lang == 'en') and (filename is not None):
API_URL = "https://api-inference.huggingface.co/models/facebook/wav2vec2-base-960h"
with open(filename, "rb") as f:
data = f.read()
try:
response = httpx.request("POST", API_URL, headers=headers, data=data)
command = json.loads(response.content.decode("utf-8"))
command = command['text']
if command != '':
#print(command)
return command.lower()
else:
print('No response yet')
except KeyError as e:
pass
#print('Model is still loading..., please wait!'
else:
pass