louise840115 commited on
Commit
0b7691e
·
verified ·
1 Parent(s): 05870bc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -39
app.py CHANGED
@@ -1,46 +1,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(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
- # 创建 Gradio 接口
43
- iface = gr.Chatbot(fn=chatbot, title="國中地理老師聊天機器人", description="問我任何與地理相關的問題,我會引導你進入地理的世界!")
 
 
44
 
45
- # 启动 Gradio 应用
46
- iface.launch()
 
1
+ 我想要用 gradio 的 gr.chatbot() 做一個 chatbot
2
+ 我要用 groq 這個套件,API key name 我存在 secret 叫做:groq_key,用os.getenv 設定
3
+ groq 安裝請用:
 
4
  try:
5
+ from groq import Groq
6
  except ImportError:
7
+ os.system('pip install groq')
8
+ from groq import Groq
 
 
 
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 an video", gr.Video("https://github.com/gradio-app/gradio/raw/main/demo/video_component/files/world.mp4"))
38
+ ]
39
 
40
+ with gr.Blocks() as demo:
41
+ chatbot = gr.Chatbot()
42
+ button = gr.Button("Load audio and video")
43
+ button.click(load, None, chatbot)
44
 
45
+ demo.launch(
46
+ 全部寫在同一份檔案,給我一份 app.py 的 code 就好