Update app.py
Browse files
app.py
CHANGED
|
@@ -1,135 +1,35 @@
|
|
| 1 |
-
|
| 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 |
-
def log_to_notion(name, chinese_term="", user_input="", bot_response=""):
|
| 27 |
-
"""Log interaction to Notion database"""
|
| 28 |
-
try:
|
| 29 |
-
notion.pages.create(
|
| 30 |
-
parent={"database_id": NOTION_DB_ID},
|
| 31 |
-
properties={
|
| 32 |
-
"Name": {"title": [{"text": {"content": name}}]},
|
| 33 |
-
"Timestamp": {"date": {"start": datetime.now().isoformat()}},
|
| 34 |
-
"Chinese Term": {"rich_text": [{"text": {"content": chinese_term}}]},
|
| 35 |
-
"User Input": {"rich_text": [{"text": {"content": user_input}}]},
|
| 36 |
-
"Bot Response": {"rich_text": [{"text": {"content": bot_response}}]}
|
| 37 |
-
}
|
| 38 |
-
)
|
| 39 |
-
except Exception as e:
|
| 40 |
-
print(f"Error logging to Notion: {e}")
|
| 41 |
|
| 42 |
def translate_to_english(name, word):
|
| 43 |
"""Generate math-related English translation with bilingual explanation"""
|
| 44 |
messages = [
|
| 45 |
{
|
| 46 |
"role": "system",
|
| 47 |
-
"content": """
|
| 48 |
-
|
| 49 |
-
1. 英文翻譯:[列出數學相關的英文翻譯]
|
| 50 |
-
|
| 51 |
-
2. 中文解釋:[用中文詳細解釋這個詞在數學上的意義]
|
| 52 |
-
- 在解釋中的數學式要用 LaTeX 格式,並且確保數學式與文字在同一行
|
| 53 |
-
- 正確範例:若 $$b^y = x$$,則 $$y$$ 稱為 $$x$$ 以 $$b$$ 為底的對數,記為 $$\log_b x$$
|
| 54 |
-
- 變數請使用 $$x$$, $$y$$, $$a$$, $$b$$ 等格式
|
| 55 |
-
- 運算符號請用 $$+$$, $$-$$, $$\times$$, $$\div$$ 等格式
|
| 56 |
-
- 不要在數學式前後使用換行
|
| 57 |
-
- 如果需要換行,確保完整的數學表達式在同一行
|
| 58 |
-
|
| 59 |
-
3. English Explanation:[用英文重述上述中文解釋的內容]
|
| 60 |
-
- 使用相同的 LaTeX 格式來表示數學式
|
| 61 |
-
- 例如:If $$b^y = x$$, then $$y$$ is called the logarithm of $$x$$ with base $$b$$, denoted as $$\log_b x$$
|
| 62 |
-
- 保持數學式與周圍文字在同一行
|
| 63 |
-
|
| 64 |
-
4. 數學使用場景:[說明在哪些數學情境會使用到這個詞]
|
| 65 |
-
- 包含具體的數學公式例子,確保公式與文字在同一行
|
| 66 |
-
- 例如:換底公式為 $$\log_a x = \frac{\log_c x}{\log_c a}$$,這個公式在計算時很有用
|
| 67 |
-
|
| 68 |
-
5. Mathematical Context:[用英文重述使用場景]
|
| 69 |
-
- 保持相同的 LaTeX 數學式格式
|
| 70 |
-
- 確保公式與文字的連續性
|
| 71 |
-
|
| 72 |
-
格式注意事項:
|
| 73 |
-
1. 所有數學符號、變數和公式都必須用 $$...$$ 包覆
|
| 74 |
-
2. 數學式不應該單獨佔一行,應該與說明文字在同一行
|
| 75 |
-
3. 避免在數學式前後使用換行符
|
| 76 |
-
4. 較長的句子可以換行,但要確保每個數學式都完整地在同一行內
|
| 77 |
-
5. 如果有公式推導,相關的公式應該保持在同一行"""
|
| 78 |
-
},
|
| 79 |
-
{
|
| 80 |
-
"role": "user",
|
| 81 |
-
"content": f"請提供中文詞彙 '{word}' 在數學上的英文翻譯和詳細解釋。"
|
| 82 |
-
}
|
| 83 |
-
]
|
| 84 |
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
max_tokens=400,
|
| 90 |
-
stream=False
|
| 91 |
-
)
|
| 92 |
-
response = completion.choices[0].message.content
|
| 93 |
-
|
| 94 |
-
# Log to Notion
|
| 95 |
-
log_to_notion(name=name, chinese_term=word, bot_response=response)
|
| 96 |
-
return response
|
| 97 |
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
{
|
| 102 |
-
"role": "system",
|
| 103 |
-
"content": """你是一個數學教師。請按照以下格式提供例句:
|
| 104 |
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
- 例如:The equation $$\log_2 x = 3$$ has the solution $$x = 8$$
|
| 108 |
|
| 109 |
-
2.
|
| 110 |
-
- 保持相同的 LaTeX 數學式格式
|
| 111 |
-
- 確保數學式與文字在同一行
|
| 112 |
-
- 例如:方程式 $$\log_2 x = 3$$ 的解為 $$x = 8$$
|
| 113 |
|
| 114 |
-
3.
|
| 115 |
-
- 所有數學符號和公式都需要用 LaTeX 格式,並與文字在同一行
|
| 116 |
-
- 例如:因為 $$2^3 = 8$$,所以我們可以說 $$\log_2 8 = 3$$
|
| 117 |
|
| 118 |
-
4.
|
| 119 |
-
- 保持相同的 LaTeX 數學式格式
|
| 120 |
-
- 確保所有數學式與文字在同一行
|
| 121 |
|
| 122 |
-
|
| 123 |
-
1. 所有數學符號、變數和公式都必須用 $$...$$ 包覆
|
| 124 |
-
2. 數學式必須與說明文字在同一行,不能單獨成行
|
| 125 |
-
3. 使用 $$\times$$ 代替 * 號
|
| 126 |
-
4. 使用 $$\div$$ 代替 / 號
|
| 127 |
-
5. 分數使用 $$\frac{分子}{分母}$$ 格式
|
| 128 |
-
6. 避免在數學式前後使用換行"""
|
| 129 |
},
|
| 130 |
{
|
| 131 |
"role": "user",
|
| 132 |
-
"content": f"
|
| 133 |
}
|
| 134 |
]
|
| 135 |
|
|
@@ -146,67 +46,19 @@ def generate_example(name, word):
|
|
| 146 |
log_to_notion(name=name, chinese_term=word, bot_response=response)
|
| 147 |
return response
|
| 148 |
|
| 149 |
-
|
| 150 |
-
"""Generate response for chatbot"""
|
| 151 |
-
messages = [
|
| 152 |
-
{
|
| 153 |
-
"role": "system",
|
| 154 |
-
"content": """你是一個高中數學老師,使用的語言是英文。學生用中文問妳任何字彙,你都可以告訴他那個中文對應的英文和例句,以及在數學上的可能用法以及數學例題和解法。
|
| 155 |
-
說明數學上的可能用法時,先用中文講一遍再用B1程度的英文複述一遍。
|
| 156 |
-
|
| 157 |
-
格式要求:
|
| 158 |
-
1. 所有數學公式都要用 LaTeX 格式書寫(使用 $$...$$ 符號包覆)
|
| 159 |
-
2. 數學式必須與文字在同一行,不能單獨成行
|
| 160 |
-
3. 變數使用 $$x$$, $$y$$, $$a$$, $$b$$ 等格式
|
| 161 |
-
4. 運算符號使用 $$+$$, $$-$$, $$\times$$, $$\div$$ 等格式
|
| 162 |
-
5. 分數使用 $$\frac{分子}{分母}$$ 格式
|
| 163 |
-
6. 示範解題時:
|
| 164 |
-
- 每個步驟的說明文字和數學式要在同一行
|
| 165 |
-
- 不要在數學式前後使用換行
|
| 166 |
-
- 確保完整的數學表達式在同一行內
|
| 167 |
-
7. 回答要有條理,適時使用換行來分隔不同段落,但確保每個段落中的數學式不被斷開"""
|
| 168 |
-
}
|
| 169 |
-
]
|
| 170 |
-
|
| 171 |
-
for msg in chat_history:
|
| 172 |
-
messages.append({"role": "user", "content": msg[0]})
|
| 173 |
-
if msg[1]:
|
| 174 |
-
messages.append({"role": "assistant", "content": msg[1]})
|
| 175 |
-
|
| 176 |
-
messages.append({"role": "user", "content": message})
|
| 177 |
-
|
| 178 |
-
completion = client.chat.completions.create(
|
| 179 |
-
model="llama-3.3-70b-versatile",
|
| 180 |
-
messages=messages,
|
| 181 |
-
temperature=1,
|
| 182 |
-
max_tokens=1024,
|
| 183 |
-
stream=False
|
| 184 |
-
)
|
| 185 |
-
response = completion.choices[0].message.content
|
| 186 |
-
|
| 187 |
-
# Log to Notion
|
| 188 |
-
log_to_notion(name=name, user_input=message, bot_response=response)
|
| 189 |
-
return response
|
| 190 |
-
|
| 191 |
-
def respond(name, message, history):
|
| 192 |
-
"""Process chatbot response and update history"""
|
| 193 |
-
response = chat_response(name, message, history)
|
| 194 |
-
history.append((message, response))
|
| 195 |
-
return "", history
|
| 196 |
-
|
| 197 |
-
# Custom CSS for math rendering and layout
|
| 198 |
custom_css = """
|
| 199 |
.math-container .katex {
|
| 200 |
font-size: 1.1em;
|
| 201 |
-
|
|
|
|
| 202 |
}
|
| 203 |
.input-box {
|
| 204 |
font-size: 1.1em;
|
| 205 |
}
|
| 206 |
.output-box {
|
| 207 |
font-size: 1.1em;
|
| 208 |
-
line-height:
|
| 209 |
-
white-space: pre-line;
|
| 210 |
}
|
| 211 |
.title {
|
| 212 |
text-align: center;
|
|
@@ -222,6 +74,7 @@ custom_css = """
|
|
| 222 |
padding: 1em;
|
| 223 |
margin: 0.5em;
|
| 224 |
border-radius: 8px;
|
|
|
|
| 225 |
}
|
| 226 |
.user-message {
|
| 227 |
background-color: #e2e8f0;
|
|
@@ -233,8 +86,12 @@ custom_css = """
|
|
| 233 |
white-space: normal !important;
|
| 234 |
}
|
| 235 |
.math-inline {
|
| 236 |
-
display: inline
|
| 237 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 238 |
}
|
| 239 |
"""
|
| 240 |
|
|
@@ -266,30 +123,13 @@ with gr.Blocks(css=custom_css) as demo:
|
|
| 266 |
output_text = gr.Markdown(
|
| 267 |
label="翻譯結果 | Translation Result",
|
| 268 |
value="翻譯結果將在這裡顯示... | Translation results will be displayed here...",
|
| 269 |
-
elem_classes=["output-box", "math-container", "contains-math"]
|
| 270 |
)
|
| 271 |
|
| 272 |
translate_btn.click(translate_to_english, inputs=[name_input, word_input], outputs=output_text)
|
| 273 |
example_btn.click(generate_example, inputs=[name_input, word_input], outputs=output_text)
|
| 274 |
|
| 275 |
-
|
| 276 |
-
chatbot = gr.Chatbot(
|
| 277 |
-
[],
|
| 278 |
-
elem_id="chatbot",
|
| 279 |
-
bubble_full_width=False,
|
| 280 |
-
avatar_images=("👨🎓", "��🏫"),
|
| 281 |
-
elem_classes=["math-container", "contains-math"]
|
| 282 |
-
)
|
| 283 |
-
|
| 284 |
-
msg = gr.Textbox(
|
| 285 |
-
label="發送訊息 | Send Message",
|
| 286 |
-
placeholder="請輸入您的問題... | Enter your question...",
|
| 287 |
-
show_label=False,
|
| 288 |
-
elem_classes=["input-box"]
|
| 289 |
-
)
|
| 290 |
-
|
| 291 |
-
clear = gr.ClearButton([msg, chatbot], value="🗑️ 清除對話 | Clear Chat")
|
| 292 |
-
msg.submit(respond, [name_input, msg, chatbot], [msg, chatbot])
|
| 293 |
|
| 294 |
if __name__ == "__main__":
|
| 295 |
demo.launch()
|
|
|
|
| 1 |
+
# [Previous imports and initialization code remains the same...]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
def translate_to_english(name, word):
|
| 4 |
"""Generate math-related English translation with bilingual explanation"""
|
| 5 |
messages = [
|
| 6 |
{
|
| 7 |
"role": "system",
|
| 8 |
+
"content": """你是一個數學翻譯專家。請用以下規則提供翻譯:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
+
格式規範:
|
| 11 |
+
1. 文字和數學式必須在同一行,不得換行
|
| 12 |
+
2. 在需要換行處使用 <br> 標記
|
| 13 |
+
3. 所有數學式用 $$...$$ 包覆
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
+
示例格式:
|
| 16 |
+
對數是指若 $$b^y = x$$,則 $$y$$ 稱為 $$x$$ 以 $$b$$ 為底的對數,記為 $$\log_b x$$。<br>
|
| 17 |
+
換底公式為 $$\log_a x = \frac{\log_c x}{\log_c a}$$。
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
+
請按此格式提供:
|
| 20 |
+
1. 英文翻譯:[列出數學相關的英文翻譯]<br>
|
|
|
|
| 21 |
|
| 22 |
+
2. 中文解釋:[連貫的中文解釋,所有數學式與文字在同一行]<br>
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
+
3. English Explanation:[連貫的英文解釋,所有數學式與文字在同一行]<br>
|
|
|
|
|
|
|
| 25 |
|
| 26 |
+
4. 數學使用場景:[數學使用場景說明,公式與文字在同一行]<br>
|
|
|
|
|
|
|
| 27 |
|
| 28 |
+
5. Mathematical Context:[英文場景說明,公式與文字在同一行]"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
},
|
| 30 |
{
|
| 31 |
"role": "user",
|
| 32 |
+
"content": f"請提供中文詞彙 '{word}' 在數學上的英文翻譯和詳細解釋。"
|
| 33 |
}
|
| 34 |
]
|
| 35 |
|
|
|
|
| 46 |
log_to_notion(name=name, chinese_term=word, bot_response=response)
|
| 47 |
return response
|
| 48 |
|
| 49 |
+
# 更新 CSS 樣式
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
custom_css = """
|
| 51 |
.math-container .katex {
|
| 52 |
font-size: 1.1em;
|
| 53 |
+
display: inline-block !important;
|
| 54 |
+
white-space: nowrap !important;
|
| 55 |
}
|
| 56 |
.input-box {
|
| 57 |
font-size: 1.1em;
|
| 58 |
}
|
| 59 |
.output-box {
|
| 60 |
font-size: 1.1em;
|
| 61 |
+
line-height: 2;
|
|
|
|
| 62 |
}
|
| 63 |
.title {
|
| 64 |
text-align: center;
|
|
|
|
| 74 |
padding: 1em;
|
| 75 |
margin: 0.5em;
|
| 76 |
border-radius: 8px;
|
| 77 |
+
white-space: normal !important;
|
| 78 |
}
|
| 79 |
.user-message {
|
| 80 |
background-color: #e2e8f0;
|
|
|
|
| 86 |
white-space: normal !important;
|
| 87 |
}
|
| 88 |
.math-inline {
|
| 89 |
+
display: inline !important;
|
| 90 |
+
white-space: nowrap !important;
|
| 91 |
+
}
|
| 92 |
+
.gradio-markdown {
|
| 93 |
+
overflow-wrap: normal !important;
|
| 94 |
+
word-break: keep-all !important;
|
| 95 |
}
|
| 96 |
"""
|
| 97 |
|
|
|
|
| 123 |
output_text = gr.Markdown(
|
| 124 |
label="翻譯結果 | Translation Result",
|
| 125 |
value="翻譯結果將在這裡顯示... | Translation results will be displayed here...",
|
| 126 |
+
elem_classes=["output-box", "math-container", "contains-math", "gradio-markdown"]
|
| 127 |
)
|
| 128 |
|
| 129 |
translate_btn.click(translate_to_english, inputs=[name_input, word_input], outputs=output_text)
|
| 130 |
example_btn.click(generate_example, inputs=[name_input, word_input], outputs=output_text)
|
| 131 |
|
| 132 |
+
# [Rest of the interface code remains the same...]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 133 |
|
| 134 |
if __name__ == "__main__":
|
| 135 |
demo.launch()
|