Update app.py
Browse files
app.py
CHANGED
|
@@ -32,23 +32,20 @@ os.makedirs(BASE_DIR, exist_ok=True)
|
|
| 32 |
|
| 33 |
DISK_LIMIT_GB = 45
|
| 34 |
|
| 35 |
-
# ================= FASTAPI (
|
| 36 |
|
| 37 |
web = FastAPI()
|
| 38 |
|
| 39 |
@web.get("/")
|
| 40 |
def home():
|
| 41 |
-
return {"status": "running"
|
| 42 |
|
| 43 |
@web.get("/health")
|
| 44 |
def health():
|
| 45 |
free = shutil.disk_usage("/data").free
|
| 46 |
-
return {
|
| 47 |
-
"ok": True,
|
| 48 |
-
"free_gb": round(free / (1024**3), 2)
|
| 49 |
-
}
|
| 50 |
|
| 51 |
-
# =================
|
| 52 |
|
| 53 |
app = Client(
|
| 54 |
"hf_bot",
|
|
@@ -61,14 +58,14 @@ hf = HfApi(token=HF_TOKEN)
|
|
| 61 |
|
| 62 |
# ================= UTIL =================
|
| 63 |
|
| 64 |
-
def hash_id(
|
| 65 |
-
return hashlib.sha256(
|
| 66 |
|
| 67 |
-
def
|
| 68 |
return hash_id(f"{uid}:{uname}")
|
| 69 |
|
| 70 |
-
def
|
| 71 |
-
path = os.path.join(BASE_DIR,
|
| 72 |
os.makedirs(path, exist_ok=True)
|
| 73 |
return path
|
| 74 |
|
|
@@ -89,13 +86,13 @@ async def keepalive():
|
|
| 89 |
|
| 90 |
# ================= HF UPLOAD =================
|
| 91 |
|
| 92 |
-
async def
|
| 93 |
loop = asyncio.get_event_loop()
|
| 94 |
await loop.run_in_executor(
|
| 95 |
None,
|
| 96 |
lambda: hf.upload_file(
|
| 97 |
-
path_or_fileobj=
|
| 98 |
-
path_in_repo=
|
| 99 |
repo_id=DATASET_REPO,
|
| 100 |
repo_type="dataset"
|
| 101 |
)
|
|
@@ -111,7 +108,7 @@ async def worker():
|
|
| 111 |
|
| 112 |
msg = job["msg"]
|
| 113 |
file_id = job["file_id"]
|
| 114 |
-
|
| 115 |
fake = job["fake"]
|
| 116 |
|
| 117 |
if not disk_ok():
|
|
@@ -120,14 +117,14 @@ async def worker():
|
|
| 120 |
|
| 121 |
status = await msg.reply("β¬ Downloading...")
|
| 122 |
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
|
|
|
| 126 |
|
| 127 |
await status.edit("β Uploading to HuggingFace...")
|
| 128 |
|
| 129 |
-
|
| 130 |
-
await upload_to_hf(path, remote_path)
|
| 131 |
|
| 132 |
try:
|
| 133 |
os.remove(path)
|
|
@@ -136,21 +133,24 @@ async def worker():
|
|
| 136 |
|
| 137 |
await status.edit("β
Done")
|
| 138 |
|
| 139 |
-
# ================= HANDLERS =================
|
| 140 |
|
| 141 |
pending = {}
|
| 142 |
|
| 143 |
-
|
|
|
|
| 144 |
async def start(_, msg):
|
| 145 |
-
|
|
|
|
| 146 |
|
| 147 |
-
|
|
|
|
| 148 |
async def receive(_, msg):
|
| 149 |
user = msg.from_user
|
| 150 |
if not user:
|
| 151 |
return
|
| 152 |
|
| 153 |
-
|
| 154 |
|
| 155 |
file_id = (
|
| 156 |
msg.document.file_id if msg.document else
|
|
@@ -162,7 +162,7 @@ async def receive(_, msg):
|
|
| 162 |
|
| 163 |
pending[user.id] = {
|
| 164 |
"file_id": file_id,
|
| 165 |
-
"folder":
|
| 166 |
"fake": fake,
|
| 167 |
"msg": msg
|
| 168 |
}
|
|
@@ -176,6 +176,7 @@ async def receive(_, msg):
|
|
| 176 |
|
| 177 |
await msg.reply("File received. Upload?", reply_markup=kb)
|
| 178 |
|
|
|
|
| 179 |
@app.on_callback_query()
|
| 180 |
async def cb(_, q):
|
| 181 |
uid = q.from_user.id
|
|
@@ -193,12 +194,7 @@ async def cb(_, q):
|
|
| 193 |
# ================= STARTUP =================
|
| 194 |
|
| 195 |
async def run_web():
|
| 196 |
-
config = uvicorn.Config(
|
| 197 |
-
web,
|
| 198 |
-
host="0.0.0.0",
|
| 199 |
-
port=7860,
|
| 200 |
-
log_level="info"
|
| 201 |
-
)
|
| 202 |
server = uvicorn.Server(config)
|
| 203 |
await server.serve()
|
| 204 |
|
|
|
|
| 32 |
|
| 33 |
DISK_LIMIT_GB = 45
|
| 34 |
|
| 35 |
+
# ================= FASTAPI (7860) =================
|
| 36 |
|
| 37 |
web = FastAPI()
|
| 38 |
|
| 39 |
@web.get("/")
|
| 40 |
def home():
|
| 41 |
+
return {"status": "running"}
|
| 42 |
|
| 43 |
@web.get("/health")
|
| 44 |
def health():
|
| 45 |
free = shutil.disk_usage("/data").free
|
| 46 |
+
return {"free_gb": round(free / (1024**3), 2)}
|
|
|
|
|
|
|
|
|
|
| 47 |
|
| 48 |
+
# ================= BOT =================
|
| 49 |
|
| 50 |
app = Client(
|
| 51 |
"hf_bot",
|
|
|
|
| 58 |
|
| 59 |
# ================= UTIL =================
|
| 60 |
|
| 61 |
+
def hash_id(x: str):
|
| 62 |
+
return hashlib.sha256(x.encode()).hexdigest()[:16]
|
| 63 |
|
| 64 |
+
def folder(uid, uname):
|
| 65 |
return hash_id(f"{uid}:{uname}")
|
| 66 |
|
| 67 |
+
def user_dir(f):
|
| 68 |
+
path = os.path.join(BASE_DIR, f)
|
| 69 |
os.makedirs(path, exist_ok=True)
|
| 70 |
return path
|
| 71 |
|
|
|
|
| 86 |
|
| 87 |
# ================= HF UPLOAD =================
|
| 88 |
|
| 89 |
+
async def upload_hf(local, remote):
|
| 90 |
loop = asyncio.get_event_loop()
|
| 91 |
await loop.run_in_executor(
|
| 92 |
None,
|
| 93 |
lambda: hf.upload_file(
|
| 94 |
+
path_or_fileobj=local,
|
| 95 |
+
path_in_repo=remote,
|
| 96 |
repo_id=DATASET_REPO,
|
| 97 |
repo_type="dataset"
|
| 98 |
)
|
|
|
|
| 108 |
|
| 109 |
msg = job["msg"]
|
| 110 |
file_id = job["file_id"]
|
| 111 |
+
folder_name = job["folder"]
|
| 112 |
fake = job["fake"]
|
| 113 |
|
| 114 |
if not disk_ok():
|
|
|
|
| 117 |
|
| 118 |
status = await msg.reply("β¬ Downloading...")
|
| 119 |
|
| 120 |
+
path = await app.download_media(
|
| 121 |
+
file_id,
|
| 122 |
+
file_name=os.path.join(user_dir(folder_name), fake)
|
| 123 |
+
)
|
| 124 |
|
| 125 |
await status.edit("β Uploading to HuggingFace...")
|
| 126 |
|
| 127 |
+
await upload_hf(path, f"{folder_name}/{fake}")
|
|
|
|
| 128 |
|
| 129 |
try:
|
| 130 |
os.remove(path)
|
|
|
|
| 133 |
|
| 134 |
await status.edit("β
Done")
|
| 135 |
|
| 136 |
+
# ================= HANDLERS (FIXED ORDER) =================
|
| 137 |
|
| 138 |
pending = {}
|
| 139 |
|
| 140 |
+
# π₯ IMPORTANT: START FIRST (private only)
|
| 141 |
+
@app.on_message(filters.private & filters.command("start"))
|
| 142 |
async def start(_, msg):
|
| 143 |
+
log.info("START RECEIVED")
|
| 144 |
+
await msg.reply("π Bot is working. Send files to store them.")
|
| 145 |
|
| 146 |
+
# π₯ FILE HANDLER (private only)
|
| 147 |
+
@app.on_message(filters.private & (filters.document | filters.video | filters.audio))
|
| 148 |
async def receive(_, msg):
|
| 149 |
user = msg.from_user
|
| 150 |
if not user:
|
| 151 |
return
|
| 152 |
|
| 153 |
+
f = folder(user.id, user.username or str(user.id))
|
| 154 |
|
| 155 |
file_id = (
|
| 156 |
msg.document.file_id if msg.document else
|
|
|
|
| 162 |
|
| 163 |
pending[user.id] = {
|
| 164 |
"file_id": file_id,
|
| 165 |
+
"folder": f,
|
| 166 |
"fake": fake,
|
| 167 |
"msg": msg
|
| 168 |
}
|
|
|
|
| 176 |
|
| 177 |
await msg.reply("File received. Upload?", reply_markup=kb)
|
| 178 |
|
| 179 |
+
# π₯ CALLBACKS
|
| 180 |
@app.on_callback_query()
|
| 181 |
async def cb(_, q):
|
| 182 |
uid = q.from_user.id
|
|
|
|
| 194 |
# ================= STARTUP =================
|
| 195 |
|
| 196 |
async def run_web():
|
| 197 |
+
config = uvicorn.Config(web, host="0.0.0.0", port=7860)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 198 |
server = uvicorn.Server(config)
|
| 199 |
await server.serve()
|
| 200 |
|