Kims12 commited on
Commit
c753d25
·
verified ·
1 Parent(s): f1d1009

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +159 -218
app.py CHANGED
@@ -1,7 +1,6 @@
1
  import gradio as gr
2
  from huggingface_hub import InferenceClient
3
  import openai
4
- import os
5
 
6
  # 제거할 모델들을 MODELS 사전에서 제외
7
  MODELS = {
@@ -17,7 +16,6 @@ MODELS = {
17
  # Cohere Command R+ 모델 ID 정의
18
  COHERE_MODEL = "CohereForAI/c4ai-command-r-plus-08-2024"
19
 
20
-
21
  def get_client(model_name, hf_token):
22
  """
23
  모델 이름에 맞춰 InferenceClient 생성.
@@ -35,91 +33,68 @@ def get_client(model_name, hf_token):
35
  return InferenceClient(model_id, token=hf_token)
36
 
37
 
38
- def respond(
39
- message,
40
- chat_history,
41
- model_name,
42
- max_tokens,
43
- temperature,
44
- top_p,
45
- system_message,
46
- hf_token, # HF 토큰을 추가로 받음
47
  ):
 
 
 
48
  try:
49
  client = get_client(model_name, hf_token)
50
  except ValueError as e:
51
- chat_history.append((message, str(e)))
52
- return chat_history
53
 
54
- messages = [{"role": "system", "content": system_message}]
55
- for human, assistant in chat_history:
56
- messages.append({"role": "user", "content": human})
57
- messages.append({"role": "assistant", "content": assistant})
58
- messages.append({"role": "user", "content": message})
59
 
60
  try:
61
- if model_name == "Cohere Command R+":
62
- # Cohere Command R+ 모델을 위한 비스트리밍 처리
63
- response = client.chat_completion(
64
- messages,
65
- max_tokens=max_tokens,
66
- temperature=temperature,
67
- top_p=top_p,
68
- )
69
- assistant_message = response.choices[0].message.content
70
- chat_history.append((message, assistant_message))
71
- return chat_history
72
- else:
73
- # 다른 모델들을 위한 스트리밍 처리
74
- stream = client.chat_completion(
75
- messages,
76
- max_tokens=max_tokens,
77
- temperature=temperature,
78
- top_p=top_p,
79
- stream=True,
80
- )
81
- partial_message = ""
82
- for response in stream:
83
- if response.choices[0].delta.content is not None:
84
- partial_message += response.choices[0].delta.content
85
- if len(chat_history) > 0 and chat_history[-1][0] == message:
86
- chat_history[-1] = (message, partial_message)
87
- else:
88
- chat_history.append((message, partial_message))
89
- yield chat_history
90
  except Exception as e:
91
- error_message = f"오류가 발생했습니다: {str(e)}"
92
- chat_history.append((message, error_message))
93
- yield chat_history
94
-
95
-
96
- def cohere_respond(
97
- message,
98
- chat_history,
99
- system_message,
100
- max_tokens,
101
- temperature,
102
- top_p,
103
- hf_token, # HF 토큰 추가
104
  ):
 
 
 
105
  model_name = "Cohere Command R+"
106
  try:
107
  client = get_client(model_name, hf_token)
108
  except ValueError as e:
109
- chat_history.append((message, str(e)))
110
- return chat_history
111
-
112
- messages = [{"role": "system", "content": system_message}]
113
- for human, assistant in chat_history:
114
- if human:
115
- messages.append({"role": "user", "content": human})
116
- if assistant:
117
- messages.append({"role": "assistant", "content": assistant})
118
 
119
- messages.append({"role": "user", "content": message})
 
 
 
120
 
121
  try:
122
- # Cohere Command R+ 모델을 위한 비스트리밍 처리
123
  response_full = client.chat_completion(
124
  messages,
125
  max_tokens=max_tokens,
@@ -127,65 +102,50 @@ def cohere_respond(
127
  top_p=top_p,
128
  )
129
  assistant_message = response_full.choices[0].message.content
130
- chat_history.append((message, assistant_message))
131
- return chat_history
132
  except Exception as e:
133
- error_message = f"오류가 발생했습니다: {str(e)}"
134
- chat_history.append((message, error_message))
135
- return chat_history
136
-
137
-
138
- def chatgpt_respond(
139
- message,
140
- chat_history,
141
- system_message,
142
- max_tokens,
143
- temperature,
144
- top_p,
145
- openai_token, # openai 토큰 추가
146
  ):
147
  """
148
- chatgpt용 응답. openai_token을 UI에서 입력받아 사용하도록 변경.
149
  """
150
  if not openai_token:
151
- chat_history.append((message, "OpenAI API 토큰이 필요합니다."))
152
- return chat_history
153
 
154
- # openai.api_key = os.getenv("OPENAI_API_KEY") # 기존 코드 주석
155
- openai.api_key = openai_token # UI에서 받은 토큰 사용
156
 
157
- messages = [{"role": "system", "content": system_message}]
158
- for human, assistant in chat_history:
159
- messages.append({"role": "user", "content": human})
160
- messages.append({"role": "assistant", "content": assistant})
161
- messages.append({"role": "user", "content": message})
162
 
163
  try:
164
  response = openai.ChatCompletion.create(
165
- model="gpt-4o-mini", # 또는 다른 모델 ID 사용
166
  messages=messages,
167
  max_tokens=max_tokens,
168
  temperature=temperature,
169
  top_p=top_p,
170
  )
171
  assistant_message = response.choices[0].message['content']
172
- chat_history.append((message, assistant_message))
173
- return chat_history
174
  except Exception as e:
175
- error_message = f"오류가 발생했습니다: {str(e)}"
176
- chat_history.append((message, error_message))
177
- return chat_history
178
-
179
-
180
- def clear_conversation():
181
- return []
182
 
183
 
184
  with gr.Blocks() as demo:
185
- gr.Markdown("# Prompting AI Chatbot")
186
- gr.Markdown("언어모델별 프롬프트 테스트 챗봇입니다.")
187
 
188
- # --- 토큰 입력 UI 추가 ---
189
  with gr.Row():
190
  hf_token_box = gr.Textbox(
191
  label="HuggingFace 토큰 (비공개)",
@@ -198,127 +158,108 @@ with gr.Blocks() as demo:
198
  placeholder="OpenAI API 토큰을 입력하세요..."
199
  )
200
 
 
201
  with gr.Tab("일반 모델"):
202
- with gr.Row():
203
- with gr.Column(scale=1):
204
- model_name = gr.Radio(
205
- choices=list(MODELS.keys()),
206
- label="Language Model",
207
- value="Zephyr 7B Beta"
208
- )
209
- max_tokens = gr.Slider(minimum=0, maximum=2000, value=500, step=100, label="Max Tokens")
210
- temperature = gr.Slider(minimum=0.1, maximum=2.0, value=0.7, step=0.05, label="Temperature")
211
- top_p = gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p")
212
- system_message = gr.Textbox(
213
- value="""반드시 한글로 답변할 것.
214
  너는 최고의 비서이다.
215
  내가 요구하는것들을 최대한 자세하고 정확하게 답변하라.
216
  """,
217
- label="System Message",
218
- lines=3
219
- )
220
-
221
- with gr.Column(scale=2):
222
- chatbot = gr.Chatbot()
223
- msg = gr.Textbox(label="메세지를 입력하세요")
224
- with gr.Row():
225
- submit_button = gr.Button("전송")
226
- clear_button = gr.Button("대화 내역 지우기")
227
-
228
- # respond 함수에 hf_token 인자를 전달하도록 수정
229
- inputs_for_normal = [
230
- msg,
231
- chatbot,
232
- model_name,
233
- max_tokens,
234
- temperature,
235
- top_p,
236
- system_message,
237
- hf_token_box
238
- ]
239
- msg.submit(respond, inputs_for_normal, chatbot)
240
- submit_button.click(respond, inputs_for_normal, chatbot)
241
- clear_button.click(clear_conversation, outputs=chatbot, queue=False)
242
-
243
  with gr.Tab("Cohere Command R+"):
244
- with gr.Row():
245
- cohere_system_message = gr.Textbox(
246
- value="""반드시 한글로 답변할 것.
247
  너는 최고의 비서이다.
248
  내가 요구하는것들을 최대한 자세하고 정확하게 답변하라.
249
  """,
250
- label="System Message",
251
- lines=3
252
- )
253
- cohere_max_tokens = gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens")
254
- cohere_temperature = gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature")
255
- cohere_top_p = gr.Slider(
256
- minimum=0.1,
257
- maximum=1.0,
258
- value=0.95,
259
- step=0.05,
260
- label="Top-P",
261
- )
262
-
263
- cohere_chatbot = gr.Chatbot(height=600)
264
- cohere_msg = gr.Textbox(label="메세지를 입력하세요")
265
- with gr.Row():
266
- cohere_submit_button = gr.Button("전송")
267
- cohere_clear_button = gr.Button("대화 내역 지우기")
268
-
269
- # cohere_respond 함수에 hf_token 인자를 전달하도록 수정
270
- inputs_for_cohere = [
271
- cohere_msg,
272
- cohere_chatbot,
273
- cohere_system_message,
274
- cohere_max_tokens,
275
- cohere_temperature,
276
- cohere_top_p,
277
- hf_token_box
278
- ]
279
- cohere_msg.submit(cohere_respond, inputs_for_cohere, cohere_chatbot)
280
- cohere_submit_button.click(cohere_respond, inputs_for_cohere, cohere_chatbot)
281
- cohere_clear_button.click(clear_conversation, outputs=cohere_chatbot, queue=False)
282
-
283
  with gr.Tab("ChatGPT"):
284
- with gr.Row():
285
- chatgpt_system_message = gr.Textbox(
286
- value="""반드시 한글로 답변할 것.
287
  너는 ChatGPT, OpenAI에서 개발한 언어 모델이다.
288
  내가 요구하는 것을 최대한 자세하고 정확하게 답변하라.
289
  """,
290
- label="System Message",
291
- lines=3
292
- )
293
- chatgpt_max_tokens = gr.Slider(minimum=1, maximum=4096, value=1024, step=1, label="Max Tokens")
294
- chatgpt_temperature = gr.Slider(minimum=0.1, maximum=2.0, value=0.7, step=0.05, label="Temperature")
295
- chatgpt_top_p = gr.Slider(
296
- minimum=0.1,
297
- maximum=1.0,
298
- value=0.95,
299
- step=0.05,
300
- label="Top-P",
301
- )
302
-
303
- chatgpt_chatbot = gr.Chatbot(height=600)
304
- chatgpt_msg = gr.Textbox(label="메세지를 입력하세요")
305
- with gr.Row():
306
- chatgpt_submit_button = gr.Button("전송")
307
- chatgpt_clear_button = gr.Button("대화 내역 지우기")
308
-
309
- # chatgpt_respond 함수에 openai_token 인자를 전달하도록 수정
310
- inputs_for_chatgpt = [
311
- chatgpt_msg,
312
- chatgpt_chatbot,
313
- chatgpt_system_message,
314
- chatgpt_max_tokens,
315
- chatgpt_temperature,
316
- chatgpt_top_p,
317
- openai_token_box
318
- ]
319
- chatgpt_msg.submit(chatgpt_respond, inputs_for_chatgpt, chatgpt_chatbot)
320
- chatgpt_submit_button.click(chatgpt_respond, inputs_for_chatgpt, chatgpt_chatbot)
321
- chatgpt_clear_button.click(clear_conversation, outputs=chatgpt_chatbot, queue=False)
322
 
323
  if __name__ == "__main__":
324
  demo.launch()
 
1
  import gradio as gr
2
  from huggingface_hub import InferenceClient
3
  import openai
 
4
 
5
  # 제거할 모델들을 MODELS 사전에서 제외
6
  MODELS = {
 
16
  # Cohere Command R+ 모델 ID 정의
17
  COHERE_MODEL = "CohereForAI/c4ai-command-r-plus-08-2024"
18
 
 
19
  def get_client(model_name, hf_token):
20
  """
21
  모델 이름에 맞춰 InferenceClient 생성.
 
33
  return InferenceClient(model_id, token=hf_token)
34
 
35
 
36
+ def respond_hf_qna(
37
+ question: str,
38
+ model_name: str,
39
+ max_tokens: int,
40
+ temperature: float,
41
+ top_p: float,
42
+ system_message: str,
43
+ hf_token: str
 
44
  ):
45
+ """
46
+ HuggingFace 모델(Zephyr 등)에 대해 한 번의 질문(question)에 대한 답변을 반환하는 함수.
47
+ """
48
  try:
49
  client = get_client(model_name, hf_token)
50
  except ValueError as e:
51
+ return f"오류: {str(e)}"
 
52
 
53
+ # 시스템 메시지 + 유저 질문을 한 번만 전달
54
+ messages = [
55
+ {"role": "system", "content": system_message},
56
+ {"role": "user", "content": question}
57
+ ]
58
 
59
  try:
60
+ # 스트리밍 대신 전체 답변(비스트리밍) 호출
61
+ response = client.chat_completion(
62
+ messages,
63
+ max_tokens=max_tokens,
64
+ temperature=temperature,
65
+ top_p=top_p,
66
+ stream=False, # 스트리밍 비활성화
67
+ )
68
+ assistant_message = response.choices[0].message.content
69
+ return assistant_message
70
+
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
71
  except Exception as e:
72
+ return f"오류가 발생했습니다: {str(e)}"
73
+
74
+
75
+ def respond_cohere_qna(
76
+ question: str,
77
+ system_message: str,
78
+ max_tokens: int,
79
+ temperature: float,
80
+ top_p: float,
81
+ hf_token: str
 
 
 
82
  ):
83
+ """
84
+ Cohere Command R+ 모델을 이용해 한 번의 질문(question)에 대한 답변을 반환하는 함수.
85
+ """
86
  model_name = "Cohere Command R+"
87
  try:
88
  client = get_client(model_name, hf_token)
89
  except ValueError as e:
90
+ return f"오류: {str(e)}"
 
 
 
 
 
 
 
 
91
 
92
+ messages = [
93
+ {"role": "system", "content": system_message},
94
+ {"role": "user", "content": question}
95
+ ]
96
 
97
  try:
 
98
  response_full = client.chat_completion(
99
  messages,
100
  max_tokens=max_tokens,
 
102
  top_p=top_p,
103
  )
104
  assistant_message = response_full.choices[0].message.content
105
+ return assistant_message
 
106
  except Exception as e:
107
+ return f"오류가 발생했습니다: {str(e)}"
108
+
109
+
110
+ def respond_chatgpt_qna(
111
+ question: str,
112
+ system_message: str,
113
+ max_tokens: int,
114
+ temperature: float,
115
+ top_p: float,
116
+ openai_token: str
 
 
 
117
  ):
118
  """
119
+ ChatGPT(OpenAI) 모델을 이용해 번의 질문(question)에 대한 답변을 반환하는 함수.
120
  """
121
  if not openai_token:
122
+ return "OpenAI API 토큰이 필요합니다."
 
123
 
124
+ openai.api_key = openai_token
 
125
 
126
+ messages = [
127
+ {"role": "system", "content": system_message},
128
+ {"role": "user", "content": question}
129
+ ]
 
130
 
131
  try:
132
  response = openai.ChatCompletion.create(
133
+ model="gpt-4o-mini", # 필요한 경우 변경
134
  messages=messages,
135
  max_tokens=max_tokens,
136
  temperature=temperature,
137
  top_p=top_p,
138
  )
139
  assistant_message = response.choices[0].message['content']
140
+ return assistant_message
 
141
  except Exception as e:
142
+ return f"오류가 발생했습니다: {str(e)}"
 
 
 
 
 
 
143
 
144
 
145
  with gr.Blocks() as demo:
146
+ gr.Markdown("# Prompting AI - 일반 문답형 데모")
147
+ gr.Markdown("언어모델별 문답형 테스트 데모입니다. 한 번에 한 질문씩만 주고받습니다.")
148
 
 
149
  with gr.Row():
150
  hf_token_box = gr.Textbox(
151
  label="HuggingFace 토큰 (비공개)",
 
158
  placeholder="OpenAI API 토큰을 입력하세요..."
159
  )
160
 
161
+ # --- Tab: 일반 모델 ---
162
  with gr.Tab("일반 모델"):
163
+ model_name = gr.Radio(
164
+ choices=list(MODELS.keys()),
165
+ label="Language Model (HuggingFace)",
166
+ value="Zephyr 7B Beta"
167
+ )
168
+ max_tokens = gr.Slider(minimum=0, maximum=2000, value=500, step=100, label="Max Tokens")
169
+ temperature = gr.Slider(minimum=0.1, maximum=2.0, value=0.7, step=0.05, label="Temperature")
170
+ top_p = gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p")
171
+ system_message = gr.Textbox(
172
+ value="""반드시 한글로 답변할 것.
 
 
173
  너는 최고의 비서이다.
174
  내가 요구하는것들을 최대한 자세하고 정확하게 답변하라.
175
  """,
176
+ label="System Message",
177
+ lines=3
178
+ )
179
+
180
+ question_input = gr.Textbox(label="질문을 입력하세요")
181
+ answer_output = gr.Textbox(label="답변", interactive=False)
182
+
183
+ # '전송' 버튼 클릭 시 HF 모델 QnA 함수 호출
184
+ submit_button = gr.Button("전송")
185
+
186
+ submit_button.click(
187
+ fn=respond_hf_qna,
188
+ inputs=[
189
+ question_input,
190
+ model_name,
191
+ max_tokens,
192
+ temperature,
193
+ top_p,
194
+ system_message,
195
+ hf_token_box
196
+ ],
197
+ outputs=answer_output
198
+ )
199
+
200
+ # --- Tab: Cohere Command R+ ---
 
201
  with gr.Tab("Cohere Command R+"):
202
+ cohere_system_message = gr.Textbox(
203
+ value="""반드시 한글로 답변할 것.
 
204
  너는 최고의 비서이다.
205
  내가 요구하는것들을 최대한 자세하고 정확하게 답변하라.
206
  """,
207
+ label="System Message",
208
+ lines=3
209
+ )
210
+ cohere_max_tokens = gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max Tokens")
211
+ cohere_temperature = gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature")
212
+ cohere_top_p = gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-P")
213
+
214
+ cohere_question_input = gr.Textbox(label="질문을 입력하세요")
215
+ cohere_answer_output = gr.Textbox(label="답변", interactive=False)
216
+
217
+ cohere_submit_button = gr.Button("전송")
218
+
219
+ cohere_submit_button.click(
220
+ fn=respond_cohere_qna,
221
+ inputs=[
222
+ cohere_question_input,
223
+ cohere_system_message,
224
+ cohere_max_tokens,
225
+ cohere_temperature,
226
+ cohere_top_p,
227
+ hf_token_box
228
+ ],
229
+ outputs=cohere_answer_output
230
+ )
231
+
232
+ # --- Tab: ChatGPT ---
 
 
 
 
 
 
 
233
  with gr.Tab("ChatGPT"):
234
+ chatgpt_system_message = gr.Textbox(
235
+ value="""반드시 한글로 답변할 것.
 
236
  너는 ChatGPT, OpenAI에서 개발한 언어 모델이다.
237
  내가 요구하는 것을 최대한 자세하고 정확하게 답변하라.
238
  """,
239
+ label="System Message",
240
+ lines=3
241
+ )
242
+ chatgpt_max_tokens = gr.Slider(minimum=1, maximum=4096, value=1024, step=1, label="Max Tokens")
243
+ chatgpt_temperature = gr.Slider(minimum=0.1, maximum=2.0, value=0.7, step=0.05, label="Temperature")
244
+ chatgpt_top_p = gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-P")
245
+
246
+ chatgpt_question_input = gr.Textbox(label="질문을 입력하세요")
247
+ chatgpt_answer_output = gr.Textbox(label="답변", interactive=False)
248
+
249
+ chatgpt_submit_button = gr.Button("전송")
250
+
251
+ chatgpt_submit_button.click(
252
+ fn=respond_chatgpt_qna,
253
+ inputs=[
254
+ chatgpt_question_input,
255
+ chatgpt_system_message,
256
+ chatgpt_max_tokens,
257
+ chatgpt_temperature,
258
+ chatgpt_top_p,
259
+ openai_token_box
260
+ ],
261
+ outputs=chatgpt_answer_output
262
+ )
 
 
 
 
 
 
 
 
263
 
264
  if __name__ == "__main__":
265
  demo.launch()