Rooobert commited on
Commit
26b84ef
·
verified ·
1 Parent(s): 0553cb4

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +129 -0
app.py ADDED
@@ -0,0 +1,129 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from groq import Groq
3
+
4
+ class GroqChatbot:
5
+ def __init__(self, api_key, model="mixtral-8x7b-32768"):
6
+ """
7
+ 初始化 Groq AI 聊天機器人
8
+
9
+ :param api_key: Groq API 金鑰
10
+ :param model: 要使用的模型,預設為 mixtral-8x7b-32768
11
+ """
12
+ self.client = Groq(api_key=api_key)
13
+ self.model = model
14
+ self.conversation_history = []
15
+
16
+ def generate_response(self, user_message):
17
+ """
18
+ 使用 Groq AI 產生回覆
19
+
20
+ :param user_message: 使用者輸入的訊息
21
+ :return: AI 的回覆
22
+ """
23
+ try:
24
+ # 準備對話歷史記錄
25
+ messages = [
26
+ {"role": "system", "content": "你是一個樂於助人的 AI 助理。請用繁體中文回答問題。"}
27
+ ]
28
+
29
+ # 新增先前的對話歷史
30
+ for past_user, past_ai in self.conversation_history:
31
+ messages.append({"role": "user", "content": past_user})
32
+ messages.append({"role": "assistant", "content": past_ai})
33
+
34
+ # 新增目前使用者訊息
35
+ messages.append({"role": "user", "content": user_message})
36
+
37
+ # 呼叫 Groq API
38
+ chat_completion = self.client.chat.completions.create(
39
+ messages=messages,
40
+ model=self.model
41
+ )
42
+
43
+ # 取得回覆
44
+ response = chat_completion.choices[0].message.content
45
+ return response
46
+
47
+ except Exception as e:
48
+ return f"發生錯誤:{str(e)}"
49
+
50
+ def chat_interface(self, user_message):
51
+ """
52
+ 主要聊天介面方法
53
+
54
+ :param user_message: 使用者輸入的訊息
55
+ :return: AI 回覆和對話歷史
56
+ """
57
+ # 產生回覆
58
+ response = self.generate_response(user_message)
59
+
60
+ # 更新對話歷史
61
+ self.conversation_history.append((user_message, response))
62
+
63
+ # 準備歷史顯示
64
+ history_text = ""
65
+ for i, (question, answer) in enumerate(self.conversation_history):
66
+ history_text += f"問題 {i+1}:{question}\n"
67
+ history_text += f"回覆 {i+1}:{answer}\n\n"
68
+
69
+ return response, history_text
70
+
71
+ def create_gradio_interface(api_key):
72
+ """
73
+ 建立 Gradio 聊天介面
74
+
75
+ :param api_key: Groq API 金鑰
76
+ :return: Gradio 介面
77
+ """
78
+ # 初始化聊天機器人
79
+ chatbot = GroqChatbot(api_key)
80
+
81
+ # 示範問題
82
+ examples = [
83
+ "什麼是人工智能?",
84
+ "請解釋量子運算的基本原理",
85
+ "能分享一個有趣的科技創新故事嗎?"
86
+ ]
87
+
88
+ # 建立 Gradio 介面
89
+ with gr.Blocks() as demo:
90
+ gr.Markdown("# 🤖 Groq AI 智能助理 🌐")
91
+ gr.Markdown("使用 Groq AI 技術的智慧對話助手")
92
+
93
+ with gr.Row():
94
+ with gr.Column(scale=3):
95
+ input_text = gr.Textbox(label="請在此輸入您的問題...")
96
+ submit_btn = gr.Button("傳送")
97
+
98
+ with gr.Column(scale=1):
99
+ gr.Markdown("### 快速範例")
100
+ example_buttons = [gr.Button(ex) for ex in examples]
101
+
102
+ output_text = gr.Textbox(label="AI 回覆")
103
+ history_text = gr.Textbox(label="對話紀錄")
104
+
105
+ # 傳送按鈕邏輯
106
+ submit_btn.click(
107
+ fn=chatbot.chat_interface,
108
+ inputs=input_text,
109
+ outputs=[output_text, history_text]
110
+ )
111
+
112
+ # 範例按鈕邏輯
113
+ for btn, ex in zip(example_buttons, examples):
114
+ btn.click(
115
+ fn=chatbot.chat_interface,
116
+ inputs=gr.State(ex),
117
+ outputs=[output_text, history_text]
118
+ )
119
+
120
+ return demo
121
+
122
+ # 使用方法
123
+ if __name__ == "__main__":
124
+ # 請更換為您的 Groq API 金鑰
125
+ GROQ_API_KEY = "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"
126
+
127
+ # 啟動 Gradio 介面
128
+ demo = create_gradio_interface(GROQ_API_KEY)
129
+ demo.launch()