ReneeHWT commited on
Commit
577b886
·
verified ·
1 Parent(s): b760d5d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -27
app.py CHANGED
@@ -1,59 +1,62 @@
1
  import os
2
  import gradio as gr
3
- try:
4
- from groq import Groq
5
- except ImportError:
6
- os.system('pip install groq')
7
- from groq import Groq
8
 
 
 
9
 
10
- # 設定 API Key
11
  groq_key = os.getenv("groq_key")
12
 
13
  # 初始化 Groq 客戶端
14
  client = Groq(api_key=groq_key)
15
 
16
- # 定義 Chatbot 回應函數
17
  def chatbot_response(user_input):
18
  completion = client.chat.completions.create(
19
- model="llama-3.1-70b-versatile",
20
  messages=[
21
  {
22
  "role": "system",
23
- "content": "You are a chatbot. You have a humorous temperament, and are capable of discussing serious matters with a sense of humor.\nYou speak Traditional Chinese(zh-tw)."
24
  },
25
  {
26
  "role": "user",
27
  "content": user_input
28
  }
29
  ],
30
- temperature=1.14,
31
  max_tokens=1024,
32
  top_p=1,
33
- stream=False,
34
  stop=None,
35
  )
36
-
37
- # 檢查並解析回應
38
  response = ""
39
  for chunk in completion:
40
- if hasattr(chunk, 'choices') and len(chunk.choices) > 0:
41
- delta_content = getattr(chunk.choices[0], 'delta', {}).get('content', "")
42
- if delta_content:
43
- response += delta_content
44
-
45
  return response
46
 
47
- # 設定 Gradio Chatbot 介面
48
- iface = gr.Interface(
49
- fn=chatbot_response,
50
- inputs="text",
51
- outputs="text",
52
- title="Groq Chatbot",
53
- description="一個用於與 Groq API 互動的 Chatbot",
 
 
 
 
 
 
 
 
54
  )
55
 
56
- # 啟動 Gradio 應用程式
57
  if __name__ == "__main__":
58
- iface.launch()
 
59
 
 
1
  import os
2
  import gradio as gr
3
+ from groq import Groq
 
 
 
 
4
 
5
+ # 安裝 groq 套件
6
+ os.system('pip install groq')
7
 
8
+ # 從環境變量中獲取 Groq 的 API key
9
  groq_key = os.getenv("groq_key")
10
 
11
  # 初始化 Groq 客戶端
12
  client = Groq(api_key=groq_key)
13
 
14
+ # 定義聊天機器人回應邏輯
15
  def chatbot_response(user_input):
16
  completion = client.chat.completions.create(
17
+ model="llama-3.1-8b-instant",
18
  messages=[
19
  {
20
  "role": "system",
21
+ "content": "你是一個有幽默感的聊天機器人,你擅長用詼諧的方式來討論嚴肅的事情,尤其是政治方面的議題。你使用的語言是繁體中文(zh-tw)"
22
  },
23
  {
24
  "role": "user",
25
  "content": user_input
26
  }
27
  ],
28
+ temperature=1.18,
29
  max_tokens=1024,
30
  top_p=1,
31
+ stream=True,
32
  stop=None,
33
  )
34
+
 
35
  response = ""
36
  for chunk in completion:
37
+ response += chunk.choices[0].delta.content or ""
38
+
 
 
 
39
  return response
40
 
41
+ # 建立 Gradio 聊天機器人介面
42
+ chatbot = gr.Chatbot()
43
+ input_text = gr.inputs.Textbox(placeholder="輸入你的訊息...")
44
+
45
+ def respond_to_user(input_text, history):
46
+ response = chatbot_response(input_text)
47
+ history.append((input_text, response))
48
+ return history, history
49
+
50
+ interface = gr.Interface(
51
+ fn=respond_to_user,
52
+ inputs=[input_text, chatbot],
53
+ outputs=[chatbot, chatbot],
54
+ live=True,
55
+ allow_flagging='never',
56
  )
57
 
58
+ # 啟動應用
59
  if __name__ == "__main__":
60
+ interface.launch()
61
+
62