Update app.py
Browse files
app.py
CHANGED
|
@@ -1,35 +1,34 @@
|
|
| 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
|
| 31 |
white-space: nowrap !important;
|
| 32 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
.input-box {
|
| 34 |
font-size: 1.1em;
|
| 35 |
}
|
|
@@ -41,6 +40,7 @@ custom_css = """
|
|
| 41 |
border: 1px solid #e2e8f0;
|
| 42 |
border-radius: 8px;
|
| 43 |
margin-top: 1em;
|
|
|
|
| 44 |
}
|
| 45 |
.title {
|
| 46 |
text-align: center;
|
|
@@ -78,7 +78,6 @@ custom_css = """
|
|
| 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},
|
|
@@ -93,32 +92,44 @@ def log_to_notion(name, chinese_term="", user_input="", bot_response=""):
|
|
| 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 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 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",
|
|
@@ -134,32 +145,25 @@ def translate_to_english(name, word):
|
|
| 134 |
stream=False
|
| 135 |
)
|
| 136 |
response = completion.choices[0].message.content
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 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 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
|
| 161 |
-
|
| 162 |
-
|
|
|
|
|
|
|
|
|
|
| 163 |
},
|
| 164 |
{
|
| 165 |
"role": "user",
|
|
@@ -175,32 +179,23 @@ def generate_example(name, word):
|
|
| 175 |
stream=False
|
| 176 |
)
|
| 177 |
response = completion.choices[0].message.content
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 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 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 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 |
|
|
@@ -219,22 +214,18 @@ def chat_response(name, message, chat_history):
|
|
| 219 |
stream=False
|
| 220 |
)
|
| 221 |
response = completion.choices[0].message.content
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
| 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:
|
| 235 |
gr.Markdown("# 雙語數學詞彙學習系統 | Bilingual Mathematics Learning System", elem_classes=["title"])
|
| 236 |
-
|
| 237 |
-
# Add name input at the top
|
| 238 |
name_input = gr.Textbox(
|
| 239 |
label="請輸入您的名字 | Enter Your Name",
|
| 240 |
placeholder="請輸入您的名字... | Please enter your name...",
|
|
|
|
| 1 |
import os
|
| 2 |
+
import gradio as gr
|
| 3 |
from datetime import datetime
|
| 4 |
|
|
|
|
| 5 |
try:
|
| 6 |
from notion_client import Client
|
| 7 |
except ImportError:
|
| 8 |
os.system('pip install notion-client')
|
| 9 |
from notion_client import Client
|
| 10 |
|
|
|
|
| 11 |
try:
|
| 12 |
from groq import Groq
|
| 13 |
except ImportError:
|
| 14 |
os.system('pip install groq')
|
| 15 |
from groq import Groq
|
| 16 |
|
|
|
|
| 17 |
client = Groq(api_key=os.getenv('groq_key'))
|
|
|
|
|
|
|
| 18 |
notion = Client(auth=os.getenv('NOTION_API_KEY'))
|
| 19 |
NOTION_DB_ID = os.getenv('NOTION_DB_ID')
|
| 20 |
|
|
|
|
| 21 |
custom_css = """
|
| 22 |
.math-container .katex {
|
| 23 |
font-size: 1.1em;
|
| 24 |
+
display: inline !important;
|
| 25 |
white-space: nowrap !important;
|
| 26 |
}
|
| 27 |
+
.math-container p {
|
| 28 |
+
display: inline-block !important;
|
| 29 |
+
width: 100% !important;
|
| 30 |
+
white-space: normal !important;
|
| 31 |
+
}
|
| 32 |
.input-box {
|
| 33 |
font-size: 1.1em;
|
| 34 |
}
|
|
|
|
| 40 |
border: 1px solid #e2e8f0;
|
| 41 |
border-radius: 8px;
|
| 42 |
margin-top: 1em;
|
| 43 |
+
white-space: normal !important;
|
| 44 |
}
|
| 45 |
.title {
|
| 46 |
text-align: center;
|
|
|
|
| 78 |
"""
|
| 79 |
|
| 80 |
def log_to_notion(name, chinese_term="", user_input="", bot_response=""):
|
|
|
|
| 81 |
try:
|
| 82 |
notion.pages.create(
|
| 83 |
parent={"database_id": NOTION_DB_ID},
|
|
|
|
| 92 |
except Exception as e:
|
| 93 |
print(f"Error logging to Notion: {e}")
|
| 94 |
|
| 95 |
+
def format_math_response(response):
|
| 96 |
+
response = '\n\n'.join(line.strip() for line in response.split('\n') if line.strip())
|
| 97 |
+
|
| 98 |
+
in_math = False
|
| 99 |
+
formatted_lines = []
|
| 100 |
+
current_line = []
|
| 101 |
+
|
| 102 |
+
for char in response:
|
| 103 |
+
if char == '$' and (len(current_line) == 0 or current_line[-1] != '\\'):
|
| 104 |
+
in_math = not in_math
|
| 105 |
+
current_line.append(char)
|
| 106 |
+
elif char == '\n' and not in_math:
|
| 107 |
+
formatted_lines.append(''.join(current_line))
|
| 108 |
+
current_line = []
|
| 109 |
+
else:
|
| 110 |
+
current_line.append(char)
|
| 111 |
+
|
| 112 |
+
if current_line:
|
| 113 |
+
formatted_lines.append(''.join(current_line))
|
| 114 |
+
|
| 115 |
+
return '<br>'.join(formatted_lines)
|
| 116 |
+
|
| 117 |
def translate_to_english(name, word):
|
|
|
|
| 118 |
messages = [
|
| 119 |
{
|
| 120 |
"role": "system",
|
| 121 |
+
"content": """你是一個數學翻譯專家。請統一用一行文字回應,數學式與文字在同一行,不可斷行。用以下規則提供翻譯:
|
| 122 |
+
|
| 123 |
+
1. 所有數學式用 $$...$$ 包覆
|
| 124 |
+
2. 用 <br> 分隔不同段落
|
| 125 |
+
3. 確保數學式前後不會斷行
|
| 126 |
+
|
| 127 |
+
回應格式:
|
| 128 |
+
1. 英文翻譯:[英文翻譯,數學式與文字在同一行]<br>
|
| 129 |
+
2. 中文解釋:[中文解釋,數學式與文字在同一行]<br>
|
| 130 |
+
3. English Explanation:[英文解釋,數學式與文字在同一行]<br>
|
| 131 |
+
4. 數學使用場景:[數學使用場景,數學式與文字在同一行]<br>
|
| 132 |
+
5. Mathematical Context:[英文場景說明,數學式與文字在同一行]"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 133 |
},
|
| 134 |
{
|
| 135 |
"role": "user",
|
|
|
|
| 145 |
stream=False
|
| 146 |
)
|
| 147 |
response = completion.choices[0].message.content
|
| 148 |
+
formatted_response = format_math_response(response)
|
| 149 |
+
log_to_notion(name=name, chinese_term=word, bot_response=formatted_response)
|
| 150 |
+
return formatted_response
|
|
|
|
| 151 |
|
| 152 |
def generate_example(name, word):
|
|
|
|
| 153 |
messages = [
|
| 154 |
{
|
| 155 |
"role": "system",
|
| 156 |
+
"content": """你是一個數學教師。請統一用一行文字回應,確保數學式與文字在同一行。用以下規則提供例句:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 157 |
|
| 158 |
+
1. 所有數學式用 $$...$$ 包覆
|
| 159 |
+
2. 用 <br> 分隔不同段落
|
| 160 |
+
3. 確保數學式前後不會斷行
|
| 161 |
|
| 162 |
+
回應格式:
|
| 163 |
+
1. 英文例句:[英文例句,數學式與文字在同一行]<br>
|
| 164 |
+
2. 中文翻譯:[中文翻譯,數學式與文字在同一行]<br>
|
| 165 |
+
3. 句子解釋:[中文解釋,數學式與文字在同一行]<br>
|
| 166 |
+
4. Sentence Explanation:[英文解釋,數學式與文字在同一行]"""
|
| 167 |
},
|
| 168 |
{
|
| 169 |
"role": "user",
|
|
|
|
| 179 |
stream=False
|
| 180 |
)
|
| 181 |
response = completion.choices[0].message.content
|
| 182 |
+
formatted_response = format_math_response(response)
|
| 183 |
+
log_to_notion(name=name, chinese_term=word, bot_response=formatted_response)
|
| 184 |
+
return formatted_response
|
|
|
|
| 185 |
|
| 186 |
def chat_response(name, message, chat_history):
|
|
|
|
| 187 |
messages = [
|
| 188 |
{
|
| 189 |
"role": "system",
|
| 190 |
+
"content": """你是一個高中數學老師,使用英文回應。請確保:
|
| 191 |
+
|
| 192 |
+
1. 所有數學式用 $$...$$ 包覆
|
| 193 |
+
2. 數學式必須與文字在同一行
|
| 194 |
+
3. 用 <br> 分隔不同段落
|
| 195 |
+
4. 變數使用 $$x$$, $$y$$ 等格式
|
| 196 |
+
5. 運算符號使用 $$+$$, $$-$$, $$\times$$, $$\div$$ 等格式
|
| 197 |
+
6. 分數使用 $$\frac{分子}{分母}$$ 格式
|
| 198 |
+
7. 每個步驟的說明文字和數學式在同一行"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 199 |
}
|
| 200 |
]
|
| 201 |
|
|
|
|
| 214 |
stream=False
|
| 215 |
)
|
| 216 |
response = completion.choices[0].message.content
|
| 217 |
+
formatted_response = format_math_response(response)
|
| 218 |
+
log_to_notion(name=name, user_input=message, bot_response=formatted_response)
|
| 219 |
+
return formatted_response
|
|
|
|
| 220 |
|
| 221 |
def respond(name, message, history):
|
|
|
|
| 222 |
response = chat_response(name, message, history)
|
| 223 |
history.append((message, response))
|
| 224 |
return "", history
|
| 225 |
|
|
|
|
| 226 |
with gr.Blocks(css=custom_css) as demo:
|
| 227 |
gr.Markdown("# 雙語數學詞彙學習系統 | Bilingual Mathematics Learning System", elem_classes=["title"])
|
| 228 |
+
|
|
|
|
| 229 |
name_input = gr.Textbox(
|
| 230 |
label="請輸入您的名字 | Enter Your Name",
|
| 231 |
placeholder="請輸入您的名字... | Please enter your name...",
|