From 83fcccbeca277cac6e5edc64c74bcc3c008dd1c0 Mon Sep 17 00:00:00 2001 From: valentinfrlch Date: Wed, 22 May 2024 07:49:54 +0200 Subject: [PATCH] Better method to validate localai server is running --- custom_components/gpt4vision/config_flow.py | 15 ++++++++------- localai.py | 5 +++++ 2 files changed, 13 insertions(+), 7 deletions(-) create mode 100644 localai.py diff --git a/custom_components/gpt4vision/config_flow.py b/custom_components/gpt4vision/config_flow.py index 145707a..2e5d248 100644 --- a/custom_components/gpt4vision/config_flow.py +++ b/custom_components/gpt4vision/config_flow.py @@ -4,7 +4,7 @@ from .const import DOMAIN, CONF_OPENAI_API_KEY, CONF_IP_ADDRESS, CONF_PORT import voluptuous as vol import logging -import socket +import requests _LOGGER = logging.getLogger(__name__) @@ -35,13 +35,14 @@ def validate_openai(user_input: dict): def validate_connection(ip_address, port): - sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) - sock.settimeout(1) # One second timeout + url = f'http://{ip_address}:{port}/readyz' try: - sock.connect((ip_address, port)) - sock.close() - return True - except socket.error: + response = requests.get(url) + if response.status_code == 200: + return True + else: + return False + except requests.exceptions.RequestException as e: return False diff --git a/localai.py b/localai.py new file mode 100644 index 0000000..306febd --- /dev/null +++ b/localai.py @@ -0,0 +1,5 @@ +import requests + +url = 'http://localhost:8080/readyz' +response = requests.get(url) +print(response) \ No newline at end of file