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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +53 -123
app.py CHANGED
@@ -1,153 +1,83 @@
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
 
20
- # Initialize Notion client
21
- notion = Client(auth=os.getenv('NOTION_API_KEY'))
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 說明數學上的可能用法時,先用中文講一遍再用 B1 程度的英文複述一遍\n"
82
  }
83
  ]
84
 
85
- for msg in chat_history:
86
- messages.append({"role": "user", "content": msg[0]})
87
- if msg[1]:
88
- messages.append({"role": "assistant", "content": msg[1]})
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
- 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
-
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
- if __name__ == "__main__":
152
- demo.launch()
 
 
 
 
 
 
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()