water64 commited on
Commit
2642298
·
verified ·
1 Parent(s): dfa8ed8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -59
app.py CHANGED
@@ -1,82 +1,61 @@
1
  import os
2
  import gradio as gr
3
-
4
- # 手動加載 .env 文件
5
- def load_env_file(file_path=".env"):
6
- """
7
- 手動讀取 .env 文件,並將內容載入到環境變數中。
8
- """
9
- if not os.path.exists(file_path):
10
- raise FileNotFoundError(f"找不到 .env 文件:{file_path}")
11
-
12
- with open(file_path, "r") as f:
13
- for line in f:
14
- # 忽略空行和註解
15
- line = line.strip()
16
- if not line or line.startswith("#"):
17
- continue
18
- # 拆分 KEY=VALUE 格式
19
- key, value = line.split("=", 1)
20
- os.environ[key.strip()] = value.strip()
21
-
22
- # 加載 .env 文件
23
- load_env_file()
24
-
25
- # 從環境變數中讀取 groq_key
26
- groq_key = os.getenv("groq_key")
27
- if not groq_key:
28
- raise ValueError("請確保已設定環境變數 'groq_key' 或 .env 文件包含該變數。")
29
-
30
- # 嘗試安裝和匯入 groq
31
  try:
32
  from groq import Groq
33
  except ImportError:
34
  os.system('pip install groq')
35
  from groq import Groq
36
 
37
- # 初始化 Groq 客戶端
38
- client = Groq(api_key=groq_key)
39
 
40
- # 定義角色設定
41
- system_message = {
42
- "role": "system",
43
- "content": "你是一個美術老師,請用繁體中文稱讚學生的問題,問什麼問題都會引導到藝術"
44
- }
45
 
46
- def chat_with_groq(history):
47
- messages = [system_message] + [
48
- {"role": "user" if i % 2 == 0 else "assistant", "content": msg}
49
- for i, msg in enumerate(sum(history, ()))
 
 
50
  ]
 
 
 
 
 
 
 
 
51
 
 
52
  completion = client.chat.completions.create(
53
  model="llama3-8b-8192",
54
  messages=messages,
55
  temperature=1,
56
  max_tokens=1024,
57
  top_p=1,
58
- stream=False,
59
- stop=None,
60
  )
61
 
62
- response = completion.choices[0].message.content
63
- return response
64
-
65
- def gradio_chatbot(user_message, history):
66
- if history is None:
67
- history = []
68
- history.append((user_message, None))
69
- bot_response = chat_with_groq(history)
70
- history[-1] = (user_message, bot_response)
71
- return history, history
72
-
73
- with gr.Blocks() as demo:
74
- chatbot = gr.Chatbot()
75
- msg = gr.Textbox(label="輸入您的問題:", placeholder="請輸入訊息...")
76
- clear = gr.Button("清除對話")
77
-
78
- msg.submit(gradio_chatbot, [msg, chatbot], [chatbot, chatbot])
79
- clear.click(lambda: None, None, chatbot)
80
 
81
  if __name__ == "__main__":
82
  demo.launch()
 
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
+ # Initialize Groq client
10
+ client = Groq(api_key=os.getenv("groq_key"))
11
 
12
+ # Chat history to store conversation
13
+ chat_history = []
 
 
 
14
 
15
+ def chat(message, history):
16
+ messages = [
17
+ {
18
+ "role": "system",
19
+ "content": "你是一個美術老師,請用繁體中文稱讚學生的問題,問什麼問題都會引導到藝術"
20
+ }
21
  ]
22
+
23
+ # Add chat history
24
+ for human, assistant in history:
25
+ messages.append({"role": "user", "content": human})
26
+ messages.append({"role": "assistant", "content": assistant})
27
+
28
+ # Add current message
29
+ messages.append({"role": "user", "content": message})
30
 
31
+ # Get response from Groq
32
  completion = client.chat.completions.create(
33
  model="llama3-8b-8192",
34
  messages=messages,
35
  temperature=1,
36
  max_tokens=1024,
37
  top_p=1,
38
+ stream=True,
39
+ stop=None
40
  )
41
 
42
+ # Build response text from stream
43
+ response = ""
44
+ for chunk in completion:
45
+ if chunk.choices[0].delta.content is not None:
46
+ response += chunk.choices[0].delta.content
47
+ yield response
48
+
49
+ # Create Gradio interface
50
+ demo = gr.ChatInterface(
51
+ fn=chat,
52
+ title="藝術老師聊天機器人",
53
+ description="我是一位熱愛藝術的老師,讓我們來聊聊藝術吧!",
54
+ examples=["你好", "我想學畫畫", "什麼是藝術"],
55
+ retry_btn=None,
56
+ undo_btn=None,
57
+ clear_btn="清除對話",
58
+ )
 
59
 
60
  if __name__ == "__main__":
61
  demo.launch()