Spaces:
Sleeping
Sleeping
try operation
Browse files
app.py
CHANGED
@@ -14,6 +14,7 @@ import tempfile
|
|
14 |
import urllib.parse
|
15 |
import pandas as pd
|
16 |
import re
|
|
|
17 |
|
18 |
# From other files
|
19 |
from storage_service import GoogleCloudStorage
|
@@ -1241,84 +1242,101 @@ def get_chinese_conversation_thread_id(thread_id):
|
|
1241 |
thread_id = thread.id
|
1242 |
return thread_id
|
1243 |
|
1244 |
-
def
|
1245 |
-
|
1246 |
-
|
1247 |
-
|
1248 |
-
|
1249 |
-
|
1250 |
-
|
1251 |
-
|
1252 |
-
|
1253 |
-
content_list = content.split("```json")
|
1254 |
-
content_text = content_list[0]
|
1255 |
-
print(f"content_text: {content_text}")
|
1256 |
-
content_json = content_list[1].split("```")[0]
|
1257 |
-
print(f"content_json: {content_json}")
|
1258 |
|
1259 |
-
|
|
|
|
|
1260 |
headers = ["架構", "評分", "解釋"]
|
1261 |
-
|
1262 |
-
["主題與內容", data['主題與內容']['level'], data['主題與內容']['explanation']],
|
1263 |
-
["遣詞造句", data['遣詞造句']['level'], data['遣詞造句']['explanation']],
|
1264 |
-
]
|
1265 |
-
|
1266 |
-
# 段落結構
|
1267 |
-
paragraph_structure_content = generate_content_by_open_ai_assistant_structure_robot(paragraph, thread_id=None, model_name=model)
|
1268 |
-
print(f"paragraph_structure_content: {paragraph_structure_content}")
|
1269 |
-
|
1270 |
-
if "```json" not in paragraph_structure_content:
|
1271 |
-
raise gr.Error("網路塞車,或是內容有誤,請稍後重新嘗試!")
|
1272 |
-
|
1273 |
-
paragraph_structure_content_list = paragraph_structure_content.split("```json")
|
1274 |
-
paragraph_structure_content_text = paragraph_structure_content_list[0]
|
1275 |
-
paragraph_structure_content_json = paragraph_structure_content_list[1].split("```")[0]
|
1276 |
-
paragraph_structure_content_table = json.loads(paragraph_structure_content_json)["results"]["段落結構"]
|
1277 |
-
paragraph_structure_table_data = [
|
1278 |
-
["段落結構", paragraph_structure_content_table['level'], paragraph_structure_content_table['explanation']],
|
1279 |
-
]
|
1280 |
|
1281 |
-
#
|
1282 |
-
|
1283 |
-
|
1284 |
-
|
1285 |
-
|
1286 |
-
|
1287 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1288 |
|
1289 |
-
|
1290 |
-
|
1291 |
-
|
1292 |
-
|
1293 |
-
|
1294 |
-
|
1295 |
-
|
1296 |
-
|
1297 |
-
|
1298 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
1299 |
|
1300 |
-
#
|
1301 |
-
|
1302 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1303 |
|
1304 |
# 綜合評分
|
1305 |
-
|
1306 |
-
|
1307 |
-
|
1308 |
-
|
1309 |
-
|
1310 |
-
|
1311 |
-
|
1312 |
-
|
|
|
1313 |
|
1314 |
-
|
1315 |
-
|
1316 |
-
|
1317 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1318 |
|
1319 |
content_table = gr.update(value=table_data, headers=headers, visible=True)
|
1320 |
|
1321 |
-
return
|
1322 |
|
1323 |
def get_chinese_paragraph_1st_evaluate_content(
|
1324 |
thread_id,
|
|
|
14 |
import urllib.parse
|
15 |
import pandas as pd
|
16 |
import re
|
17 |
+
import time
|
18 |
|
19 |
# From other files
|
20 |
from storage_service import GoogleCloudStorage
|
|
|
1242 |
thread_id = thread.id
|
1243 |
return thread_id
|
1244 |
|
1245 |
+
def retry_operation(operation, max_attempts=3, delay=2):
|
1246 |
+
for attempt in range(max_attempts):
|
1247 |
+
try:
|
1248 |
+
return operation()
|
1249 |
+
except Exception as e:
|
1250 |
+
if attempt == max_attempts - 1:
|
1251 |
+
raise e
|
1252 |
+
time.sleep(delay)
|
|
|
|
|
|
|
|
|
|
|
|
|
1253 |
|
1254 |
+
def get_chinese_paragraph_evaluate_content(thread_id, model, user_content, paragraph):
|
1255 |
+
content_text = ""
|
1256 |
+
table_data = []
|
1257 |
headers = ["架構", "評分", "解釋"]
|
1258 |
+
error_messages = []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1259 |
|
1260 |
+
# 主題與內容、遣詞造句
|
1261 |
+
try:
|
1262 |
+
def content_operation():
|
1263 |
+
content = generate_content_by_open_ai_assistant(user_content, thread_id, model_name=model)
|
1264 |
+
if "```json" not in content:
|
1265 |
+
raise ValueError("回應格式不正確")
|
1266 |
+
return content
|
1267 |
+
|
1268 |
+
content = retry_operation(content_operation)
|
1269 |
+
content_list = content.split("```json")
|
1270 |
+
content_text = content_list[0]
|
1271 |
+
content_json = content_list[1].split("```")[0]
|
1272 |
+
data = json.loads(content_json)["results"]
|
1273 |
+
table_data.extend([
|
1274 |
+
["主題與內容", data['主題與內容']['level'], data['主題與內容']['explanation']],
|
1275 |
+
["遣詞造句", data['遣詞造句']['level'], data['遣詞造句']['explanation']],
|
1276 |
+
])
|
1277 |
+
except Exception as e:
|
1278 |
+
error_messages.append(f"主題與內容、遣詞造句評估出錯: {str(e)}")
|
1279 |
|
1280 |
+
# 段落結構
|
1281 |
+
try:
|
1282 |
+
def structure_operation():
|
1283 |
+
content = generate_content_by_open_ai_assistant_structure_robot(paragraph, thread_id=None, model_name=model)
|
1284 |
+
if "```json" not in content:
|
1285 |
+
raise ValueError("回應格式不正確")
|
1286 |
+
return content
|
1287 |
+
|
1288 |
+
paragraph_structure_content = retry_operation(structure_operation)
|
1289 |
+
structure_content_list = paragraph_structure_content.split("```json")
|
1290 |
+
content_text += "\n" + structure_content_list[0]
|
1291 |
+
structure_json = structure_content_list[1].split("```")[0]
|
1292 |
+
structure_data = json.loads(structure_json)["results"]["段落結構"]
|
1293 |
+
table_data.append(["段落結構", structure_data['level'], structure_data['explanation']])
|
1294 |
+
except Exception as e:
|
1295 |
+
error_messages.append(f"段落結構評估出錯: {str(e)}")
|
1296 |
|
1297 |
+
# 錯別字
|
1298 |
+
try:
|
1299 |
+
def spelling_operation():
|
1300 |
+
content = generate_content_by_open_ai_assistant_spelling_robot(paragraph, thread_id=None, model_name=model)
|
1301 |
+
if "```json" not in content:
|
1302 |
+
raise ValueError("回應格式不正確")
|
1303 |
+
return content
|
1304 |
+
|
1305 |
+
spelling_content = retry_operation(spelling_operation)
|
1306 |
+
spelling_content_list = spelling_content.split("```json")
|
1307 |
+
content_text += "\n" + spelling_content_list[0]
|
1308 |
+
spelling_json = spelling_content_list[1].split("```")[0]
|
1309 |
+
spelling_data = json.loads(spelling_json)["results"]["錯別字"]
|
1310 |
+
table_data.append(["錯別字", spelling_data['level'], spelling_data['explanation']])
|
1311 |
+
except Exception as e:
|
1312 |
+
error_messages.append(f"錯別字檢查出錯: {str(e)}")
|
1313 |
|
1314 |
# 綜合評分
|
1315 |
+
if len(table_data) == 4: # 確保所有評估都成功完成
|
1316 |
+
grade = assign_grade(
|
1317 |
+
table_data[0][1], # 主題與內容
|
1318 |
+
table_data[2][1], # 段落結構
|
1319 |
+
table_data[1][1], # 遣詞造句
|
1320 |
+
table_data[3][1] # 錯別字
|
1321 |
+
)
|
1322 |
+
grade_content_text = f"# 綜合評分:{grade}"
|
1323 |
+
content_text = grade_content_text + "\n" + content_text
|
1324 |
|
1325 |
+
# 綜合回饋
|
1326 |
+
feedback_match = re.search(r"綜合回饋(.*?)評分標準與回饋", content_text, re.DOTALL)
|
1327 |
+
feedback_text = feedback_match.group(1).strip() if feedback_match else "無法提取綜合回饋"
|
1328 |
+
table_data.append(["綜合評分", grade, feedback_text])
|
1329 |
+
else:
|
1330 |
+
error_messages.append("無法進行綜合評分,因為部分評估未完成")
|
1331 |
+
|
1332 |
+
# 處理錯誤信息
|
1333 |
+
if error_messages:
|
1334 |
+
error_content = "\n".join(error_messages)
|
1335 |
+
content_text = f"評估過程中遇到以下問題:\n{error_content}\n\n" + content_text
|
1336 |
|
1337 |
content_table = gr.update(value=table_data, headers=headers, visible=True)
|
1338 |
|
1339 |
+
return content_text, content_table
|
1340 |
|
1341 |
def get_chinese_paragraph_1st_evaluate_content(
|
1342 |
thread_id,
|