Skip to content

Commit 061c332

Browse files
committed
Fix Assistant
1 parent d0768e0 commit 061c332

File tree

3 files changed

+402
-10
lines changed

3 files changed

+402
-10
lines changed

setup.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
setuptools.setup(
77
name="text2text",
8-
version="1.6.1",
8+
version="1.6.2",
99
author="artitw",
1010
author_email="artitw@gmail.com",
1111
description="Text2Text: Crosslingual NLP/G toolkit",

text2text/assistant.py

+31-9
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import time
55
import subprocess
66
import warnings
7+
import importlib.resources
78

89
from llama_index.llms.ollama import Ollama
910
from llama_index.core.llms import ChatMessage
@@ -14,9 +15,28 @@ def ollama_version():
1415
if result.startswith("ollama version "):
1516
return result.replace("ollama version ", "")
1617
except Exception as e:
17-
warnings.warn(str(e))
18+
pass
1819
return ""
1920

21+
def run_sh(script_string):
22+
try:
23+
process = subprocess.Popen(
24+
['sh'],
25+
stdin=subprocess.PIPE,
26+
stdout=subprocess.PIPE,
27+
stderr=subprocess.PIPE,
28+
text=True # Treat input/output as text
29+
)
30+
output, error = process.communicate(input=script_string)
31+
32+
if process.returncode == 0:
33+
return output
34+
else:
35+
return error
36+
37+
except Exception as e:
38+
return str(e)
39+
2040
class Assistant(object):
2141
def __init__(self, **kwargs):
2242
self.host = kwargs.get("host", "http://localhost")
@@ -42,18 +62,20 @@ def load_model(self):
4262
if return_code != 0:
4363
raise Exception("Cannot install lshw.")
4464

45-
return_code = os.system("curl -fsSL https://ollama.com/install.sh | sh")
46-
if return_code != 0:
47-
raise Exception("Cannot install ollama.")
48-
49-
return_code = os.system("sudo systemctl enable ollama")
50-
if return_code != 0:
51-
raise Exception("Cannot enable ollama.")
65+
resource_path = 'ollama_install.sh'
66+
with importlib.resources.open_text('text2text.utils', resource_path) as f:
67+
install_script = f.read()
68+
result = run_sh(install_script)
69+
if "Install complete." not in result and "will run in CPU-only mode." not in result:
70+
raise Exception(result)
5271

5372
self.ollama_serve_proc = subprocess.Popen(["ollama", "serve"])
5473
time.sleep(1)
5574

56-
result = subprocess.check_output(["ollama", "-v"], stderr=subprocess.STDOUT).decode("utf-8")
75+
result = subprocess.check_output(
76+
["ollama", "-v"],
77+
stderr=subprocess.STDOUT
78+
).decode("utf-8")
5779
if not result.startswith("ollama version"):
5880
raise Exception(result)
5981

0 commit comments

Comments
 (0)