import gradio as gr
from transformers import AutoModelForSeq2SeqLM, AutoTokenizer

# Load your model and tokenizer
model_name = "akhmat-s/t5-base-grammar-corrector"  # or "akhmat-s/t5-large-quant-grammar-corrector"
model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
tokenizer = AutoTokenizer.from_pretrained(model_name)

def correct_grammar(text):
    inputs = tokenizer.encode("fix: " + text, return_tensors="pt", max_length=512, truncation=True)
    outputs = model.generate(inputs, max_length=512, num_beams=4, early_stopping=True)
    corrected_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
    return corrected_text

iface = gr.Interface(
    fn=correct_grammar,
    inputs=gr.Textbox(lines=5, label="Input Text"),
    outputs=gr.Textbox(label="Corrected Text"),
    title="Grammar Correction Chat",
    description="Enter text with grammatical errors, and the model will provide corrections."
)

if __name__ == "__main__":
    iface.launch()