import gradio as gr from transformers import pipeline # 使用预训练的情感分析模型充当“作文打分器” scorer = pipeline("text-classification", model="distilbert-base-uncased-finetuned-sst-2-english") def grade_essay(essay): if not essay.strip(): return "⚠️ Please enter an essay text." # 模型输出情感预测结果 result = scorer(essay[:512])[0] # 限制长度以防过长 score = result['score'] label = result['label'] # 模拟作文评分逻辑 if label == "POSITIVE": final_score = round(60 + 40 * score, 2) # 高分作文 else: final_score = round(40 * (1 - score), 2) # 低分作文 feedback = f"📊 Sentiment: {label}\n🧾 Estimated Essay Score: {final_score}/100" return feedback # 构建 Gradio 前端 demo = gr.Interface( fn=grade_essay, inputs=gr.Textbox(lines=10, placeholder="Paste your essay here..."), outputs="text", title="AI Essay Grader", description="A simple demo using Hugging Face Transformers to auto-grade essays (simulated).", ) if __name__ == "__main__": demo.launch()