Update app.py
Browse files
app.py
CHANGED
|
@@ -1,153 +1,83 @@
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
-
|
| 4 |
-
import
|
| 5 |
try:
|
| 6 |
from groq import Groq
|
| 7 |
except ImportError:
|
| 8 |
os.system('pip install groq')
|
| 9 |
from groq import Groq
|
| 10 |
|
| 11 |
-
|
| 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 |
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
NOTION_DB_ID = os.getenv('NOTION_DB_ID')
|
| 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
|
| 82 |
}
|
| 83 |
]
|
| 84 |
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
|
|
|
|
| 90 |
messages.append({"role": "user", "content": message})
|
| 91 |
-
|
|
|
|
| 92 |
completion = client.chat.completions.create(
|
| 93 |
model="llama-3.3-70b-versatile",
|
| 94 |
messages=messages,
|
| 95 |
temperature=1,
|
| 96 |
max_tokens=1024,
|
| 97 |
-
|
|
|
|
|
|
|
| 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 |
-
|
| 108 |
-
|
| 109 |
-
if not name.strip():
|
| 110 |
-
return "請先輸入您的名字 | Please enter your name first", history
|
| 111 |
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
|
|
|
|
|
|
| 117 |
|
| 118 |
# Create Gradio interface
|
| 119 |
-
with gr.Blocks(
|
| 120 |
-
gr.
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
elem_classes=["input-box"]
|
| 131 |
-
)
|
| 132 |
-
|
| 133 |
-
chatbot = gr.Chatbot(
|
| 134 |
-
[],
|
| 135 |
-
elem_id="chatbot",
|
| 136 |
-
bubble_full_width=False,
|
| 137 |
-
avatar_images=("👨🎓", "👨🏫")
|
| 138 |
-
)
|
| 139 |
-
|
| 140 |
-
msg = gr.Textbox(
|
| 141 |
-
label="發送訊息 | Send Message",
|
| 142 |
-
placeholder="請輸入您的問題... | Enter your question...",
|
| 143 |
-
show_label=False,
|
| 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 |
-
|
| 152 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 153 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
+
|
| 4 |
+
# Install and import Groq
|
| 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 with API key
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
client = Groq(api_key=os.getenv('groq_key'))
|
| 13 |
|
| 14 |
+
def process_message(message, history):
|
| 15 |
+
# Prepare the messages including history and new message
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
messages = [
|
| 17 |
{
|
| 18 |
"role": "system",
|
| 19 |
+
"content": "你是一個高中數學老師,使用的語言是英文。學生用中文問妳任何字彙,你都可以告訴他那個中文對應的英文和例句,以及在數學上的可能用法以及數學例題和解法。\n說明數學上的可能用法時,先用中文講一遍再用B1程度的英文複述一遍\n"
|
| 20 |
}
|
| 21 |
]
|
| 22 |
|
| 23 |
+
# Add chat history
|
| 24 |
+
for user_msg, bot_msg in history:
|
| 25 |
+
messages.append({"role": "user", "content": user_msg})
|
| 26 |
+
messages.append({"role": "assistant", "content": bot_msg})
|
| 27 |
|
| 28 |
+
# Add the new message
|
| 29 |
messages.append({"role": "user", "content": message})
|
| 30 |
+
|
| 31 |
+
# Get response from Groq
|
| 32 |
completion = client.chat.completions.create(
|
| 33 |
model="llama-3.3-70b-versatile",
|
| 34 |
messages=messages,
|
| 35 |
temperature=1,
|
| 36 |
max_tokens=1024,
|
| 37 |
+
top_p=1,
|
| 38 |
+
stream=True,
|
| 39 |
+
stop=None,
|
| 40 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
+
# Initialize response text
|
| 43 |
+
response_text = ""
|
|
|
|
|
|
|
| 44 |
|
| 45 |
+
# Stream the response
|
| 46 |
+
for chunk in completion:
|
| 47 |
+
delta_content = chunk.choices[0].delta.content
|
| 48 |
+
if delta_content is not None:
|
| 49 |
+
response_text += delta_content
|
| 50 |
+
# Yield partial response for real-time updates
|
| 51 |
+
yield response_text
|
| 52 |
|
| 53 |
# Create Gradio interface
|
| 54 |
+
with gr.Blocks() as demo:
|
| 55 |
+
chatbot = gr.Chatbot(
|
| 56 |
+
height=600,
|
| 57 |
+
show_label=False,
|
| 58 |
+
container=True,
|
| 59 |
+
)
|
| 60 |
+
msg = gr.Textbox(
|
| 61 |
+
placeholder="輸入您的問題...",
|
| 62 |
+
show_label=False,
|
| 63 |
+
)
|
| 64 |
+
clear = gr.Button("清除對話")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
|
| 66 |
+
def user(user_message, history):
|
| 67 |
+
return "", history + [[user_message, None]]
|
| 68 |
+
|
| 69 |
+
def bot(history):
|
| 70 |
+
history[-1][1] = ""
|
| 71 |
+
for response in process_message(history[-1][0], history[:-1]):
|
| 72 |
+
history[-1][1] = response
|
| 73 |
+
yield history
|
| 74 |
|
| 75 |
+
msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then(
|
| 76 |
+
bot, chatbot, chatbot
|
| 77 |
+
)
|
| 78 |
+
clear.click(lambda: None, None, chatbot, queue=False)
|
| 79 |
+
|
| 80 |
+
# Launch the app
|
| 81 |
+
if __name__ == "__main__":
|
| 82 |
+
demo.queue()
|
| 83 |
+
demo.launch()
|