derek-thomas HF staff commited on
Commit
6a95e74
1 Parent(s): 3165ac3

Moving and updating visualize_logs.py

Browse files
Files changed (2) hide show
  1. app.py +1 -46
  2. utilities/visualize_logs.py +51 -0
app.py CHANGED
@@ -2,12 +2,10 @@ import os
2
  from pathlib import Path
3
 
4
  import gradio as gr
5
- from bs4 import BeautifulSoup
6
  from huggingface_hub import WebhookPayload, WebhooksServer
7
- from rich.console import Console
8
- from rich.syntax import Syntax
9
 
10
  from utilities.my_logger import setup_logger
 
11
 
12
  proj_dir = Path(__name__).parent
13
 
@@ -24,49 +22,6 @@ WEBHOOK_SECRET = os.getenv("HF_WEBHOOK_SECRET", 'secret')
24
  logger = setup_logger(__name__)
25
 
26
 
27
- def log_file_to_html_string():
28
- log_file = "mylog.log"
29
- num_lines_visualize = 50
30
-
31
- console = Console(record=True, width=150, style="#272822")
32
- with open(log_file, "rt") as f:
33
- # Seek to the end of the file minus 300 lines
34
- # Read the last 300 lines of the file
35
- lines = f.readlines()
36
- lines = lines[-num_lines_visualize:]
37
-
38
- # Syntax-highlight the last 300 lines of the file using the Python lexer and Monokai style
39
- output = "".join(lines)
40
- syntax = Syntax(output, "python", theme="monokai", word_wrap=True)
41
-
42
- console.print(syntax);
43
- html_content = console.export_html(inline_styles=True)
44
-
45
- # Parse the HTML content using BeautifulSoup
46
- soup = BeautifulSoup(html_content, 'lxml')
47
-
48
- # Modify the <pre> tag
49
- pre_tag = soup.pre
50
- pre_tag['class'] = 'scrollable'
51
- del pre_tag['style']
52
-
53
- # Add your custom styles and the .scrollable CSS to the <style> tag
54
- style_tag = soup.style
55
- style_content = """
56
- pre, code {
57
- background-color: #272822;
58
- }
59
- .scrollable {
60
- font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace;
61
- height: 500px;
62
- overflow: auto;
63
- }
64
- """
65
- style_tag.append(style_content)
66
-
67
- return soup.prettify()
68
-
69
-
70
  intro_md = f"""
71
  # Reddit Dataset Creator
72
  This is a reddit dataset creator which builds and updates [{DATASET_NAME}](https://huggingface.co/datasets/{DATASET_NAME})
 
2
  from pathlib import Path
3
 
4
  import gradio as gr
 
5
  from huggingface_hub import WebhookPayload, WebhooksServer
 
 
6
 
7
  from utilities.my_logger import setup_logger
8
+ from utilities.visualize_logs import log_file_to_html_string
9
 
10
  proj_dir = Path(__name__).parent
11
 
 
22
  logger = setup_logger(__name__)
23
 
24
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25
  intro_md = f"""
26
  # Reddit Dataset Creator
27
  This is a reddit dataset creator which builds and updates [{DATASET_NAME}](https://huggingface.co/datasets/{DATASET_NAME})
utilities/visualize_logs.py ADDED
@@ -0,0 +1,51 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from io import StringIO
2
+ from pathlib import Path
3
+
4
+ from bs4 import BeautifulSoup
5
+ from rich.console import Console
6
+ from rich.syntax import Syntax
7
+
8
+ proj_dir = Path(__file__).parents[1]
9
+
10
+
11
+ def log_file_to_html_string():
12
+ log_file = proj_dir / "mylog.log"
13
+ num_lines_visualize = 50
14
+
15
+ with open(log_file, "rt") as f:
16
+ # Seek to the end of the file minus 300 lines
17
+ # Read the last 300 lines of the file
18
+ lines = f.readlines()
19
+ lines = lines[-num_lines_visualize:]
20
+
21
+ # Syntax-highlight the last 300 lines of the file using the Python lexer and Monokai style
22
+ output = "".join(lines)
23
+ syntax = Syntax(output, "python", theme="monokai", word_wrap=True)
24
+
25
+ console = Console(record=True, width=150, style="#272822", file=StringIO())
26
+ console.print(syntax)
27
+ html_content = console.export_html(inline_styles=True)
28
+
29
+ # Parse the HTML content using BeautifulSoup
30
+ soup = BeautifulSoup(html_content, 'lxml')
31
+
32
+ # Modify the <pre> tag
33
+ pre_tag = soup.pre
34
+ pre_tag['class'] = 'scrollable'
35
+ del pre_tag['style']
36
+
37
+ # Add your custom styles and the .scrollable CSS to the <style> tag
38
+ style_tag = soup.style
39
+ style_content = """
40
+ pre, code {
41
+ background-color: #272822;
42
+ }
43
+ .scrollable {
44
+ font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace;
45
+ height: 500px;
46
+ overflow: auto;
47
+ }
48
+ """
49
+ style_tag.append(style_content)
50
+
51
+ return soup.prettify()