Spaces:
Running
Running
streaming_chat_greeting
Browse files
app.py
CHANGED
@@ -1673,7 +1673,7 @@ def get_instructions(content_subject, content_grade, key_moments):
|
|
1673 |
def chat_with_ai(ai_name, password, video_id, trascript_state, key_moments, user_message, chat_history, content_subject, content_grade, socratic_mode=False):
|
1674 |
verify_password(password)
|
1675 |
|
1676 |
-
if chat_history is not None and len(chat_history) >
|
1677 |
error_msg = "此次對話超過上限(對話一輪10次)"
|
1678 |
raise gr.Error(error_msg)
|
1679 |
|
@@ -1754,14 +1754,14 @@ def chat_with_opan_ai_assistant(password, youtube_id, thread_id, trascript_state
|
|
1754 |
|
1755 |
client = OPEN_AI_CLIENT
|
1756 |
# 直接安排逐字稿資料 in instructions
|
1757 |
-
if isinstance(trascript_state, str):
|
1758 |
-
|
1759 |
-
else:
|
1760 |
-
|
1761 |
-
# 移除 embed_url, screenshot_path
|
1762 |
-
for entry in trascript_json:
|
1763 |
-
|
1764 |
-
trascript_text = json.dumps(trascript_json, ensure_ascii=False)
|
1765 |
|
1766 |
if isinstance(key_moments, str):
|
1767 |
key_moments_json = json.loads(key_moments)
|
@@ -1905,7 +1905,7 @@ def streaming_chat_with_open_ai(user_message, chat_history, password, thread_id,
|
|
1905 |
raise gr.Error(error_msg)
|
1906 |
|
1907 |
# 如果 chat_history 超過 10 則訊息,直接 return "對話超過上限"
|
1908 |
-
if chat_history is not None and len(chat_history) >
|
1909 |
error_msg = "此次對話超過上限(對話一輪10次)"
|
1910 |
raise gr.Error(error_msg)
|
1911 |
|
@@ -1914,13 +1914,13 @@ def streaming_chat_with_open_ai(user_message, chat_history, password, thread_id,
|
|
1914 |
# assistant_id = "asst_5SaUElqvL3U0ybSi9PRM8x3P" #GPT 3.5 turbo
|
1915 |
client = OPEN_AI_CLIENT
|
1916 |
# 直接安排逐字稿資料 in instructions
|
1917 |
-
if isinstance(trascript, str):
|
1918 |
-
|
1919 |
-
else:
|
1920 |
-
|
1921 |
-
trascript_text = json.dumps(trascript_json, ensure_ascii=False)
|
1922 |
-
# trascript_text 移除 \n, 空白
|
1923 |
-
trascript_text = trascript_text.replace("\n", "").replace(" ", "")
|
1924 |
|
1925 |
if isinstance(key_moments, str):
|
1926 |
key_moments_json = json.loads(key_moments)
|
@@ -2144,7 +2144,17 @@ with gr.Blocks(theme=gr.themes.Base(primary_hue=gr.themes.colors.orange, seconda
|
|
2144 |
user_avatar = "https://em-content.zobj.net/source/google/263/flushed-face_1f633.png"
|
2145 |
bot_avatar = "https://junyitopicimg.s3.amazonaws.com/s4byy--icon.jpe?v=20200513013523726"
|
2146 |
latex_delimiters = [{"left": "$", "right": "$", "display": False}]
|
2147 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2148 |
thread_id = gr.Textbox(label="thread_id", visible=False)
|
2149 |
socratic_mode_btn = gr.Checkbox(label="蘇格拉底家教助理模式", value=True, visible=False)
|
2150 |
with gr.Row():
|
@@ -2154,11 +2164,17 @@ with gr.Blocks(theme=gr.themes.Base(primary_hue=gr.themes.colors.orange, seconda
|
|
2154 |
btn_3 = gr.Button("問題一")
|
2155 |
gr.Markdown("### 重新生成問題")
|
2156 |
btn_create_question = gr.Button("生成其他問題", variant="primary")
|
2157 |
-
openai_chatbot_audio_input = gr.Audio(sources=["microphone"], type="filepath", max_length=60)
|
2158 |
with gr.Row():
|
2159 |
msg = gr.Textbox(label="訊息",scale=3)
|
2160 |
send_button = gr.Button("送出", variant="primary", scale=1)
|
2161 |
with gr.Tab("飛特音速"):
|
|
|
|
|
|
|
|
|
|
|
|
|
2162 |
additional_inputs = [password, streaming_chat_thread_id_state, trascript_state, key_moments_state, content_subject_state, content_grade_state]
|
2163 |
streaming_chat = gr.ChatInterface(
|
2164 |
fn=streaming_chat_with_open_ai,
|
@@ -2167,18 +2183,28 @@ with gr.Blocks(theme=gr.themes.Base(primary_hue=gr.themes.colors.orange, seconda
|
|
2167 |
retry_btn=None,
|
2168 |
undo_btn="⏪ 上一步",
|
2169 |
clear_btn="🗑️ 清除全部",
|
2170 |
-
stop_btn=
|
|
|
2171 |
)
|
2172 |
with gr.Tab("其他精靈"):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2173 |
ai_name = gr.Dropdown(label="選擇 AI 助理", choices=[("梨梨","jutor"), ("麥麥","claude3"), ("狐狸貓","groq")], value="jutor")
|
2174 |
-
ai_chatbot = gr.Chatbot(avatar_images=[user_avatar, bot_avatar], label="ai_chatbot", show_share_button=False, likeable=True, show_label=False, latex_delimiters=latex_delimiters)
|
2175 |
ai_chatbot_socratic_mode_btn = gr.Checkbox(label="蘇格拉底家教助理模式", value=True, visible=False)
|
2176 |
with gr.Row():
|
2177 |
with gr.Accordion("你也有類似的問題想問嗎?", open=False) as ask_questions_accordion_2:
|
2178 |
ai_chatbot_question_1 = gr.Button("問題一")
|
2179 |
ai_chatbot_question_2 = gr.Button("問題一")
|
2180 |
ai_chatbot_question_3 = gr.Button("問題一")
|
2181 |
-
ai_chatbot_audio_input = gr.Audio(sources=["microphone"], type="filepath", max_length=60)
|
2182 |
with gr.Row():
|
2183 |
ai_msg = gr.Textbox(label="訊息輸入",scale=3)
|
2184 |
ai_send_button = gr.Button("送出", variant="primary",scale=1)
|
|
|
1673 |
def chat_with_ai(ai_name, password, video_id, trascript_state, key_moments, user_message, chat_history, content_subject, content_grade, socratic_mode=False):
|
1674 |
verify_password(password)
|
1675 |
|
1676 |
+
if chat_history is not None and len(chat_history) > 11:
|
1677 |
error_msg = "此次對話超過上限(對話一輪10次)"
|
1678 |
raise gr.Error(error_msg)
|
1679 |
|
|
|
1754 |
|
1755 |
client = OPEN_AI_CLIENT
|
1756 |
# 直接安排逐字稿資料 in instructions
|
1757 |
+
# if isinstance(trascript_state, str):
|
1758 |
+
# trascript_json = json.loads(trascript_state)
|
1759 |
+
# else:
|
1760 |
+
# trascript_json = trascript_state
|
1761 |
+
# # 移除 embed_url, screenshot_path
|
1762 |
+
# for entry in trascript_json:
|
1763 |
+
# entry.pop('end_time', None)
|
1764 |
+
# trascript_text = json.dumps(trascript_json, ensure_ascii=False)
|
1765 |
|
1766 |
if isinstance(key_moments, str):
|
1767 |
key_moments_json = json.loads(key_moments)
|
|
|
1905 |
raise gr.Error(error_msg)
|
1906 |
|
1907 |
# 如果 chat_history 超過 10 則訊息,直接 return "對話超過上限"
|
1908 |
+
if chat_history is not None and len(chat_history) > 11:
|
1909 |
error_msg = "此次對話超過上限(對話一輪10次)"
|
1910 |
raise gr.Error(error_msg)
|
1911 |
|
|
|
1914 |
# assistant_id = "asst_5SaUElqvL3U0ybSi9PRM8x3P" #GPT 3.5 turbo
|
1915 |
client = OPEN_AI_CLIENT
|
1916 |
# 直接安排逐字稿資料 in instructions
|
1917 |
+
# if isinstance(trascript, str):
|
1918 |
+
# trascript_json = json.loads(trascript)
|
1919 |
+
# else:
|
1920 |
+
# trascript_json = trascript
|
1921 |
+
# trascript_text = json.dumps(trascript_json, ensure_ascii=False)
|
1922 |
+
# # trascript_text 移除 \n, 空白
|
1923 |
+
# trascript_text = trascript_text.replace("\n", "").replace(" ", "")
|
1924 |
|
1925 |
if isinstance(key_moments, str):
|
1926 |
key_moments_json = json.loads(key_moments)
|
|
|
2144 |
user_avatar = "https://em-content.zobj.net/source/google/263/flushed-face_1f633.png"
|
2145 |
bot_avatar = "https://junyitopicimg.s3.amazonaws.com/s4byy--icon.jpe?v=20200513013523726"
|
2146 |
latex_delimiters = [{"left": "$", "right": "$", "display": False}]
|
2147 |
+
chatbot_greeting = [[
|
2148 |
+
None,
|
2149 |
+
"""Hi,我是您的AI學伴 - 飛特精靈,我可以陪你一起學習本次的內容,有什麼問題都可以問我喔!
|
2150 |
+
🤔 如果你不知道怎麼發問,可以點擊左下方的問題一、問題二、問題三,我會幫你生成問題!
|
2151 |
+
🗣️ 也可以點擊右下方用語音輸入,我會幫你轉換成文字,厲害吧!
|
2152 |
+
🔠 或是直接鍵盤輸入你的問題,我會盡力回答你的問題喔!
|
2153 |
+
💤 但我還在成長,體力有限,每一次學習只能回答十個問題,請讓我休息一下再問問題喔!
|
2154 |
+
🦄 如果達到上限,或是遇到精靈很累,請問問其他朋友,像是飛特音速說話的速度比較快,你是否跟得上呢?你也可以和其他精靈互動看看喔!
|
2155 |
+
""",
|
2156 |
+
]]
|
2157 |
+
chatbot = gr.Chatbot(avatar_images=[user_avatar, bot_avatar], label="OPEN AI", show_share_button=False, likeable=True, show_label=False, latex_delimiters=latex_delimiters,value=chatbot_greeting)
|
2158 |
thread_id = gr.Textbox(label="thread_id", visible=False)
|
2159 |
socratic_mode_btn = gr.Checkbox(label="蘇格拉底家教助理模式", value=True, visible=False)
|
2160 |
with gr.Row():
|
|
|
2164 |
btn_3 = gr.Button("問題一")
|
2165 |
gr.Markdown("### 重新生成問題")
|
2166 |
btn_create_question = gr.Button("生成其他問題", variant="primary")
|
2167 |
+
openai_chatbot_audio_input = gr.Audio(sources=["microphone"], type="filepath", max_length=60, label="語音輸入")
|
2168 |
with gr.Row():
|
2169 |
msg = gr.Textbox(label="訊息",scale=3)
|
2170 |
send_button = gr.Button("送出", variant="primary", scale=1)
|
2171 |
with gr.Tab("飛特音速"):
|
2172 |
+
streaming_chat_greeting = """
|
2173 |
+
Hi,我是飛特音速,說話比較快,但有什麼問題都可以問我喔! \n
|
2174 |
+
🚀 我沒有預設問題、也沒有語音輸入,適合快問快答的你 \n
|
2175 |
+
🔠 鍵盤輸入你的問題,我會盡力回答你的問題喔!\n
|
2176 |
+
💤 我還在成長,體力有限,每一次學習只能回答十個問題,請讓我休息一下再問問題喔!
|
2177 |
+
"""
|
2178 |
additional_inputs = [password, streaming_chat_thread_id_state, trascript_state, key_moments_state, content_subject_state, content_grade_state]
|
2179 |
streaming_chat = gr.ChatInterface(
|
2180 |
fn=streaming_chat_with_open_ai,
|
|
|
2183 |
retry_btn=None,
|
2184 |
undo_btn="⏪ 上一步",
|
2185 |
clear_btn="🗑️ 清除全部",
|
2186 |
+
stop_btn=None,
|
2187 |
+
description=streaming_chat_greeting
|
2188 |
)
|
2189 |
with gr.Tab("其他精靈"):
|
2190 |
+
ai_chatbot_greeting = [[
|
2191 |
+
None,
|
2192 |
+
"""Hi,我是飛特精靈的朋友們(梨梨、麥麥、狐狸貓),也可以陪你一起學習本次的內容,有什麼問題都可以問我喔!
|
2193 |
+
🤔 如果你不知道怎麼發問,可以點擊左下方的問題一、問題二、問題三,我會幫你生成問題!
|
2194 |
+
🗣️ 也可以點擊右下方用語音輸入,我會幫你轉換成文字,厲害吧!
|
2195 |
+
🔠 或是直接鍵盤輸入你的問題,我會盡力回答你的問題喔!
|
2196 |
+
💤 精靈們體力都有限,每一次學習只能回答十個問題,請讓我休息一下再問問題喔!
|
2197 |
+
""",
|
2198 |
+
]]
|
2199 |
ai_name = gr.Dropdown(label="選擇 AI 助理", choices=[("梨梨","jutor"), ("麥麥","claude3"), ("狐狸貓","groq")], value="jutor")
|
2200 |
+
ai_chatbot = gr.Chatbot(avatar_images=[user_avatar, bot_avatar], label="ai_chatbot", show_share_button=False, likeable=True, show_label=False, latex_delimiters=latex_delimiters, value=ai_chatbot_greeting)
|
2201 |
ai_chatbot_socratic_mode_btn = gr.Checkbox(label="蘇格拉底家教助理模式", value=True, visible=False)
|
2202 |
with gr.Row():
|
2203 |
with gr.Accordion("你也有類似的問題想問嗎?", open=False) as ask_questions_accordion_2:
|
2204 |
ai_chatbot_question_1 = gr.Button("問題一")
|
2205 |
ai_chatbot_question_2 = gr.Button("問題一")
|
2206 |
ai_chatbot_question_3 = gr.Button("問題一")
|
2207 |
+
ai_chatbot_audio_input = gr.Audio(sources=["microphone"], type="filepath", max_length=60, label="語音輸入")
|
2208 |
with gr.Row():
|
2209 |
ai_msg = gr.Textbox(label="訊息輸入",scale=3)
|
2210 |
ai_send_button = gr.Button("送出", variant="primary",scale=1)
|