ในยุคที่การศึกษาออนไลน์เติบโตอย่างรวดเร็ว ครูและอาจารย์ต้องเผชิญกับภาระงานจำนวนมากในการตรวจการบ้าน บทความนี้จะพาคุณสร้าง AI Homework Grading System ที่สามารถตรวจทั้งข้อความ รูปภาพ และเสียงพูดได้ในคราวเดียว โดยใช้ HolySheep AI เป็นแกนหลัก
ทำไมต้องเลือก HolySheep สำหรับระบบตรวจการบ้าน
การสร้างระบบตรวจการบ้านด้วย API อย่างเป็นทางการมีค่าใช้จ่ายสูงมาก แต่ HolySheep AI เสนอราคาที่ประหยัดกว่า 85% พร้อมความเร็วตอบสนองต่ำกว่า 50 มิลลิวินาที และรองรับการชำระเงินผ่าน WeChat และ Alipay
ตารางเปรียบเทียบบริการ AI API สำหรับระบบตรวจการบ้าน
| เกณฑ์เปรียบเทียบ | HolySheep AI | API อย่างเป็นทางการ | บริการรีเลย์อื่นๆ |
|---|---|---|---|
| ราคา GPT-4.1 (ต่อล้าน Token) | $8.00 | $15.00 | $10.00 - $12.00 |
| ราคา Claude Sonnet 4.5 (ต่อล้าน Token) | $15.00 | $45.00 | $25.00 - $35.00 |
| ราคา Gemini 2.5 Flash (ต่อล้าน Token) | $2.50 | $7.50 | $5.00 - $6.00 |
| ราคา DeepSeek V3.2 (ต่อล้าน Token) | $0.42 | ไม่มีบริการ | $0.50 - $1.00 |
| ความเร็วตอบสนอง | <50 มิลลิวินาที | 100-300 มิลลิวินาที | 80-200 มิลลิวินาที |
| วิธีการชำระเงิน | WeChat, Alipay, บัตรเครดิต | บัตรเครดิตเท่านั้น | บัตรเครดิต, PayPal |
| เครดิตฟรีเมื่อลงทะเบียน | ✓ มี | ✗ ไม่มี | ขึ้นอยู่กับผู้ให้บริการ |
| รองรับ Multi-modal | ✓ รองรับเต็มรูปแบบ | ✓ รองรับ | จำกัด |
สถาปัตยกรรมระบบตรวจการบ้านแบบ Multi-modal
ระบบตรวจการบ้านอัตโนมัติที่ดีต้องสามารถจัดการกับข้อมูลหลายรูปแบบพร้อมกัน ทั้งข้อความที่พิมพ์ รูปภาพของงานเขียนมือ สูตรคณิตศาสตร์ และเสียงพูดของนักเรียนที่อธิบายคำตอบ
การติดตั้งและตั้งค่า
pip install requests python-dotenv Pillow openai
import os
import base64
import json
from pathlib import Path
from openai import OpenAI
ตั้งค่า HolySheep AI
client = OpenAI(
api_key="YOUR_HOLYSHEEP_API_KEY",
base_url="https://api.holysheep.ai/v1"
)
การสร้างระบบตรวจการบ้านแบบ Multi-modal
def encode_image_to_base64(image_path):
"""แปลงรูปภาพเป็น base64 สำหรับส่งไปยัง API"""
with open(image_path, "rb") as image_file:
return base64.b64encode(image_file.read()).decode("utf-8")
def grade_homework_with_ai(image_path, student_answer, subject="general"):
"""
ตรวจการบ้านโดยใช้ AI วิเคราะห์รูปภาพและข้อความ
Args:
image_path: ที่อยู่ไฟล์รูปภาพงานนักเรียน
student_answer: คำตอบที่นักเรียนตอบในรูปแบบข้อความ
subject: วิชาที่ต้องการตรวจ (math, science, language, general)
"""
# แปลงรูปภาพเป็น base64
image_base64 = encode_image_to_base64(image_path)
# กำหนด rubric สำหรับการตรวจตามวิชา
rubric_prompts = {
"math": "ตรวจคำตอบทางคณิตศาสตร์ พร้อมระบุขั้นตอนที่ถูกต้อง",
"science": "ตรวจคำตอบวิทยาศาสตร์ รวมถึงการอธิบายหลักการ",
"language": "ตรวจคำผิด ไวยากรณ์ และความเหมาะสมของเนื้อหา",
"general": "ตรวจความถูกต้องทั่วไปและให้คำแนะนำ"
}
system_prompt = f"""คุณเป็นครูผู้เชี่ยวชาญในการตรวจการบ้าน
{rubric_prompts.get(subject, rubric_prompts['general'])}
กรุณาตรวจและให้ผลลัพธ์ในรูปแบบ JSON:
{{
"score": คะแนน (0-100),
"correct_answers": ["คำตอบที่ถูกต้อง"],
"wrong_answers": ["คำตอบที่ผิดพร้อมเฉลย"],
"feedback": "คำแนะนำสำหรับนักเรียน",
"areas_to_improve": ["จุดที่ควรปรับปรุง"]
}}"""
try:
response = client.chat.completions.create(
model="gpt-4o",
messages=[
{
"role": "system",
"content": system_prompt
},
{
"role": "user",
"content": [
{
"type": "text",
"text": f"งานนักเรียน:\n{student_answer}"
},
{
"type": "image_url",
"image_url": {
"url": f"data:image/jpeg;base64,{image_base64}"
}
}
]
}
],
max_tokens=2000,
temperature=0.3
)
result_text = response.choices[0].message.content
# ตัดข้อความ JSON จาก response
if "```json" in result_text:
result_text = result_text.split("``json")[1].split("``")[0]
elif "```" in result_text:
result_text = result_text.split("``")[1].split("``")[0]
return json.loads(result_text.strip())
except Exception as e:
return {"error": str(e), "score": 0}
ตัวอย่างการใช้งาน
result = grade_homework_with_ai(
image_path="student_homework.jpg",
student_answer="1. 2x + 3 = 7, x = 2\n2. พื้นที่สามเหลี่ยม = 1/2 × 4 × 3 = 6",
subject="math"
)
print(f"คะแนน: {result['score']}")
print(f"คำตอบที่ถูกต้อง: {result.get('correct_answers', [])}")
ระบบตรวจเสียงพูดอธิบายคำตอบ
import base64
import requests
def transcribe_and_grade_audio(audio_file_path, correct_answer, question):
"""
ตรวจการบ้านจากเสียงพูดของนักเรียน
Args:
audio_file_path: ที่อยู่ไฟล์เสียง
correct_answer: คำตอบที่ถูกต้อง
question: คำถามที่ต้องตอบ
"""
# อัปโหลดไฟล์เสียงไปยัง API
with open(audio_file_path, "rb") as audio_file:
audio_base64 = base64.b64encode(audio_file.read()).decode("utf-8")
# สร้าง prompt สำหรับการวิเคราะห์เสียง
system_prompt = f"""คุณเป็นครูที่กำลังฟังนักเรียนอธิบายคำตอบ
คำถาม: {question}
คำตอบที่ถูกต้อง: {correct_answer}
วิเคราะห์เสียงพูดและให้ผลลัพธ์ในรูปแบบ JSON:
{{
"transcription": "ข้อความที่พูด",
"score": คะแนน (0-100),
"explanation_quality": "คุณภาพการอธิบาย (ดี/พอใช้/ต้องปรับปรุง)",
"feedback": "คำแนะนำสำหรับนักเรียน",
"is_correct": true/false
}}"""
try:
response = client.chat.completions.create(
model="gpt-4o-audio-preview",
modalities=["text", "audio"],
audio={"voice": "alloy", "format": "wav"},
messages=[
{
"role": "system",
"content": system_prompt
},
{
"role": "user",
"content": [
{
"type": "text",
"text": "กรุณาวิเคราะห์เสียงพูดนี้:"
},
{
"type": "input_audio",
"input_audio": {
"data": audio_base64,
"format": "wav"
}
}
]
}
],
max_tokens=1500
)
result_text = response.choices[0].message.content
if "```json" in result_text:
result_text = result_text.split("``json")[1].split("``")[0]
return json.loads(result_text.strip())
except Exception as e:
return {"error": str(e), "score": 0, "transcription": ""}
ตัวอย่างการใช้งาน
result = transcribe_and_grade_audio(
audio_file_path="student_explanation.wav",
correct_answer="สูตรพื้นที่วงกลม = πr²",
question="จงหาพื้นที่วงกลมที่มีรัศมี 5 ซม."
)
print(f"คะแนน: {result['score']}")
print(f"สิ่งที่พูด: {result['transcription']}")
การสร้าง Batch Processing สำหรับตรวจงานหลายคน
from concurrent.futures import ThreadPoolExecutor
import time
def batch_grade_homework(homework_list, max_workers=5):
"""
ตรวจการบ้านหลายคนพร้อมกัน
Args:
homework_list: รายการ dict ที่มี image_path, student_answer, student_id
max_workers: จำนวนงานที่ทำพร้อมกัน
Returns:
dict: ผลลัพธ์การตรวจทั้งหมด
"""
results = {}
def process_single_homework(homework):
student_id = homework.get("student_id", "unknown")
try:
result = grade_homework_with_ai(
image_path=homework["image_path"],
student_answer=homework["student_answer"],
subject=homework.get("subject", "general")
)
return student_id, result
except Exception as e:
return student_id, {"error": str(e), "score": 0}
start_time = time.time()
with ThreadPoolExecutor(max_workers=max_workers) as executor:
futures = [executor.submit(process_single_homework, hw)
for hw in homework_list]
for future in futures:
student_id, result = future.result()
results[student_id] = result
elapsed = time.time() - start_time
return {
"results": results,
"summary": {
"total_students": len(results),
"average_score": sum(r.get("score", 0) for r in results.values()) / len(results),
"processing_time": f"{elapsed:.2f} วินาที"
}
}
ตัวอย่างการใช้งาน
homework_batch = [
{
"student_id": "ST001",
"image_path": "hw_st001.jpg",
"student_answer": "คำตอบของนักเรียนคนที่ 1",
"subject":