Spaces:
Running
Running
import logging | |
from functools import partial | |
from io import StringIO | |
import gradio as gr | |
from apscheduler.schedulers.background import BackgroundScheduler | |
from bs4 import BeautifulSoup | |
from rich.console import Console | |
from rich.syntax import Syntax | |
from src.backend import backend_routine | |
from src.logging import configure_root_logger, log_file, setup_logger | |
logging.getLogger("httpx").setLevel(logging.WARNING) | |
logging.getLogger("numexpr").setLevel(logging.WARNING) | |
logging.getLogger("absl").setLevel(logging.WARNING) | |
configure_root_logger() | |
logging.basicConfig(level=logging.INFO) | |
logger = setup_logger(__name__) | |
def log_file_to_html_string(reverse=True): | |
with open(log_file, "rt") as f: | |
lines = f.readlines() | |
lines = lines[-300:] | |
if reverse: | |
lines = reversed(lines) | |
output = "".join(lines) | |
syntax = Syntax(output, "python", theme="monokai", word_wrap=True) | |
console = Console(record=True, width=150, style="#272822", file=StringIO()) | |
console.print(syntax) | |
html_content = console.export_html(inline_styles=True) | |
# Parse the HTML content using BeautifulSoup | |
soup = BeautifulSoup(html_content, "lxml") | |
# Modify the <pre> tag and add custom styles | |
pre_tag = soup.pre | |
pre_tag["class"] = "scrollable" | |
del pre_tag["style"] | |
# Add your custom styles and the .scrollable CSS to the <style> tag | |
style_tag = soup.style | |
style_tag.append( | |
""" | |
pre, code { | |
background-color: #272822; | |
} | |
.scrollable { | |
font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace; | |
height: 500px; | |
overflow: auto; | |
} | |
""" | |
) | |
return soup.prettify() | |
REPO_ID = "open-rl-leaderboard/leaderboard" | |
RESULTS_REPO = "open-rl-leaderboard/results" | |
links_md = f""" | |
# Important links | |
| Description | Link | | |
|-----------------|------| | |
| Leaderboard | [{REPO_ID}](https://huggingface.co/spaces/{REPO_ID}) | | |
| Results Repo | [{RESULTS_REPO}](https://huggingface.co/datasets/{RESULTS_REPO}) | | |
""" | |
with gr.Blocks() as demo: | |
gr.Markdown(links_md) | |
gr.HTML(partial(log_file_to_html_string), every=1) | |
with gr.Row(): | |
gr.DownloadButton("Download Log File", value=log_file) | |
scheduler = BackgroundScheduler() | |
scheduler.add_job(func=backend_routine, trigger="interval", seconds=5 * 60, max_instances=1) | |
scheduler.start() | |
if __name__ == "__main__": | |
demo.queue().launch() | |