Basshole commited on
Commit
936f5c5
·
verified ·
1 Parent(s): 45ed6b9

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +157 -21
app.py CHANGED
@@ -1,4 +1,20 @@
 
1
  import gradio as gr
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
 
3
  # 定義每個類型的句子
4
  achievement_statements = [
@@ -44,10 +60,16 @@ status_statements = [
44
  "比起和他人待在一起,我更喜歡獨處",
45
  ]
46
 
47
- # 定義一個函數來處理選擇並生成提示
48
 
49
- def generate_prompt(achievement, emotion, information, possession, power, status):
50
- # 對應的資料結構
 
 
 
 
 
 
 
51
  category_map = {
52
  '成就型': achievement_statements,
53
  '情感型': emotion_statements,
@@ -87,26 +109,140 @@ def generate_prompt(achievement, emotion, information, possession, power, status
87
 
88
  return prompt
89
 
90
- # 使用 Gradio 建立介面
91
- with gr.Blocks() as demo:
92
- gr.Markdown("## Prompt 產生器")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
93
 
94
- with gr.Row():
95
- achievement = gr.Radio(["正", "負"], label="成就型", value="正")
96
- emotion = gr.Radio(["正", "負"], label="情感型", value="正")
97
- information = gr.Radio(["正", "負"], label="資訊型", value="正")
98
- possession = gr.Radio(["正", "負"], label="佔有型", value="正")
99
- power = gr.Radio(["正", "負"], label="權力型", value="正")
100
- status = gr.Radio(["正", "負"], label="地位型", value="正")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
101
 
102
- prompt_output = gr.Textbox(label="生成的 Prompt", lines=20)
103
- generate_button = gr.Button("生成 Prompt")
104
 
105
- generate_button.click(
106
- fn=generate_prompt,
107
- inputs=[achievement, emotion, information, possession, power, status],
108
- outputs=prompt_output
109
- )
110
 
111
  if __name__ == "__main__":
112
- demo.launch()
 
1
+ import os
2
  import gradio as gr
3
+ import openai
4
+ # 如需使用 Claude,請先安裝 "anthropic" 套件: pip install anthropic
5
+ from typing import Optional
6
+
7
+ try:
8
+ from anthropic import Anthropic, HUMAN_PROMPT, AI_PROMPT
9
+ except ImportError:
10
+ Anthropic = None
11
+ HUMAN_PROMPT = ""
12
+ AI_PROMPT = ""
13
+
14
+ # 從環境變數中取得金鑰
15
+ openai.api_key = os.environ.get("gptkey", "")
16
+
17
+ claude_api_key = os.environ.get("claudekey", "")
18
 
19
  # 定義每個類型的句子
20
  achievement_statements = [
 
60
  "比起和他人待在一起,我更喜歡獨處",
61
  ]
62
 
 
63
 
64
+ ############################################
65
+ # 產生 Prompt
66
+ ############################################
67
+
68
+ def generate_prompt(achievement: str, emotion: str, information: str,
69
+ possession: str, power: str, status: str) -> str:
70
+ """
71
+ 根據使用者針對六個類型選擇「正」或「負」,產生對應的 Prompt。
72
+ """
73
  category_map = {
74
  '成就型': achievement_statements,
75
  '情感型': emotion_statements,
 
109
 
110
  return prompt
111
 
112
+ ############################################
113
+ # 呼叫模型 API (OpenAI / Claude)
114
+ ############################################
115
+
116
+ def call_openai_api(prompt: str, model_name: str = "gpt-3.5-turbo") -> str:
117
+ """呼叫 OpenAI API"""
118
+ if not openai.api_key:
119
+ return "[OpenAI API Key 未設定,無法呼叫 API]"
120
+
121
+ try:
122
+ response = openai.ChatCompletion.create(
123
+ model=model_name,
124
+ messages=[{"role": "user", "content": prompt}],
125
+ temperature=0.7,
126
+ max_tokens=1024
127
+ )
128
+ return response.choices[0].message.content
129
+ except Exception as e:
130
+ return f"[OpenAI API 呼叫失敗]: {str(e)}"
131
+
132
+
133
+ def call_claude_api(prompt: str) -> str:
134
+ """呼叫 Claude API"""
135
+ if not claude_api_key or not Anthropic:
136
+ return "[Claude API Key 未設定或未安裝 anthropic 套件,無法呼叫 API]"
137
+
138
+ try:
139
+ client = Anthropic(api_key=claude_api_key)
140
+ conversation = client.completions.create(
141
+ prompt=f"{HUMAN_PROMPT} {prompt}{AI_PROMPT}",
142
+ model="claude-v1.3", # 這裡可調整對應的 Claude 模型
143
+ max_tokens_to_sample=1024,
144
+ temperature=0.7,
145
+ )
146
+ return conversation.completion.strip()
147
+ except Exception as e:
148
+ return f"[Claude API 呼叫失敗]: {str(e)}"
149
+
150
+
151
+ def get_model_answer(model_choice: str, final_prompt: str, question: str) -> str:
152
+ """
153
+ 根據 model_choice 呼叫對應的 API。
154
+ """
155
+ # 最終送出給模型的內容
156
+ content_to_send = final_prompt + "\n\n" + question
157
+
158
+ if model_choice == "ChatGPT 4o":
159
+ return call_openai_api(content_to_send, model_name="gpt-4")
160
+ elif model_choice == "ChatGPT o1":
161
+ return call_openai_api(content_to_send, model_name="gpt-3.5-turbo")
162
+ elif model_choice == "Claude 3.5 Sonnet":
163
+ return call_claude_api(content_to_send)
164
+ else:
165
+ return "[錯誤] 未知的模型選擇"
166
+
167
+
168
+ ############################################
169
+ # Gradio 介面
170
+ ############################################
171
+
172
+ def main():
173
+ # 預設問題
174
+ predefined_questions = {
175
+ "請介紹你自己": "請介紹你自己,讓我們了解你是什麼樣的人。你可以分享你的經歷、興趣、價值觀,以及未來規劃。特別說明一下,當你遇到挑戰時,你通常會如何面對?",
176
+ "如何分配意外收入": "如果你突然收到一筆意外收入,你會如何分配這筆錢來購物?請列出前三項想購買的東西,並說明購買原因。",
177
+ "新專案團隊問題": "假設你被指派帶領一個新專案,但發現團隊成員能力參差不齊,預算有限,且截止日期緊迫,你會如何處理?",
178
+ "自訂提問": ""
179
+ }
180
 
181
+ with gr.Blocks() as demo:
182
+ gr.Markdown("## Prompt 產生器")
183
+
184
+ with gr.Row():
185
+ achievement = gr.Radio(["正", "負"], label="成就型", value="正")
186
+ emotion = gr.Radio(["正", "負"], label="情感型", value="正")
187
+ information = gr.Radio(["正", "負"], label="資訊型", value="正")
188
+ possession = gr.Radio(["正", "負"], label="佔有型", value="正")
189
+ power = gr.Radio(["正", "負"], label="權力型", value="正")
190
+ status = gr.Radio(["正", "負"], label="地位型", value="正")
191
+
192
+ prompt_output = gr.Textbox(label="生成的 Prompt", lines=10)
193
+ generate_button = gr.Button("生成 Prompt")
194
+
195
+ # 下方選擇模型
196
+ model_choice = gr.Radio(
197
+ ["ChatGPT 4o", "ChatGPT o1", "Claude 3.5 Sonnet"],
198
+ label="選擇模型",
199
+ value="ChatGPT 4o"
200
+ )
201
+
202
+ # 選擇問題或自訂
203
+ question_choice = gr.Radio(
204
+ list(predefined_questions.keys()),
205
+ label="選擇一個提問",
206
+ value="請介紹你自己"
207
+ )
208
+ custom_question = gr.Textbox(
209
+ label="自訂提問 (若選擇 '自訂提問' 時才使用)",
210
+ lines=2,
211
+ placeholder="若上面選擇了 '自訂提問',請在此輸入問題"
212
+ )
213
+
214
+ answer_output = gr.Textbox(label="模型回應", lines=10)
215
+ ask_button = gr.Button("送出問題並取得回答")
216
+
217
+ def on_generate_prompt(a, e, i, p, pow_, s):
218
+ return generate_prompt(a, e, i, p, pow_, s)
219
+
220
+ generate_button.click(
221
+ fn=on_generate_prompt,
222
+ inputs=[achievement, emotion, information, possession, power, status],
223
+ outputs=prompt_output
224
+ )
225
+
226
+ def on_ask_question(m_choice, p_output, q_choice, c_question):
227
+ if q_choice == "自訂提問":
228
+ if not c_question.strip():
229
+ return "[錯誤] 你選擇了自訂提問,但沒有輸入內容"
230
+ question = c_question.strip()
231
+ else:
232
+ question = predefined_questions[q_choice]
233
+
234
+ # 呼叫對應的模型
235
+ answer = get_model_answer(m_choice, p_output, question)
236
+ return answer
237
+
238
+ ask_button.click(
239
+ fn=on_ask_question,
240
+ inputs=[model_choice, prompt_output, question_choice, custom_question],
241
+ outputs=answer_output
242
+ )
243
 
244
+ demo.launch()
 
245
 
 
 
 
 
 
246
 
247
  if __name__ == "__main__":
248
+ main()