|
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: |
|
|
|
return "[Traditional grammar checking is not implemented. Please use LLM mode.]" |
|
|