Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,46 +1,64 @@
|
|
1 |
-
|
2 |
-
|
3 |
-
|
|
|
4 |
try:
|
5 |
-
|
6 |
except ImportError:
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
from groq import Groq
|
11 |
|
|
|
12 |
client = Groq()
|
13 |
-
completion = client.chat.completions.create(
|
14 |
-
model="llama-3.1-70b-versatile",
|
15 |
-
messages=[
|
16 |
-
{
|
17 |
-
"role": "system",
|
18 |
-
"content": "你是一位國中地理老師,使用的語言為繁體中文(zh-tw)。你的興趣是繪製等高線圖,熱愛講冷笑話。無論學生問你什麼問題,你都會把話題引導到地理相關的討論上。"
|
19 |
-
}
|
20 |
-
],
|
21 |
-
temperature=1,
|
22 |
-
max_tokens=1024,
|
23 |
-
top_p=1,
|
24 |
-
stream=True,
|
25 |
-
stop=None,
|
26 |
-
)
|
27 |
-
|
28 |
-
for chunk in completion:
|
29 |
-
print(chunk.choices[0].delta.content or "", end="")
|
30 |
-
加上 role: system 的設定
|
31 |
-
這是 gradio chatbot 的 document ,請遵循
|
32 |
-
import gradio as gr
|
33 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
34 |
def load():
|
35 |
return [
|
36 |
("Here's an audio", gr.Audio("https://github.com/gradio-app/gradio/raw/main/test/test_files/audio_sample.wav")),
|
37 |
-
("Here's
|
38 |
]
|
39 |
|
|
|
40 |
with gr.Blocks() as demo:
|
41 |
-
|
42 |
button = gr.Button("Load audio and video")
|
43 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
44 |
|
45 |
-
|
46 |
-
|
|
|
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 |
+
# 获取 API 密钥
|
15 |
+
api_key = os.getenv('groq_key')
|
16 |
+
|
17 |
+
# 定义聊天函数
|
18 |
+
def chatbot_response(message):
|
19 |
+
completion = client.chat.completions.create(
|
20 |
+
model="llama-3.1-70b-versatile",
|
21 |
+
messages=[
|
22 |
+
{
|
23 |
+
"role": "system",
|
24 |
+
"content": "你是一位國中地理老師,使用的語言為繁體中文(zh-tw)。你的興趣是繪製等高線圖,熱愛講冷笑話。無論學生問你什麼問題,你都會把話題引導到地理相關的討論上。"
|
25 |
+
},
|
26 |
+
{"role": "user", "content": message}
|
27 |
+
],
|
28 |
+
temperature=1,
|
29 |
+
max_tokens=1024,
|
30 |
+
top_p=1,
|
31 |
+
stream=True,
|
32 |
+
stop=None,
|
33 |
+
)
|
34 |
+
|
35 |
+
# 处理返回的消息
|
36 |
+
response = ""
|
37 |
+
for chunk in completion:
|
38 |
+
response += chunk.choices[0].delta.content or ""
|
39 |
+
|
40 |
+
return response
|
41 |
+
|
42 |
+
# 定义加载函数
|
43 |
def load():
|
44 |
return [
|
45 |
("Here's an audio", gr.Audio("https://github.com/gradio-app/gradio/raw/main/test/test_files/audio_sample.wav")),
|
46 |
+
("Here's a video", gr.Video("https://github.com/gradio-app/gradio/raw/main/demo/video_component/files/world.mp4"))
|
47 |
]
|
48 |
|
49 |
+
# 创建 Gradio 界面
|
50 |
with gr.Blocks() as demo:
|
51 |
+
chatbot_ui = gr.Chatbot()
|
52 |
button = gr.Button("Load audio and video")
|
53 |
+
|
54 |
+
# 按钮点击事件
|
55 |
+
button.click(load, None, chatbot_ui)
|
56 |
+
|
57 |
+
# 文本输入和响应
|
58 |
+
message_input = gr.Textbox(placeholder="在此輸入您的問題...")
|
59 |
+
message_output = gr.Textbox(label="Chatbot Response")
|
60 |
+
|
61 |
+
message_input.submit(chatbot_response, message_input, message_output)
|
62 |
|
63 |
+
# 启动 Gradio 应用
|
64 |
+
demo.launch()
|