all ruff checking and modify Converter class

This commit is contained in:
2024-06-21 12:30:27 +07:00
parent cd3bae23e6
commit e1034fb927
19 changed files with 1067 additions and 995 deletions
+38 -35
View File
@@ -5,12 +5,24 @@ class Converter:
"""
Этот класс выполняет главную функцию, конвертирует голос в текст
"""
def __init__(self, path_to_file: str, message, model, language: str = "ru-RU"):
self.language = language
def __init__(self, path_to_file: str):
self.path_to_file = path_to_file
self.service = None
self.message = message
self.model = model
self.ip_list = ["192.168.0.108", "192.168.0.109", "localhost"] # Список IP-адресов
@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:
"""
@@ -18,33 +30,24 @@ class Converter:
:return: str - текст + служебная информация
"""
try:
r = requests.get("http://192.168.0.108:5000/ping")
if r.status_code == 200:
print(self.path_to_file)
response = requests.post(
"http://192.168.0.108: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": "Не удалось получить текст."
}
else:
result = self.model.transcribe(self.path_to_file)
return {
"timestamp": "Тут такое не работает, "
"жди пока главный сервер поднимется",
"final_text": result["text"]
}
except requests.ConnectionError:
result = self.model.transcribe(self.path_to_file)
return {
"timestamp": "Тут такое не работает, "
"жди пока главный сервер поднимется",
"final_text": result["text"]
}
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": "Не удалось получить текст или все сервера недоступны."
}