Update app.py
Browse files
app.py
CHANGED
|
@@ -1,13 +1,19 @@
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
from datetime import datetime
|
| 4 |
-
|
| 5 |
try:
|
| 6 |
from groq import Groq
|
| 7 |
except ImportError:
|
| 8 |
os.system('pip install groq')
|
| 9 |
from groq import Groq
|
| 10 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
# Initialize Groq client
|
| 12 |
client = Groq(api_key=os.getenv('groq_key'))
|
| 13 |
|
|
@@ -17,87 +23,62 @@ NOTION_DB_ID = os.getenv('NOTION_DB_ID')
|
|
| 17 |
|
| 18 |
def log_to_notion(name, user_input, bot_response):
|
| 19 |
"""Log conversation to Notion database"""
|
|
|
|
|
|
|
|
|
|
| 20 |
try:
|
| 21 |
notion.pages.create(
|
| 22 |
parent={"database_id": NOTION_DB_ID},
|
| 23 |
properties={
|
| 24 |
-
"Name": {
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
}
|
| 29 |
)
|
|
|
|
| 30 |
except Exception as e:
|
| 31 |
print(f"Error logging to Notion: {e}")
|
|
|
|
| 32 |
|
| 33 |
-
def
|
| 34 |
-
"""Generate math-related English translation with bilingual explanation"""
|
| 35 |
-
# [Previous implementation remains the same]
|
| 36 |
-
messages = [
|
| 37 |
-
{
|
| 38 |
-
"role": "system",
|
| 39 |
-
"content": """你是一個數學翻譯專家。請按照以下格式提供翻譯:
|
| 40 |
-
|
| 41 |
-
1. 英文翻譯:[列出數學相關的英文翻譯]
|
| 42 |
-
2. 中文解釋:[用中文詳細解釋這個詞在數學上的意義]
|
| 43 |
-
3. English Explanation:[用英文重述上述中文解釋的內容]
|
| 44 |
-
4. 數學使用場景:[說明在哪些數學情境會使用到這個詞]
|
| 45 |
-
5. Mathematical Context:[用英文重述使用場景]
|
| 46 |
-
|
| 47 |
-
如果該詞彙沒有數學相關的意思,請說明。"""
|
| 48 |
-
},
|
| 49 |
-
{
|
| 50 |
-
"role": "user",
|
| 51 |
-
"content": f"請提供中文詞彙 '{word}' 在數學上的英文翻譯和詳細解釋。"
|
| 52 |
-
}
|
| 53 |
-
]
|
| 54 |
-
|
| 55 |
-
completion = client.chat.completions.create(
|
| 56 |
-
model="llama-3.3-70b-versatile",
|
| 57 |
-
messages=messages,
|
| 58 |
-
temperature=0.7,
|
| 59 |
-
max_tokens=400,
|
| 60 |
-
stream=False
|
| 61 |
-
)
|
| 62 |
-
|
| 63 |
-
return completion.choices[0].message.content
|
| 64 |
-
|
| 65 |
-
def generate_example(word):
|
| 66 |
-
"""Generate math-related example sentence for Chinese word"""
|
| 67 |
-
# [Previous implementation remains the same]
|
| 68 |
-
messages = [
|
| 69 |
-
{
|
| 70 |
-
"role": "system",
|
| 71 |
-
"content": """你是一個數學教師。請按照以下格式提供例句:
|
| 72 |
-
|
| 73 |
-
1. 英文例句:[寫出一個數學相關的英文例句]
|
| 74 |
-
2. 中文翻譯:[該例句的中文翻譯]
|
| 75 |
-
3. 句子解釋:[用中文解釋這個例句中的數學概念]
|
| 76 |
-
4. Sentence Explanation:[用英文重述上述解釋]"""
|
| 77 |
-
},
|
| 78 |
-
{
|
| 79 |
-
"role": "user",
|
| 80 |
-
"content": f"請用中文詞彙 '{word}' 的英文翻譯造一個數學相關的例句。"
|
| 81 |
-
}
|
| 82 |
-
]
|
| 83 |
-
|
| 84 |
-
completion = client.chat.completions.create(
|
| 85 |
-
model="llama-3.3-70b-versatile",
|
| 86 |
-
messages=messages,
|
| 87 |
-
temperature=0.7,
|
| 88 |
-
max_tokens=400,
|
| 89 |
-
stream=False
|
| 90 |
-
)
|
| 91 |
-
|
| 92 |
-
return completion.choices[0].message.content
|
| 93 |
-
|
| 94 |
-
def chat_response(message, chat_history):
|
| 95 |
"""Generate response for chatbot"""
|
| 96 |
-
# [Previous implementation remains the same]
|
| 97 |
messages = [
|
| 98 |
{
|
| 99 |
"role": "system",
|
| 100 |
-
"content": "
|
| 101 |
}
|
| 102 |
]
|
| 103 |
|
|
@@ -116,84 +97,36 @@ def chat_response(message, chat_history):
|
|
| 116 |
stream=False
|
| 117 |
)
|
| 118 |
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
def respond(message, name, history):
|
| 122 |
-
"""Process chatbot response and update history"""
|
| 123 |
-
response = chat_response(message, history)
|
| 124 |
|
| 125 |
# Log to Notion
|
| 126 |
-
|
| 127 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 128 |
|
|
|
|
| 129 |
history.append((message, response))
|
| 130 |
return "", history
|
| 131 |
|
| 132 |
-
#
|
| 133 |
-
custom_css = """
|
| 134 |
-
#component-0 {
|
| 135 |
-
background-image: linear-gradient(rgba(255, 255, 255, 0.9), rgba(255, 255, 255, 0.9)),
|
| 136 |
-
url("data:image/svg+xml,%3Csvg width='60' height='60' viewBox='0 0 60 60' xmlns='http://www.w3.org/2000/svg'%3E%3Cg fill='none' fill-rule='evenodd'%3E%3Cg fill='%23000000' fill-opacity='0.05'%3E%3Cpath d='M36 34v-4h-2v4h-4v2h4v4h2v-4h4v-2h-4zm0-30V0h-2v4h-4v2h4v4h2V6h4V4h-4zM6 34v-4H4v4H0v2h4v4h2v-4h4v-2H6zM6 4V0H4v4H0v2h4v4h2V6h4V4H6z'/%3E%3C/g%3E%3C/g%3E%3C/svg%3E");
|
| 137 |
-
min-height: 100vh;
|
| 138 |
-
padding: 20px;
|
| 139 |
-
}
|
| 140 |
-
|
| 141 |
-
.contain {
|
| 142 |
-
max-width: 1200px !important;
|
| 143 |
-
margin: auto !important;
|
| 144 |
-
}
|
| 145 |
-
|
| 146 |
-
.title {
|
| 147 |
-
text-align: center;
|
| 148 |
-
color: #1a237e;
|
| 149 |
-
font-size: 2.5em;
|
| 150 |
-
font-weight: bold;
|
| 151 |
-
margin-bottom: 1em;
|
| 152 |
-
}
|
| 153 |
-
|
| 154 |
-
.input-box {
|
| 155 |
-
border-radius: 8px !important;
|
| 156 |
-
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05) !important;
|
| 157 |
-
}
|
| 158 |
-
|
| 159 |
-
.output-box {
|
| 160 |
-
border-radius: 8px !important;
|
| 161 |
-
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05) !important;
|
| 162 |
-
}
|
| 163 |
-
"""
|
| 164 |
|
| 165 |
# Create Gradio interface
|
| 166 |
with gr.Blocks(css=custom_css) as demo:
|
| 167 |
gr.Markdown("# 雙語數學詞彙學習系統 | Bilingual Mathematics Learning System", elem_classes=["title"])
|
| 168 |
|
| 169 |
with gr.Tab("📚 單字英譯系統"):
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
label="請輸入中文詞彙 | Enter Chinese Term",
|
| 173 |
-
placeholder="輸入中文詞彙(例如:向量、函數、極限)...",
|
| 174 |
-
lines=1,
|
| 175 |
-
elem_classes=["input-box"]
|
| 176 |
-
)
|
| 177 |
-
|
| 178 |
-
with gr.Row():
|
| 179 |
-
translate_btn = gr.Button("🔄 英譯 | Translate", variant="primary")
|
| 180 |
-
example_btn = gr.Button("📝 例句 | Example")
|
| 181 |
-
|
| 182 |
-
output_text = gr.Textbox(
|
| 183 |
-
label="翻譯結果 | Translation Result",
|
| 184 |
-
lines=10,
|
| 185 |
-
placeholder="翻譯結果將在這裡顯示... | Translation results will be displayed here...",
|
| 186 |
-
elem_classes=["output-box"]
|
| 187 |
-
)
|
| 188 |
-
|
| 189 |
-
translate_btn.click(translate_to_english, inputs=word_input, outputs=output_text)
|
| 190 |
-
example_btn.click(generate_example, inputs=word_input, outputs=output_text)
|
| 191 |
|
| 192 |
with gr.Tab("💬 數學對話系統"):
|
| 193 |
name_input = gr.Textbox(
|
| 194 |
-
label="
|
| 195 |
-
placeholder="
|
| 196 |
-
lines=1,
|
| 197 |
elem_classes=["input-box"]
|
| 198 |
)
|
| 199 |
|
|
@@ -211,9 +144,9 @@ with gr.Blocks(css=custom_css) as demo:
|
|
| 211 |
elem_classes=["input-box"]
|
| 212 |
)
|
| 213 |
|
| 214 |
-
clear = gr.ClearButton([msg, chatbot
|
| 215 |
|
| 216 |
-
msg.submit(respond, [msg,
|
| 217 |
|
| 218 |
if __name__ == "__main__":
|
| 219 |
demo.launch()
|
|
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
from datetime import datetime
|
| 4 |
+
import pytz
|
| 5 |
try:
|
| 6 |
from groq import Groq
|
| 7 |
except ImportError:
|
| 8 |
os.system('pip install groq')
|
| 9 |
from groq import Groq
|
| 10 |
|
| 11 |
+
try:
|
| 12 |
+
from notion_client import Client
|
| 13 |
+
except ImportError:
|
| 14 |
+
os.system('pip install notion-client')
|
| 15 |
+
from notion_client import Client
|
| 16 |
+
|
| 17 |
# Initialize Groq client
|
| 18 |
client = Groq(api_key=os.getenv('groq_key'))
|
| 19 |
|
|
|
|
| 23 |
|
| 24 |
def log_to_notion(name, user_input, bot_response):
|
| 25 |
"""Log conversation to Notion database"""
|
| 26 |
+
taipei_tz = pytz.timezone('Asia/Taipei')
|
| 27 |
+
current_time = datetime.now(taipei_tz).strftime("%Y-%m-%d %H:%M:%S")
|
| 28 |
+
|
| 29 |
try:
|
| 30 |
notion.pages.create(
|
| 31 |
parent={"database_id": NOTION_DB_ID},
|
| 32 |
properties={
|
| 33 |
+
"Name": {
|
| 34 |
+
"title": [
|
| 35 |
+
{
|
| 36 |
+
"text": {
|
| 37 |
+
"content": name
|
| 38 |
+
}
|
| 39 |
+
}
|
| 40 |
+
]
|
| 41 |
+
},
|
| 42 |
+
"Timestamp": {
|
| 43 |
+
"rich_text": [
|
| 44 |
+
{
|
| 45 |
+
"text": {
|
| 46 |
+
"content": current_time
|
| 47 |
+
}
|
| 48 |
+
}
|
| 49 |
+
]
|
| 50 |
+
},
|
| 51 |
+
"User Input": {
|
| 52 |
+
"rich_text": [
|
| 53 |
+
{
|
| 54 |
+
"text": {
|
| 55 |
+
"content": user_input
|
| 56 |
+
}
|
| 57 |
+
}
|
| 58 |
+
]
|
| 59 |
+
},
|
| 60 |
+
"Bot Response": {
|
| 61 |
+
"rich_text": [
|
| 62 |
+
{
|
| 63 |
+
"text": {
|
| 64 |
+
"content": bot_response[:2000] if bot_response else "" # Notion has a 2000 character limit
|
| 65 |
+
}
|
| 66 |
+
}
|
| 67 |
+
]
|
| 68 |
+
}
|
| 69 |
}
|
| 70 |
)
|
| 71 |
+
return True
|
| 72 |
except Exception as e:
|
| 73 |
print(f"Error logging to Notion: {e}")
|
| 74 |
+
return False
|
| 75 |
|
| 76 |
+
def chat_response(message, chat_history, name):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
"""Generate response for chatbot"""
|
|
|
|
| 78 |
messages = [
|
| 79 |
{
|
| 80 |
"role": "system",
|
| 81 |
+
"content": "你是一個高中數學老師,使用的語言是英文。學生用中文問妳任何字彙,你都可以告訴他那個中文對應的英文和例句,以及在數學上的可能用法以及數學例題和解法。\n 說明數學上的可能用法時,先用中文講一遍再用 B1 程度的英文複述一遍\n"
|
| 82 |
}
|
| 83 |
]
|
| 84 |
|
|
|
|
| 97 |
stream=False
|
| 98 |
)
|
| 99 |
|
| 100 |
+
response = completion.choices[0].message.content
|
|
|
|
|
|
|
|
|
|
|
|
|
| 101 |
|
| 102 |
# Log to Notion
|
| 103 |
+
log_to_notion(name, message, response)
|
| 104 |
+
|
| 105 |
+
return response
|
| 106 |
+
|
| 107 |
+
def respond(message, history, name):
|
| 108 |
+
"""Process chatbot response and update history"""
|
| 109 |
+
if not name.strip():
|
| 110 |
+
return "請先輸入您的名字 | Please enter your name first", history
|
| 111 |
|
| 112 |
+
response = chat_response(message, history, name)
|
| 113 |
history.append((message, response))
|
| 114 |
return "", history
|
| 115 |
|
| 116 |
+
# Custom CSS remains the same...
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 117 |
|
| 118 |
# Create Gradio interface
|
| 119 |
with gr.Blocks(css=custom_css) as demo:
|
| 120 |
gr.Markdown("# 雙語數學詞彙學習系統 | Bilingual Mathematics Learning System", elem_classes=["title"])
|
| 121 |
|
| 122 |
with gr.Tab("📚 單字英譯系統"):
|
| 123 |
+
# Your existing translation system code...
|
| 124 |
+
pass
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 125 |
|
| 126 |
with gr.Tab("💬 數學對話系統"):
|
| 127 |
name_input = gr.Textbox(
|
| 128 |
+
label="您的名字 | Your Name",
|
| 129 |
+
placeholder="請輸入您的名字... | Please enter your name...",
|
|
|
|
| 130 |
elem_classes=["input-box"]
|
| 131 |
)
|
| 132 |
|
|
|
|
| 144 |
elem_classes=["input-box"]
|
| 145 |
)
|
| 146 |
|
| 147 |
+
clear = gr.ClearButton([msg, chatbot], value="🗑️ 清除對話 | Clear Chat")
|
| 148 |
|
| 149 |
+
msg.submit(respond, [msg, chatbot, name_input], [msg, chatbot])
|
| 150 |
|
| 151 |
if __name__ == "__main__":
|
| 152 |
demo.launch()
|