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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
# Create Gradio interface
|
| 4 |
with gr.Blocks(css=custom_css) as demo:
|
|
@@ -60,59 +290,5 @@ with gr.Blocks(css=custom_css) as demo:
|
|
| 60 |
clear = gr.ClearButton([msg, chatbot], value="🗑️ 清除對話 | Clear Chat")
|
| 61 |
msg.submit(respond, [name_input, msg, chatbot], [msg, chatbot])
|
| 62 |
|
| 63 |
-
# Update custom CSS to style the new layout
|
| 64 |
-
custom_css = """
|
| 65 |
-
.math-container .katex {
|
| 66 |
-
font-size: 1.1em;
|
| 67 |
-
display: inline-block !important;
|
| 68 |
-
white-space: nowrap !important;
|
| 69 |
-
}
|
| 70 |
-
.input-box {
|
| 71 |
-
font-size: 1.1em;
|
| 72 |
-
}
|
| 73 |
-
.output-box {
|
| 74 |
-
font-size: 1.1em;
|
| 75 |
-
line-height: 2;
|
| 76 |
-
min-height: 200px;
|
| 77 |
-
padding: 1em;
|
| 78 |
-
border: 1px solid #e2e8f0;
|
| 79 |
-
border-radius: 8px;
|
| 80 |
-
margin-top: 1em;
|
| 81 |
-
}
|
| 82 |
-
.title {
|
| 83 |
-
text-align: center;
|
| 84 |
-
font-size: 1.8em;
|
| 85 |
-
margin-bottom: 1em;
|
| 86 |
-
color: #2a4365;
|
| 87 |
-
}
|
| 88 |
-
#chatbot {
|
| 89 |
-
height: 600px;
|
| 90 |
-
overflow-y: auto;
|
| 91 |
-
}
|
| 92 |
-
.message {
|
| 93 |
-
padding: 1em;
|
| 94 |
-
margin: 0.5em;
|
| 95 |
-
border-radius: 8px;
|
| 96 |
-
white-space: normal !important;
|
| 97 |
-
}
|
| 98 |
-
.user-message {
|
| 99 |
-
background-color: #e2e8f0;
|
| 100 |
-
}
|
| 101 |
-
.bot-message {
|
| 102 |
-
background-color: #edf2f7;
|
| 103 |
-
}
|
| 104 |
-
.contains-math {
|
| 105 |
-
white-space: normal !important;
|
| 106 |
-
}
|
| 107 |
-
.math-inline {
|
| 108 |
-
display: inline !important;
|
| 109 |
-
white-space: nowrap !important;
|
| 110 |
-
}
|
| 111 |
-
.gradio-markdown {
|
| 112 |
-
overflow-wrap: normal !important;
|
| 113 |
-
word-break: keep-all !important;
|
| 114 |
-
}
|
| 115 |
-
"""
|
| 116 |
-
|
| 117 |
if __name__ == "__main__":
|
| 118 |
demo.launch()
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import gradio as gr
|
| 3 |
+
from datetime import datetime
|
| 4 |
+
|
| 5 |
+
# Handle notion-client import
|
| 6 |
+
try:
|
| 7 |
+
from notion_client import Client
|
| 8 |
+
except ImportError:
|
| 9 |
+
os.system('pip install notion-client')
|
| 10 |
+
from notion_client import Client
|
| 11 |
+
|
| 12 |
+
# Handle groq import
|
| 13 |
+
try:
|
| 14 |
+
from groq import Groq
|
| 15 |
+
except ImportError:
|
| 16 |
+
os.system('pip install groq')
|
| 17 |
+
from groq import Groq
|
| 18 |
+
|
| 19 |
+
# Initialize Groq client
|
| 20 |
+
client = Groq(api_key=os.getenv('groq_key'))
|
| 21 |
+
|
| 22 |
+
# Initialize Notion client
|
| 23 |
+
notion = Client(auth=os.getenv('NOTION_API_KEY'))
|
| 24 |
+
NOTION_DB_ID = os.getenv('NOTION_DB_ID')
|
| 25 |
+
|
| 26 |
+
# Define custom CSS first
|
| 27 |
+
custom_css = """
|
| 28 |
+
.math-container .katex {
|
| 29 |
+
font-size: 1.1em;
|
| 30 |
+
display: inline-block !important;
|
| 31 |
+
white-space: nowrap !important;
|
| 32 |
+
}
|
| 33 |
+
.input-box {
|
| 34 |
+
font-size: 1.1em;
|
| 35 |
+
}
|
| 36 |
+
.output-box {
|
| 37 |
+
font-size: 1.1em;
|
| 38 |
+
line-height: 2;
|
| 39 |
+
min-height: 200px;
|
| 40 |
+
padding: 1em;
|
| 41 |
+
border: 1px solid #e2e8f0;
|
| 42 |
+
border-radius: 8px;
|
| 43 |
+
margin-top: 1em;
|
| 44 |
+
}
|
| 45 |
+
.title {
|
| 46 |
+
text-align: center;
|
| 47 |
+
font-size: 1.8em;
|
| 48 |
+
margin-bottom: 1em;
|
| 49 |
+
color: #2a4365;
|
| 50 |
+
}
|
| 51 |
+
#chatbot {
|
| 52 |
+
height: 600px;
|
| 53 |
+
overflow-y: auto;
|
| 54 |
+
}
|
| 55 |
+
.message {
|
| 56 |
+
padding: 1em;
|
| 57 |
+
margin: 0.5em;
|
| 58 |
+
border-radius: 8px;
|
| 59 |
+
white-space: normal !important;
|
| 60 |
+
}
|
| 61 |
+
.user-message {
|
| 62 |
+
background-color: #e2e8f0;
|
| 63 |
+
}
|
| 64 |
+
.bot-message {
|
| 65 |
+
background-color: #edf2f7;
|
| 66 |
+
}
|
| 67 |
+
.contains-math {
|
| 68 |
+
white-space: normal !important;
|
| 69 |
+
}
|
| 70 |
+
.math-inline {
|
| 71 |
+
display: inline !important;
|
| 72 |
+
white-space: nowrap !important;
|
| 73 |
+
}
|
| 74 |
+
.gradio-markdown {
|
| 75 |
+
overflow-wrap: normal !important;
|
| 76 |
+
word-break: keep-all !important;
|
| 77 |
+
}
|
| 78 |
+
"""
|
| 79 |
+
|
| 80 |
+
def log_to_notion(name, chinese_term="", user_input="", bot_response=""):
|
| 81 |
+
"""Log interaction to Notion database"""
|
| 82 |
+
try:
|
| 83 |
+
notion.pages.create(
|
| 84 |
+
parent={"database_id": NOTION_DB_ID},
|
| 85 |
+
properties={
|
| 86 |
+
"Name": {"title": [{"text": {"content": name}}]},
|
| 87 |
+
"Timestamp": {"date": {"start": datetime.now().isoformat()}},
|
| 88 |
+
"Chinese Term": {"rich_text": [{"text": {"content": chinese_term}}]},
|
| 89 |
+
"User Input": {"rich_text": [{"text": {"content": user_input}}]},
|
| 90 |
+
"Bot Response": {"rich_text": [{"text": {"content": bot_response}}]}
|
| 91 |
+
}
|
| 92 |
+
)
|
| 93 |
+
except Exception as e:
|
| 94 |
+
print(f"Error logging to Notion: {e}")
|
| 95 |
+
|
| 96 |
+
def translate_to_english(name, word):
|
| 97 |
+
"""Generate math-related English translation with bilingual explanation"""
|
| 98 |
+
messages = [
|
| 99 |
+
{
|
| 100 |
+
"role": "system",
|
| 101 |
+
"content": """你是一個數學翻譯專家。請用以下規則提供翻譯:
|
| 102 |
+
|
| 103 |
+
格式規範:
|
| 104 |
+
1. 文字和數學式必須在同一行,不得換行
|
| 105 |
+
2. 在需要換行處使用 <br> 標記
|
| 106 |
+
3. 所有數學式用 $$...$$ 包覆
|
| 107 |
+
|
| 108 |
+
示例格式:
|
| 109 |
+
對數是指若 $$b^y = x$$,則 $$y$$ 稱為 $$x$$ 以 $$b$$ 為底的對數,記為 $$\log_b x$$。<br>
|
| 110 |
+
換底公式為 $$\log_a x = \frac{\log_c x}{\log_c a}$$。
|
| 111 |
+
|
| 112 |
+
請按此格式提供:
|
| 113 |
+
1. 英文翻譯:[列出數學相關的英文翻譯]<br>
|
| 114 |
+
|
| 115 |
+
2. 中文解釋:[連貫的中文解釋,所有數學式與文字在同一行]<br>
|
| 116 |
+
|
| 117 |
+
3. English Explanation:[連貫的英文解釋,所有數學式與文字在同一行]<br>
|
| 118 |
+
|
| 119 |
+
4. 數學使用場景:[數學使用場景說明,公式與文字在同一行]<br>
|
| 120 |
+
|
| 121 |
+
5. Mathematical Context:[英文場景說明,公式與文字在同一行]"""
|
| 122 |
+
},
|
| 123 |
+
{
|
| 124 |
+
"role": "user",
|
| 125 |
+
"content": f"請提供中文詞彙 '{word}' 在數學上的英文翻譯和詳細解釋。"
|
| 126 |
+
}
|
| 127 |
+
]
|
| 128 |
+
|
| 129 |
+
completion = client.chat.completions.create(
|
| 130 |
+
model="llama-3.3-70b-versatile",
|
| 131 |
+
messages=messages,
|
| 132 |
+
temperature=0.7,
|
| 133 |
+
max_tokens=400,
|
| 134 |
+
stream=False
|
| 135 |
+
)
|
| 136 |
+
response = completion.choices[0].message.content
|
| 137 |
+
|
| 138 |
+
# Log to Notion
|
| 139 |
+
log_to_notion(name=name, chinese_term=word, bot_response=response)
|
| 140 |
+
return response
|
| 141 |
+
|
| 142 |
+
def generate_example(name, word):
|
| 143 |
+
"""Generate math-related example sentence for Chinese word"""
|
| 144 |
+
messages = [
|
| 145 |
+
{
|
| 146 |
+
"role": "system",
|
| 147 |
+
"content": """你是一個數學教師。請用以下規則提供例句:
|
| 148 |
+
|
| 149 |
+
1. 英文例句:[寫出一個數學相關的英文例句]
|
| 150 |
+
- 數學公式必須用 $$...$$ 包覆,並與文字在同一行
|
| 151 |
+
- 例如:The solution of $$\log_2 x = 3$$ is $$x = 8$$<br>
|
| 152 |
+
|
| 153 |
+
2. 中文翻譯:[該例句的中文翻譯]
|
| 154 |
+
- 保持相同的 LaTeX 數學式格式,確保與文字在同一行
|
| 155 |
+
- 例如:方程式 $$\log_2 x = 3$$ 的解為 $$x = 8$$<br>
|
| 156 |
+
|
| 157 |
+
3. 句子解釋:[用中文解釋這個例句中的數學概念]
|
| 158 |
+
- 所有數學符號和公式都需要用 LaTeX 格式,並與文字在同一行
|
| 159 |
+
- 例如:因為 $$2^3 = 8$$,所以 $$\log_2 8 = 3$$<br>
|
| 160 |
+
|
| 161 |
+
4. Sentence Explanation:[用英文重述上述解釋]
|
| 162 |
+
- 保持相同的 LaTeX 數學式格式,確保與文字在同一行"""
|
| 163 |
+
},
|
| 164 |
+
{
|
| 165 |
+
"role": "user",
|
| 166 |
+
"content": f"請用中文詞彙 '{word}' 的英文翻譯造一個數學相關的例句。"
|
| 167 |
+
}
|
| 168 |
+
]
|
| 169 |
+
|
| 170 |
+
completion = client.chat.completions.create(
|
| 171 |
+
model="llama-3.3-70b-versatile",
|
| 172 |
+
messages=messages,
|
| 173 |
+
temperature=0.7,
|
| 174 |
+
max_tokens=400,
|
| 175 |
+
stream=False
|
| 176 |
+
)
|
| 177 |
+
response = completion.choices[0].message.content
|
| 178 |
+
|
| 179 |
+
# Log to Notion
|
| 180 |
+
log_to_notion(name=name, chinese_term=word, bot_response=response)
|
| 181 |
+
return response
|
| 182 |
+
|
| 183 |
+
def chat_response(name, message, chat_history):
|
| 184 |
+
"""Generate response for chatbot"""
|
| 185 |
+
messages = [
|
| 186 |
+
{
|
| 187 |
+
"role": "system",
|
| 188 |
+
"content": """你是一個高中數學老師,使用的語言是英文。學生用中文問妳任何字彙,你都可以告訴他那個中文對應的英文和例句,以及在數學上的可能用法以及數學例題和解法。
|
| 189 |
+
|
| 190 |
+
格式要求:
|
| 191 |
+
1. 所有數學公式都要用 LaTeX 格式書寫(使用 $$...$$ 符號包覆)
|
| 192 |
+
2. 數學式必須與文字在同一行,中間使用 <br> 換行
|
| 193 |
+
3. 變數使用 $$x$$, $$y$$, $$a$$, $$b$$ 等格式
|
| 194 |
+
4. 運算符號使用 $$+$$, $$-$$, $$\times$$, $$\div$$ 等格式
|
| 195 |
+
5. 分數使用 $$\frac{分子}{分母}$$ 格式
|
| 196 |
+
6. 示範解題時,每個步驟的說明文字和數學式要在同一行
|
| 197 |
+
|
| 198 |
+
示例格式:
|
| 199 |
+
讓我們來看一道對數方程式:$$\log_2 x = 3$$<br>
|
| 200 |
+
解題步驟:<br>
|
| 201 |
+
1. 利用指數的定義:$$2^3 = x$$<br>
|
| 202 |
+
2. 計算得到:$$x = 8$$<br>
|
| 203 |
+
因此,方程式 $$\log_2 x = 3$$ 的解為 $$x = 8$$。"""
|
| 204 |
+
}
|
| 205 |
+
]
|
| 206 |
+
|
| 207 |
+
for msg in chat_history:
|
| 208 |
+
messages.append({"role": "user", "content": msg[0]})
|
| 209 |
+
if msg[1]:
|
| 210 |
+
messages.append({"role": "assistant", "content": msg[1]})
|
| 211 |
+
|
| 212 |
+
messages.append({"role": "user", "content": message})
|
| 213 |
+
|
| 214 |
+
completion = client.chat.completions.create(
|
| 215 |
+
model="llama-3.3-70b-versatile",
|
| 216 |
+
messages=messages,
|
| 217 |
+
temperature=1,
|
| 218 |
+
max_tokens=1024,
|
| 219 |
+
stream=False
|
| 220 |
+
)
|
| 221 |
+
response = completion.choices[0].message.content
|
| 222 |
+
|
| 223 |
+
# Log to Notion
|
| 224 |
+
log_to_notion(name=name, user_input=message, bot_response=response)
|
| 225 |
+
return response
|
| 226 |
+
|
| 227 |
+
def respond(name, message, history):
|
| 228 |
+
"""Process chatbot response and update history"""
|
| 229 |
+
response = chat_response(name, message, history)
|
| 230 |
+
history.append((message, response))
|
| 231 |
+
return "", history
|
| 232 |
|
| 233 |
# Create Gradio interface
|
| 234 |
with gr.Blocks(css=custom_css) as demo:
|
|
|
|
| 290 |
clear = gr.ClearButton([msg, chatbot], value="🗑️ 清除對話 | Clear Chat")
|
| 291 |
msg.submit(respond, [name_input, msg, chatbot], [msg, chatbot])
|
| 292 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 293 |
if __name__ == "__main__":
|
| 294 |
demo.launch()
|