dev-strender commited on
Commit
20a14d9
ยท
1 Parent(s): 6095079

Replace 500-char limit with paragraph-greedy chunking + parallel run

Browse files
Files changed (1) hide show
  1. blindtest/ui.py +50 -15
blindtest/ui.py CHANGED
@@ -11,6 +11,7 @@ Flow:
11
  from __future__ import annotations
12
 
13
  import time
 
14
  from typing import Any
15
 
16
  import gradio as gr
@@ -19,6 +20,35 @@ from pipelines import run_pipeline
19
 
20
  from . import db
21
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22
  PipelineConfig = tuple[str, str, str] # (pipeline_key, model, prompt_key)
23
 
24
 
@@ -48,13 +78,12 @@ def build_feedback_tab(
48
 
49
  pipeline_run_id_state = gr.State(None)
50
 
51
- MAX_CHARS = 500
52
 
53
  input_text = gr.Textbox(
54
- label=f"์›๋ฌธ ์ž…๋ ฅ (์ตœ๋Œ€ {MAX_CHARS}์ž)",
55
  lines=8,
56
- placeholder=f"๊ต์ •ํ•  ํ…์ŠคํŠธ๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”. ({MAX_CHARS}์ž ์ด๋‚ด)",
57
- max_length=MAX_CHARS,
58
  )
59
 
60
  run_btn = gr.Button(
@@ -87,14 +116,6 @@ def build_feedback_tab(
87
  None,
88
  gr.update(value=""),
89
  )
90
- if len(text) > MAX_CHARS:
91
- return (
92
- gr.update(value=f"์ž…๋ ฅ์ด {MAX_CHARS}์ž๋ฅผ ์ดˆ๊ณผํ–ˆ์Šต๋‹ˆ๋‹ค ({len(text)}์ž). ์ค„์—ฌ์„œ ๋‹ค์‹œ ์‹œ๋„ํ•ด์ฃผ์„ธ์š”."),
93
- gr.update(value=""),
94
- gr.update(value=""),
95
- None,
96
- gr.update(value=""),
97
- )
98
  if client is None:
99
  return (
100
  gr.update(value="UPSTAGE_API_KEY ๋ฏธ์„ค์ •."),
@@ -104,9 +125,23 @@ def build_feedback_tab(
104
  gr.update(value=""),
105
  )
106
 
 
 
 
 
107
  start = time.time()
 
108
  try:
109
- result = run_pipeline(text, pipeline_key, model, prompt_key, client, vocabulary)
 
 
 
 
 
 
 
 
 
110
  except Exception as exc:
111
  return (
112
  gr.update(value=f"์—๋Ÿฌ: {exc}"),
@@ -116,8 +151,8 @@ def build_feedback_tab(
116
  gr.update(value=""),
117
  )
118
 
119
- elapsed = result.get("processing_time", time.time() - start)
120
- out_text = result.get("output", "")
121
 
122
  article_id = db.save_article(text)
123
  run_id = db.save_pipeline_run(
 
11
  from __future__ import annotations
12
 
13
  import time
14
+ from concurrent.futures import ThreadPoolExecutor
15
  from typing import Any
16
 
17
  import gradio as gr
 
20
 
21
  from . import db
22
 
23
+
24
+ def _chunk_by_paragraph(text: str, max_chars: int) -> list[str]:
25
+ """Greedy paragraph-level packing.
26
+
27
+ Splits on blank-line paragraph boundaries and packs paragraphs into chunks
28
+ whose total length stays โ‰ค max_chars. A single paragraph longer than
29
+ max_chars stands alone (we never split mid-paragraph to preserve context).
30
+ """
31
+ paragraphs = [p for p in text.split("\n\n") if p.strip()]
32
+ if not paragraphs:
33
+ return [text] if text.strip() else []
34
+
35
+ sep = "\n\n"
36
+ chunks: list[str] = []
37
+ current: list[str] = []
38
+ current_len = 0
39
+ for p in paragraphs:
40
+ added = (len(sep) if current else 0) + len(p)
41
+ if current and current_len + added > max_chars:
42
+ chunks.append(sep.join(current))
43
+ current = [p]
44
+ current_len = len(p)
45
+ else:
46
+ current.append(p)
47
+ current_len += added
48
+ if current:
49
+ chunks.append(sep.join(current))
50
+ return chunks
51
+
52
  PipelineConfig = tuple[str, str, str] # (pipeline_key, model, prompt_key)
53
 
54
 
 
78
 
79
  pipeline_run_id_state = gr.State(None)
80
 
81
+ CHUNK_MAX_CHARS = 500
82
 
83
  input_text = gr.Textbox(
84
+ label="์›๋ฌธ ์ž…๋ ฅ",
85
  lines=8,
86
+ placeholder="๊ต์ •ํ•  ํ…์ŠคํŠธ๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”.",
 
87
  )
88
 
89
  run_btn = gr.Button(
 
116
  None,
117
  gr.update(value=""),
118
  )
 
 
 
 
 
 
 
 
119
  if client is None:
120
  return (
121
  gr.update(value="UPSTAGE_API_KEY ๋ฏธ์„ค์ •."),
 
125
  gr.update(value=""),
126
  )
127
 
128
+ chunks = _chunk_by_paragraph(text, CHUNK_MAX_CHARS)
129
+ if not chunks:
130
+ chunks = [text]
131
+
132
  start = time.time()
133
+ chunk_results: list[dict] = [{} for _ in chunks]
134
  try:
135
+ with ThreadPoolExecutor(max_workers=min(len(chunks), 4)) as ex:
136
+ futures = {
137
+ ex.submit(
138
+ run_pipeline, chunk, pipeline_key, model, prompt_key, client, vocabulary
139
+ ): i
140
+ for i, chunk in enumerate(chunks)
141
+ }
142
+ for fut in futures:
143
+ i = futures[fut]
144
+ chunk_results[i] = fut.result()
145
  except Exception as exc:
146
  return (
147
  gr.update(value=f"์—๋Ÿฌ: {exc}"),
 
151
  gr.update(value=""),
152
  )
153
 
154
+ elapsed = time.time() - start
155
+ out_text = "\n\n".join(r.get("output", "") for r in chunk_results)
156
 
157
  article_id = db.save_article(text)
158
  run_id = db.save_pipeline_run(