Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
|
| 4 |
+
# 使用预训练的情感分析模型充当“作文打分器”
|
| 5 |
+
scorer = pipeline("text-classification", model="distilbert-base-uncased-finetuned-sst-2-english")
|
| 6 |
+
|
| 7 |
+
def grade_essay(essay):
|
| 8 |
+
if not essay.strip():
|
| 9 |
+
return "⚠️ Please enter an essay text."
|
| 10 |
+
|
| 11 |
+
# 模型输出情感预测结果
|
| 12 |
+
result = scorer(essay[:512])[0] # 限制长度以防过长
|
| 13 |
+
score = result['score']
|
| 14 |
+
label = result['label']
|
| 15 |
+
|
| 16 |
+
# 模拟作文评分逻辑
|
| 17 |
+
if label == "POSITIVE":
|
| 18 |
+
final_score = round(60 + 40 * score, 2) # 高分作文
|
| 19 |
+
else:
|
| 20 |
+
final_score = round(40 * (1 - score), 2) # 低分作文
|
| 21 |
+
|
| 22 |
+
feedback = f"📊 Sentiment: {label}\n🧾 Estimated Essay Score: {final_score}/100"
|
| 23 |
+
return feedback
|
| 24 |
+
|
| 25 |
+
# 构建 Gradio 前端
|
| 26 |
+
demo = gr.Interface(
|
| 27 |
+
fn=grade_essay,
|
| 28 |
+
inputs=gr.Textbox(lines=10, placeholder="Paste your essay here..."),
|
| 29 |
+
outputs="text",
|
| 30 |
+
title="AI Essay Grader",
|
| 31 |
+
description="A simple demo using Hugging Face Transformers to auto-grade essays (simulated).",
|
| 32 |
+
)
|
| 33 |
+
|
| 34 |
+
if __name__ == "__main__":
|
| 35 |
+
demo.launch()
|