Update app.py
Browse files
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 |
-
#
|
| 38 |
-
client = Groq(api_key=groq_key)
|
| 39 |
|
| 40 |
-
#
|
| 41 |
-
|
| 42 |
-
"role": "system",
|
| 43 |
-
"content": "你是一個美術老師,請用繁體中文稱讚學生的問題,問什麼問題都會引導到藝術"
|
| 44 |
-
}
|
| 45 |
|
| 46 |
-
def
|
| 47 |
-
messages = [
|
| 48 |
-
{
|
| 49 |
-
|
|
|
|
|
|
|
| 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=
|
| 59 |
-
stop=None
|
| 60 |
)
|
| 61 |
|
| 62 |
-
response
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 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()
|