File size: 2,535 Bytes
21e66bc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
137
138
139
140
141
142
143
144
145
146
147
148
149
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel
from transformers import pipeline
import re

app = FastAPI(
    title="AI Text Cleaner API",
    version="1.0"
)

# CORS
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

# Load model
pipe = pipeline(
    "text-generation",
    model="HuggingFaceTB/SmolLM2-1.7B-Instruct",
    device_map="cpu"
)

# Request Model
class TextRequest(BaseModel):
    text: str
    mode: str = "clean"

# Fast local cleanup before AI
def basic_cleanup(text):
    
    # remove extra spaces
    text = re.sub(r'\s+', ' ', text)

    # remove repeated empty lines
    text = re.sub(r'\n+', '\n', text)

    # trim
    text = text.strip()

    return text

# Prompt templates
PROMPTS = {

    "clean": """
You are an AI text cleaning assistant.

Tasks:
- Remove messy formatting
- Fix weird spacing
- Improve readability
- Keep original meaning
- Return only cleaned text

Text:
{text}
""",

    "ocr": """
You are an OCR text repair assistant.

Tasks:
- Fix OCR mistakes
- Merge broken words
- Remove strange symbols
- Fix broken lines
- Return only repaired text

Text:
{text}
""",

    "format": """
You are a text formatting assistant.

Tasks:
- Format text properly
- Improve structure
- Fix capitalization
- Make text readable
- Return only formatted text

Text:
{text}
""",

    "prompt": """
You are an AI prompt optimizer.

Tasks:
- Clean the prompt
- Improve clarity
- Keep original intent
- Make prompt concise
- Return only optimized prompt

Prompt:
{text}
"""
}

@app.get("/")
async def root():
    return {
        "status": "running",
        "service": "AI Text Cleaner API"
    }

@app.post("/process")
async def process_text(req: TextRequest):

    # input limit
    if len(req.text) > 3000:
        return {
            "error": "Text too long"
        }

    # local cleanup
    cleaned_input = basic_cleanup(req.text)

    # select prompt
    prompt_template = PROMPTS.get(req.mode, PROMPTS["clean"])

    prompt = prompt_template.format(text=cleaned_input)

    # generate
    result = pipe(
        prompt,
        max_new_tokens=180,
        temperature=0.2,
        do_sample=False
    )

    output = result[0]["generated_text"]

    # remove prompt echo
    if prompt in output:
        output = output.replace(prompt, "").strip()

    return {
        "success": True,
        "mode": req.mode,
        "result": output
    }