From e83f5db53e3b0c796f66891df16d8f3c66f3b17a Mon Sep 17 00:00:00 2001 From: Jared Wilber Date: Mon, 18 Sep 2023 13:01:12 -0700 Subject: [PATCH 1/3] replace ngrok with localhost.run --- pykoi/application.py | 55 ++++++++++++++++++++++++++++++++++++++------ 1 file changed, 48 insertions(+), 7 deletions(-) diff --git a/pykoi/application.py b/pykoi/application.py index 32072ce..fd18611 100644 --- a/pykoi/application.py +++ b/pykoi/application.py @@ -3,6 +3,10 @@ import socket import threading import time +import subprocess +import re +import asyncio + from datetime import datetime from typing import List, Optional, Any, Dict, Union @@ -488,17 +492,28 @@ async def inference( try: print("[/retrieval]: model inference.....", request_body.prompt) component["component"].retrieval_model.re_init(request_body.file_names) - output = component["component"].retrieval_model.run_with_return_source_documents({"query": request_body.prompt}) + output = component[ + "component" + ].retrieval_model.run_with_return_source_documents( + {"query": request_body.prompt} + ) id = component["component"].database.insert_question_answer( request_body.prompt, output["result"] ) - print('output', output, output["result"]) + print("output", output, output["result"]) if output["source_documents"] == []: source = "N/A" source_content = "N/A" else: - source = output["source_documents"][0].metadata.get('file_name', 'No file name found') - source_content = "1. " + output["source_documents"][0].page_content + "\n2. " + output["source_documents"][1].page_content + source = output["source_documents"][0].metadata.get( + "file_name", "No file name found" + ) + source_content = ( + "1. " + + output["source_documents"][0].page_content + + "\n2. " + + output["source_documents"][1].page_content + ) return { "id": id, "log": "Inference complete", @@ -671,13 +686,38 @@ async def read_item( self._telemetry.capture(start_event) if self._share: - public_url = ngrok.connect(self._host + ":" + str(self._port)) - print("Public URL:", public_url) + import nest_asyncio + + nest_asyncio.apply() + command = f"ssh -R 80:{self._host}:{self._port} nokey@localhost.run" + + process = subprocess.Popen( + command, + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + shell=True, + text=True, + ) + # Get the public URL without waiting for the process to complete + while True: + line = process.stdout.readline() + if not line: + break + + match = re.search(r"(\bhttp[s]?://[^\s]+)", line) + if match: + public_url = match.group(1) + print("Public URL:", public_url) + break + + # The process will continue to run in the background here import uvicorn uvicorn.run(app, host=self._host, port=self._port) print("Stopping server...") - ngrok.disconnect(public_url) + + # Once done, you may choose to terminate the ssh process + process.terminate() else: import uvicorn @@ -694,6 +734,7 @@ def display(self): """ Run the application. """ + print("hey2") import nest_asyncio nest_asyncio.apply() From c00ea95a2e8457e4fabffa5f9da772adf6f926d0 Mon Sep 17 00:00:00 2001 From: Jared Wilber Date: Wed, 27 Sep 2023 08:50:24 -0700 Subject: [PATCH 2/3] add localhost.run instead of ngrok --- pykoi/application.py | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/pykoi/application.py b/pykoi/application.py index fd18611..5f5eee3 100644 --- a/pykoi/application.py +++ b/pykoi/application.py @@ -1,12 +1,11 @@ """Application module.""" +import asyncio import os +import re import socket +import subprocess import threading import time -import subprocess -import re -import asyncio - from datetime import datetime from typing import List, Optional, Any, Dict, Union @@ -689,8 +688,7 @@ async def read_item( import nest_asyncio nest_asyncio.apply() - command = f"ssh -R 80:{self._host}:{self._port} nokey@localhost.run" - + command = f"ssh -o StrictHostKeyChecking=no -R 80:{self._host}:{self._port} nokey@localhost.run" process = subprocess.Popen( command, stdout=subprocess.PIPE, @@ -701,6 +699,7 @@ async def read_item( # Get the public URL without waiting for the process to complete while True: line = process.stdout.readline() + if not line: break From 3810dca83d3630e7a8bd54012bbf9fa5719c2848 Mon Sep 17 00:00:00 2001 From: Jared Wilber Date: Wed, 27 Sep 2023 11:20:46 -0700 Subject: [PATCH 3/3] add localhost.run --- pykoi/application.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pykoi/application.py b/pykoi/application.py index 5f5eee3..50d2850 100644 --- a/pykoi/application.py +++ b/pykoi/application.py @@ -702,7 +702,7 @@ async def read_item( if not line: break - + # return url match = re.search(r"(\bhttp[s]?://[^\s]+)", line) if match: public_url = match.group(1)