import gradio as gr from transformers import AutoTokenizer, AutoModelForSeq2SeqLM # 加载模型(无Java依赖) tokenizer = AutoTokenizer.from_pretrained("prithivida/grammar_error_correcter_v1") model = AutoModelForSeq2SeqLM.from_pretrained("prithivida/grammar_error_correcter_v1") def correct_grammar(text): if not text.strip(): return "请输入英文文本。" inputs = tokenizer.encode(text, return_tensors="pt") outputs = model.generate(inputs, max_length=256) corrected = tokenizer.decode(outputs[0], skip_special_tokens=True) return corrected demo = gr.Interface( fn=correct_grammar, inputs=gr.Textbox(lines=5, placeholder="Enter English text..."), outputs=gr.Textbox(label="Corrected Text"), title="English Grammar Corrector", description="Automatically correct English grammar and spelling using a transformer model." ) if __name__ == "__main__": demo.launch()