Spaces:
Sleeping
Sleeping
Download src/methodologies/humanizer.py from thanhnv2323/humanize-text: direct link, hf CLI and curl.
- Browser
- Download file 4.06 kB
-
https://huggingface.co/spaces/thanhnv2323/humanize-text/resolve/main/src/methodologies/humanizer.py
- Command line
-
hf download hf://spaces/thanhnv2323/humanize-text/src/methodologies/humanizer.py
-
curl -L -o humanizer.py https://huggingface.co/spaces/thanhnv2323/humanize-text/resolve/main/src/methodologies/humanizer.py
4.06 kB
| """v1.0 multi-method dispatcher (reference). | |
| Original entry point that routes between the 4 methodologies. Kept for | |
| backwards compatibility and reference. New code should use the v1.5 | |
| Standard Pipeline directly: | |
| from src.standard import run_standard_pipeline | |
| """ | |
| import time | |
| from dataclasses import dataclass | |
| import click | |
| import toml | |
| import uvicorn | |
| from fastapi import FastAPI | |
| from pydantic import BaseModel | |
| from .translation_chain import TranslationChainProcessor | |
| from .llm_rewriter import LLMRewriteProcessor | |
| from .detection_pipeline import DetectionGuidedProcessor | |
| from .mixed_engine import MixedEngineProcessor | |
| class HumanizeResult: | |
| text: str | |
| method_used: str | |
| processing_time: float | |
| class Humanizer: | |
| METHODS = { | |
| "translation_chain": TranslationChainProcessor, | |
| "llm_rewrite": LLMRewriteProcessor, | |
| "detection_guided": DetectionGuidedProcessor, | |
| "mixed_engine": MixedEngineProcessor, | |
| } | |
| def __init__(self, config_path: str = "config/config.toml"): | |
| import os | |
| from pathlib import Path | |
| resolved = config_path | |
| if not os.path.exists(resolved): | |
| pkg_config = Path(__file__).resolve().parent.parent.parent / "config" / "config.toml" | |
| if pkg_config.exists(): | |
| resolved = str(pkg_config) | |
| with open(resolved, "r", encoding="utf-8") as f: | |
| self.config = toml.load(f) | |
| def process(self, text: str, method: str = None, **kwargs) -> HumanizeResult: | |
| method = method or self.config["general"]["default_method"] | |
| if method not in self.METHODS: | |
| raise ValueError(f"Unknown method: {method}. Available: {list(self.METHODS.keys())}") | |
| processor = self.METHODS[method](self.config) | |
| start = time.time() | |
| result_text = processor.process(text, **kwargs) | |
| elapsed = time.time() - start | |
| return HumanizeResult( | |
| text=result_text, | |
| method_used=method, | |
| processing_time=elapsed, | |
| ) | |
| # --- FastAPI app (used by Docker) --- | |
| app = FastAPI(title="AI-Humanizer API") | |
| class HumanizeRequest(BaseModel): | |
| text: str | |
| method: str = "translation_chain" | |
| language: str = "en" | |
| tier: str = "standard" | |
| def api_humanize(req: HumanizeRequest): | |
| import os | |
| config_path = os.environ.get("CONFIG_PATH", "config/config.toml") | |
| h = Humanizer(config_path=config_path) | |
| result = h.process(req.text, method=req.method, tier=req.tier) | |
| return { | |
| "result": result.text, | |
| "method": result.method_used, | |
| "processing_time_ms": int(result.processing_time * 1000), | |
| } | |
| def api_methods(): | |
| return {"methods": list(Humanizer.METHODS.keys())} | |
| def api_health(): | |
| return {"status": "ok"} | |
| # --- CLI --- | |
| def main(input_text, method, output, config, tier, language, serve): | |
| if serve: | |
| uvicorn.run(app, host="0.0.0.0", port=8000) | |
| return | |
| import os | |
| if os.path.isfile(input_text): | |
| with open(input_text, "r", encoding="utf-8") as f: | |
| input_text = f.read() | |
| h = Humanizer(config_path=config) | |
| result = h.process(input_text, method=method, tier=tier) | |
| if output: | |
| with open(output, "w", encoding="utf-8") as f: | |
| f.write(result.text) | |
| click.echo(f"Written to {output} ({result.processing_time:.1f}s)") | |
| else: | |
| click.echo(result.text) | |
| if __name__ == "__main__": | |
| main() | |