QAway-to commited on
Commit
761bd44
·
1 Parent(s): fa85e5b

New style APP (Tab Changes v1.1)

Browse files
Files changed (2) hide show
  1. app.py +0 -1
  2. core/formatters.py +58 -42
app.py CHANGED
@@ -104,7 +104,6 @@ h2, h3, .gr-markdown {
104
 
105
 
106
  def run_comparison(p1, p2):
107
- """Wrapper: LLM comparison + HTML formatting."""
108
  raw_text = "".join(comparer.run(p1, p2))
109
  return format_comparison_output(raw_text)
110
 
 
104
 
105
 
106
  def run_comparison(p1, p2):
 
107
  raw_text = "".join(comparer.run(p1, p2))
108
  return format_comparison_output(raw_text)
109
 
core/formatters.py CHANGED
@@ -1,9 +1,9 @@
1
  """
2
  🇬🇧 Module: formatters.py
3
- Purpose: Converts Markdown-style LLM comparison outputs into styled HTML.
4
 
5
  🇷🇺 Модуль: formatters.py
6
- Назначение: форматирует текстовый Markdown-ответ LLM в таблицу и визуально читаемый отчёт.
7
  """
8
 
9
  import re
@@ -11,58 +11,74 @@ from html import escape
11
 
12
 
13
  def highlight_metrics(text: str) -> str:
14
- """Highlight positive/negative numeric values."""
15
  def colorize(match):
16
  val = match.group(0)
17
  try:
18
  num = float(val.replace("%", ""))
19
- if num > 0:
20
- return f"<span style='color:#10b981;font-weight:600;'>{val}</span>"
21
- elif num < 0:
22
- return f"<span style='color:#ef4444;font-weight:600;'>{val}</span>"
23
  except ValueError:
24
- pass
25
- return val
26
-
27
  return re.sub(r"-?\d+\.?\d*%", colorize, text)
28
 
29
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
30
  def format_comparison_output(raw: str) -> str:
31
- """Convert LLM Markdown table into styled HTML."""
32
  if not raw:
33
- return "<i>No data received.</i>"
34
 
35
- # Format headers
36
- formatted = raw
37
- formatted = re.sub(r"### ?1️⃣", "<h3 style='color:#93c5fd;'>1. Summary Table</h3>", formatted)
38
- formatted = re.sub(r"### ?2️⃣", "<h3 style='color:#a78bfa;'>2. Comparative Analysis</h3>", formatted)
39
- formatted = re.sub(r"### ?3️⃣", "<h3 style='color:#fbbf24;'>3. Recommendation</h3>", formatted)
40
 
41
- # Convert Markdown tables to HTML tables
42
- def md_table_to_html(md_table):
43
- rows = [r.strip() for r in md_table.strip().splitlines() if "|" in r]
44
- if len(rows) < 2:
45
- return md_table
46
- headers = [escape(c.strip()) for c in rows[0].split("|")[1:-1]]
47
- html = "<table style='width:100%;border-collapse:collapse;margin-top:6px;'>"
48
- html += "<thead><tr>" + "".join(
49
- f"<th style='border-bottom:1px solid #30363d;text-align:center;padding:6px;color:#c9d1d9;font-weight:600;'>{h}</th>"
50
- for h in headers
51
- ) + "</tr></thead><tbody>"
52
- for row in rows[2:]:
53
- cols = [highlight_metrics(c.strip()) for c in row.split("|")[1:-1]]
54
- html += "<tr>" + "".join(
55
- f"<td style='padding:5px 6px;text-align:center;border-bottom:1px solid #30363d;color:#e6edf3;'>{c}</td>"
56
- for c in cols
57
- ) + "</tr>"
58
- html += "</tbody></table>"
59
- return html
60
 
61
- formatted = re.sub(r"(\|.*\|[\r\n\|\-\.\w\s%]+)", lambda m: md_table_to_html(m.group(0)), formatted)
 
 
 
 
62
 
63
- # Final cleanup and minor formatting
64
- formatted = highlight_metrics(formatted)
65
- formatted = formatted.replace("**", "")
66
- formatted = formatted.replace("\n", "<br>")
67
 
68
- return f"<div style='font-family:Inter, sans-serif;font-size:15px;line-height:1.5;color:#e6edf3;'>{formatted}</div>"
 
 
 
 
 
 
1
  """
2
  🇬🇧 Module: formatters.py
3
+ Purpose: Converts LLM Markdown output (comparison/analysis) into styled HTML tables.
4
 
5
  🇷🇺 Модуль: formatters.py
6
+ Назначение: преобразует Markdown-таблицы и текст отчёта LLM в аккуратное HTML-представление.
7
  """
8
 
9
  import re
 
11
 
12
 
13
  def highlight_metrics(text: str) -> str:
14
+ """Highlight numeric percentage values in green/red."""
15
  def colorize(match):
16
  val = match.group(0)
17
  try:
18
  num = float(val.replace("%", ""))
19
+ color = "#10b981" if num > 0 else "#ef4444" if num < 0 else "#e6edf3"
20
+ return f"<span style='color:{color};font-weight:600;'>{val}</span>"
 
 
21
  except ValueError:
22
+ return val
 
 
23
  return re.sub(r"-?\d+\.?\d*%", colorize, text)
24
 
25
 
26
+ def md_table_to_html(md_table: str) -> str:
27
+ """Convert a single Markdown table block into HTML."""
28
+ lines = [ln.strip() for ln in md_table.strip().splitlines() if "|" in ln]
29
+ if len(lines) < 2:
30
+ return md_table
31
+
32
+ # remove the Markdown alignment line (---|---)
33
+ header_line = lines[0]
34
+ body_lines = [ln for ln in lines[2:]]
35
+
36
+ headers = [escape(c.strip()) for c in header_line.split("|")[1:-1]]
37
+ html = (
38
+ "<table style='width:100%;border-collapse:collapse;"
39
+ "margin:10px 0;font-size:14px;font-family:Inter,sans-serif;'>"
40
+ "<thead><tr>"
41
+ + "".join(
42
+ f"<th style='border-bottom:1px solid #30363d;padding:6px;color:#c9d1d9;font-weight:600;text-align:center;'>"
43
+ f"{h}</th>" for h in headers
44
+ )
45
+ + "</tr></thead><tbody>"
46
+ )
47
+ for ln in body_lines:
48
+ cols = [highlight_metrics(escape(c.strip())) for c in ln.split("|")[1:-1]]
49
+ html += "<tr>" + "".join(
50
+ f"<td style='padding:6px 8px;border-bottom:1px solid #30363d;text-align:center;color:#e6edf3;'>"
51
+ f"{c}</td>" for c in cols
52
+ ) + "</tr>"
53
+ html += "</tbody></table>"
54
+ return html
55
+
56
+
57
  def format_comparison_output(raw: str) -> str:
58
+ """Format LLM Markdown response into rich HTML."""
59
  if not raw:
60
+ return "<i>No comparison data available.</i>"
61
 
62
+ text = raw.strip()
 
 
 
 
63
 
64
+ # Section titles
65
+ text = re.sub(r"###\s*1️⃣.*", "<h3 style='color:#93c5fd;margin-top:12px;'>1. Summary Table</h3>", text)
66
+ text = re.sub(r"###\s*2️⃣.*", "<h3 style='color:#a78bfa;margin-top:12px;'>2. Comparative Analysis</h3>", text)
67
+ text = re.sub(r"###\s*3️⃣.*", "<h3 style='color:#fbbf24;margin-top:12px;'>3. Recommendation</h3>", text)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
68
 
69
+ # Extract and replace Markdown tables
70
+ tables = re.findall(r"(\|[^\n]+\|\n(\|[^\n]+\|\n)+)", text)
71
+ for tbl, _ in tables:
72
+ html_table = md_table_to_html(tbl)
73
+ text = text.replace(tbl, html_table)
74
 
75
+ text = highlight_metrics(text)
76
+ text = text.replace("**", "")
77
+ text = text.replace("\n", "<br>")
 
78
 
79
+ return (
80
+ "<div style='font-family:Inter,sans-serif;font-size:15px;line-height:1.6;"
81
+ "color:#e6edf3;'>"
82
+ + text +
83
+ "</div>"
84
+ )