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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
}
|
| 39 |
.custom-scroll::-webkit-scrollbar-thumb {
|
| 40 |
background: #9ca3af;
|
|
|
|
| 1 |
+
import json
|
| 2 |
+
import time
|
| 3 |
+
from typing import Dict, Any
|
| 4 |
+
import gradio as gr
|
| 5 |
+
|
| 6 |
+
# --- Simulation Setup for LLM API ---
|
| 7 |
+
# This section simulates the core AI generation logic without requiring a live API key.
|
| 8 |
+
LLM_MODEL = "gemini-2.5-flash-preview-09-2025"
|
| 9 |
+
API_KEY = "" # API Key Placeholder
|
| 10 |
+
|
| 11 |
+
def simulate_gemini_api_call(payload: Dict[str, Any], fields: Dict[str, Any]) -> Dict[str, Any]:
|
| 12 |
+
"""
|
| 13 |
+
Simulates a structured response from the Gemini API based on the task type.
|
| 14 |
+
In a real application, this function would make a fetch call to the Gemini API.
|
| 15 |
+
"""
|
| 16 |
+
|
| 17 |
+
# Simulate API latency
|
| 18 |
+
time.sleep(1.0)
|
| 19 |
+
|
| 20 |
+
user_query = payload['contents'][0]['parts'][0]['text']
|
| 21 |
+
system_instruction = payload.get('systemInstruction', {}).get('parts', [{}])[0].get('text', 'No system instruction')
|
| 22 |
+
|
| 23 |
+
# Check system instruction to determine the output type (Admin or Teaching)
|
| 24 |
+
if "台灣中學學務處行政書記" in system_instruction:
|
| 25 |
+
# Simulate Admin Copilot (Meeting Minutes) output
|
| 26 |
+
mock_text_result = json.dumps({
|
| 27 |
+
"文件類型 (Document Type)": "學務處會議記錄 (Academic Affairs Meeting Minutes)",
|
| 28 |
+
"meeting_info": {
|
| 29 |
+
"date": fields.get('date', '2025-01-10'),
|
| 30 |
+
"location": fields.get('location', '學務處會議室'),
|
| 31 |
+
"topic": fields.get('topic', '模擬會議主題')
|
| 32 |
+
},
|
| 33 |
+
"attendees": ["校長", "學務主任", "衛生組長", "生輔組長"],
|
| 34 |
+
"key_points": [
|
| 35 |
+
"期末獎懲核定程序已完成,共核定 30 件。建議將名單呈報校長核閱。",
|
| 36 |
+
"新生訓練場地佈置進度達 80%,物資清單已交付總務處採購。",
|
| 37 |
+
f"重點輸入: {fields.get('key_input', 'N/A')}"
|
| 38 |
+
],
|
| 39 |
+
"resolutions": [
|
| 40 |
+
{"item": "發布正式期末獎懲公告。", "responsible": "教務處", "deadline": "2025-01-15"},
|
| 41 |
+
{"item": "新生訓練場地佈置於活動前一天完成驗收。", "responsible": "總務處", "deadline": "2025-08-20"}
|
| 42 |
+
],
|
| 43 |
+
"audit_note": "文件根據校內行政公文標準格式生成。"
|
| 44 |
+
}, ensure_ascii=False, indent=2)
|
| 45 |
+
|
| 46 |
+
elif "台灣國高中資深教師與課程設計師" in system_instruction:
|
| 47 |
+
# Simulate Teaching Designer (Lesson Plan & Rubric) output
|
| 48 |
+
mock_text_result = json.dumps({
|
| 49 |
+
"文件類型 (Document Type)": "單元教案與評量規準 (Lesson Plan & Rubric)",
|
| 50 |
+
"lesson_plan_title": f"【{fields.get('subject', 'N/A')}】探索 {fields.get('topic', 'N/A')} ({fields.get('hours', 0)} 課時)",
|
| 51 |
+
"grade_level": fields.get('grade', 'N/A'),
|
| 52 |
+
"curriculum_alignment": ["A2 邏輯推理與批判思辨", "B3 獨立思考與探究精神"],
|
| 53 |
+
"learning_objectives": ["學生能解釋核心概念 X。", "學生能應用方法 Y 進行分析。", "學生能製作報告Z進行表達。"],
|
| 54 |
+
"activities": [
|
| 55 |
+
{"time_min": 15, "stage": "引導", "method": "提問式教學", "description": "使用新聞案例引導核心概念。"},
|
| 56 |
+
{"time_min": 30, "stage": "活動一", "method": "合作學習", "description": "分組完成專題研究和實作練習。"},
|
| 57 |
+
],
|
| 58 |
+
"rubric": {
|
| 59 |
+
"title": "單元評量規準 (4 級 X 4 指標)",
|
| 60 |
+
"criteria": [
|
| 61 |
+
{"name": "概念理解", "A": "清晰精確地解釋所有核心概念。", "D": "只能回答簡單問題。"},
|
| 62 |
+
{"name": "協作能力", "A": "積極領導團隊完成任務。", "D": "未參與討論。"}
|
| 63 |
+
]
|
| 64 |
+
},
|
| 65 |
+
"differentiation_advice": f"根據班級特性 ({fields.get('class_needs', 'N/A')}),建議提供圖像化教材並進行分組輔導。"
|
| 66 |
+
}, ensure_ascii=False, indent=2)
|
| 67 |
+
|
| 68 |
+
else:
|
| 69 |
+
mock_text_result = json.dumps({"error": "Unknown or missing task instruction."})
|
| 70 |
+
|
| 71 |
+
|
| 72 |
+
# Return the simulated API response structure
|
| 73 |
+
return {
|
| 74 |
+
"candidates": [{
|
| 75 |
+
"content": {
|
| 76 |
+
"parts": [{ "text": mock_text_result }]
|
| 77 |
+
},
|
| 78 |
+
"groundingMetadata": {}
|
| 79 |
+
}]
|
| 80 |
+
}
|
| 81 |
+
|
| 82 |
+
# --- Module A: Admin Copilot Generator (Gradio Wrapper) ---
|
| 83 |
+
|
| 84 |
+
def admin_copilot_generator(template_id: str, topic: str, date: str, location: str, key_input: str) -> str:
|
| 85 |
+
"""
|
| 86 |
+
Handles the Admin Copilot UI inputs and calls the simulation.
|
| 87 |
+
"""
|
| 88 |
+
fields = {
|
| 89 |
+
"topic": topic,
|
| 90 |
+
"date": date,
|
| 91 |
+
"location": location,
|
| 92 |
+
"key_input": key_input
|
| 93 |
+
}
|
| 94 |
+
|
| 95 |
+
# System Prompt defined for the Admin Copilot
|
| 96 |
+
system_prompt = (
|
| 97 |
+
"角色:台灣中學學務處行政書記\n"
|
| 98 |
+
"輸出:JSON(會議資訊、出席、重點、決議、待辦、負責人、期限)\n"
|
| 99 |
+
"格式規範:用詞正式、避免口語、保留專有名詞\n"
|
| 100 |
+
"限制:所有決議必須有負責人和明確期限。"
|
| 101 |
+
)
|
| 102 |
+
|
| 103 |
+
# Response Schema is implicitly defined but would be included in a real API call.
|
| 104 |
+
# The Gradio JSON output will just display the resulting JSON string.
|
| 105 |
+
|
| 106 |
+
user_query = f"請生成一份會議記錄。主題: {topic}; 輸入重點(或逐字稿):{key_input}"
|
| 107 |
+
|
| 108 |
+
payload = {
|
| 109 |
+
"contents": [{ "parts": [{ "text": user_query }] }],
|
| 110 |
+
"systemInstruction": { "parts": [{ "text": system_prompt }] },
|
| 111 |
+
# Simplified generationConfig for simulation
|
| 112 |
+
"generationConfig": { "responseMimeType": "application/json" }
|
| 113 |
+
}
|
| 114 |
+
|
| 115 |
+
api_response = simulate_gemini_api_call(payload, fields)
|
| 116 |
+
|
| 117 |
+
try:
|
| 118 |
+
json_string = api_response['candidates'][0]['content']['parts'][0]['text']
|
| 119 |
+
# For Gradio, we return the JSON string directly
|
| 120 |
+
return json_string
|
| 121 |
+
except (KeyError, json.JSONDecodeError) as e:
|
| 122 |
+
return f"ERROR: Failed to parse LLM structured output. {e}"
|
| 123 |
+
|
| 124 |
+
# --- Module B: Teaching AI Designer (Gradio Wrapper) ---
|
| 125 |
+
|
| 126 |
+
def lesson_plan_designer(grade: str, subject: str, topic: str, hours: float, method: str, equipment: str, class_needs: str) -> str:
|
| 127 |
+
"""
|
| 128 |
+
Handles the Teaching Designer UI inputs and calls the simulation.
|
| 129 |
+
Note: hours is float because Gradio Slider output is float
|
| 130 |
+
"""
|
| 131 |
+
fields = {
|
| 132 |
+
"grade": grade,
|
| 133 |
+
"subject": subject,
|
| 134 |
+
"topic": topic,
|
| 135 |
+
"hours": int(hours), # Convert back to int for display consistency
|
| 136 |
+
"method": method,
|
| 137 |
+
"equipment": equipment,
|
| 138 |
+
"class_needs": class_needs
|
| 139 |
+
}
|
| 140 |
+
|
| 141 |
+
# System Prompt defined for the Teaching Designer
|
| 142 |
+
system_prompt = (
|
| 143 |
+
"角色:台灣國高中資深教師與課程設計師\n"
|
| 144 |
+
"輸出:JSON(教案標題、目標、課綱對齊、活動步驟、評量規準、差異化建議)\n"
|
| 145 |
+
"限制:活動分鏡以 15 分鐘粒度;至少 2 項形成性評量。\n"
|
| 146 |
+
"對齊:請將輸出中的 'curriculum_alignment' 欄位,對齊台灣課綱的關鍵能力/素養。"
|
| 147 |
+
)
|
| 148 |
+
|
| 149 |
+
user_query = (
|
| 150 |
+
f"請根據以下資訊設計一個單元教案、評量規準和差異化建議:\n"
|
| 151 |
+
f"年級/學科/單元主題: {grade}/{subject}/{topic}\n"
|
| 152 |
+
f"課時數: {int(hours)} 節\n"
|
| 153 |
+
f"教學法偏好: {method}\n"
|
| 154 |
+
f"可用設備: {equipment}\n"
|
| 155 |
+
f"班級特性: {class_needs}"
|
| 156 |
+
)
|
| 157 |
+
|
| 158 |
+
payload = {
|
| 159 |
+
"contents": [{ "parts": [{ "text": user_query }] }],
|
| 160 |
+
"systemInstruction": { "parts": [{ "text": system_prompt }] },
|
| 161 |
+
# Simplified generationConfig for simulation
|
| 162 |
+
"generationConfig": { "responseMimeType": "application/json" }
|
| 163 |
+
}
|
| 164 |
+
|
| 165 |
+
api_response = simulate_gemini_api_call(payload, fields)
|
| 166 |
+
|
| 167 |
+
try:
|
| 168 |
+
json_string = api_response['candidates'][0]['content']['parts'][0]['text']
|
| 169 |
+
return json_string
|
| 170 |
+
except (KeyError, json.JSONDecodeError) as e:
|
| 171 |
+
return f"ERROR: Failed to parse LLM structured output. {e}"
|
| 172 |
+
|
| 173 |
+
# --- Gradio Interface Definition ---
|
| 174 |
+
|
| 175 |
+
# Module A Interface (Admin Copilot)
|
| 176 |
+
admin_copilot_interface = gr.Interface(
|
| 177 |
+
fn=admin_copilot_generator,
|
| 178 |
+
inputs=[
|
| 179 |
+
gr.Textbox(label="模板 ID (Template ID - Fixed for MVP)", value="meeting_minutes_standard", interactive=False),
|
| 180 |
+
gr.Textbox(label="會議主題 (Meeting Topic)", value="學務處期末獎懲與新生訓練籌備會議"),
|
| 181 |
+
gr.Textbox(label="日期 (Date)", value="2025-01-10"),
|
| 182 |
+
gr.Textbox(label="地點 (Location)", value="學務處會議室"),
|
| 183 |
+
gr.Textbox(label="輸入重點/逐字稿 (Key Input/Transcript)", value="討論期末獎懲核定程序。新生訓練場地佈置、人員編組確認。", lines=5),
|
| 184 |
+
],
|
| 185 |
+
outputs=gr.JSON(label="AI 生成結構化 JSON (用於 DOCX 模板匯出)"),
|
| 186 |
+
title="行政 Copilot:會議記錄生成 (Admin Copilot: Meeting Minutes Generation)",
|
| 187 |
+
description="🎯 生成���式嚴謹的行政文件 JSON 結構,可直接用於 DOCX 模板套印。",
|
| 188 |
+
allow_flagging="never",
|
| 189 |
+
)
|
| 190 |
+
|
| 191 |
+
# Module B Interface (Teaching Designer)
|
| 192 |
+
lesson_plan_designer_interface = gr.Interface(
|
| 193 |
+
fn=lesson_plan_designer,
|
| 194 |
+
inputs=[
|
| 195 |
+
gr.Dropdown(label="年級 (Grade)", choices=["國中", "高中", "國小"], value="高中"),
|
| 196 |
+
gr.Textbox(label="學科 (Subject)", value="歷史"),
|
| 197 |
+
gr.Textbox(label="單元主題 (Unit Topic)", value="從茶葉看全球化:17-19世紀的貿易網絡"),
|
| 198 |
+
gr.Slider(label="課時數 (Number of Sessions)", minimum=1, maximum=10, step=1, value=4),
|
| 199 |
+
gr.Dropdown(label="教學法偏好 (Pedagogy Preference)", choices=["探究式、PBL", "翻轉教學", "合作學習", "講述法"], value="探究式、PBL"),
|
| 200 |
+
gr.Textbox(label="可用設備 (Available Equipment)", value="平板電腦、投影設備、網路"),
|
| 201 |
+
gr.Textbox(label="班級特性 (Class Characteristics)", value="班級組成多元,需考慮多樣化的史料呈現方式。"),
|
| 202 |
+
],
|
| 203 |
+
outputs=gr.JSON(label="AI 生成教案與評量規準 JSON (Lesson Plan & Rubric JSON)"),
|
| 204 |
+
title="教學 AI 設計器:教案與 Rubric 生成 (Teaching AI Designer: Lesson Plan & Rubric)",
|
| 205 |
+
description="📘 生成符合課綱精神的單元教案結構和評量規準 JSON。",
|
| 206 |
+
allow_flagging="never",
|
| 207 |
+
)
|
| 208 |
+
|
| 209 |
+
# Integrate the two modules into a Tabbed Interface
|
| 210 |
+
demo = gr.TabbedInterface(
|
| 211 |
+
[admin_copilot_interface, lesson_plan_designer_interface],
|
| 212 |
+
["模組 A: 行政 Copilot", "模組 B: 教學設計器"],
|
| 213 |
+
title="CampusAI Suite (台灣校園 AI 文書/教學 MVP 演示)",
|
| 214 |
+
theme=gr.themes.Soft()
|
| 215 |
+
)
|
| 216 |
+
|
| 217 |
+
# The Gradio app is launched by the environment, so we omit the if __name__ == "__main__": demo.launch()
|
| 218 |
+
# demo.launch() border-radius: 10px;
|
| 219 |
}
|
| 220 |
.custom-scroll::-webkit-scrollbar-thumb {
|
| 221 |
background: #9ca3af;
|