{
  "cells": [
    {
      "attachments": {},
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "**Links**<br>\n",
        "Extensions API GitHub: https://github.com/SillyTavern/SillyTavern-extras/<br>\n",
        "SillyTavern community Discord (support and discussion): https://discord.gg/RZdyAEUPvj"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "#@title <-- Tap this if you run on Mobile { display-mode: \"form\" }\n",
        "#Taken from KoboldAI colab\n",
        "%%html\n",
        "<b>Press play on the audio player to keep the tab alive. (Uses only 13MB of data)</b><br/>\n",
        "<audio src=\"https://henk.tech/colabkobold/silence.m4a\" controls>"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "cellView": "form",
        "id": "lVftocpwCoYw"
      },
      "outputs": [],
      "source": [
        "#@markdown (RECOMMENDED) Generates an API key for you to use with the API\n",
        "secure = False #@param {type:\"boolean\"}\n",
        "#@markdown Enables hosting of extensions backend for SillyTavern Extras\n",
        "use_cpu = False #@param {type:\"boolean\"}\n",
        "#@markdown Allows to run SillyTavern Extras on CPU (use if you're out of daily GPU allowance)\n",
        "use_sd_cpu = False #@param {type:\"boolean\"}\n",
        "#@markdown Allows to run Stable Diffusion pipeline on CPU (slow!)\n",
        "extras_enable_captioning = True #@param {type:\"boolean\"}\n",
        "#@markdown Loads the image captioning module\n",
        "Captions_Model = \"Salesforce/blip-image-captioning-large\" #@param [ \"Salesforce/blip-image-captioning-large\", \"Salesforce/blip-image-captioning-base\" ]\n",
        "#@markdown * Salesforce/blip-image-captioning-large - good base model\n",
        "#@markdown * Salesforce/blip-image-captioning-base - slightly faster but less accurate\n",
        "extras_enable_emotions = True #@param {type:\"boolean\"}\n",
        "#@markdown Loads the sentiment classification model\n",
        "Emotions_Model = \"nateraw/bert-base-uncased-emotion\" #@param [\"nateraw/bert-base-uncased-emotion\", \"joeddav/distilbert-base-uncased-go-emotions-student\"]\n",
        "#@markdown * nateraw/bert-base-uncased-emotion = 6 supported emotions<br>\n",
        "#@markdown * joeddav/distilbert-base-uncased-go-emotions-student = 28 supported emotions\n",
        "extras_enable_memory = True #@param {type:\"boolean\"}\n",
        "#@markdown Loads the story summarization module\n",
        "Memory_Model = \"slauw87/bart_summarisation\" #@param [ \"slauw87/bart_summarisation\", \"Qiliang/bart-large-cnn-samsum-ChatGPT_v3\", \"Qiliang/bart-large-cnn-samsum-ElectrifAi_v10\", \"distilbart-xsum-12-3\" ]\n",
        "#@markdown * slauw87/bart_summarisation - general purpose summarization model\n",
        "#@markdown * Qiliang/bart-large-cnn-samsum-ChatGPT_v3 - summarization model optimized for chats\n",
        "#@markdown * Qiliang/bart-large-cnn-samsum-ElectrifAi_v10 - nice results so far, but still being evaluated\n",
        "#@markdown * distilbart-xsum-12-3 - faster, but pretty basic alternative\n",
        "extras_enable_silero_tts = True #@param {type:\"boolean\"}\n",
        "#@markdown Enables Silero text-to-speech module\n",
        "extras_enable_edge_tts = True #@param {type:\"boolean\"}\n",
        "#@markdown Enables Microsoft Edge text-to-speech module\n",
        "extras_enable_sd = True #@param {type:\"boolean\"}\n",
        "#@markdown Enables SD picture generation\n",
        "SD_Model = \"ckpt/anything-v4.5-vae-swapped\" #@param [ \"ckpt/anything-v4.5-vae-swapped\", \"hakurei/waifu-diffusion\", \"philz1337/clarity\", \"prompthero/openjourney\", \"ckpt/sd15\", \"stabilityai/stable-diffusion-2-1-base\" ]\n",
        "#@markdown * ckpt/anything-v4.5-vae-swapped - anime style model\n",
        "#@markdown * hakurei/waifu-diffusion - anime style model\n",
        "#@markdown * philz1337/clarity - realistic style model\n",
        "#@markdown * prompthero/openjourney - midjourney style model\n",
        "#@markdown * ckpt/sd15 - base SD 1.5\n",
        "#@markdown * stabilityai/stable-diffusion-2-1-base - base SD 2.1\n",
        "extras_enable_chromadb = True #@param {type:\"boolean\"}\n",
        "#@markdown Enables ChromaDB module\n",
        "\n",
        "import subprocess\n",
        "import secrets\n",
        "\n",
        "# ---\n",
        "# SillyTavern extras\n",
        "extras_url = '(disabled)'\n",
        "params = []\n",
        "if use_cpu:\n",
        "    params.append('--cpu')\n",
        "if use_sd_cpu:\n",
        "    params.append('--sd-cpu')\n",
        "if secure:\n",
        "    params.append('--secure')\n",
        "params.append('--share')\n",
        "ExtrasModules = []\n",
        "\n",
        "if (extras_enable_captioning):\n",
        "  ExtrasModules.append('caption')\n",
        "if (extras_enable_memory):\n",
        "  ExtrasModules.append('summarize')\n",
        "if (extras_enable_emotions):\n",
        "  ExtrasModules.append('classify')\n",
        "if (extras_enable_sd):\n",
        "  ExtrasModules.append('sd')\n",
        "if (extras_enable_silero_tts):\n",
        "  ExtrasModules.append('silero-tts')\n",
        "if extras_enable_edge_tts:\n",
        "  ExtrasModules.append('edge-tts')\n",
        "if (extras_enable_chromadb):\n",
        "  ExtrasModules.append('chromadb')\n",
        "\n",
        "params.append(f'--classification-model={Emotions_Model}')\n",
        "params.append(f'--summarization-model={Memory_Model}')\n",
        "params.append(f'--captioning-model={Captions_Model}')\n",
        "params.append(f'--sd-model={SD_Model}')\n",
        "params.append(f'--enable-modules={\",\".join(ExtrasModules)}')\n",
        "\n",
        "\n",
        "%cd /\n",
        "!git clone https://github.com/SillyTavern/SillyTavern-extras\n",
        "%cd /SillyTavern-extras\n",
        "!git clone https://github.com/Cohee1207/tts_samples\n",
        "!npm install -g localtunnel\n",
        "!pip install -r requirements-complete.txt\n",
        "!pip install tensorflow==2.14\n",
        "!pip install colorama\n",
        "!pip install Flask-Cors\n",
        "!pip install Flask-Compress\n",
        "!pip install transformers\n",
        "!pip install Flask_Cloudflared\n",
        "!pip install webuiapi\n",
        "!pip install diffusers\n",
        "!pip install accelerate\n",
        "!pip install silero_api_server\n",
        "!pip install edge_tts\n",
        "!pip install chromadb\n",
        "!pip install sentence_transformers\n",
        "!wget https://github.com/cloudflare/cloudflared/releases/download/2023.5.0/cloudflared-linux-amd64 -O /tmp/cloudflared-linux-amd64\n",
        "!chmod +x /tmp/cloudflared-linux-amd64\n",
        "\n",
        "# Generate a random API key\n",
        "api_key = secrets.token_hex(5)\n",
        "\n",
        "# Write the API key to api_key.txt\n",
        "with open('./api_key.txt', 'w') as f:\n",
        "    f.write(api_key)\n",
        "print(f\"API Key generated: {api_key}\")\n",
        "\n",
        "cmd = f\"python server.py {' '.join(params)}\"\n",
        "print(cmd)\n",
        "extras_process = subprocess.Popen(\n",
        "    cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, cwd='/SillyTavern-extras', shell=True)\n",
        "print('processId:', extras_process.pid)\n",
        "while True:\n",
        "    line = extras_process.stdout.readline().decode().strip()\n",
        "    if line != None and line != '':\n",
        "        print(line)\n"
      ]
    }
  ],
  "metadata": {
    "accelerator": "GPU",
    "colab": {
      "private_outputs": true,
      "provenance": []
    },
    "gpuClass": "standard",
    "kernelspec": {
      "display_name": "Python 3",
      "name": "python3"
    },
    "language_info": {
      "name": "python"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}
