Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -96,7 +96,7 @@ def json_to_lesson_plan_markdown(data: Dict[str, Any]) -> str:
|
|
| 96 |
|
| 97 |
# 全域變數:模型與 API URL
|
| 98 |
LLM_MODEL = "gemini-2.5-flash-preview-09-2025"
|
| 99 |
-
API_URL = f"https://
|
| 100 |
|
| 101 |
def call_gemini_api(payload: Dict[str, Any]) -> str:
|
| 102 |
"""
|
|
@@ -108,7 +108,7 @@ def call_gemini_api(payload: Dict[str, Any]) -> str:
|
|
| 108 |
return json.dumps({
|
| 109 |
"error": "找不到 GEMINI_API_KEY",
|
| 110 |
"message": "請設定 GEMINI_API_KEY 環境變數才能使用 API。"
|
| 111 |
-
})
|
| 112 |
|
| 113 |
# 2. 配置請求標頭和參數
|
| 114 |
headers = {"Content-Type": "application/json"}
|
|
@@ -129,18 +129,20 @@ def call_gemini_api(payload: Dict[str, Any]) -> str:
|
|
| 129 |
return json.dumps({
|
| 130 |
"error": f"API 錯誤: {response.status_code}",
|
| 131 |
"message": response.text
|
| 132 |
-
})
|
| 133 |
|
| 134 |
except requests.exceptions.RequestException as e:
|
| 135 |
return json.dumps({
|
| 136 |
"error": "連線錯誤",
|
| 137 |
"message": str(e)
|
| 138 |
-
})
|
| 139 |
except (KeyError, IndexError, TypeError) as e:
|
|
|
|
|
|
|
| 140 |
return json.dumps({
|
| 141 |
"error": "未預期的 API 回應",
|
| 142 |
-
"message": f"無法解析回應: {e}. 收到的回應: {
|
| 143 |
-
})
|
| 144 |
|
| 145 |
# --- 模組 A: 行政 Copilot 生成器 (Gradio 封裝) ---
|
| 146 |
# (已修改為使用 call_gemini_api)
|
|
@@ -239,7 +241,7 @@ def lesson_plan_designer(grade: str, subject: str, topic: str, hours: float, met
|
|
| 239 |
}
|
| 240 |
|
| 241 |
user_query = (
|
| 242 |
-
f"請根據以下資訊設計一個單元教案、評量規
|
| 243 |
f"年級/學科/單元主題: {grade}/{subject}/{topic}\n"
|
| 244 |
f"課時數: {int(hours)} 節\n"
|
| 245 |
f"教學法偏好: {method}\n"
|
|
@@ -256,7 +258,7 @@ def lesson_plan_designer(grade: str, subject: str, topic: str, hours: float, met
|
|
| 256 |
}
|
| 257 |
}
|
| 258 |
|
| 259 |
-
#
|
| 260 |
json_string = call_gemini_api(payload)
|
| 261 |
|
| 262 |
try:
|
|
|
|
| 96 |
|
| 97 |
# 全域變數:模型與 API URL
|
| 98 |
LLM_MODEL = "gemini-2.5-flash-preview-09-2025"
|
| 99 |
+
API_URL = f"https://generativelanguage.googleapis.com/v1beta/models/{LLM_MODEL}:generateContent"
|
| 100 |
|
| 101 |
def call_gemini_api(payload: Dict[str, Any]) -> str:
|
| 102 |
"""
|
|
|
|
| 108 |
return json.dumps({
|
| 109 |
"error": "找不到 GEMINI_API_KEY",
|
| 110 |
"message": "請設定 GEMINI_API_KEY 環境變數才能使用 API。"
|
| 111 |
+
}, ensure_ascii=False)
|
| 112 |
|
| 113 |
# 2. 配置請求標頭和參數
|
| 114 |
headers = {"Content-Type": "application/json"}
|
|
|
|
| 129 |
return json.dumps({
|
| 130 |
"error": f"API 錯誤: {response.status_code}",
|
| 131 |
"message": response.text
|
| 132 |
+
}, ensure_ascii=False)
|
| 133 |
|
| 134 |
except requests.exceptions.RequestException as e:
|
| 135 |
return json.dumps({
|
| 136 |
"error": "連線錯誤",
|
| 137 |
"message": str(e)
|
| 138 |
+
}, ensure_ascii=False)
|
| 139 |
except (KeyError, IndexError, TypeError) as e:
|
| 140 |
+
# 捕捉解析回應時的潛在錯誤
|
| 141 |
+
raw_response_text = response.text if 'response' in locals() else 'N/A'
|
| 142 |
return json.dumps({
|
| 143 |
"error": "未預期的 API 回應",
|
| 144 |
+
"message": f"無法解析回應: {e}. 收到的回應: {raw_response_text}"
|
| 145 |
+
}, ensure_ascii=False)
|
| 146 |
|
| 147 |
# --- 模組 A: 行政 Copilot 生成器 (Gradio 封裝) ---
|
| 148 |
# (已修改為使用 call_gemini_api)
|
|
|
|
| 241 |
}
|
| 242 |
|
| 243 |
user_query = (
|
| 244 |
+
f"請根據以下資訊設計一個單元教案、評量規數與差異化建議:\n"
|
| 245 |
f"年級/學科/單元主題: {grade}/{subject}/{topic}\n"
|
| 246 |
f"課時數: {int(hours)} 節\n"
|
| 247 |
f"教學法偏好: {method}\n"
|
|
|
|
| 258 |
}
|
| 259 |
}
|
| 260 |
|
| 261 |
+
# Llamar a la API real
|
| 262 |
json_string = call_gemini_api(payload)
|
| 263 |
|
| 264 |
try:
|