radames commited on
Commit
a4b71bb
1 Parent(s): a101d9b
Files changed (5) hide show
  1. .gitignore +2 -1
  2. README.md +1 -1
  3. app.py +25 -4
  4. db.py +11 -8
  5. requirements.txt +1 -2
.gitignore CHANGED
@@ -18,4 +18,5 @@ data
18
  data.db
19
  data.json
20
  rooms_data.db
21
- sd-multiplayer-data/
 
 
18
  data.db
19
  data.json
20
  rooms_data.db
21
+ sd-multiplayer-data/
22
+ diffusers-gallery-data/
README.md CHANGED
@@ -1,5 +1,5 @@
1
  ---
2
- title: Sd Multiplayer Bot
3
  emoji: 🤖
4
  colorFrom: red
5
  colorTo: indigo
 
1
  ---
2
+ title: Diffusers Gallery Bot
3
  emoji: 🤖
4
  colorFrom: red
5
  colorTo: indigo
app.py CHANGED
@@ -5,18 +5,30 @@ import aiohttp
5
  import requests
6
  import json
7
  from tqdm import tqdm
8
-
9
  from huggingface_hub import Repository
10
 
11
  from fastapi import FastAPI, BackgroundTasks
12
  from fastapi_utils.tasks import repeat_every
13
- from fastapi.staticfiles import StaticFiles
14
 
15
  from db import Database
16
 
17
  HF_TOKEN = os.environ.get("HF_TOKEN")
18
 
19
- database = Database()
 
 
 
 
 
 
 
 
 
 
 
 
20
 
21
 
22
  async def check_image_url(url):
@@ -115,6 +127,13 @@ async def sync_data():
115
 
116
 
117
  app = FastAPI()
 
 
 
 
 
 
 
118
 
119
 
120
  @ app.get("/sync")
@@ -152,7 +171,9 @@ def get_page(page: int = 1):
152
  }
153
 
154
 
155
- app.mount("/", StaticFiles(directory="static", html=True), name="static")
 
 
156
 
157
  # @app.on_event("startup")
158
  # @repeat_every(seconds=1800)
 
5
  import requests
6
  import json
7
  from tqdm import tqdm
8
+ from pathlib import Path
9
  from huggingface_hub import Repository
10
 
11
  from fastapi import FastAPI, BackgroundTasks
12
  from fastapi_utils.tasks import repeat_every
13
+ from fastapi.middleware.cors import CORSMiddleware
14
 
15
  from db import Database
16
 
17
  HF_TOKEN = os.environ.get("HF_TOKEN")
18
 
19
+
20
+ DB_FOLDER = Path("diffusers-gallery-data")
21
+
22
+
23
+ repo = Repository(
24
+ local_dir=DB_FOLDER,
25
+ repo_type="dataset",
26
+ clone_from="huggingface-projects/diffusers-gallery-data",
27
+ use_auth_token=True,
28
+ )
29
+ repo.git_pull()
30
+
31
+ database = Database(DB_FOLDER)
32
 
33
 
34
  async def check_image_url(url):
 
127
 
128
 
129
  app = FastAPI()
130
+ app.add_middleware(
131
+ CORSMiddleware,
132
+ allow_origins=["*"],
133
+ allow_credentials=True,
134
+ allow_methods=["*"],
135
+ allow_headers=["*"],
136
+ )
137
 
138
 
139
  @ app.get("/sync")
 
171
  }
172
 
173
 
174
+ @app.get("/")
175
+ def read_root():
176
+ return "Just a bot to sync data from diffusers gallery"
177
 
178
  # @app.on_event("startup")
179
  # @repeat_every(seconds=1800)
db.py CHANGED
@@ -2,22 +2,25 @@ import sqlite3
2
  from pathlib import Path
3
 
4
 
5
- class Database:
6
- DB_PATH = Path("data/")
7
- DB_FILE = DB_PATH / "models.db"
8
 
9
- def __init__(self):
10
- if not self.DB_FILE.exists():
 
 
 
 
 
 
11
  print("Creating database")
12
- print("DB_FILE", self.DB_FILE)
13
- db = sqlite3.connect(self.DB_FILE)
14
  with open(Path("schema.sql"), "r") as f:
15
  db.executescript(f.read())
16
  db.commit()
17
  db.close()
18
 
19
  def get_db(self):
20
- db = sqlite3.connect(self.DB_FILE, check_same_thread=False)
21
  db.row_factory = sqlite3.Row
22
  return db
23
 
 
2
  from pathlib import Path
3
 
4
 
 
 
 
5
 
6
+
7
+ class Database:
8
+ def __init__(self, db_path=None):
9
+ if db_path is None:
10
+ raise ValueError("db_path must be provided")
11
+ self.db_path = db_path
12
+ self.db_file = self.db_path / "models.db"
13
+ if not self.db_file.exists():
14
  print("Creating database")
15
+ print("DB_FILE", self.db_file)
16
+ db = sqlite3.connect(self.db_file)
17
  with open(Path("schema.sql"), "r") as f:
18
  db.executescript(f.read())
19
  db.commit()
20
  db.close()
21
 
22
  def get_db(self):
23
+ db = sqlite3.connect(self.db_file, check_same_thread=False)
24
  db.row_factory = sqlite3.Row
25
  return db
26
 
requirements.txt CHANGED
@@ -5,5 +5,4 @@ tqdm
5
  fastapi
6
  requests
7
  asyncio
8
- aiohttp
9
- sqlite3
 
5
  fastapi
6
  requests
7
  asyncio
8
+ aiohttp