#@title <b>TavernAI</b>
#@markdown <- Click For Start (≖ ‸ ≖ ✿)

Model = "PPO_Pygway-V8p4_Dev-6b" #@param ["PPO_Pygway-V8p4_Dev-6b", "Dolly_Shygmalion-6b"] {allow-input: true}
Version = "Official" 
KoboldAI_Provider = "Localtunnel" #@param ["Localtunnel", "Cloudflare"]
use_google_drive = True #@param {type:"boolean"}
Provider = KoboldAI_Provider
!nvidia-smi
import subprocess
import time
import sys
import os
import threading
import shutil
from google.colab import drive

    
if use_google_drive:
  drive.mount('/content/drive/')
  if not os.path.exists("/content/drive/MyDrive/TavernAI/"):
    os.mkdir("/content/drive/MyDrive/TavernAI/")
  if not os.path.exists("/content/drive/MyDrive/TavernAI/characters/"):
    os.mkdir("/content/drive/MyDrive/TavernAI/characters/")
  if not os.path.exists("/content/drive/MyDrive/TavernAI/chats/"):
    os.mkdir("/content/drive/MyDrive/TavernAI/chats/")
else:
  if not os.path.exists("/content/drive"):
    os.mkdir("/content/drive")
  if not os.path.exists("/content/drive/MyDrive/"):
    os.mkdir("/content/drive/MyDrive/")

def copy_characters(use_google_drive=False):
  if not use_google_drive:
    return
  
  src_folder = "/TavernAIColab/public/characters"
  dst_folder = "/content/drive/MyDrive/TavernAI/characters"

  for filename in os.listdir(src_folder):
    src_file = os.path.join(src_folder, filename)
    dst_file = os.path.join(dst_folder, filename)

    if os.path.exists(dst_file):
      print(f"{dst_file} already exists. Skipping...")
      continue

    shutil.copy(src_file, dst_folder)
    print(f"{src_file} copied to {dst_folder}")
Revision = ""

if Model == "Dolly_Shygmalion-6b":
  Model = "TehVenom/Dolly_Shygmalion-6b"
  path = ""
  download = ""
  Version = "United"
elif Model == "PPO_Pygway-V8p4_Dev-6b":
  Model = "TehVenom/PPO_Pygway-V8p4_Dev-6b"
  Revision = "--revision dev"
  path = ""
  Version = "United"
  download = ""

if Provider == "Localtunnel":
  tunnel = "--localtunnel yes"
else:
  tunnel = ""



#Henk's KoboldAI script
!wget https://koboldai.org/ckds && chmod +x ckds
!./ckds --init only
if Provider == "Localtunnel":
  p = subprocess.Popen(['/content/ckds', '--model', Model, '--localtunnel', 'yes'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
else:
  p = subprocess.Popen(['/content/ckds', '--model', Model], stdout=subprocess.PIPE, stderr=subprocess.PIPE)


#Do not repeat! Tricks performed by a professional!
url = ''
while True:
    line = p.stdout.readline().decode().strip()
    if "KoboldAI has finished loading and is available at the following link: " in line:
        print(line)
        url = line.split("KoboldAI has finished loading and is available at the following link: ")[1]
        print(url)
        break
    if "KoboldAI has finished loading and is available at the following link for UI 1: " in line:
        print(line)
        url = line.split("KoboldAI has finished loading and is available at the following link for UI 1: ")[1]
        print(url)
        break
    if not line:
        break
    print(line)
    if "INIT" in line and "Transformers" in line:
      print("Model loading... (It will take 2 - 5 minutes)")


#TavernAI
%cd /
!curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.37.2/install.sh | bash
!nvm install 19.1.0
!nvm use 19.1.0
!node -v
!git clone https://github.com/TavernAI/TavernAIColab
copy_characters(use_google_drive)
%cd TavernAIColab
!npm install
time.sleep(1)
%env colab=2
%env colaburl=$url
if use_google_drive:
  %env googledrive=2
!nohup node server.js &
time.sleep(3)
print('KoboldAI LINK:')
print(url)
print('')
print('###TavernAI LINK###')
!lt --port 800
{}
region:us
TAGS
#region-us 

#@title <b>TavernAI</b>
#@markdown <- Click For Start (≖ ‸ ≖ )

Model = "PPO_Pygway-V8p4_Dev-6b" #@param ["PPO_Pygway-V8p4_Dev-6b", "Dolly_Shygmalion-6b"] {allow-input: true}
Version = "Official" 
KoboldAI_Provider = "Localtunnel" #@param ["Localtunnel", "Cloudflare"]
use_google_drive = True #@param {type:"boolean"}
Provider = KoboldAI_Provider
!nvidia-smi
import subprocess
import time
import sys
import os
import threading
import shutil
from URL import drive

    
if use_google_drive:
  URL('/content/drive/')
  if not URL("/content/drive/MyDrive/TavernAI/"):
    URL("/content/drive/MyDrive/TavernAI/")
  if not URL("/content/drive/MyDrive/TavernAI/characters/"):
    URL("/content/drive/MyDrive/TavernAI/characters/")
  if not URL("/content/drive/MyDrive/TavernAI/chats/"):
    URL("/content/drive/MyDrive/TavernAI/chats/")
else:
  if not URL("/content/drive"):
    URL("/content/drive")
  if not URL("/content/drive/MyDrive/"):
    URL("/content/drive/MyDrive/")

def copy_characters(use_google_drive=False):
  if not use_google_drive:
    return
  
  src_folder = "/TavernAIColab/public/characters"
  dst_folder = "/content/drive/MyDrive/TavernAI/characters"

  for filename in os.listdir(src_folder):
    src_file = URL(src_folder, filename)
    dst_file = URL(dst_folder, filename)

    if URL(dst_file):
      print(f"{dst_file} already exists. Skipping...")
      continue

    URL(src_file, dst_folder)
    print(f"{src_file} copied to {dst_folder}")
Revision = ""

if Model == "Dolly_Shygmalion-6b":
  Model = "TehVenom/Dolly_Shygmalion-6b"
  path = ""
  download = ""
  Version = "United"
elif Model == "PPO_Pygway-V8p4_Dev-6b":
  Model = "TehVenom/PPO_Pygway-V8p4_Dev-6b"
  Revision = "--revision dev"
  path = ""
  Version = "United"
  download = ""

if Provider == "Localtunnel":
  tunnel = "--localtunnel yes"
else:
  tunnel = ""



#Henk's KoboldAI script
!wget URL && chmod +x ckds
!./ckds --init only
if Provider == "Localtunnel":
  p = subprocess.Popen(['/content/ckds', '--model', Model, '--localtunnel', 'yes'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
else:
  p = subprocess.Popen(['/content/ckds', '--model', Model], stdout=subprocess.PIPE, stderr=subprocess.PIPE)


#Do not repeat! Tricks performed by a professional!
url = ''
while True:
    line = p.stdout.readline().decode().strip()
    if "KoboldAI has finished loading and is available at the following link: " in line:
        print(line)
        url = URL("KoboldAI has finished loading and is available at the following link: ")[1]
        print(url)
        break
    if "KoboldAI has finished loading and is available at the following link for UI 1: " in line:
        print(line)
        url = URL("KoboldAI has finished loading and is available at the following link for UI 1: ")[1]
        print(url)
        break
    if not line:
        break
    print(line)
    if "INIT" in line and "Transformers" in line:
      print("Model loading... (It will take 2 - 5 minutes)")


#TavernAI
%cd /
!curl -o- URL | bash
!nvm install 19.1.0
!nvm use 19.1.0
!node -v
!git clone URL
copy_characters(use_google_drive)
%cd TavernAIColab
!npm install
URL(1)
%env colab=2
%env colaburl=$url
if use_google_drive:
  %env googledrive=2
!nohup node URL &
URL(3)
print('KoboldAI LINK:')
print(url)
print('')
print('###TavernAI LINK###')
!lt --port 800
TAGS
#region-us 

TAGS
#region-us 
