gittobl1 / app.py
o7c1's picture
Update app.py
e9b7089 verified
Raw History Blame Contribute Delete
2.94 kB
import gradio as gr
import threading
import asyncio
import logging
import zipfile
import os
import tempfile
from bot import bot_main, init_db
logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s")
logger = logging.getLogger(__name__)
def start_bot_thread():
"""Run the bot's async main in a separate event loop."""
init_db()
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
try:
logger.info("πŸ€– Bot thread started")
loop.run_until_complete(bot_main())
except Exception as e:
logger.error(f"πŸ’₯ Bot thread crashed: {e}", exc_info=True)
finally:
loop.close()
def zip_repository():
"""Zips the repository files safely, excluding sensitive or large files."""
try:
temp_dir = tempfile.gettempdir()
zip_path = os.path.join(temp_dir, "repo_download.zip")
# Files and directories to exclude for security, database privacy, and size
exclude_set = {
".git", ".env", "subscriptions.db", "config.py",
"__pycache__", ".venv", "venv", ".gitignore", ".gitattributes"
}
with zipfile.ZipFile(zip_path, 'w', zipfile.ZIP_DEFLATED) as zf:
for root, dirs, files in os.walk('.'):
# Prune excluded and hidden directories from traversal
dirs[:] = [d for d in dirs if d not in exclude_set and not d.startswith('.')]
for file in files:
# Ignore excluded files and active SQLite transient/database files
if file in exclude_set or file.startswith('.') or "subscriptions.db" in file:
continue
# Skip any previously generated zip files
if file.endswith('.zip') and root == '.':
continue
file_path = os.path.join(root, file)
arcname = os.path.relpath(file_path, '.')
zf.write(file_path, arcname=arcname)
logger.info(f"πŸ“ Repository zipped safely at: {zip_path}")
return zip_path
except Exception as e:
logger.error(f"❌ Failed to zip repository: {e}")
return None
# Launch the bot in a background daemon thread
thread = threading.Thread(target=start_bot_thread, daemon=True)
thread.start()
logger.info("🟒 Bot thread launched")
# Gradio interface
with gr.Blocks() as demo:
gr.Markdown("# πŸ€– GitHub Release Bot for Bale")
gr.Markdown("The bot is running in the background. Check the Bale app!")
gr.Markdown("### πŸ› οΈ Workspace Actions")
with gr.Row():
download_btn = gr.Button("πŸ“¦ Prepare Repository ZIP", variant="primary")
file_output = gr.File(label="Downloaded Archive")
download_btn.click(fn=zip_repository, outputs=file_output)
demo.launch()