helen573 commited on
Commit
f56c317
·
verified ·
1 Parent(s): e67cf59

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +58 -8
app.py CHANGED
@@ -1,5 +1,8 @@
1
  import os
 
 
2
  import gradio as gr
 
3
 
4
  # Install and import Groq
5
  try:
@@ -11,6 +14,25 @@ except ImportError:
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 = [
@@ -19,12 +41,12 @@ def process_message(message, history):
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
 
@@ -41,40 +63,68 @@ def process_message(message, history):
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
 
1
  import os
2
+ from datetime import datetime
3
+ from notion_client import Client
4
  import gradio as gr
5
+ import pytz
6
 
7
  # Install and import Groq
8
  try:
 
14
  # Initialize Groq client with API key
15
  client = Groq(api_key=os.getenv('groq_key'))
16
 
17
+ # Initialize Notion client
18
+ notion = Client(auth=os.getenv('NOTION_API_KEY'))
19
+ NOTION_DB_ID = os.getenv('NOTION_DB_ID')
20
+
21
+ def log_to_notion(name, user_input, bot_response):
22
+ """Log chat interaction to Notion database"""
23
+ taipei_tz = pytz.timezone('Asia/Taipei')
24
+ current_time = datetime.now(taipei_tz).strftime("%Y-%m-%d %H:%M:%S")
25
+
26
+ notion.pages.create(
27
+ parent={"database_id": NOTION_DB_ID},
28
+ properties={
29
+ "Name": {"title": [{"text": {"content": name}}]},
30
+ "Timestamp": {"rich_text": [{"text": {"content": current_time}}]},
31
+ "User Input": {"rich_text": [{"text": {"content": user_input}}]},
32
+ "Bot Response": {"rich_text": [{"text": {"content": bot_response}}]}
33
+ }
34
+ )
35
+
36
  def process_message(message, history):
37
  # Prepare the messages including history and new message
38
  messages = [
 
41
  "content": "你是一個高中數學老師,使用的語言是英文。學生用中文問妳任何字彙,你都可以告訴他那個中文對應的英文和例句,以及在數學上的可能用法以及數學例題和解法。\n說明數學上的可能用法時,先用中文講一遍再用B1程度的英文複述一遍\n"
42
  }
43
  ]
44
+
45
  # Add chat history
46
  for user_msg, bot_msg in history:
47
  messages.append({"role": "user", "content": user_msg})
48
  messages.append({"role": "assistant", "content": bot_msg})
49
+
50
  # Add the new message
51
  messages.append({"role": "user", "content": message})
52
 
 
63
 
64
  # Initialize response text
65
  response_text = ""
66
+
67
  # Stream the response
68
  for chunk in completion:
69
  delta_content = chunk.choices[0].delta.content
70
  if delta_content is not None:
71
  response_text += delta_content
 
72
  yield response_text
73
 
74
  # Create Gradio interface
75
  with gr.Blocks() as demo:
76
+ # Add name input field at the top
77
+ name_input = gr.Textbox(
78
+ placeholder="請輸入您的姓名...",
79
+ label="姓名",
80
+ show_label=True,
81
+ )
82
+
83
  chatbot = gr.Chatbot(
84
  height=600,
85
  show_label=False,
86
  container=True,
87
  )
88
+
89
  msg = gr.Textbox(
90
  placeholder="輸入您的問題...",
91
  show_label=False,
92
  )
93
+
94
  clear = gr.Button("清除對話")
95
 
96
+ def user(user_message, history, name):
97
+ # Validate name input
98
+ if not name.strip():
99
+ return "請先輸入姓名", history
100
  return "", history + [[user_message, None]]
101
 
102
+ def bot(history, name):
103
  history[-1][1] = ""
104
+ full_response = ""
105
  for response in process_message(history[-1][0], history[:-1]):
106
  history[-1][1] = response
107
+ full_response = response
108
  yield history
109
+
110
+ # Log to Notion after the complete response is generated
111
+ log_to_notion(
112
+ name=name.strip(),
113
+ user_input=history[-1][0],
114
+ bot_response=full_response
115
+ )
116
 
117
+ msg.submit(
118
+ user,
119
+ inputs=[msg, chatbot, name_input],
120
+ outputs=[msg, chatbot],
121
+ queue=False
122
+ ).then(
123
+ bot,
124
+ inputs=[chatbot, name_input],
125
+ outputs=[chatbot]
126
  )
127
+
128
  clear.click(lambda: None, None, chatbot, queue=False)
129
 
130
  # Launch the app