Rejekts commited on
Commit
d0751b3
·
verified ·
1 Parent(s): 97f00ef

Update setup_llama.py

Browse files
Files changed (1) hide show
  1. setup_llama.py +27 -13
setup_llama.py CHANGED
@@ -6,41 +6,55 @@ from pyngrok import ngrok
6
 
7
  os.environ["OLLAMA_HOST"] = "0.0.0.0"
8
 
9
- def run_command(command, check=True):
10
- """Run a shell command and handle output/errors."""
11
  try:
12
- subprocess.run(command, shell=True, check=check)
13
  except subprocess.CalledProcessError as e:
14
- print(f"Error running command: {command}\n{e}")
15
  exit(1)
16
 
17
  def download_model():
18
  """Download the Llama model from HuggingFace."""
19
  model_url = "https://huggingface.co/Orenguteng/Llama-3.1-8B-Lexi-Uncensored-V2-GGUF/resolve/main/Llama-3.1-8B-Lexi-Uncensored_V2_Q4.gguf"
20
  print(f"Downloading model from {model_url}...")
21
- run_command(f"wget -O unllama.gguf {model_url} ")
22
 
23
  def start_ollama_service():
24
- ngrok.set_auth_token(os.getenv("NGROK_AUTHTOKEN"))
 
 
 
 
 
 
 
25
  print("Starting Ollama service...")
26
- run_command("ollama serve &")
 
 
 
 
 
 
27
  while True:
28
  try:
29
  response = requests.get("http://localhost:11434")
30
  if response.status_code == 200:
31
- URL = ngrok.connect(11434)
32
- print(URL)
 
33
  break
34
  except requests.ConnectionError:
35
  time.sleep(2)
36
 
37
  def create_model():
38
- run_command("ollama create unllama -f mf.yaml")
39
- print("Created unllama")
 
40
 
41
  if __name__ == "__main__":
42
- run_command(["sudo", "curl", "-fsSL", "https://ollama.com/install.sh", "|", "sudo", "sh"], use_shell=True)
43
- start_ollama_service()
44
  download_model()
45
  create_model()
46
  print("Ollama service is running and accessible through ngrok.")
 
6
 
7
  os.environ["OLLAMA_HOST"] = "0.0.0.0"
8
 
9
+ def run_command(command_list, check=True, use_shell=False):
10
+ """Run a shell command using a list format, with optional shell support."""
11
  try:
12
+ subprocess.run(command_list, check=check, shell=use_shell)
13
  except subprocess.CalledProcessError as e:
14
+ print(f"Error running command: {' '.join(command_list)}\n{e}")
15
  exit(1)
16
 
17
  def download_model():
18
  """Download the Llama model from HuggingFace."""
19
  model_url = "https://huggingface.co/Orenguteng/Llama-3.1-8B-Lexi-Uncensored-V2-GGUF/resolve/main/Llama-3.1-8B-Lexi-Uncensored_V2_Q4.gguf"
20
  print(f"Downloading model from {model_url}...")
21
+ run_command(["wget", "-O", "unllama.gguf", model_url])
22
 
23
  def start_ollama_service():
24
+ """Start the Ollama service and expose via ngrok."""
25
+ ngrok_token = os.getenv("NGROK_AUTHTOKEN")
26
+ if not ngrok_token:
27
+ print("NGROK_AUTHTOKEN is not set!")
28
+ exit(1)
29
+
30
+ ngrok.set_auth_token(ngrok_token)
31
+
32
  print("Starting Ollama service...")
33
+
34
+ # Install Ollama with superuser privileges
35
+ run_command("sudo curl -fsSL https://ollama.com/install.sh | sudo sh", use_shell=True)
36
+
37
+ # Start Ollama in the background
38
+ subprocess.Popen(["ollama", "serve"])
39
+
40
  while True:
41
  try:
42
  response = requests.get("http://localhost:11434")
43
  if response.status_code == 200:
44
+ print("Ollama service is up and running!")
45
+ public_url = ngrok.connect(11434)
46
+ print(f"Service is accessible at: {public_url}")
47
  break
48
  except requests.ConnectionError:
49
  time.sleep(2)
50
 
51
  def create_model():
52
+ """Create a model using the downloaded GGUF file."""
53
+ run_command(["ollama", "create", "unllama", "-f", "unllama.gguf"])
54
+ print("Created unllama model")
55
 
56
  if __name__ == "__main__":
57
+ start_ollama_service() # Ensure service is running before creating the model
 
58
  download_model()
59
  create_model()
60
  print("Ollama service is running and accessible through ngrok.")