File size: 2,129 Bytes
29969bf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import json
from transformers import pipeline

class EndpointHandler:
    def __init__(self, path=""):
        self.pipeline = pipeline("text-generation", model=path, device=0)

    def __call__(self, data):
        inputs = data.get("inputs", "")
        style_guide = data.get("style_guide", "Apply general AsciiDoc best practices.")
        max_tokens = data.get("max_tokens", 2048)

        system_prompt = f"""
You are an expert technical editor specializing in AsciiDoc document correction. Your task is to analyze the provided AsciiDoc text and suggest corrections based on the following style guide:
{style_guide}

**Output Requirements**:
- Return corrections **only** in valid JSON format, enclosed in curly braces: {{"corrections": [...]}}.
- Each correction must include:
  - "original_line": The exact line from the input text.
  - "corrected_line": The corrected version of the line.
  - "explanation": A brief reason for the correction.
- If no corrections are needed, return: {{"corrections": []}}.
- Ensure the JSON is complete, valid, and concise to avoid truncation.
- Do **not** include any text, comments, or explanations outside the JSON object.
- Do **not** include placeholder text like "<original AsciiDoc line>".
- Only correct lines with AsciiDoc syntax, style, or technical accuracy issues (e.g., missing punctuation, incorrect headers, malformed attributes like :gls_prefix:).

Analyze the following AsciiDoc lines and provide corrections in JSON format:
"""
        prompt = f"{system_prompt}\n{inputs}"

        try:
            response = self.pipeline(
                prompt,
                max_new_tokens=max_tokens,
                temperature=0.3,
                return_full_text=False
            )[0]["generated_text"].strip()
            json_start = response.find('{')
            json_end = response.rfind('}') + 1
            if json_start == -1 or json_end == -1:
                return {"corrections": []}
            correction_json = json.loads(response[json_start:json_end])
            return correction_json
        except Exception as e:
            return {"corrections": []}