radames commited on
Commit
e8ea372
0 Parent(s):

Duplicate from huggingface-projects/sd-multiplayer-bot

Browse files
Files changed (7) hide show
  1. .gitattributes +34 -0
  2. .gitignore +21 -0
  3. Dockerfile +25 -0
  4. README.md +12 -0
  5. app.py +120 -0
  6. requirements.txt +6 -0
  7. schema.sql +59 -0
.gitattributes ADDED
@@ -0,0 +1,34 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ *.7z filter=lfs diff=lfs merge=lfs -text
2
+ *.arrow filter=lfs diff=lfs merge=lfs -text
3
+ *.bin filter=lfs diff=lfs merge=lfs -text
4
+ *.bz2 filter=lfs diff=lfs merge=lfs -text
5
+ *.ckpt filter=lfs diff=lfs merge=lfs -text
6
+ *.ftz filter=lfs diff=lfs merge=lfs -text
7
+ *.gz filter=lfs diff=lfs merge=lfs -text
8
+ *.h5 filter=lfs diff=lfs merge=lfs -text
9
+ *.joblib filter=lfs diff=lfs merge=lfs -text
10
+ *.lfs.* filter=lfs diff=lfs merge=lfs -text
11
+ *.mlmodel filter=lfs diff=lfs merge=lfs -text
12
+ *.model filter=lfs diff=lfs merge=lfs -text
13
+ *.msgpack filter=lfs diff=lfs merge=lfs -text
14
+ *.npy filter=lfs diff=lfs merge=lfs -text
15
+ *.npz filter=lfs diff=lfs merge=lfs -text
16
+ *.onnx filter=lfs diff=lfs merge=lfs -text
17
+ *.ot filter=lfs diff=lfs merge=lfs -text
18
+ *.parquet filter=lfs diff=lfs merge=lfs -text
19
+ *.pb filter=lfs diff=lfs merge=lfs -text
20
+ *.pickle filter=lfs diff=lfs merge=lfs -text
21
+ *.pkl filter=lfs diff=lfs merge=lfs -text
22
+ *.pt filter=lfs diff=lfs merge=lfs -text
23
+ *.pth filter=lfs diff=lfs merge=lfs -text
24
+ *.rar filter=lfs diff=lfs merge=lfs -text
25
+ *.safetensors filter=lfs diff=lfs merge=lfs -text
26
+ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
27
+ *.tar.* filter=lfs diff=lfs merge=lfs -text
28
+ *.tflite filter=lfs diff=lfs merge=lfs -text
29
+ *.tgz filter=lfs diff=lfs merge=lfs -text
30
+ *.wasm filter=lfs diff=lfs merge=lfs -text
31
+ *.xz filter=lfs diff=lfs merge=lfs -text
32
+ *.zip filter=lfs diff=lfs merge=lfs -text
33
+ *.zst filter=lfs diff=lfs merge=lfs -text
34
+ *tfevents* filter=lfs diff=lfs merge=lfs -text
.gitignore ADDED
@@ -0,0 +1,21 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ .DS_Store
2
+ node_modules
3
+ /build
4
+ /.svelte-kit
5
+ /package
6
+ .env
7
+ .env.*
8
+ !.env.example
9
+
10
+ # Ignore files for PNPM, NPM and YARN
11
+ pnpm-lock.yaml
12
+ package-lock.json
13
+ yarn.lock
14
+ venv/
15
+ __pycache__/
16
+ flagged/
17
+ data
18
+ data.db
19
+ data.json
20
+ rooms_data.db
21
+ sd-multiplayer-data/
Dockerfile ADDED
@@ -0,0 +1,25 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM python:3.9
2
+
3
+ WORKDIR /code
4
+
5
+ COPY ./requirements.txt /code/requirements.txt
6
+
7
+ RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
8
+
9
+ # Git LFS
10
+ RUN apt-get update && apt-get install -y git-lfs
11
+ RUN git lfs install
12
+
13
+
14
+ # User
15
+ RUN useradd -m -u 1000 user
16
+ USER user
17
+ ENV HOME /home/user
18
+ ENV PATH $HOME/.local/bin:$PATH
19
+ WORKDIR $HOME
20
+ RUN mkdir app
21
+ WORKDIR $HOME/app
22
+
23
+ COPY . $HOME/app
24
+
25
+ CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
README.md ADDED
@@ -0,0 +1,12 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ title: Sd Multiplayer Bot
3
+ emoji: 🤖
4
+ colorFrom: red
5
+ colorTo: indigo
6
+ sdk: docker
7
+ app_port: 7860
8
+ pinned: false
9
+ duplicated_from: huggingface-projects/sd-multiplayer-bot
10
+ ---
11
+
12
+ Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
app.py ADDED
@@ -0,0 +1,120 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import boto3
2
+ import os
3
+ import re
4
+ import json
5
+ from pathlib import Path
6
+ import sqlite3
7
+ from huggingface_hub import Repository, HfFolder
8
+ import tqdm
9
+ import subprocess
10
+
11
+ from fastapi import FastAPI
12
+ from fastapi_utils.tasks import repeat_every
13
+
14
+
15
+ AWS_ACCESS_KEY_ID = os.getenv('AWS_ACCESS_KEY_ID')
16
+ AWS_SECRET_KEY = os.getenv('AWS_SECRET_KEY')
17
+ AWS_S3_BUCKET_NAME = os.getenv('AWS_S3_BUCKET_NAME')
18
+
19
+ s3 = boto3.client(service_name='s3',
20
+ aws_access_key_id=AWS_ACCESS_KEY_ID,
21
+ aws_secret_access_key=AWS_SECRET_KEY)
22
+
23
+
24
+ paginator = s3.get_paginator('list_objects_v2')
25
+
26
+
27
+ S3_DATA_FOLDER = Path("sd-multiplayer-data")
28
+ ROOMS_DATA_DB = S3_DATA_FOLDER / "rooms_data.db"
29
+
30
+
31
+ repo = Repository(
32
+ local_dir=S3_DATA_FOLDER,
33
+ repo_type="dataset",
34
+ clone_from="huggingface-projects/sd-multiplayer-data",
35
+ use_auth_token=True,
36
+ )
37
+ repo.git_pull()
38
+
39
+
40
+ if not ROOMS_DATA_DB.exists():
41
+ print("Creating database")
42
+ print("ROOMS_DATA_DB", ROOMS_DATA_DB)
43
+ db = sqlite3.connect(ROOMS_DATA_DB)
44
+ with open(Path("schema.sql"), "r") as f:
45
+ db.executescript(f.read())
46
+ db.commit()
47
+ db.close()
48
+
49
+
50
+ def get_db(db_path):
51
+ db = sqlite3.connect(db_path, check_same_thread=False)
52
+ db.row_factory = sqlite3.Row
53
+ try:
54
+ yield db
55
+ except Exception:
56
+ db.rollback()
57
+ finally:
58
+ db.close()
59
+
60
+
61
+ def sync_rooms_to_dataset():
62
+ for room_data_db in get_db(ROOMS_DATA_DB):
63
+ rooms = room_data_db.execute("SELECT * FROM rooms").fetchall()
64
+ cursor = room_data_db.cursor()
65
+ for row in tqdm.tqdm(rooms):
66
+ room_id = row["room_id"]
67
+ print("syncing room data: ", room_id)
68
+
69
+ objects = []
70
+ for result in paginator.paginate(Bucket=AWS_S3_BUCKET_NAME, Prefix=f'{room_id}/', Delimiter='/'):
71
+ results = []
72
+ for obj in result.get('Contents'):
73
+ try:
74
+ key = obj.get('Key')
75
+ time = obj.get('LastModified').isoformat()
76
+ split_str = re.split(r'[-/.]', key)
77
+ uuid = split_str[3]
78
+ x, y = [int(n)
79
+ for n in re.split(r'[_]', split_str[4])]
80
+ prompt = ' '.join(split_str[4:])
81
+ results.append(
82
+ {'x': x, 'y': y, 'prompt': prompt, 'time': time, 'key': key, 'uuid': uuid})
83
+ cursor.execute(
84
+ 'INSERT INTO rooms_data VALUES (NULL, ?, ?, ?, ?, ?, ?, ?)', (room_id, uuid, x, y, prompt, time, key))
85
+ except Exception as e:
86
+ print(e)
87
+ continue
88
+
89
+ objects += results
90
+ room_data_db.commit()
91
+
92
+ all_rows = [dict(row) for row in room_data_db.execute(
93
+ "SELECT * FROM rooms_data WHERE room_id = ?", (room_id,)).fetchall()]
94
+ data_path = S3_DATA_FOLDER / f"{room_id}.json"
95
+ with open(data_path, 'w') as f:
96
+ json.dump(all_rows, f, separators=(',', ':'))
97
+ print("Updating repository")
98
+ subprocess.Popen(
99
+ "git add . && git commit --amend -m 'update' && git push --force", cwd=S3_DATA_FOLDER, shell=True)
100
+
101
+
102
+ app = FastAPI()
103
+
104
+
105
+ @app.get("/")
106
+ def read_root():
107
+ return "Just a bot to sync data to huggingface datasets and tweet tha latest data"
108
+
109
+
110
+ @app.get("/sync")
111
+ def sync():
112
+ sync_rooms_to_dataset()
113
+ return "Synced data to huggingface datasets"
114
+
115
+
116
+ @app.on_event("startup")
117
+ @repeat_every(seconds=1800)
118
+ def repeat_sync():
119
+ sync_rooms_to_dataset()
120
+ return "Synced data to huggingface datasets"
requirements.txt ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ huggingface-hub==0.10
2
+ fastapi-utils==0.2
3
+ uvicorn==0.19
4
+ tqdm==4.64
5
+ boto3==1.26
6
+ fastapi==0.86
schema.sql ADDED
@@ -0,0 +1,59 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ PRAGMA foreign_keys=OFF;
2
+ BEGIN TRANSACTION;
3
+ CREATE TABLE rooms (
4
+ id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
5
+ room_id TEXT NOT NULL
6
+ );INSERT INTO rooms VALUES(1,'room-0');
7
+ INSERT INTO rooms VALUES(2,'room-1');
8
+ INSERT INTO rooms VALUES(3,'room-2');
9
+ INSERT INTO rooms VALUES(4,'room-3');
10
+ INSERT INTO rooms VALUES(5,'room-4');
11
+ INSERT INTO rooms VALUES(6,'room-5');
12
+ INSERT INTO rooms VALUES(7,'room-6');
13
+ INSERT INTO rooms VALUES(8,'room-7');
14
+ INSERT INTO rooms VALUES(9,'room-8');
15
+ INSERT INTO rooms VALUES(10,'room-9');
16
+ INSERT INTO rooms VALUES(11,'room-10');
17
+ INSERT INTO rooms VALUES(12,'room-11');
18
+ INSERT INTO rooms VALUES(13,'room-12');
19
+ INSERT INTO rooms VALUES(14,'room-13');
20
+ INSERT INTO rooms VALUES(15,'room-14');
21
+ INSERT INTO rooms VALUES(16,'room-15');
22
+ INSERT INTO rooms VALUES(17,'room-16');
23
+ INSERT INTO rooms VALUES(18,'room-17');
24
+ INSERT INTO rooms VALUES(19,'room-18');
25
+ INSERT INTO rooms VALUES(20,'room-19');
26
+ INSERT INTO rooms VALUES(21,'room-20');
27
+ INSERT INTO rooms VALUES(22,'room-21');
28
+ INSERT INTO rooms VALUES(23,'room-22');
29
+ INSERT INTO rooms VALUES(24,'room-23');
30
+ INSERT INTO rooms VALUES(25,'room-24');
31
+ INSERT INTO rooms VALUES(26,'room-25');
32
+ INSERT INTO rooms VALUES(27,'room-26');
33
+ INSERT INTO rooms VALUES(28,'room-27');
34
+ INSERT INTO rooms VALUES(29,'room-28');
35
+ INSERT INTO rooms VALUES(30,'room-29');
36
+ INSERT INTO rooms VALUES(31,'room-30');
37
+ INSERT INTO rooms VALUES(32,'room-31');
38
+ INSERT INTO rooms VALUES(33,'room-32');
39
+ INSERT INTO rooms VALUES(34,'room-33');
40
+ INSERT INTO rooms VALUES(35,'room-34');
41
+ INSERT INTO rooms VALUES(36,'room-35');
42
+ INSERT INTO rooms VALUES(37,'room-36');
43
+ INSERT INTO rooms VALUES(38,'room-37');
44
+ INSERT INTO rooms VALUES(39,'room-38');
45
+ INSERT INTO rooms VALUES(40,'room-39');
46
+ INSERT INTO rooms VALUES(41,'room-40');
47
+ DELETE FROM sqlite_sequence;
48
+ INSERT INTO sqlite_sequence VALUES('rooms',41);
49
+ CREATE TABLE rooms_data (
50
+ id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
51
+ room_id TEXT NOT NULL,
52
+ uuid TEXT NOT NULL,
53
+ x INTEGER NOT NULL,
54
+ y INTEGER NOT NULL,
55
+ prompt TEXT NOT NULL,
56
+ time DATETIME NOT NULL,
57
+ key TEXT NOT NULL,
58
+ UNIQUE (key) ON CONFLICT IGNORE
59
+ );COMMIT;