Update app.py
Browse files
app.py
CHANGED
|
@@ -1,35 +1,36 @@
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
|
| 4 |
-
#
|
| 5 |
try:
|
| 6 |
from groq import Groq
|
| 7 |
except ImportError:
|
| 8 |
os.system('pip install groq')
|
| 9 |
from groq import Groq
|
| 10 |
|
| 11 |
-
#
|
| 12 |
groq_key = os.getenv("groq_key")
|
| 13 |
if not groq_key:
|
| 14 |
raise ValueError("API key for Groq is missing. Set it in the environment variable 'groq_key'.")
|
| 15 |
|
| 16 |
-
|
|
|
|
| 17 |
|
| 18 |
-
#
|
| 19 |
def chatbot(input_text, history=[]):
|
| 20 |
messages = [
|
| 21 |
-
{"role": "system", "content": "你是要幫學生打分數的老師,請你先填入你的教師名稱 再選擇學生的名字 最後再決定加扣分,請用正體中文完成這些對話\n "},
|
| 22 |
]
|
| 23 |
|
| 24 |
-
#
|
| 25 |
for user_input, bot_response in history:
|
| 26 |
messages.append({"role": "user", "content": user_input})
|
| 27 |
messages.append({"role": "assistant", "content": bot_response})
|
| 28 |
|
| 29 |
-
#
|
| 30 |
messages.append({"role": "user", "content": input_text})
|
| 31 |
|
| 32 |
-
#
|
| 33 |
try:
|
| 34 |
completion = client.chat.completions.create(
|
| 35 |
model="llama3-groq-70b-8192-tool-use-preview",
|
|
@@ -40,7 +41,7 @@ def chatbot(input_text, history=[]):
|
|
| 40 |
stream=False
|
| 41 |
)
|
| 42 |
|
| 43 |
-
#
|
| 44 |
response = "".join(chunk.choices[0].delta.content or "" for chunk in completion)
|
| 45 |
history.append((input_text, response))
|
| 46 |
return response, history
|
|
@@ -50,7 +51,7 @@ def chatbot(input_text, history=[]):
|
|
| 50 |
history.append((input_text, error_message))
|
| 51 |
return error_message, history
|
| 52 |
|
| 53 |
-
# Gradio
|
| 54 |
def reset_history():
|
| 55 |
return []
|
| 56 |
|
|
@@ -65,5 +66,5 @@ with gr.Blocks() as demo:
|
|
| 65 |
submit_button.click(chatbot, inputs=[input_textbox, state], outputs=[chatbot_component, state])
|
| 66 |
clear_button.click(reset_history, inputs=[], outputs=state)
|
| 67 |
|
| 68 |
-
#
|
| 69 |
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 |
+
# 從環境變數中獲取 API key
|
| 12 |
groq_key = os.getenv("groq_key")
|
| 13 |
if not groq_key:
|
| 14 |
raise ValueError("API key for Groq is missing. Set it in the environment variable 'groq_key'.")
|
| 15 |
|
| 16 |
+
# 初始化 Groq 客戶端
|
| 17 |
+
client = Groq(api_key=groq_key)
|
| 18 |
|
| 19 |
+
# 定義 Chatbot 函數
|
| 20 |
def chatbot(input_text, history=[]):
|
| 21 |
messages = [
|
| 22 |
+
{"role": "system", "content": "你是要幫學生打分數的老師,請你先填入你的教師名稱 再選擇學生的名字 最後再決定加扣分,請用正體中文完成這些對話\\n "},
|
| 23 |
]
|
| 24 |
|
| 25 |
+
# 添加歷史對話
|
| 26 |
for user_input, bot_response in history:
|
| 27 |
messages.append({"role": "user", "content": user_input})
|
| 28 |
messages.append({"role": "assistant", "content": bot_response})
|
| 29 |
|
| 30 |
+
# 添加使用者輸入
|
| 31 |
messages.append({"role": "user", "content": input_text})
|
| 32 |
|
| 33 |
+
# 調用 Groq API
|
| 34 |
try:
|
| 35 |
completion = client.chat.completions.create(
|
| 36 |
model="llama3-groq-70b-8192-tool-use-preview",
|
|
|
|
| 41 |
stream=False
|
| 42 |
)
|
| 43 |
|
| 44 |
+
# 獲取回應內容
|
| 45 |
response = "".join(chunk.choices[0].delta.content or "" for chunk in completion)
|
| 46 |
history.append((input_text, response))
|
| 47 |
return response, history
|
|
|
|
| 51 |
history.append((input_text, error_message))
|
| 52 |
return error_message, history
|
| 53 |
|
| 54 |
+
# Gradio 介面
|
| 55 |
def reset_history():
|
| 56 |
return []
|
| 57 |
|
|
|
|
| 66 |
submit_button.click(chatbot, inputs=[input_textbox, state], outputs=[chatbot_component, state])
|
| 67 |
clear_button.click(reset_history, inputs=[], outputs=state)
|
| 68 |
|
| 69 |
+
# 啟動應用程式
|
| 70 |
demo.launch()
|