youngtsai commited on
Commit
5c0db57
1 Parent(s): 8080a34
Files changed (1) hide show
  1. app.py +62 -41
app.py CHANGED
@@ -706,10 +706,12 @@ def split_data(df_string, word_base=100000):
706
 
707
  return segments
708
 
709
- def generate_content_by_open_ai(sys_content, user_content, response_format=None):
710
  print("LLM using OPEN AI")
711
- # model = "gpt-4-turbo"
712
- model = "gpt-4o"
 
 
713
  print(f"model: {model}")
714
 
715
  messages = [
@@ -753,7 +755,7 @@ def generate_content_by_open_ai(sys_content, user_content, response_format=None)
753
  # content = response_body.get('content')[0].get('text')
754
  # return content
755
 
756
- def generate_content_by_LLM(sys_content, user_content, response_format=None, LLM_model=None):
757
  # 使用 OpenAI 生成基于上传数据的问题
758
 
759
  # if LLM_model == "anthropic-claude-3-sonnet":
@@ -761,7 +763,8 @@ def generate_content_by_LLM(sys_content, user_content, response_format=None, LLM
761
  # content = generate_content_by_bedrock(sys_content, user_content)
762
  # else:
763
  print(f"LLM: {LLM_model}")
764
- content = generate_content_by_open_ai(sys_content, user_content, response_format)
 
765
 
766
  print("=====content=====")
767
  print(content)
@@ -815,22 +818,28 @@ def generate_reading_passage(df_string, LLM_model=None):
815
  print("===generate_reading_passage===")
816
  segments = split_data(df_string, word_base=100000)
817
  all_content = []
 
 
818
 
819
  for segment in segments:
820
  sys_content = "你是一個擅長資料分析跟影片教學的老師,user 為學生,請精讀資料文本,自行判斷資料的種類,使用 zh-TW"
821
  user_content = f"""
822
- 請根據 {segment}
823
- 文本自行判斷資料的種類
824
- 幫我組合成 Reading Passage
825
- 並潤稿讓文句通順
826
- 請一定要使用繁體中文 zh-TW,並用台灣人的口語
827
- 產生的結果不要前後文解釋,也不要敘述這篇文章怎麼產生的
828
- 只需要專注提供 Reading Passage,字數在 500 字以內
829
- 敘述中,請把數學或是專業術語,用 Latex 包覆($...$),並且不要去改原本的文章
830
- 加減乘除、根號、次方等等的運算式口語也換成 LATEX 數學符號
831
- 請直接給出文章,不用介紹怎麼處理的或是文章字數等等
832
- """
833
- content = generate_content_by_LLM(sys_content, user_content, response_format=None, LLM_model=LLM_model)
 
 
 
 
834
  all_content.append(content + "\n")
835
 
836
  # 將所有生成的閱讀理解段落合併成一個完整的文章
@@ -899,7 +908,7 @@ def generate_mind_map(df_string, LLM_model=None):
899
  注意:不需要前後文敘述,直接給出 markdown 文本即可
900
  這對我很重要
901
  """
902
- content = generate_content_by_LLM(sys_content, user_content, response_format=None, LLM_model=LLM_model)
903
  all_content.append(content + "\n")
904
 
905
  # 將所有生成的閱讀理解段落合併成一個完整的文章
@@ -1019,7 +1028,7 @@ def generate_summarise(df_string, metadata=None, LLM_model=None):
1019
  ## ❓ 延伸小問題
1020
  - (一個 bullet point....請圍繞「課程名稱」為學習重點,進行重點整理,不要整理跟情境故事相關的問題)
1021
  """
1022
- content = generate_content_by_LLM(sys_content, user_content, response_format=None, LLM_model=LLM_model)
1023
  all_content.append(content + "\n")
1024
 
1025
  if len(all_content) > 1:
@@ -1058,7 +1067,7 @@ def generate_summarise(df_string, metadata=None, LLM_model=None):
1058
  ## ❓ 延伸小問題
1059
  - ( {all_content_cnt} 個 bullet point....請圍繞「課程名稱」為學習重點,進行重點整理,不要整理跟情境故事相關的問題)
1060
  """
1061
- final_content = generate_content_by_LLM(sys_content, user_content, response_format=None, LLM_model=LLM_model)
1062
  else:
1063
  final_content = all_content[0]
1064
 
@@ -1140,7 +1149,7 @@ def generate_questions(df_string, LLM_model=None):
1140
  }}
1141
  """
1142
  response_format = { "type": "json_object" }
1143
- questions = generate_content_by_LLM(sys_content, user_content, response_format, LLM_model)
1144
  questions_list = json.loads(questions)["questions"]
1145
  print("=====json_response=====")
1146
  print(questions_list)
@@ -1200,7 +1209,7 @@ def generate_questions_answers(df_string, LLM_model=None):
1200
  }}
1201
  """
1202
  response_format = { "type": "json_object" }
1203
- content = generate_content_by_LLM(sys_content, user_content, response_format, LLM_model)
1204
  content_json = json.loads(content)["questions_answers"]
1205
  all_content += content_json
1206
 
@@ -1303,14 +1312,21 @@ def generate_key_moments(formatted_simple_transcript, formatted_transcript, LLM_
1303
  for segment in segments:
1304
  sys_content = "你是一個擅長資料分析跟影片教學的老師,user 為學生,請精讀資料文本,自行判斷資料的種類,使用 zh-TW"
1305
  user_content = f"""
1306
- 請根據 {segment} 文本,提取出重點摘要,並給出對應的時間軸
1307
- 1. 小範圍切出不同段落的相對應時間軸的重點摘要,
1308
- 2. 每一小段最多不超過 1/5 的總內容,也就是大約 3~5段的重點(例如五~十分鐘的影片就一段大約1~2分鐘,最多三分鐘,但如果是超過十分鐘的影片,那一小段大約 2~3分鐘,以此類推)
1309
- 3. 注意不要遺漏任何一段時間軸的內容 從零秒開始
1310
- 4. 如果頭尾的情節不是重點,特別是打招呼或是介紹人物、或是say goodbye 就是不重要的情節,就不用擷取
1311
- 5. 以這種方式分析整個文本,從零秒開始分析,直到結束。這很重要
1312
- 6. 關鍵字從transcript extract to keyword,保留專家名字、專業術語、年份、數字、期刊名稱、地名、數學公式
1313
- 7. text, keywords please use or transfer zh-TW, it's very important
 
 
 
 
 
 
 
1314
 
1315
  Example: retrun JSON
1316
  {{key_moments:[{{
@@ -1322,7 +1338,7 @@ def generate_key_moments(formatted_simple_transcript, formatted_transcript, LLM_
1322
  }}
1323
  """
1324
  response_format = { "type": "json_object" }
1325
- content = generate_content_by_LLM(sys_content, user_content, response_format, LLM_model)
1326
  key_moments = json.loads(content)["key_moments"]
1327
 
1328
  # "transcript": get text from formatted_simple_transcript
@@ -1370,7 +1386,7 @@ def generate_key_moments_keywords(transcript, LLM_model=None):
1370
  不用給上���文,直接給出關鍵字,使用 zh-TW,用逗號分隔, example: 關鍵字1, 關鍵字2
1371
  transcript:{segment}
1372
  """
1373
- content = generate_content_by_LLM(sys_content, user_content, response_format=None, LLM_model=LLM_model)
1374
  keywords = content.strip().split(",")
1375
  all_content += keywords
1376
 
@@ -1389,18 +1405,20 @@ def generate_key_moments_suggested_images(key_moment, LLM_model=None):
1389
  # Prepare the user prompt with text and keywords
1390
  sys_content = "你是一個擅長資料分析跟影片教學的老師,user 為學生,請精讀資料文本,自行判斷資料的種類,使用 zh-TW"
1391
  user_content = f"""
 
 
 
1392
  # Rule:
1393
  1. 保留有圖表或是數據的圖片
1394
- 2.根據以下的文本和關鍵字,選擇出最合適的圖片。
1395
- - 文本: {text}
1396
- - 關鍵字: {keywords}
1397
  3. 總是保留最後一張,除非他是一張空白圖片,或是一張沒有任何內容的圖片
1398
 
1399
  # Restrictions:
1400
- 1. 不要有相似或是概念重複的圖片
1401
- 2. 移除整張圖片是黑色、藍色或是白色的圖片
1402
- 3. 移除沒有任何內容的圖片
1403
- 4. 不需要理會字幕的差益,只需要看圖片的內容
 
1404
 
1405
  請根據這些信息,圖片列表如下:
1406
  {images_list_prompt}
@@ -1413,7 +1431,7 @@ def generate_key_moments_suggested_images(key_moment, LLM_model=None):
1413
  """
1414
 
1415
  response_format = { "type": "json_object" }
1416
- response = generate_content_by_LLM(sys_content, user_content, response_format, LLM_model)
1417
  print("===generate_key_moments_suggested_images===")
1418
  print(response)
1419
  print("===generate_key_moments_suggested_images===")
@@ -1603,7 +1621,10 @@ def get_key_moments_html(key_moments):
1603
  key_moments_html = css
1604
 
1605
  for i, moment in enumerate(key_moments):
1606
- images = moment['suggested_images']
 
 
 
1607
  image_elements = ""
1608
 
1609
  for j, image in enumerate(images):
 
706
 
707
  return segments
708
 
709
+ def generate_content_by_open_ai(sys_content, user_content, response_format=None, model_name=None):
710
  print("LLM using OPEN AI")
711
+ if model_name == "gpt-4-turbo":
712
+ model = "gpt-4-turbo"
713
+ else:
714
+ model = "gpt-4o"
715
  print(f"model: {model}")
716
 
717
  messages = [
 
755
  # content = response_body.get('content')[0].get('text')
756
  # return content
757
 
758
+ def generate_content_by_LLM(sys_content, user_content, response_format=None, LLM_model=None, model_name=None):
759
  # 使用 OpenAI 生成基于上传数据的问题
760
 
761
  # if LLM_model == "anthropic-claude-3-sonnet":
 
763
  # content = generate_content_by_bedrock(sys_content, user_content)
764
  # else:
765
  print(f"LLM: {LLM_model}")
766
+ print(f"model_name: {model_name}")
767
+ content = generate_content_by_open_ai(sys_content, user_content, response_format, model_name=model_name)
768
 
769
  print("=====content=====")
770
  print(content)
 
818
  print("===generate_reading_passage===")
819
  segments = split_data(df_string, word_base=100000)
820
  all_content = []
821
+ model_name = "gpt-4-turbo"
822
+ # model_name = "gpt-4o"
823
 
824
  for segment in segments:
825
  sys_content = "你是一個擅長資料分析跟影片教學的老師,user 為學生,請精讀資料文本,自行判斷資料的種類,使用 zh-TW"
826
  user_content = f"""
827
+ # 文本 {segment}
828
+
829
+ # rules:
830
+ - 根據文本,抓取重點
831
+ - 去除人類講課時口語的問答句,重新拆解成文章,建立適合閱讀語句通順的 Reading Passage
832
+ - 只需要專注提供 Reading Passage,字數在 500 字以內
833
+ - 敘述中,請把數學或是專業術語,用 Latex 包覆($...$)
834
+ - 加減乘除、根號、次方等等的運算式口語也換成 LATEX 數學符號
835
+
836
+ # restrictions:
837
+ - 請一定要使用繁體中文 zh-TW,這很重要
838
+ - 產生的結果不要前後文解釋,也不要敘述這篇文章怎麼產生的
839
+ - 請直接給出文章,不用介紹怎麼處理的或是文章字數等等
840
+ - 字數在 500 字以內
841
+ """
842
+ content = generate_content_by_LLM(sys_content, user_content, response_format=None, LLM_model=LLM_model, model_name=model_name)
843
  all_content.append(content + "\n")
844
 
845
  # 將所有生成的閱讀理解段落合併成一個完整的文章
 
908
  注意:不需要前後文敘述,直接給出 markdown 文本即可
909
  這對我很重要
910
  """
911
+ content = generate_content_by_LLM(sys_content, user_content, response_format=None, LLM_model=LLM_model, model_name=None)
912
  all_content.append(content + "\n")
913
 
914
  # 將所有生成的閱讀理解段落合併成一個完整的文章
 
1028
  ## ❓ 延伸小問題
1029
  - (一個 bullet point....請圍繞「課程名稱」為學習重點,進行重點整理,不要整理跟情境故事相關的問題)
1030
  """
1031
+ content = generate_content_by_LLM(sys_content, user_content, response_format=None, LLM_model=LLM_model, model_name=None)
1032
  all_content.append(content + "\n")
1033
 
1034
  if len(all_content) > 1:
 
1067
  ## ❓ 延伸小問題
1068
  - ( {all_content_cnt} 個 bullet point....請圍繞「課程名稱」為學習重點,進行重點整理,不要整理跟情境故事相關的問題)
1069
  """
1070
+ final_content = generate_content_by_LLM(sys_content, user_content, response_format=None, LLM_model=LLM_model, model_name=None)
1071
  else:
1072
  final_content = all_content[0]
1073
 
 
1149
  }}
1150
  """
1151
  response_format = { "type": "json_object" }
1152
+ questions = generate_content_by_LLM(sys_content, user_content, response_format, LLM_model, model_name=None)
1153
  questions_list = json.loads(questions)["questions"]
1154
  print("=====json_response=====")
1155
  print(questions_list)
 
1209
  }}
1210
  """
1211
  response_format = { "type": "json_object" }
1212
+ content = generate_content_by_LLM(sys_content, user_content, response_format, LLM_model, model_name=None)
1213
  content_json = json.loads(content)["questions_answers"]
1214
  all_content += content_json
1215
 
 
1312
  for segment in segments:
1313
  sys_content = "你是一個擅長資料分析跟影片教學的老師,user 為學生,請精讀資料文本,自行判斷資料的種類,使用 zh-TW"
1314
  user_content = f"""
1315
+ # 文本:{segment}
1316
+
1317
+ # Rule
1318
+ 1. 請根據文本,提取出 5 段重點摘要,並給出對應的時間軸,每一段重點的時間軸範圍大於1分鐘,但小於 1/3 總逐字稿長度
1319
+ 2. 內容當中,如果有列舉方法、模式或是工具,就用 bulletpoint 或是 編號方式 列出,並在列舉部分的頭尾用[]匡列(example: FAANG 是以下五間公司: [1. A公司 2.B公司 3.C公司 4.D公司 5.E公司 ],...)
1320
+ 3. 注意不要遺漏任何一段時間軸的內容 從零秒開始,以這種方式分析整個文本,從零秒開始分析,直到結束。這很重要
1321
+ 4. 結尾的時間如果有總結性的話,也要擷取
1322
+ 5. 如果頭尾的情節不是重點,特別是打招呼或是介紹自己是誰、或是finally say goodbye 就是不重要的情節,就不用擷取
1323
+ 6. 關鍵字從transcript extract to keyword,保留專家名字、專業術語、年份、數字、期刊名稱、地名、數學公式
1324
+ 7. 最後再檢查一遍,text, keywords please use or transfer to zh-TW, it's very important
1325
+
1326
+ # restrictions
1327
+ 1. 請一定要用 zh-TW,這非常重要!
1328
+ 2. 如果是疑似主播、主持人的圖片場景,且沒有任何有用的資訊,請不要選取
1329
+ 3. 如果頭尾的情節不是重點,特別是打招呼或是介紹自己是誰、或是finally say goodbye 就是不重要的情節,就不用擷取
1330
 
1331
  Example: retrun JSON
1332
  {{key_moments:[{{
 
1338
  }}
1339
  """
1340
  response_format = { "type": "json_object" }
1341
+ content = generate_content_by_LLM(sys_content, user_content, response_format, LLM_model, model_name=None)
1342
  key_moments = json.loads(content)["key_moments"]
1343
 
1344
  # "transcript": get text from formatted_simple_transcript
 
1386
  不用給上���文,直接給出關鍵字,使用 zh-TW,用逗號分隔, example: 關鍵字1, 關鍵字2
1387
  transcript:{segment}
1388
  """
1389
+ content = generate_content_by_LLM(sys_content, user_content, response_format=None, LLM_model=LLM_model, model_name=None)
1390
  keywords = content.strip().split(",")
1391
  all_content += keywords
1392
 
 
1405
  # Prepare the user prompt with text and keywords
1406
  sys_content = "你是一個擅長資料分析跟影片教學的老師,user 為學生,請精讀資料文本,自行判斷資料的種類,使用 zh-TW"
1407
  user_content = f"""
1408
+ - 文本: {text}
1409
+ - 關鍵字: {keywords}
1410
+
1411
  # Rule:
1412
  1. 保留有圖表或是數據的圖片
1413
+ 2. 根據文本和關鍵字,選擇出最合適的圖片。
 
 
1414
  3. 總是保留最後一張,除非他是一張空白圖片,或是一張沒有任何內容的圖片
1415
 
1416
  # Restrictions:
1417
+ 1. 如果是疑似主播、主持人的圖片場景,且沒有任何有用的資訊,請不要選取,這很重要
1418
+ 2. 不要有相似或是概念重複的圖片
1419
+ 3. 移除整張圖片是黑色、藍色或是白色的圖片
1420
+ 4. 移除沒有任何內容的圖片
1421
+ 5. 不需要理會字幕的差益,只需要看圖片的內容
1422
 
1423
  請根據這些信息,圖片列表如下:
1424
  {images_list_prompt}
 
1431
  """
1432
 
1433
  response_format = { "type": "json_object" }
1434
+ response = generate_content_by_LLM(sys_content, user_content, response_format, LLM_model, model_name=None)
1435
  print("===generate_key_moments_suggested_images===")
1436
  print(response)
1437
  print("===generate_key_moments_suggested_images===")
 
1621
  key_moments_html = css
1622
 
1623
  for i, moment in enumerate(key_moments):
1624
+ if "suggested_images" in moment:
1625
+ images = moment['suggested_images']
1626
+ else:
1627
+ images = moment['images']
1628
  image_elements = ""
1629
 
1630
  for j, image in enumerate(images):