testLLLMM commited on
Commit
fb1cdce
·
verified ·
1 Parent(s): 3122be9

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +180 -0
app.py ADDED
@@ -0,0 +1,180 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Gradio app for Hugging Face Spaces: view detokenized samples by category.
3
+ No processing — only serves pre-built sampled.jsonl. Data path via DATA_PATH env (default: sampled.jsonl).
4
+ """
5
+
6
+ import json
7
+ import os
8
+ from pathlib import Path
9
+
10
+ import gradio as gr
11
+
12
+
13
+ def load_sampled_jsonl(path: Path) -> dict[str, list[dict]]:
14
+ """Load JSONL into category -> list of {path, text, num_tokens}."""
15
+ by_cat: dict[str, list[dict]] = {}
16
+ with open(path, encoding="utf-8") as f:
17
+ for line in f:
18
+ line = line.strip()
19
+ if not line:
20
+ continue
21
+ rec = json.loads(line)
22
+ cat = rec.get("category", "unknown")
23
+ if cat not in by_cat:
24
+ by_cat[cat] = []
25
+ by_cat[cat].append(rec)
26
+ return by_cat
27
+
28
+
29
+ PAGE_SIZE = 500
30
+
31
+
32
+ def format_page(records: list[dict], page: int) -> str:
33
+ """Format one page of samples (up to PAGE_SIZE) as one string. Page is 1-based."""
34
+ if not records:
35
+ return ""
36
+ start = (page - 1) * PAGE_SIZE
37
+ end = min(start + PAGE_SIZE, len(records))
38
+ page_recs = records[start:end]
39
+ sep = "\n\n" + "—" * 50 + "\n\n"
40
+ parts = [r.get("text", "") for r in page_recs]
41
+ return sep.join(parts)
42
+
43
+
44
+ def create_demo():
45
+ data_path = Path(os.environ.get("DATA_PATH", "sampled.jsonl")).resolve()
46
+ if not data_path.is_file():
47
+ raise FileNotFoundError(
48
+ f"Data file not found: {data_path}. Upload sampled.jsonl to this Space (or set DATA_PATH)."
49
+ )
50
+ by_cat = load_sampled_jsonl(data_path)
51
+ categories = sorted(by_cat.keys())
52
+
53
+ def _page_label(page: int, max_page: int) -> str:
54
+ return f"Page {page} of {max_page}"
55
+
56
+ def on_category_change(cat: str | None):
57
+ if not cat or cat not in by_cat:
58
+ return "", (1, 1), "Page 1 of 1", gr.update(choices=["1"], value="1")
59
+ recs = by_cat[cat]
60
+ max_page = max(1, (len(recs) + PAGE_SIZE - 1) // PAGE_SIZE)
61
+ text = format_page(recs, 1)
62
+ choices = [str(i) for i in range(1, max_page + 1)]
63
+ return text, (1, max_page), _page_label(1, max_page), gr.update(choices=choices, value="1")
64
+
65
+ def on_next(cat: str | None, state: tuple):
66
+ page, max_page = state
67
+ if not cat or cat not in by_cat:
68
+ return "", state, _page_label(1, state[1]), gr.update()
69
+ page = min(max_page, page + 1)
70
+ recs = by_cat[cat]
71
+ text = format_page(recs, page)
72
+ return text, (page, max_page), _page_label(page, max_page), gr.update(value=str(page))
73
+
74
+ def on_prev(cat: str | None, state: tuple):
75
+ page, max_page = state
76
+ if not cat or cat not in by_cat:
77
+ return "", state, _page_label(1, state[1]), gr.update()
78
+ page = max(1, page - 1)
79
+ recs = by_cat[cat]
80
+ text = format_page(recs, page)
81
+ return text, (page, max_page), _page_label(page, max_page), gr.update(value=str(page))
82
+
83
+ def on_page_select(cat: str | None, page_str: str | None, state: tuple):
84
+ if not cat or cat not in by_cat or not page_str:
85
+ return "", state, _page_label(state[0], state[1])
86
+ try:
87
+ page = int(page_str)
88
+ except ValueError:
89
+ return "", state, _page_label(state[0], state[1])
90
+ _, max_page = state
91
+ page = max(1, min(max_page, page))
92
+ recs = by_cat[cat]
93
+ text = format_page(recs, page)
94
+ return text, (page, max_page), _page_label(page, max_page)
95
+
96
+ custom_css = """
97
+ .pagination-row { display: flex !important; flex-wrap: nowrap !important; align-items: center !important; gap: 12px !important; }
98
+ .gradio-container textarea { font-size: 18px !important; line-height: 1.6; }
99
+ .gradio-container .label { font-size: 17px !important; }
100
+ .gradio-container input, .gradio-container select { font-size: 17px !important; }
101
+ .home-page .prose, .home-page p, .home-page h1, .home-page .markdown { font-size: 24px !important; color: #212529 !important; }
102
+ .home-page h1 { font-size: 32px !important; color: #212529 !important; }
103
+ .home-page .card-wrap { display: inline-block; margin: 1em 0; border-radius: 16px; box-shadow: 0 8px 24px rgba(0,0,0,0.08); padding: 4px; background: #f8f9fa; border: 1px solid #e9ecef; }
104
+ .home-page .card-wrap .primary, .home-page .card-wrap button { font-size: 26px !important; padding: 40px 64px !important; min-height: 100px !important; border-radius: 12px !important; border: none !important; background: linear-gradient(180deg, #ffffff 0%, #f1f3f5 100%) !important; box-shadow: 0 2px 8px rgba(0,0,0,0.06) !important; cursor: pointer !important; transition: box-shadow 0.2s ease, transform 0.2s ease !important; font-weight: 600 !important; color: #212529 !important; }
105
+ .home-page .card-wrap .primary:hover, .home-page .card-wrap button:hover { box-shadow: 0 6px 20px rgba(0,0,0,0.12) !important; transform: translateY(-2px) !important; }
106
+ .home-page .card-desc, .home-page .card-wrap .markdown, .home-page .card-wrap p, .home-page .card-wrap div[class*="markdown"] { font-size: 20px !important; color: #212529 !important; margin-top: 0.5em !important; }
107
+ """
108
+ with gr.Blocks(title="Token sample viewer", css=custom_css) as demo:
109
+ home_col = gr.Column(visible=True, elem_classes=["home-page"])
110
+ with home_col:
111
+ gr.Markdown("# Home")
112
+ gr.Markdown("Select a dataset to view samples:")
113
+ with gr.Column(elem_classes=["card-wrap"]):
114
+ card_btn = gr.Button("OLMo3", elem_classes=["primary"])
115
+ gr.Markdown("OLMo3 / Dolma 3 pre-training data (Allen AI)", elem_classes=["card-desc"])
116
+
117
+ olmo3_col = gr.Column(visible=False)
118
+ with olmo3_col:
119
+ back_btn = gr.Button("← Back to Home")
120
+ gr.Markdown("""
121
+ **Dataset:** OLMo3 / Dolma 3 pre-training data (Allen AI).
122
+ Source: Hugging Face (e.g. `allenai/dolma3_pool`, `dolma3_mix_*`). Tokenized with Dolma; each **category** is one data split.
123
+ Text below is detokenized from `.npy` for inspection; document boundaries are not preserved.
124
+ """)
125
+ gr.Markdown("## View detokenized samples by category")
126
+ cat_dd = gr.Dropdown(
127
+ choices=categories,
128
+ label="Category",
129
+ value=categories[0] if categories else None,
130
+ )
131
+ page_state = gr.State((1, 1))
132
+ with gr.Row(elem_classes=["pagination-row"]):
133
+ prev_btn = gr.Button("← Previous")
134
+ next_btn = gr.Button("Next →")
135
+ page_label = gr.Markdown("Page 1 of 1", show_label=False)
136
+ page_dd = gr.Dropdown(choices=["1"], value="1", label="Go to page")
137
+ text_out = gr.Textbox(
138
+ label="Samples (max 500 per page)",
139
+ lines=25,
140
+ )
141
+
142
+ def show_olmo3():
143
+ return gr.update(visible=False), gr.update(visible=True)
144
+
145
+ def show_home():
146
+ return gr.update(visible=True), gr.update(visible=False)
147
+
148
+ card_btn.click(fn=show_olmo3, outputs=[home_col, olmo3_col])
149
+ back_btn.click(fn=show_home, outputs=[home_col, olmo3_col])
150
+ cat_dd.change(
151
+ fn=on_category_change,
152
+ inputs=[cat_dd],
153
+ outputs=[text_out, page_state, page_label, page_dd],
154
+ )
155
+ next_btn.click(
156
+ fn=on_next,
157
+ inputs=[cat_dd, page_state],
158
+ outputs=[text_out, page_state, page_label, page_dd],
159
+ )
160
+ prev_btn.click(
161
+ fn=on_prev,
162
+ inputs=[cat_dd, page_state],
163
+ outputs=[text_out, page_state, page_label, page_dd],
164
+ )
165
+ page_dd.change(
166
+ fn=on_page_select,
167
+ inputs=[cat_dd, page_dd, page_state],
168
+ outputs=[text_out, page_state, page_label],
169
+ )
170
+ demo.load(
171
+ fn=on_category_change,
172
+ inputs=[cat_dd],
173
+ outputs=[text_out, page_state, page_label, page_dd],
174
+ )
175
+ return demo
176
+
177
+
178
+ demo = create_demo()
179
+ port = int(os.environ.get("PORT", "7860"))
180
+ demo.launch(server_name="0.0.0.0", server_port=port)