youngtsai commited on
Commit
a85c4be
·
1 Parent(s): d1d5723

while attempt < max_attempts:

Browse files
Files changed (1) hide show
  1. app.py +70 -52
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
- try:
426
- response = OPEN_AI_CLIENT.chat.completions.create(**request_payload)
427
- content = response.choices[0].message.content
428
-
429
- print(f"====generate_paragraph_evaluate====")
430
- print(content)
431
-
432
- data = json.loads(content)
433
- table_data = [
434
- ["學測架構|內容(Content)", data['content']['level'], data['content']['explanation']],
435
- ["學測架構|組織(Organization)", data['organization']['level'], data['organization']['explanation']],
436
- ["學測架構|文法、句構(Grammar/Sentence Structure)", data['grammar_and_usage']['level'], data['grammar_and_usage']['explanation']],
437
- ["學測架構|字彙、拼字(Vocabulary/Spelling)", data['vocabulary']['level'], data['vocabulary']['explanation']],
438
- ["JUTOR 架構|連貫性和連接詞(Coherence and Cohesion)", data['coherence_and_cohesion']['level'], data['coherence_and_cohesion']['explanation']]
439
- ]
440
- headers = ["架構", "評分", "解釋"]
441
- gr_update = gr.update(value=table_data, headers=headers, visible=True)
442
- except Exception as e:
443
- print(f"An error occurred while generating paragraph evaluation: {e}")
444
- raise gr.Error("網路塞車,請重新嘗試一次!")
 
 
 
 
 
 
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
- try:
471
- response = OPEN_AI_CLIENT.chat.completions.create(**request_payload)
472
- content = response.choices[0].message.content
473
- data = json.loads(content)
474
- print(f"data: {data}")
475
-
476
- corrections_list = [
477
- [item['original'], item['correction'], item['explanation']]
478
- for item in data['Corrections and Explanations']
479
- ]
480
- headers = ["原文", "建議", "解釋"]
481
-
482
- corrections_list_gr_update = gr.update(value=corrections_list, headers=headers, wrap=True, visible=True)
483
- reverse_paragraph_gr_update = gr.update(value=data["Revised Paragraph"], visible=False)
484
- except Exception as e:
485
- print(f"An error occurred while generating correct grammatical and spelling errors: {e}")
486
- raise gr.Error("網路塞車,請重新嘗試一次!")
 
 
 
 
 
 
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
- try:
541
- response = OPEN_AI_CLIENT.chat.completions.create(**request_payload)
542
- content = response.choices[0].message.content
543
- data = json.loads(content)
544
- headers = ["原文", "建議", "解釋"]
545
- table_data = [
546
- [item['origin'], item['suggestion'], item['explanation']]
547
- for item in data['Suggestions and Explanations']
548
- ]
549
-
550
- refine_paragraph_gr_update = gr.update(value=table_data, headers=headers, visible=True)
551
- revised_paragraph_gr_update = gr.update(value=data["Revised Paragraph"],visible=False)
552
- except Exception as e:
553
- print(f"An error occurred while generating refine paragraph: {e}")
554
- raise gr.Error("網路塞車,請重新嘗試一次!")
 
 
 
 
 
 
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