Spaces:
Sleeping
Sleeping
while attempt < max_attempts:
Browse files
app.py
CHANGED
@@ -422,26 +422,32 @@ def generate_paragraph_evaluate(model, sys_content, paragraph, user_generate_par
|
|
422 |
"response_format": response_format
|
423 |
}
|
424 |
|
425 |
-
|
426 |
-
|
427 |
-
|
428 |
-
|
429 |
-
|
430 |
-
|
431 |
-
|
432 |
-
|
433 |
-
|
434 |
-
|
435 |
-
|
436 |
-
|
437 |
-
|
438 |
-
|
439 |
-
|
440 |
-
|
441 |
-
|
442 |
-
|
443 |
-
|
444 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
445 |
|
446 |
return gr_update
|
447 |
|
@@ -467,23 +473,29 @@ def generate_correct_grammatical_spelling_errors(model, sys_content, eng_level,
|
|
467 |
"response_format": response_format
|
468 |
}
|
469 |
|
470 |
-
|
471 |
-
|
472 |
-
|
473 |
-
|
474 |
-
|
475 |
-
|
476 |
-
|
477 |
-
|
478 |
-
|
479 |
-
|
480 |
-
|
481 |
-
|
482 |
-
|
483 |
-
|
484 |
-
|
485 |
-
|
486 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
487 |
|
488 |
return corrections_list_gr_update, reverse_paragraph_gr_update
|
489 |
|
@@ -537,21 +549,27 @@ def generate_refine_paragraph(model, sys_content, eng_level, paragraph, user_ref
|
|
537 |
"response_format": response_format
|
538 |
}
|
539 |
|
540 |
-
|
541 |
-
|
542 |
-
|
543 |
-
|
544 |
-
|
545 |
-
|
546 |
-
|
547 |
-
|
548 |
-
|
549 |
-
|
550 |
-
|
551 |
-
|
552 |
-
|
553 |
-
|
554 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
555 |
|
556 |
return refine_paragraph_gr_update, revised_paragraph_gr_update
|
557 |
|
|
|
422 |
"response_format": response_format
|
423 |
}
|
424 |
|
425 |
+
max_attempts = 2
|
426 |
+
attempt = 0
|
427 |
+
while attempt < max_attempts:
|
428 |
+
try:
|
429 |
+
response = OPEN_AI_CLIENT.chat.completions.create(**request_payload)
|
430 |
+
content = response.choices[0].message.content
|
431 |
+
|
432 |
+
print(f"====generate_paragraph_evaluate====")
|
433 |
+
print(content)
|
434 |
+
|
435 |
+
data = json.loads(content)
|
436 |
+
table_data = [
|
437 |
+
["學測架構|內容(Content)", data['content']['level'], data['content']['explanation']],
|
438 |
+
["學測架構|組織(Organization)", data['organization']['level'], data['organization']['explanation']],
|
439 |
+
["學測架構|文法、句構(Grammar/Sentence Structure)", data['grammar_and_usage']['level'], data['grammar_and_usage']['explanation']],
|
440 |
+
["學測架構|字彙、拼字(Vocabulary/Spelling)", data['vocabulary']['level'], data['vocabulary']['explanation']],
|
441 |
+
["JUTOR 架構|連貫性和連接詞(Coherence and Cohesion)", data['coherence_and_cohesion']['level'], data['coherence_and_cohesion']['explanation']]
|
442 |
+
]
|
443 |
+
headers = ["架構", "評分", "解釋"]
|
444 |
+
gr_update = gr.update(value=table_data, headers=headers, visible=True)
|
445 |
+
break
|
446 |
+
except Exception as e:
|
447 |
+
print(f"An error occurred while generating paragraph evaluate: {e}")
|
448 |
+
attempt += 1
|
449 |
+
if attempt == max_attempts:
|
450 |
+
raise gr.Error("網路塞車,請重新嘗試一次!")
|
451 |
|
452 |
return gr_update
|
453 |
|
|
|
473 |
"response_format": response_format
|
474 |
}
|
475 |
|
476 |
+
max_attempts = 2
|
477 |
+
attempt = 0
|
478 |
+
while attempt < max_attempts:
|
479 |
+
try:
|
480 |
+
response = OPEN_AI_CLIENT.chat.completions.create(**request_payload)
|
481 |
+
content = response.choices[0].message.content
|
482 |
+
data = json.loads(content)
|
483 |
+
print(f"data: {data}")
|
484 |
+
|
485 |
+
corrections_list = [
|
486 |
+
[item['original'], item['correction'], item['explanation']]
|
487 |
+
for item in data['Corrections and Explanations']
|
488 |
+
]
|
489 |
+
headers = ["原文", "建議", "解釋"]
|
490 |
+
|
491 |
+
corrections_list_gr_update = gr.update(value=corrections_list, headers=headers, wrap=True, visible=True)
|
492 |
+
reverse_paragraph_gr_update = gr.update(value=data["Revised Paragraph"], visible=False)
|
493 |
+
break
|
494 |
+
except Exception as e:
|
495 |
+
print(f"An error occurred while generating correct grammatical spelling errors: {e}")
|
496 |
+
attempt += 1
|
497 |
+
if attempt == max_attempts:
|
498 |
+
raise gr.Error("網路塞車,請重新嘗試一次!")
|
499 |
|
500 |
return corrections_list_gr_update, reverse_paragraph_gr_update
|
501 |
|
|
|
549 |
"response_format": response_format
|
550 |
}
|
551 |
|
552 |
+
max_attempts = 2
|
553 |
+
attempt = 0
|
554 |
+
while attempt < max_attempts:
|
555 |
+
try:
|
556 |
+
response = OPEN_AI_CLIENT.chat.completions.create(**request_payload)
|
557 |
+
content = response.choices[0].message.content
|
558 |
+
data = json.loads(content)
|
559 |
+
headers = ["原文", "建議", "解釋"]
|
560 |
+
table_data = [
|
561 |
+
[item['origin'], item['suggestion'], item['explanation']]
|
562 |
+
for item in data['Suggestions and Explanations']
|
563 |
+
]
|
564 |
+
|
565 |
+
refine_paragraph_gr_update = gr.update(value=table_data, headers=headers, visible=True)
|
566 |
+
revised_paragraph_gr_update = gr.update(value=data["Revised Paragraph"],visible=False)
|
567 |
+
break
|
568 |
+
except Exception as e:
|
569 |
+
print(f"An error occurred while generating refine paragraph: {e}")
|
570 |
+
attempt += 1
|
571 |
+
if attempt == max_attempts:
|
572 |
+
raise gr.Error("網路塞車,請重新嘗試一次!")
|
573 |
|
574 |
return refine_paragraph_gr_update, revised_paragraph_gr_update
|
575 |
|