water64 commited on
Commit
9eb4cf9
·
verified ·
1 Parent(s): 046ec38

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +63 -45
app.py CHANGED
@@ -1,61 +1,79 @@
1
  import os
2
  import gradio as gr
3
- import time
4
 
5
- # 安裝並導入 groq 套件
6
  try:
7
  from groq import Groq
8
  except ImportError:
9
  os.system('pip install groq')
10
  from groq import Groq
11
 
12
- # API 金鑰
13
- API_KEY = os.getenv("groq_key", None)
14
- if not API_KEY:
15
- raise ValueError("請確保已設定環境變數 groq_key,並包含有效的 API 金鑰。")
16
 
17
  # 初始化 Groq 客戶端
18
- client = Groq(api_key=API_KEY)
19
-
20
- # 定義聊天機器人回應邏輯
21
- def respond(message, chat_history):
22
- # 將用戶訊息加入對話歷史
23
- chat_history.append({"role": "user", "content": message})
24
-
25
- # 呼叫 Groq API 進行回應
26
- try:
27
- completion = client.chat.completions.create(
28
- model="llama3-8b-8192",
29
- messages=[
30
- {"role": "system", "content": "你是一個美術老師,請用繁體中文稱讚學生的問題,問什麼問題都會引導到藝術"}
31
- ] + chat_history, # 包括對話歷史
32
- temperature=1,
33
- max_tokens=1024,
34
- top_p=1,
35
- stream=False,
36
- stop=None,
37
- )
38
-
39
- # 取得 Groq 回應
40
- bot_message = completion.choices[0].message["content"]
41
- except Exception as e:
42
- bot_message = f"抱歉,發生錯誤:{str(e)}"
43
-
44
- # 將機器人回應加入對話歷史
45
- chat_history.append({"role": "assistant", "content": bot_message})
46
-
47
- # 回傳聊天歷史與空訊息框
48
- return "", chat_history
49
-
50
- # 建立 Gradio 介面
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
51
  with gr.Blocks() as demo:
52
- chatbot = gr.Chatbot(type="messages")
53
- msg = gr.Textbox(label="輸入您的訊息")
54
- clear = gr.ClearButton([msg, chatbot])
55
 
56
- # 訊息提交按鈕的回應邏輯
57
- msg.submit(respond, [msg, chatbot], [msg, chatbot])
 
58
 
59
- # 啟動應用程式
60
  if __name__ == "__main__":
61
  demo.launch()
 
1
  import os
2
  import gradio as gr
 
3
 
4
+ # 嘗試安裝和匯入 groq
5
  try:
6
  from groq import Groq
7
  except ImportError:
8
  os.system('pip install groq')
9
  from groq import Groq
10
 
11
+ # 從環境變數中讀groq_key
12
+ groq_key = os.getenv("groq_key")
13
+ if not groq_key:
14
+ raise ValueError("請確保已設定環境變數 'groq_key'.")
15
 
16
  # 初始化 Groq 客戶端
17
+ client = Groq(api_key=groq_key)
18
+
19
+ # 定義角色設定
20
+ system_message = {
21
+ "role": "system",
22
+ "content": "你是一個美術老師,請用繁體中文稱讚學生的問題,問什麼問題都會引導到藝術"
23
+ }
24
+
25
+ # 定義與 Groq 的互動邏輯
26
+ def chat_with_groq(history):
27
+ """
28
+ 與 Groq API 對話的函數,將歷史對話傳給 API 並返回模型的回應。
29
+ """
30
+ # 歷史對話轉換成 Groq 的訊息格式
31
+ messages = [system_message] + [
32
+ {"role": "user" if i % 2 == 0 else "assistant", "content": msg}
33
+ for i, msg in enumerate(sum(history, ()))
34
+ ]
35
+
36
+ # 呼叫 Groq 的 completion API
37
+ completion = client.chat.completions.create(
38
+ model="llama3-8b-8192",
39
+ messages=messages,
40
+ temperature=1,
41
+ max_tokens=1024,
42
+ top_p=1,
43
+ stream=False,
44
+ stop=None,
45
+ )
46
+
47
+ # 提取 API 回應
48
+ response = completion.choices[0].message.content
49
+ return response
50
+
51
+ # 定義 Gradio 的互動函數
52
+ def gradio_chatbot(user_message, history):
53
+ """
54
+ 與使用者互動的 Gradio 函數,處理新的訊息並更新對話歷史。
55
+ """
56
+ # 更新歷史對話
57
+ if history is None:
58
+ history = []
59
+ history.append((user_message, None))
60
+
61
+ # 呼叫 chat_with_groq 並獲得回應
62
+ bot_response = chat_with_groq(history)
63
+ history[-1] = (user_message, bot_response)
64
+
65
+ return history, history
66
+
67
+ # 建立 Gradio 界面
68
  with gr.Blocks() as demo:
69
+ chatbot = gr.Chatbot() # 建立 Chatbot 元件
70
+ msg = gr.Textbox(label="輸入您的問題:", placeholder="請輸入訊息...")
71
+ clear = gr.Button("清除對話")
72
 
73
+ # 定義互動邏輯
74
+ msg.submit(gradio_chatbot, [msg, chatbot], [chatbot, chatbot])
75
+ clear.click(lambda: None, None, chatbot) # 清空對話
76
 
77
+ # 啟動 Gradio 應用
78
  if __name__ == "__main__":
79
  demo.launch()