Spaces:
Sleeping
Sleeping
File size: 4,055 Bytes
c0b1b71 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 | """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
@dataclass
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"
@app.post("/humanize")
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),
}
@app.get("/methods")
def api_methods():
return {"methods": list(Humanizer.METHODS.keys())}
@app.get("/health")
def api_health():
return {"status": "ok"}
# --- CLI ---
@click.command()
@click.option("--input", "input_text", required=True, help="Input text or path to text file")
@click.option("--method", default=None, help="Humanization method")
@click.option("--output", default=None, help="Output file path")
@click.option("--config", default="config/config.toml", help="Config file path")
@click.option("--tier", default="standard", help="Processing tier")
@click.option("--language", default="en", help="Target language code")
@click.option("--serve", is_flag=True, help="Start API server instead of CLI processing")
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()
|