Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,12 +1,51 @@
|
|
|
|
1 |
import gradio as gr
|
2 |
-
from transformers import pipeline
|
3 |
|
4 |
-
#
|
5 |
-
|
|
|
|
|
|
|
|
|
6 |
|
7 |
-
|
8 |
-
|
9 |
-
return result['text']
|
10 |
|
11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
iface.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 客戶端
|
12 |
+
client = Groq()
|
|
|
13 |
|
14 |
+
# 定義 chatbot 回應的函數
|
15 |
+
def chat_with_groq(user_input):
|
16 |
+
completion = client.chat.completions.create(
|
17 |
+
model="llama-3.1-70b-versatile",
|
18 |
+
messages=[
|
19 |
+
{
|
20 |
+
"role": "system",
|
21 |
+
"content": "我是國文老師,專門批改國小作文"
|
22 |
+
},
|
23 |
+
{
|
24 |
+
"role": "user",
|
25 |
+
"content": user_input
|
26 |
+
}
|
27 |
+
],
|
28 |
+
temperature=1,
|
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 創建 chatbot
|
42 |
+
iface = gr.Interface(
|
43 |
+
fn=chat_with_groq,
|
44 |
+
inputs=gr.inputs.Textbox(label="請輸入您的問題:"),
|
45 |
+
outputs=gr.outputs.Textbox(label="回應:"),
|
46 |
+
title="國文老師 Chatbot",
|
47 |
+
description="這是一個專門批改國小作文的 Chatbot"
|
48 |
+
)
|
49 |
+
|
50 |
+
# 啟動應用
|
51 |
iface.launch()
|