File size: 4,330 Bytes
f38d68c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
83b234c
f38d68c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "538a3f0c-50c1-4952-9fcc-070d365c9a0f",
      "metadata": {
        "id": "538a3f0c-50c1-4952-9fcc-070d365c9a0f",
        "scrolled": true
      },
      "outputs": [],
      "source": [
        "import os\n",
        "import subprocess\n",
        "from threading import Timer\n",
        "from queue import Queue\n",
        "\n",
        "def is_colab():\n",
        "    try:\n",
        "        import google.colab\n",
        "        return True\n",
        "    except ImportError:\n",
        "        return False\n",
        "\n",
        "ROOT_DIR = \"/workspace/\" if not is_colab() else \"/content/\"\n",
        "REPO_URL = \"https://huggingface.co/spaces/Tasty-Rice/Magic_on_paper\"\n",
        "REPO_DIR = os.path.join(ROOT_DIR, \"Magic_on_paper\")\n",
        "\n",
        "NGROK_TOKEN = \"\"\n",
        "NGROK_SUBDOMAIN = \"\"\n",
        "PORT = 7860\n",
        "\n",
        "# os.environ[\"HF_TOKEN\"] = \"\"\n",
        "os.environ[\"IS_COLAB\"] = \"1\"\n",
        "os.environ[\"MODEL\"] = \"https://huggingface.co/Tasty-Rice/Magic_on_paper/blob/main/Magic_on_paper-SDXL-v3.safetensors\"\n",
        "os.environ[\"CACHE_EXAMPLES\"] = \"1\"\n",
        "\n",
        "def clone_repository(url, directory, branch=None):\n",
        "    subprocess.run([\"git\", \"clone\", url, directory], check=True)\n",
        "    if branch:\n",
        "        subprocess.run([\"git\", \"checkout\", branch], cwd=directory, check=True)\n",
        "\n",
        "def install_dependencies(directory):\n",
        "    dependencies = [\"accelerate==0.27.2\", \"diffusers==0.26.3\", \"gradio==4.20.0\",\n",
        "                    \"invisible-watermark==0.2.0\", \"spaces==0.24.0\", \"omegaconf==2.3.0\", \"timm==0.9.10\"]\n",
        "    if is_colab():\n",
        "        subprocess.run([\"pip\", \"install\"] + dependencies, check=True)\n",
        "    else:\n",
        "        requirements_path = os.path.join(directory, \"requirements.txt\")\n",
        "        subprocess.run([\"pip\", \"install\", \"-r\", requirements_path], check=True)\n",
        "\n",
        "def setup_ngrok_tunnel(port, queue, auth_token, subdomain):\n",
        "    ngrok.set_auth_token(auth_token)\n",
        "    url = ngrok.connect(port, bind_tls=True, subdomain=subdomain)\n",
        "    queue.put(url)\n",
        "\n",
        "def main():\n",
        "    if not os.path.exists(REPO_DIR):\n",
        "        print(f\"Cloning repository to {REPO_DIR}\")\n",
        "        clone_repository(REPO_URL, REPO_DIR)\n",
        "\n",
        "    print(\"Installing required Python libraries\")\n",
        "    install_dependencies(REPO_DIR)\n",
        "    print(\"Done!\")\n",
        "\n",
        "    os.chdir(REPO_DIR)\n",
        "\n",
        "    if NGROK_TOKEN:\n",
        "        try:\n",
        "            from pyngrok import conf, ngrok\n",
        "        except ImportError:\n",
        "            subprocess.run([\"pip\", \"install\", \"-qqqq\", \"--upgrade\", \"setuptools\"], check=True)\n",
        "            subprocess.run([\"pip\", \"install\", \"-qqqq\", \"-U\", \"pyngrok\"], check=True)\n",
        "            from pyngrok import conf, ngrok\n",
        "\n",
        "        ngrok.kill()\n",
        "        ngrok_output_queue = Queue()\n",
        "        ngrok_thread = Timer(2, setup_ngrok_tunnel, args=(PORT, ngrok_output_queue, NGROK_TOKEN, NGROK_SUBDOMAIN))\n",
        "        ngrok_thread.start()\n",
        "        ngrok_thread.join()\n",
        "        print(ngrok_output_queue.get())\n",
        "\n",
        "    !python app.py\n",
        "\n",
        "if __name__ == \"__main__\":\n",
        "    main()"
      ]
    }
  ],
  "metadata": {
    "colab": {
      "provenance": []
    },
    "kernelspec": {
      "display_name": "Python 3 (ipykernel)",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.10.12"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 5
}