Spaces:
Sleeping
Sleeping
model to 3.5
Browse files- app.py +37 -35
- chatbot.py +13 -14
app.py
CHANGED
@@ -1629,7 +1629,7 @@ def chat_with_ai(ai_name, password, video_id, trascript_state, key_moments, user
|
|
1629 |
|
1630 |
chatbot_config = {
|
1631 |
"video_id": video_id,
|
1632 |
-
"
|
1633 |
"key_moments": key_moments,
|
1634 |
"content_subject": content_subject,
|
1635 |
"content_grade": content_grade,
|
@@ -1656,7 +1656,7 @@ def chat_with_ai(ai_name, password, video_id, trascript_state, key_moments, user
|
|
1656 |
print(f"Error: {e}")
|
1657 |
return "请求失败,请稍后再试!", chat_history
|
1658 |
|
1659 |
-
def chat_with_opan_ai_assistant(password, youtube_id, thread_id,
|
1660 |
verify_password(password)
|
1661 |
|
1662 |
# 先計算 user_message 是否超過 500 個字
|
@@ -1670,26 +1670,33 @@ def chat_with_opan_ai_assistant(password, youtube_id, thread_id, trascript, key_
|
|
1670 |
raise gr.Error(error_msg)
|
1671 |
|
1672 |
try:
|
1673 |
-
assistant_id = "asst_kmvZLNkDUYaNkMNtZEAYxyPq"
|
|
|
|
|
1674 |
client = OPEN_AI_CLIENT
|
1675 |
# 直接安排逐字稿資料 in instructions
|
1676 |
-
|
|
|
|
|
|
|
1677 |
# 移除 embed_url, screenshot_path
|
1678 |
for entry in trascript_json:
|
1679 |
-
entry.pop('
|
1680 |
-
entry.pop('screenshot_path', None)
|
1681 |
trascript_text = json.dumps(trascript_json, ensure_ascii=False)
|
1682 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1683 |
if len(trascript_text) > 25000:
|
1684 |
-
|
1685 |
-
key_moments_json = json.loads(key_moments)
|
1686 |
-
else:
|
1687 |
-
key_moments_json = key_moments
|
1688 |
-
|
1689 |
-
# key_moments_json remove images
|
1690 |
-
for moment in key_moments_json:
|
1691 |
-
moment.pop('images', None)
|
1692 |
-
content_text = json.dumps(key_moments_json, ensure_ascii=False)
|
1693 |
print("=== transcript_text is too long, replace by key_moments_text ===")
|
1694 |
else:
|
1695 |
content_text = trascript_text
|
@@ -1702,28 +1709,26 @@ def chat_with_opan_ai_assistant(password, youtube_id, thread_id, trascript, key_
|
|
1702 |
-------------------------------------
|
1703 |
你是一個專業的{content_subject}老師, user 為{content_grade}的學生
|
1704 |
socratic_mode = {socratic_mode}
|
1705 |
-
|
1706 |
if socratic_mode is True,
|
1707 |
- 請用蘇格拉底式的提問方式,引導學生思考,並且給予學生一些提示
|
1708 |
- 一次只問一個問題,字數在100字以內
|
1709 |
- 不要直接給予答案,讓學生自己思考
|
1710 |
- 但可以給予一些提示跟引導,例如給予影片的時間軸,讓學生自己去找答案
|
1711 |
-
|
1712 |
-
if socratic_mode is False,
|
1713 |
-
- 直接回答學生問題,字數在100字以內
|
1714 |
-
|
1715 |
rule:
|
1716 |
- 請一定要用繁體中文回答 zh-TW,並用台灣人的口語表達,回答時不用特別說明這是台灣人的語氣,也不用說這是「台語的說法」
|
1717 |
-
-
|
1718 |
- 如果學生問了一些問題你無法判斷,請告訴學生你無法判斷,並建議學生可以問其他問題
|
1719 |
- 或者你可以反問學生一些問題,幫助學生更好的理解資料,字數在100字以內
|
1720 |
- 如果學生的問題與資料文本無關,請告訴學生你「無法回答超出影片範圍的問題」,並告訴他可以怎麼問什麼樣的問題(一個就好)
|
1721 |
-
- 只要是參考逐字稿資料,please use the timestamp format, example:【參考資料:00:00:00】
|
1722 |
- 回答範圍一定要在逐字稿資料內,不要引用其他資料,請嚴格執行
|
1723 |
- 並在重複問句後給予學生鼓勵,讓學生有學習的動力
|
1724 |
-
-
|
|
|
1725 |
"""
|
1726 |
|
|
|
|
|
|
|
1727 |
# 创建线程
|
1728 |
if not thread_id:
|
1729 |
thread = client.beta.threads.create()
|
@@ -1866,7 +1871,8 @@ def streaming_chat_with_open_ai(user_message, chat_history, password, thread_id,
|
|
1866 |
socratic_mode = True
|
1867 |
|
1868 |
try:
|
1869 |
-
assistant_id = "asst_kmvZLNkDUYaNkMNtZEAYxyPq"
|
|
|
1870 |
client = OPEN_AI_CLIENT
|
1871 |
# 直接安排逐字稿資料 in instructions
|
1872 |
if isinstance(trascript, str):
|
@@ -1904,20 +1910,16 @@ def streaming_chat_with_open_ai(user_message, chat_history, password, thread_id,
|
|
1904 |
- 一次只問一個問題,字數在100字以內
|
1905 |
- 不要直接給予答案,讓學生自己思考
|
1906 |
- 但可以給予一些提示跟引導,例如給予影片的時間軸,讓學生自己去找答案
|
1907 |
-
|
1908 |
-
if socratic_mode is False,
|
1909 |
-
- 直接回答學生問題,字數在100字以內
|
1910 |
-
|
1911 |
rule:
|
1912 |
- 請一定要用繁體中文回答 zh-TW,並用台灣人的口語表達,回答時不用特別說明這是台灣人的語氣,也不用說這是「台語的說法」
|
1913 |
-
-
|
1914 |
- 如果學生問了一些問題你無法判斷,請告訴學生你無法判斷,並建議學生可以問其他問題
|
1915 |
- 或者你可以反問學生一些問題,幫助學生更好的理解資料,字數在100字以內
|
1916 |
- 如果學生的問題與資料文本無關,請告訴學生你「無法回答超出影片範圍的問題」,並告訴他可以怎麼問什麼樣的問題(一個就好)
|
1917 |
-
- 只要是參考逐字稿資料,請在回答的最後標註【參考資料:(分):(秒)】
|
1918 |
- 回答範圍一定要在逐字稿資料內,不要引用其他資料,請嚴格執行
|
1919 |
- 並在重複問句後給予學生鼓勵,讓學生有學習的動力
|
1920 |
-
-
|
|
|
1921 |
"""
|
1922 |
|
1923 |
# 创建线程
|
@@ -2321,7 +2323,7 @@ with gr.Blocks(theme=gr.themes.Base(primary_hue=gr.themes.colors.orange, seconda
|
|
2321 |
# OPENAI ASSISTANT CHATBOT 模式
|
2322 |
send_button.click(
|
2323 |
chat_with_opan_ai_assistant,
|
2324 |
-
inputs=[password, video_id, thread_id,
|
2325 |
outputs=[msg, chatbot, thread_id]
|
2326 |
)
|
2327 |
openai_chatbot_audio_input.change(
|
@@ -2330,9 +2332,9 @@ with gr.Blocks(theme=gr.themes.Base(primary_hue=gr.themes.colors.orange, seconda
|
|
2330 |
outputs=[msg]
|
2331 |
)
|
2332 |
# OPENAI ASSISTANT CHATBOT 連接按鈕點擊事件
|
2333 |
-
btn_1_chat_with_opan_ai_assistant_input =[password, video_id, thread_id,
|
2334 |
-
btn_2_chat_with_opan_ai_assistant_input =[password, video_id, thread_id,
|
2335 |
-
btn_3_chat_with_opan_ai_assistant_input =[password, video_id, thread_id,
|
2336 |
btn_1.click(
|
2337 |
chat_with_opan_ai_assistant,
|
2338 |
inputs=btn_1_chat_with_opan_ai_assistant_input,
|
|
|
1629 |
|
1630 |
chatbot_config = {
|
1631 |
"video_id": video_id,
|
1632 |
+
"transcript": simple_transcript,
|
1633 |
"key_moments": key_moments,
|
1634 |
"content_subject": content_subject,
|
1635 |
"content_grade": content_grade,
|
|
|
1656 |
print(f"Error: {e}")
|
1657 |
return "请求失败,请稍后再试!", chat_history
|
1658 |
|
1659 |
+
def chat_with_opan_ai_assistant(password, youtube_id, thread_id, trascript_state, key_moments, user_message, chat_history, content_subject, content_grade, socratic_mode=False):
|
1660 |
verify_password(password)
|
1661 |
|
1662 |
# 先計算 user_message 是否超過 500 個字
|
|
|
1670 |
raise gr.Error(error_msg)
|
1671 |
|
1672 |
try:
|
1673 |
+
# assistant_id = "asst_kmvZLNkDUYaNkMNtZEAYxyPq" #GPT 4 turbo
|
1674 |
+
assistant_id = "asst_5SaUElqvL3U0ybSi9PRM8x3P" #GPT 3.5 turbo
|
1675 |
+
|
1676 |
client = OPEN_AI_CLIENT
|
1677 |
# 直接安排逐字稿資料 in instructions
|
1678 |
+
if isinstance(trascript_state, str):
|
1679 |
+
trascript_json = json.loads(trascript_state)
|
1680 |
+
else:
|
1681 |
+
trascript_json = trascript_state
|
1682 |
# 移除 embed_url, screenshot_path
|
1683 |
for entry in trascript_json:
|
1684 |
+
entry.pop('end_time', None)
|
|
|
1685 |
trascript_text = json.dumps(trascript_json, ensure_ascii=False)
|
1686 |
|
1687 |
+
if isinstance(key_moments, str):
|
1688 |
+
key_moments_json = json.loads(key_moments)
|
1689 |
+
else:
|
1690 |
+
key_moments_json = key_moments
|
1691 |
+
# key_moments_json remove images
|
1692 |
+
for moment in key_moments_json:
|
1693 |
+
moment.pop('images', None)
|
1694 |
+
moment.pop('end', None)
|
1695 |
+
moment.pop('text', None)
|
1696 |
+
key_moments_text = json.dumps(key_moments_json, ensure_ascii=False)
|
1697 |
+
|
1698 |
if len(trascript_text) > 25000:
|
1699 |
+
content_text = key_moments_text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1700 |
print("=== transcript_text is too long, replace by key_moments_text ===")
|
1701 |
else:
|
1702 |
content_text = trascript_text
|
|
|
1709 |
-------------------------------------
|
1710 |
你是一個專業的{content_subject}老師, user 為{content_grade}的學生
|
1711 |
socratic_mode = {socratic_mode}
|
|
|
1712 |
if socratic_mode is True,
|
1713 |
- 請用蘇格拉底式的提問方式,引導學生思考,並且給予學生一些提示
|
1714 |
- 一次只問一個問題,字數在100字以內
|
1715 |
- 不要直接給予答案,讓學生自己思考
|
1716 |
- 但可以給予一些提示跟引導,例如給予影片的時間軸,讓學生自己去找答案
|
|
|
|
|
|
|
|
|
1717 |
rule:
|
1718 |
- 請一定要用繁體中文回答 zh-TW,並用台灣人的口語表達,回答時不用特別說明這是台灣人的語氣,也不用說這是「台語的說法」
|
1719 |
+
- 請用 {content_grade} 的學生能懂的方式回答,不用提到「逐字稿」這個詞
|
1720 |
- 如果學生問了一些問題你無法判斷,請告訴學生你無法判斷,並建議學生可以問其他問題
|
1721 |
- 或者你可以反問學生一些問題,幫助學生更好的理解資料,字數在100字以內
|
1722 |
- 如果學生的問題與資料文本無關,請告訴學生你「無法回答超出影片範圍的問題」,並告訴他可以怎麼問什麼樣的問題(一個就好)
|
|
|
1723 |
- 回答範圍一定要在逐字稿資料內,不要引用其他資料,請嚴格執行
|
1724 |
- 並在重複問句後給予學生鼓勵,讓學生有學習的動力
|
1725 |
+
- 回答時數學式請用數學符號代替文字(Latex 用 $ 字號 render)
|
1726 |
+
- 只要是參考逐字稿資料,please use the timestamp format, example:【參考資料:00:00:00】
|
1727 |
"""
|
1728 |
|
1729 |
+
print("=== instructions ===")
|
1730 |
+
print(instructions)
|
1731 |
+
|
1732 |
# 创建线程
|
1733 |
if not thread_id:
|
1734 |
thread = client.beta.threads.create()
|
|
|
1871 |
socratic_mode = True
|
1872 |
|
1873 |
try:
|
1874 |
+
# assistant_id = "asst_kmvZLNkDUYaNkMNtZEAYxyPq" #GPT 4 turbo
|
1875 |
+
assistant_id = "asst_5SaUElqvL3U0ybSi9PRM8x3P" #GPT 3.5 turbo
|
1876 |
client = OPEN_AI_CLIENT
|
1877 |
# 直接安排逐字稿資料 in instructions
|
1878 |
if isinstance(trascript, str):
|
|
|
1910 |
- 一次只問一個問題,字數在100字以內
|
1911 |
- 不要直接給予答案,讓學生自己思考
|
1912 |
- 但可以給予一些提示跟引導,例如給予影片的時間軸,讓學生自己去找答案
|
|
|
|
|
|
|
|
|
1913 |
rule:
|
1914 |
- 請一定要用繁體中文回答 zh-TW,並用台灣人的口語表達,回答時不用特別說明這是台灣人的語氣,也不用說這是「台語的說法」
|
1915 |
+
- 請用 {content_grade} 的學生能懂的方式回答,不用提到「逐字稿」這個詞
|
1916 |
- 如果學生問了一些問題你無法判斷,請告訴學生你無法判斷,並建議學生可以問其他問題
|
1917 |
- 或者你可以反問學生一些問題,幫助學生更好的理解資料,字數在100字以內
|
1918 |
- 如果學生的問題與資料文本無關,請告訴學生你「無法回答超出影片範圍的問題」,並告訴他可以怎麼問什麼樣的問題(一個就好)
|
|
|
1919 |
- 回答範圍一定要在逐字稿資料內,不要引用其他資料,請嚴格執行
|
1920 |
- 並在重複問句後給予學生鼓勵,讓學生有學習的動力
|
1921 |
+
- 回答時數學式請用數學符號代替文字(Latex 用 $ 字號 render)
|
1922 |
+
- 只要是參考逐字稿資料,please use the timestamp format, example:【參考資料:00:00:00】
|
1923 |
"""
|
1924 |
|
1925 |
# 创建线程
|
|
|
2323 |
# OPENAI ASSISTANT CHATBOT 模式
|
2324 |
send_button.click(
|
2325 |
chat_with_opan_ai_assistant,
|
2326 |
+
inputs=[password, video_id, thread_id, trascript_state, key_moments, msg, chatbot, content_subject, content_grade, socratic_mode_btn],
|
2327 |
outputs=[msg, chatbot, thread_id]
|
2328 |
)
|
2329 |
openai_chatbot_audio_input.change(
|
|
|
2332 |
outputs=[msg]
|
2333 |
)
|
2334 |
# OPENAI ASSISTANT CHATBOT 連接按鈕點擊事件
|
2335 |
+
btn_1_chat_with_opan_ai_assistant_input =[password, video_id, thread_id, trascript_state, key_moments, btn_1, chatbot, content_subject, content_grade, ai_chatbot_socratic_mode_btn]
|
2336 |
+
btn_2_chat_with_opan_ai_assistant_input =[password, video_id, thread_id, trascript_state, key_moments, btn_2, chatbot, content_subject, content_grade, ai_chatbot_socratic_mode_btn]
|
2337 |
+
btn_3_chat_with_opan_ai_assistant_input =[password, video_id, thread_id, trascript_state, key_moments, btn_3, chatbot, content_subject, content_grade, ai_chatbot_socratic_mode_btn]
|
2338 |
btn_1.click(
|
2339 |
chat_with_opan_ai_assistant,
|
2340 |
inputs=btn_1_chat_with_opan_ai_assistant_input,
|
chatbot.py
CHANGED
@@ -8,7 +8,7 @@ class Chatbot:
|
|
8 |
self.content_subject = config.get('content_subject')
|
9 |
self.content_grade = config.get('content_grade')
|
10 |
self.jutor_chat_key = config.get('jutor_chat_key')
|
11 |
-
self.transcript_text = self.get_transcript_text(config.get('
|
12 |
self.key_moments_text = self.get_key_moments_text(config.get('key_moments'))
|
13 |
self.ai_name = config.get('ai_name')
|
14 |
self.ai_client = config.get('ai_client')
|
@@ -18,6 +18,8 @@ class Chatbot:
|
|
18 |
transcript_json = json.loads(transcript_data)
|
19 |
else:
|
20 |
transcript_json = transcript_data
|
|
|
|
|
21 |
transcript_text = json.dumps(transcript_json, ensure_ascii=False)
|
22 |
return transcript_text
|
23 |
|
@@ -47,18 +49,18 @@ class Chatbot:
|
|
47 |
content_subject = self.content_subject
|
48 |
content_grade = self.content_grade
|
49 |
video_id = self.video_id
|
50 |
-
|
51 |
key_moments_text = self.key_moments_text
|
52 |
socratic_mode = str(socratic_mode)
|
53 |
ai_name = self.ai_name
|
54 |
|
55 |
# string with maximum length 32768
|
56 |
-
# if
|
57 |
-
if len(
|
58 |
content_text = key_moments_text
|
59 |
print("=== transcript_text is too long, replace by key_moments_text ===")
|
60 |
else:
|
61 |
-
content_text =
|
62 |
print("=== transcript_text is used ===")
|
63 |
|
64 |
system_prompt = f"""
|
@@ -73,21 +75,16 @@ class Chatbot:
|
|
73 |
- 一次只問一個問題,字數在100字以內
|
74 |
- 不要直接給予答案,讓學生自己思考
|
75 |
- 但可以給予一些提示跟引導,例如給予影片的時間軸,讓學生自己去找答案
|
76 |
-
|
77 |
-
if socratic_mode is False,
|
78 |
-
- 直接回答學生問題,字數在100字以內
|
79 |
-
|
80 |
rule:
|
81 |
- 請一定要用繁體中文回答 zh-TW,並用台灣人的口語表達,回答時不用特別說明這是台灣人的語氣,也不用說這是「台語的說法」
|
82 |
-
-
|
83 |
- 如果學生問了一些問題你無法判斷,請告訴學生你無法判斷,並建議學生可以問其他問題
|
84 |
- 或者你可以反問學生一些問題,幫助學生更好的理解資料,字數在100字以內
|
85 |
- 如果學生的問題與資料文本無關,請告訴學生你「無法回答超出影片範圍的問題」,並告訴他可以怎麼問什麼樣的問題(一個就好)
|
86 |
-
- 只要是參考逐字稿資料,please use the timestamp format, example:【參考資料:00:00:00】
|
87 |
- 回答範圍一定要在逐字稿資料內,不要引用其他資料,請嚴格執行
|
88 |
-
-
|
89 |
-
- 請用 {content_grade} 的學生能懂的方式回答
|
90 |
- 回答時數學式請用數學符號代替文字(Latex 用 $ 字號 render)
|
|
|
91 |
"""
|
92 |
|
93 |
return system_prompt
|
@@ -126,12 +123,14 @@ class Chatbot:
|
|
126 |
"Content-Type": "application/json",
|
127 |
"x-api-key": self.jutor_chat_key,
|
128 |
}
|
|
|
|
|
129 |
data = {
|
130 |
"data": {
|
131 |
"messages": messages,
|
132 |
"max_tokens": 512,
|
133 |
"temperature": 0.9,
|
134 |
-
"model":
|
135 |
"stream": False,
|
136 |
}
|
137 |
}
|
|
|
8 |
self.content_subject = config.get('content_subject')
|
9 |
self.content_grade = config.get('content_grade')
|
10 |
self.jutor_chat_key = config.get('jutor_chat_key')
|
11 |
+
self.transcript_text = self.get_transcript_text(config.get('transcript'))
|
12 |
self.key_moments_text = self.get_key_moments_text(config.get('key_moments'))
|
13 |
self.ai_name = config.get('ai_name')
|
14 |
self.ai_client = config.get('ai_client')
|
|
|
18 |
transcript_json = json.loads(transcript_data)
|
19 |
else:
|
20 |
transcript_json = transcript_data
|
21 |
+
for entry in transcript_json:
|
22 |
+
entry.pop('end_time', None)
|
23 |
transcript_text = json.dumps(transcript_json, ensure_ascii=False)
|
24 |
return transcript_text
|
25 |
|
|
|
49 |
content_subject = self.content_subject
|
50 |
content_grade = self.content_grade
|
51 |
video_id = self.video_id
|
52 |
+
transcript_text = self.transcript_text
|
53 |
key_moments_text = self.key_moments_text
|
54 |
socratic_mode = str(socratic_mode)
|
55 |
ai_name = self.ai_name
|
56 |
|
57 |
# string with maximum length 32768
|
58 |
+
# if transcript_text is too long, replace by key_moments_text
|
59 |
+
if len(transcript_text) > 25000:
|
60 |
content_text = key_moments_text
|
61 |
print("=== transcript_text is too long, replace by key_moments_text ===")
|
62 |
else:
|
63 |
+
content_text = transcript_text
|
64 |
print("=== transcript_text is used ===")
|
65 |
|
66 |
system_prompt = f"""
|
|
|
75 |
- 一次只問一個問題,字數在100字以內
|
76 |
- 不要直接給予答案,讓學生自己思考
|
77 |
- 但可以給予一些提示跟引導,例如給予影片的時間軸,讓學生自己去找答案
|
|
|
|
|
|
|
|
|
78 |
rule:
|
79 |
- 請一定要用繁體中文回答 zh-TW,並用台灣人的口語表達,回答時不用特別說明這是台灣人的語氣,也不用說這是「台語的說法」
|
80 |
+
- 請用 {content_grade} 的學生能懂的方式回答,不用提到「逐字稿」這個詞
|
81 |
- 如果學生問了一些問題你無法判斷,請告訴學生你無法判斷,並建議學生可以問其他問題
|
82 |
- 或者你可以反問學生一些問題,幫助學生更好的理解資料,字數在100字以內
|
83 |
- 如果學生的問題與資料文本無關,請告訴學生你「無法回答超出影片範圍的問題」,並告訴他可以怎麼問什麼樣的問題(一個就好)
|
|
|
84 |
- 回答範圍一定要在逐字稿資料內,不要引用其他資料,請嚴格執行
|
85 |
+
- 並給予學生鼓勵,讓學生有學習的動力
|
|
|
86 |
- 回答時數學式請用數學符號代替文字(Latex 用 $ 字號 render)
|
87 |
+
- 只要是參考逐字稿資料,please use the timestamp format and give only one reference, example:【參考資料:00:00:00】
|
88 |
"""
|
89 |
|
90 |
return system_prompt
|
|
|
123 |
"Content-Type": "application/json",
|
124 |
"x-api-key": self.jutor_chat_key,
|
125 |
}
|
126 |
+
# model = "gpt-4-1106-preview"
|
127 |
+
model = "gpt-3.5-turbo-0125"
|
128 |
data = {
|
129 |
"data": {
|
130 |
"messages": messages,
|
131 |
"max_tokens": 512,
|
132 |
"temperature": 0.9,
|
133 |
+
"model": model,
|
134 |
"stream": False,
|
135 |
}
|
136 |
}
|