ReneeHWT commited on
Commit
5be466d
·
verified ·
1 Parent(s): 188e5a9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +61 -0
app.py CHANGED
@@ -0,0 +1,61 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import gradio as gr
3
+
4
+ # 手动设置 API 金钥
5
+ os.environ["groq_key"] = "your_groq_api_key"
6
+
7
+ # 确保 groq 套件已安装
8
+ try:
9
+ from groq import Groq
10
+ except ImportError:
11
+ os.system('pip install groq')
12
+ from groq import Groq
13
+
14
+ # 从环境变量中获取 API 金钥
15
+ api_key = os.getenv("groq_key")
16
+ if api_key is None:
17
+ raise ValueError("API key is not set. Please set it in your environment variables with the name 'groq_key'.")
18
+
19
+ # 初始化 Groq 客户端
20
+ client = Groq(api_key=api_key)
21
+
22
+ # 定义聊天机器人的响应函数
23
+ def chatbot_response(messages):
24
+ completion = client.chat.completions.create(
25
+ model="llama3-8b-8192",
26
+ messages=[
27
+ {
28
+ "role": "system",
29
+ "content": "You are a corporate secretary who is skilled at drafting business emails. The prompt will feed you addressee, main message, and final greetings."
30
+ }
31
+ ] + messages,
32
+ temperature=1,
33
+ max_tokens=1024,
34
+ top_p=1,
35
+ stream=True,
36
+ stop=None,
37
+ )
38
+
39
+ response = ""
40
+ for chunk in completion:
41
+ response += chunk.choices[0].delta.content or ""
42
+
43
+ return response
44
+
45
+ # 使用 gradio 创建聊天机器人界面
46
+ def respond(message, history):
47
+ history.append({"role": "user", "content": message})
48
+ bot_response = chatbot_response(history)
49
+ history.append({"role": "assistant", "content": bot_response})
50
+ return "", history
51
+
52
+ # 初始化 Gradio 应用
53
+ with gr.Blocks() as demo:
54
+ chatbot = gr.Chatbot()
55
+ msg = gr.Textbox()
56
+
57
+ msg.submit(respond, [msg, chatbot], [msg, chatbot])
58
+
59
+ # 运行应用程序
60
+ if __name__ == "__main__":
61
+ demo.launch(server_name="0.0.0.0", server_port=7860)