File size: 1,284 Bytes
ea99abb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from typing import Optional
from llms import LLM

def grammar_checking(text: str, model: str, custom_instructions: Optional[str] = None, use_llm: bool = True) -> str:
    """Grammar and spelling correction using LLM or traditional method"""
    if not text or not text.strip():
        return "Please enter input text."
    if use_llm:
        return _grammar_checking_with_llm(text, model, custom_instructions)
    else:
        return _grammar_checking_with_traditional(text, model)

def _grammar_checking_with_llm(text: str, model: str, custom_instructions: Optional[str]) -> str:
    try:
        llm = LLM(model=model)
        prompt = (
            (custom_instructions + "\n") if custom_instructions else ""
        ) + f"Check and correct grammar and spelling for the following text.\nText: {text}\nCorrected:"
        result = llm.generate(prompt)
        return result.strip()
    except Exception as e:
        print(f"Error in LLM grammar checking: {str(e)}")
        return "Oops! Something went wrong. Please try again later."

def _grammar_checking_with_traditional(text: str, model: str) -> str:
    # Placeholder for traditional grammar checking (could use LanguageTool or similar)
    return "[Traditional grammar checking is not implemented. Please use LLM mode.]"