Spaces:
Sleeping
Sleeping
Download runner.py from dev-strender/proofread-demo: direct link, hf CLI and curl.
- Browser
- Download file 6.91 kB
-
https://huggingface.co/spaces/dev-strender/proofread-demo/resolve/main/runner.py
- Command line
-
hf download hf://spaces/dev-strender/proofread-demo/runner.py
-
curl -L -o runner.py https://huggingface.co/spaces/dev-strender/proofread-demo/resolve/main/runner.py
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 | |
| 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 | |