Spaces:
Sleeping
Sleeping
# main.py | |
import os | |
import logging | |
import asyncio | |
from fastapi import FastAPI | |
from fastapi.responses import HTMLResponse | |
import uvicorn | |
from bot import bot, setup_dispatcher, TASK_QUEUE, BATCH_JOBS | |
# Setup logging | |
logging.basicConfig( | |
level=logging.INFO, | |
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", | |
) | |
app = FastAPI( | |
title="Terabox Bot Admin", | |
description="Web interface for checking Terabox Bot status.", | |
version="1.0.0", | |
) | |
dp = setup_dispatcher() | |
async def on_startup(): | |
logging.info("Starting bot polling in background...") | |
asyncio.create_task(dp.start_polling(bot)) | |
# Routes | |
async def root(): | |
return """ | |
<html> | |
<head> | |
<title>Terabox Bot - Admin Panel</title> | |
</head> | |
<body> | |
<h2>π Terabox Bot is running!</h2> | |
<p>Endpoints:</p> | |
<ul> | |
<li><a href="/health">/health</a> β Health check</li> | |
<li><a href="/status">/status</a> β Bot queue status</li> | |
</ul> | |
</body> | |
</html> | |
""" | |
async def health(): | |
return {"status": "ok", "message": "Bot is healthy!"} | |
async def status(): | |
html = "<h3>π Terabox Bot - Current Status</h3>" | |
html += f"<p>Pending queue size: {TASK_QUEUE.qsize()}</p>" | |
if not BATCH_JOBS: | |
html += "<p>β No active batches.</p>" | |
else: | |
html += "<ul>" | |
for batch_id, batch in BATCH_JOBS.items(): | |
processed = batch["processed_links"] | |
total = batch["total_links"] | |
html += f"<li>Batch <b>{batch_id[:6]}</b>: {processed}/{total} processed</li>" | |
html += "</ul>" | |
return html | |
# Run with uvicorn | |
if __name__ == "__main__": | |
port = int(os.getenv("PORT", 7860)) # HuggingFace prefers PORT env sometimes | |
uvicorn.run("main:app", host="0.0.0.0", port=port, reload=False) | |