Spaces:
Sleeping

proofread-demo / runner.py
dev-strender's picture
feat: 3 example articles; neutral review-step wording
3634984 verified
Raw History Blame Contribute Delete
6.91 kB
"""๊ณ ๊ฐ ๋ฐ๋ชจ(HF Space) ๊ต์—ด ๊ตฌ์„ฑ์„ ํ…์ŠคํŠธ 1๊ฑด์— ์‹คํ–‰ํ•˜๋Š” ํ”„๋กœ๊ทธ๋ž˜๋งคํ‹ฑ ๋Ÿฌ๋„ˆ.
๊ต์—ด ๋กœ์ง์„ ์žฌ๊ตฌํ˜„ํ•˜์ง€ ์•Š๊ณ  solar-eval ์—”์ง„์„ ๊ทธ๋Œ€๋กœ import ํ•œ๋‹ค - ๋ฐ๋ชจ๊ฐ€ ๋ณด์—ฌ์ฃผ๋Š” ๋™์ž‘์ด ํ‰๊ฐ€ run ๊ณผ
๋ฐ”์ดํŠธ ๋‹จ์œ„๋กœ ๊ฐ™์•„์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค(์‚ฌ๋ณธ ๋กœ์ง์€ ๋ฐ˜๋“œ์‹œ ๊ฐˆ๋ผ์ง„๋‹ค). ๋กœ์ปฌ์—์„œ๋Š” uv ์›Œํฌ์ŠคํŽ˜์ด์Šค์˜ solar_eval ์„,
HF Space ์—์„œ๋Š” `build_space.py` ๊ฐ€ ๋ฒค๋”๋งํ•œ ์‚ฌ๋ณธ์„ ์“ด๋‹ค(sys.path ์ƒ ์Šคํฌ๋ฆฝํŠธ ์˜†์ด ๋จผ์ €๋ผ Space ์—์„œ๋Š” ๋ฒค๋”
์‚ฌ๋ณธ์ด ์ด๊ธด๋‹ค).
๊ตฌ์„ฑ์€ (pipeline, prompt, model) ํ•œ ๋ฌถ์Œ์ด๋‹ค. 2026-10-29 ๋ถ€ํ„ฐ FT ๊ต์—ด + Opus ๊ธฐ์‚ฌ ๊ฒ€ํ† 
(`pipeline_exp_261021_ft_hybrid`, ๊ทผ๊ฑฐ `03-evaluation/insights/261021_ft_hybrid_paragraph_article.md`)
ํ•˜๋‚˜๋งŒ ๋…ธ์ถœํ•œ๋‹ค. ๊ธฐ์‚ฌ ๊ฒ€ํ†  ์Šคํ…์€ `timely:` ๋ชจ๋ธ์ด๋ผ Timely ๋ผ์šฐํ„ฐ๋ฅผ ๊ฑฐ์นœ๋‹ค - `UPSTAGE_API_KEY` ์™€
`TIMELY_API_KEY` ๊ฐ€ ๋‘˜ ๋‹ค ์žˆ์–ด์•ผ ํ•œ๋‹ค.
config ์ž์‚ฐ(ํŒŒ์ดํ”„๋ผ์ธ yamlยทํ”„๋กฌํ”„ํŠธยท์น˜ํ™˜ ์‚ฌ์ „)์€
1. `./assets/` - HF Space ๋ ˆ์ด์•„์›ƒ (build_space.py ๊ฐ€ ์กฐ๋ฆฝ)
2. `../03-evaluation/` - ๋ ˆํฌ ๋ ˆ์ด์•„์›ƒ (๋กœ์ปฌ ๊ฐœ๋ฐœ)
์ˆœ์„œ๋กœ ์ฐพ๋Š”๋‹ค.
"""
from __future__ import annotations
import asyncio
import difflib
import queue
import threading
import time
from dataclasses import dataclass
from pathlib import Path
from typing import Any
import yaml
@dataclass(frozen=True)
class Config:
"""๋ฐ๋ชจ๊ฐ€ ๋…ธ์ถœํ•˜๋Š” ํ•œ ๊ตฌ์„ฑ. `assets` ๋Š” Space ๋ฒˆ๋“ค์— ๋“ค์–ด๊ฐˆ config ํŒŒ์ผ(03-evaluation ๊ธฐ์ค€ ์ƒ๋Œ€ ๊ฒฝ๋กœ)."""
label: str
pipeline: str
prompt: str
model: str
note: str
assets: tuple[str, ...]
CONFIG = Config(
label="FT ๊ต์—ด + ๊ธฐ์‚ฌ ์ „์ฒด ๊ฒ€ํ† ",
pipeline="pipeline_exp_261021_ft_hybrid",
prompt="prompt_exp_261021_ft_hybrid",
model="solar-pro4", # ๋ชจ๋ธ์„ ์ง€์ •ํ•˜์ง€ ์•Š์€ ์Šคํ…์šฉ ๊ธฐ๋ณธ๊ฐ’ - ์ด ๊ตฌ์„ฑ์˜ LLM ์Šคํ…์€ FTยทOpus ๋ฅผ ์ง์ ‘ ์ง€์ •ํ•œ๋‹ค
note=(
"FT ๊ต์—ด ๋ชจ๋ธ์ด ๋ฌธ๋‹จ ๋ฌถ์Œ๋งˆ๋‹ค 1์ฐจ ๊ต์—ด โ†’ ๊ธฐ์‚ฌ ์ „์ฒด๋ฅผ ํ•œ ๋ฒˆ ๊ฒ€ํ† ํ•ด FT ์˜ค๊ต์ •์„ ๋˜๋Œ๋ฆฌ๊ณ  "
"์‚ฌ์‹คยท์ˆซ์žยท๋ฌธ๋งฅ ์˜ค๋ฅ˜๋ฅผ ๊ณ ์นจ โ†’ ๋”ฐ์˜ดํ‘œยท์œจ/๋ฅ ยทํ›„์ฒ˜๋ฆฌ ๊ทœ์น™."
),
assets=("resources/vocabulary.csv",),
)
#: ํ•„์š”ํ•œ ํ‚ค. ๊ธฐ์‚ฌ ๊ฒ€ํ† (Opus)๊ฐ€ Timely ๋ฅผ ๊ฑฐ์นœ๋‹ค.
REQUIRED_KEYS = ("UPSTAGE_API_KEY", "TIMELY_API_KEY")
def find_config_dir() -> Path:
"""ํŒŒ์ดํ”„๋ผ์ธ yaml ์„ ์‹ค์ œ๋กœ ๊ฐ€์ง„ ์ž์‚ฐ ๋ฃจํŠธ. Space ๋ฒˆ๋“ค์ด ๋ ˆํฌ๋ณด๋‹ค ๋จผ์ €๋‹ค."""
here = Path(__file__).resolve().parent
for cand in (here / "assets", here.parent / "03-evaluation"):
if (cand / "pipelines" / f"{CONFIG.pipeline}.yaml").is_file():
return cand
raise FileNotFoundError(
f"config ์ž์‚ฐ์„ ์ฐพ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค - {here}/assets ๋˜๋Š” ../03-evaluation ์— "
f"pipelines/{CONFIG.pipeline}.yaml ์ด ์žˆ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค (build_space.py ์ฐธ์กฐ)."
)
def build_pipeline() -> Any:
"""ํŒŒ์ดํ”„๋ผ์ธ ์ธ์Šคํ„ด์Šค. ์•ฑ์—์„œ 1ํšŒ ๋งŒ๋“ค์–ด ์žฌ์‚ฌ์šฉํ•œ๋‹ค."""
import solar_eval.pipelines.steps # noqa: F401 - STEP_REGISTRY ๋“ฑ๋ก
from solar_eval.core.dataset_loader import DatasetLoader
from solar_eval.core.pipeline_compose import compose_pipeline
from solar_eval.models.prompt_version import load_step_prompts
from solar_eval.pipelines.registry import create_pipeline
config_dir = find_config_dir()
pipelines_dir = config_dir / "pipelines"
def load_base(name: str) -> dict[str, Any]:
return yaml.safe_load((pipelines_dir / f"{name}.yaml").read_text(encoding="utf-8"))
composed = compose_pipeline(load_base(CONFIG.pipeline), load_base=load_base)
prompts = load_step_prompts(config_dir / "prompts" / CONFIG.prompt)
return create_pipeline(
"multi_step",
input_fields=["original"],
pipeline_config=composed,
prompts=prompts,
dataset_loader=DatasetLoader(),
config_dir=config_dir,
)
def word_changes(before: str, after: str) -> list[tuple[str, str]]:
"""์–ด์ ˆ diff ๋กœ (๋ฐ”๋€Œ๊ธฐ ์ „, ๋ฐ”๋€ ํ›„) ์Œ. ๊ทœ์น™ยท์žฌ์ž‘์„ฑ ์Šคํ…์˜ ๋ณ€๊ฒฝ์„ ์นด๋“œ๋กœ ๋งŒ๋“ค ๋•Œ ์“ด๋‹ค."""
a, b = before.split(), after.split()
sm = difflib.SequenceMatcher(a=a, b=b, autojunk=False)
return [
(" ".join(a[i1:i2]), " ".join(b[j1:j2]))
for op, i1, i2, j1, j2 in sm.get_opcodes()
if op != "equal"
]
async def _run_async(pipeline: Any, text: str, events: queue.Queue) -> None:
from solar_eval.models.sample import EvalSample
from solar_eval.providers import INFERENCE_ROUTES, RoutedProvider
from solar_eval.providers.upstage import UpstageProvider
start = time.monotonic()
prev = {"text": text}
def hook(name: str, sample: Any, ctx: Any) -> None:
changes = word_changes(prev["text"], ctx.text)
prev["text"] = ctx.text
events.put(
{
"kind": "step",
"step": name,
"text": ctx.text,
"changes": changes, # ์ด ์Šคํ…์ด ๋ณธ๋ฌธ์— ์‹ค์ œ๋กœ ๋‚ธ ๋ณ€๊ฒฝ(์–ด์ ˆ ์Œ)
"corrections": dict(sample.artifacts.get("corrections") or {}),
"json": dict(
sample.artifacts.get("json") or {}
), # ํŒ์ • ์Šคํ… ๊ฒฐ๊ณผ - ๋˜๋Œ๋ฆผ ๋ฒˆํ˜ธยท์‚ฌ์œ 
"hunks": dict(sample.artifacts.get("hunks") or {}), # diff_hunks ๋ณ€๊ฒฝ ๋ชฉ๋ก
"elapsed_s": time.monotonic() - start,
}
)
try:
provider = RoutedProvider(UpstageProvider(), {k: c() for k, c in INFERENCE_ROUTES.items()})
# ํ‰๊ฐ€ run ๊ณผ ๊ฐ™์€ ์กฐ๊ฑด: temp 0.0, reasoning off
result = await pipeline.run(
EvalSample(input={"original": text}),
prompts="",
provider=provider,
model=CONFIG.model,
temperature=0.0,
max_tokens=8000,
reasoning_effort=None,
on_step=hook,
)
except Exception as e: # noqa: BLE001 - ๋ฐ๋ชจ ํ‘œ๋ฉด: ์›์ธ๋งŒ ํ™”๋ฉด์œผ๋กœ
events.put(
{
"kind": "error",
"error": f"{type(e).__name__}: {e}",
"elapsed_s": time.monotonic() - start,
}
)
return
events.put({"kind": "done", "output": result.output, "elapsed_s": time.monotonic() - start})
def start_proofread(pipeline: Any, text: str) -> queue.Queue:
"""ํ…์ŠคํŠธ 1๊ฑด ๊ต์—ด์„ ์Šค๋ ˆ๋“œ๋กœ ์‹œ์ž‘ํ•˜๊ณ  ์ด๋ฒคํŠธ ํ๋ฅผ ๋Œ๋ ค์ค€๋‹ค.
์ด๋ฒคํŠธ: {"kind": "step", step, text, changes, corrections, json, hunks, elapsed_s} ๋ฅผ ์Šคํ…๋งˆ๋‹ค,
๋๋‚˜๋ฉด {"kind": "done", output, elapsed_s} ๋˜๋Š” {"kind": "error", error, elapsed_s} ํ•˜๋‚˜.
"""
events: queue.Queue = queue.Queue()
threading.Thread(
target=lambda: asyncio.run(_run_async(pipeline, text, events)), daemon=True
).start()
return events