Files
audio_resive/functions/convert.py
T
2024-06-26 10:37:32 +07:00

54 lines
1.8 KiB
Python

import requests
class Converter:
"""
Этот класс выполняет главную функцию, конвертирует голос в текст
"""
def __init__(self, path_to_file: str):
self.path_to_file = path_to_file
self.ip_list = ["192.168.0.108", "192.168.0.109", "computing"]
@staticmethod
def ping(ip):
"""
Пингует указанный IP-адрес.
:param ip: str - IP-адрес для пинга
:return: bool - True если доступен, False если нет
"""
try:
r = requests.get(f"http://{ip}:5000/ping", timeout=5)
return r.status_code == 200
except requests.RequestException:
return False
def audio_to_text(self) -> dict:
"""
Основная функция
:return: str - текст + служебная информация
"""
for ip in self.ip_list:
if self.ping(ip):
try:
response = requests.post(
f"http://{ip}:5000/decrypt_audio",
files={"file": open(self.path_to_file, "rb").read()},
timeout=6000
)
if response.status_code == 200:
return response.json()
else:
return {
"timestamp": " ",
"final_text": "Не удалось получить текст."
}
except requests.RequestException:
continue
return {
"timestamp": " ",
"final_text": "Не удалось получить текст или все сервера недоступны."
}