Update app.py
Browse files
app.py
CHANGED
|
@@ -1,4 +1,43 @@
|
|
| 1 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
def translate_to_english(name, word):
|
| 4 |
"""Generate math-related English translation with bilingual explanation"""
|
|
@@ -46,7 +85,98 @@ def translate_to_english(name, word):
|
|
| 46 |
log_to_notion(name=name, chinese_term=word, bot_response=response)
|
| 47 |
return response
|
| 48 |
|
| 49 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
custom_css = """
|
| 51 |
.math-container .katex {
|
| 52 |
font-size: 1.1em;
|
|
@@ -129,7 +259,24 @@ with gr.Blocks(css=custom_css) as demo:
|
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 133 |
|
| 134 |
if __name__ == "__main__":
|
| 135 |
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 |
+
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"""
|
|
|
|
| 85 |
log_to_notion(name=name, chinese_term=word, bot_response=response)
|
| 86 |
return response
|
| 87 |
|
| 88 |
+
def generate_example(name, word):
|
| 89 |
+
"""Generate math-related example sentence for Chinese word"""
|
| 90 |
+
messages = [
|
| 91 |
+
{
|
| 92 |
+
"role": "system",
|
| 93 |
+
"content": """你是一個數學教師。請用以下規則提供例句:
|
| 94 |
+
|
| 95 |
+
1. 英文例句:[寫出一個數學相關的英文例句]
|
| 96 |
+
- 數學公式必須用 $$...$$ 包覆,並與文字在同一行
|
| 97 |
+
- 例如:The solution of $$\log_2 x = 3$$ is $$x = 8$$<br>
|
| 98 |
+
|
| 99 |
+
2. 中文翻譯:[該例句的中文翻譯]
|
| 100 |
+
- 保持相同的 LaTeX 數學式格式,確保與文字在同一行
|
| 101 |
+
- 例如:方程式 $$\log_2 x = 3$$ 的解為 $$x = 8$$<br>
|
| 102 |
+
|
| 103 |
+
3. 句子解釋:[用中文解釋這個例句中的數學概念]
|
| 104 |
+
- 所有數學符號和公式都需要用 LaTeX 格式,並與文字在同一行
|
| 105 |
+
- 例如:因為 $$2^3 = 8$$,所以 $$\log_2 8 = 3$$<br>
|
| 106 |
+
|
| 107 |
+
4. Sentence Explanation:[用英文重述上述解釋]
|
| 108 |
+
- 保持相同的 LaTeX 數學式格式,確保與文字在同一行"""
|
| 109 |
+
},
|
| 110 |
+
{
|
| 111 |
+
"role": "user",
|
| 112 |
+
"content": f"請用中文詞彙 '{word}' 的英文翻譯造一個數學相關的例句。"
|
| 113 |
+
}
|
| 114 |
+
]
|
| 115 |
+
|
| 116 |
+
completion = client.chat.completions.create(
|
| 117 |
+
model="llama-3.3-70b-versatile",
|
| 118 |
+
messages=messages,
|
| 119 |
+
temperature=0.7,
|
| 120 |
+
max_tokens=400,
|
| 121 |
+
stream=False
|
| 122 |
+
)
|
| 123 |
+
response = completion.choices[0].message.content
|
| 124 |
+
|
| 125 |
+
# Log to Notion
|
| 126 |
+
log_to_notion(name=name, chinese_term=word, bot_response=response)
|
| 127 |
+
return response
|
| 128 |
+
|
| 129 |
+
def chat_response(name, message, chat_history):
|
| 130 |
+
"""Generate response for chatbot"""
|
| 131 |
+
messages = [
|
| 132 |
+
{
|
| 133 |
+
"role": "system",
|
| 134 |
+
"content": """你是一個高中數學老師,使用的語言是英文。學生用中文問妳任何字彙,你都可以告訴他那個中文對應的英文和例句,以及在數學上的可能用法以及數學例題和解法。
|
| 135 |
+
|
| 136 |
+
格式要求:
|
| 137 |
+
1. 所有數學公式都要用 LaTeX 格式書寫(使用 $$...$$ 符號包覆)
|
| 138 |
+
2. 數學式必須與文字在同一行,中間使用 <br> 換行
|
| 139 |
+
3. 變數使用 $$x$$, $$y$$, $$a$$, $$b$$ 等格式
|
| 140 |
+
4. 運算符號使用 $$+$$, $$-$$, $$\times$$, $$\div$$ 等格式
|
| 141 |
+
5. 分數使用 $$\frac{分子}{分母}$$ 格式
|
| 142 |
+
6. 示範解題時,每個步驟的說明文字和數學式要在同一行
|
| 143 |
+
|
| 144 |
+
示例格式:
|
| 145 |
+
讓我們來看一道對數方程式:$$\log_2 x = 3$$<br>
|
| 146 |
+
解題步驟:<br>
|
| 147 |
+
1. 利用指數的定義:$$2^3 = x$$<br>
|
| 148 |
+
2. 計算得到:$$x = 8$$<br>
|
| 149 |
+
因此,方程式 $$\log_2 x = 3$$ 的解為 $$x = 8$$。"""
|
| 150 |
+
}
|
| 151 |
+
]
|
| 152 |
+
|
| 153 |
+
for msg in chat_history:
|
| 154 |
+
messages.append({"role": "user", "content": msg[0]})
|
| 155 |
+
if msg[1]:
|
| 156 |
+
messages.append({"role": "assistant", "content": msg[1]})
|
| 157 |
+
|
| 158 |
+
messages.append({"role": "user", "content": message})
|
| 159 |
+
|
| 160 |
+
completion = client.chat.completions.create(
|
| 161 |
+
model="llama-3.3-70b-versatile",
|
| 162 |
+
messages=messages,
|
| 163 |
+
temperature=1,
|
| 164 |
+
max_tokens=1024,
|
| 165 |
+
stream=False
|
| 166 |
+
)
|
| 167 |
+
response = completion.choices[0].message.content
|
| 168 |
+
|
| 169 |
+
# Log to Notion
|
| 170 |
+
log_to_notion(name=name, user_input=message, bot_response=response)
|
| 171 |
+
return response
|
| 172 |
+
|
| 173 |
+
def respond(name, message, history):
|
| 174 |
+
"""Process chatbot response and update history"""
|
| 175 |
+
response = chat_response(name, message, history)
|
| 176 |
+
history.append((message, response))
|
| 177 |
+
return "", history
|
| 178 |
+
|
| 179 |
+
# Custom CSS for math rendering and layout
|
| 180 |
custom_css = """
|
| 181 |
.math-container .katex {
|
| 182 |
font-size: 1.1em;
|
|
|
|
| 259 |
translate_btn.click(translate_to_english, inputs=[name_input, word_input], outputs=output_text)
|
| 260 |
example_btn.click(generate_example, inputs=[name_input, word_input], outputs=output_text)
|
| 261 |
|
| 262 |
+
with gr.Tab("💬 數學對話系統"):
|
| 263 |
+
chatbot = gr.Chatbot(
|
| 264 |
+
[],
|
| 265 |
+
elem_id="chatbot",
|
| 266 |
+
bubble_full_width=False,
|
| 267 |
+
avatar_images=("👨🎓", "👨🏫"),
|
| 268 |
+
elem_classes=["math-container", "contains-math"]
|
| 269 |
+
)
|
| 270 |
+
|
| 271 |
+
msg = gr.Textbox(
|
| 272 |
+
label="發送訊息 | Send Message",
|
| 273 |
+
placeholder="請輸入您的問題... | Enter your question...",
|
| 274 |
+
show_label=False,
|
| 275 |
+
elem_classes=["input-box"]
|
| 276 |
+
)
|
| 277 |
+
|
| 278 |
+
clear = gr.ClearButton([msg, chatbot], value="🗑️ 清除對話 | Clear Chat")
|
| 279 |
+
msg.submit(respond, [name_input, msg, chatbot], [msg, chatbot])
|
| 280 |
|
| 281 |
if __name__ == "__main__":
|
| 282 |
demo.launch()
|