Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,61 +1,6 @@
|
|
1 |
import gradio as gr
|
2 |
-
from huggingface_hub import InferenceClient
|
3 |
import openai
|
4 |
-
import anthropic
|
5 |
import os
|
6 |
-
from typing import Optional
|
7 |
-
|
8 |
-
#############################
|
9 |
-
# [기본코드] - 수정/삭제 불가
|
10 |
-
#############################
|
11 |
-
|
12 |
-
# Cohere Command R+ 모델 ID 정의
|
13 |
-
COHERE_MODEL = "CohereForAI/c4ai-command-r-plus-08-2024"
|
14 |
-
|
15 |
-
def get_client(model_name):
|
16 |
-
"""
|
17 |
-
모델 이름에 맞춰 InferenceClient 생성.
|
18 |
-
토큰은 환경 변수에서 가져옴.
|
19 |
-
"""
|
20 |
-
hf_token = os.getenv("HF_TOKEN")
|
21 |
-
if not hf_token:
|
22 |
-
raise ValueError("HuggingFace API 토큰이 필요합니다.")
|
23 |
-
if model_name == "Cohere Command R+":
|
24 |
-
model_id = COHERE_MODEL
|
25 |
-
else:
|
26 |
-
raise ValueError("유효하지 않은 모델 이름입니다.")
|
27 |
-
return InferenceClient(model_id, token=hf_token)
|
28 |
-
|
29 |
-
def respond_cohere_qna(
|
30 |
-
question: str,
|
31 |
-
system_message: str,
|
32 |
-
max_tokens: int,
|
33 |
-
temperature: float,
|
34 |
-
top_p: float
|
35 |
-
):
|
36 |
-
"""
|
37 |
-
Cohere Command R+ 모델을 이용해 한 번의 질문(question)에 대한 답변을 반환하는 함수.
|
38 |
-
"""
|
39 |
-
model_name = "Cohere Command R+"
|
40 |
-
try:
|
41 |
-
client = get_client(model_name)
|
42 |
-
except ValueError as e:
|
43 |
-
return f"오류: {str(e)}"
|
44 |
-
messages = [
|
45 |
-
{"role": "system", "content": system_message},
|
46 |
-
{"role": "user", "content": question}
|
47 |
-
]
|
48 |
-
try:
|
49 |
-
response_full = client.chat_completion(
|
50 |
-
messages,
|
51 |
-
max_tokens=max_tokens,
|
52 |
-
temperature=temperature,
|
53 |
-
top_p=top_p,
|
54 |
-
)
|
55 |
-
assistant_message = response_full.choices[0].message.content
|
56 |
-
return assistant_message
|
57 |
-
except Exception as e:
|
58 |
-
return f"오류가 발생했습니다: {str(e)}"
|
59 |
|
60 |
def respond_chatgpt_qna(
|
61 |
question: str,
|
@@ -65,7 +10,7 @@ def respond_chatgpt_qna(
|
|
65 |
top_p: float
|
66 |
):
|
67 |
"""
|
68 |
-
|
69 |
"""
|
70 |
openai_token = os.getenv("OPENAI_TOKEN")
|
71 |
if not openai_token:
|
@@ -88,456 +33,46 @@ def respond_chatgpt_qna(
|
|
88 |
except Exception as e:
|
89 |
return f"오류가 발생했습니다: {str(e)}"
|
90 |
|
91 |
-
def
|
92 |
-
question: str,
|
93 |
-
system_message: str,
|
94 |
-
max_tokens: int,
|
95 |
-
temperature: float,
|
96 |
-
top_p: float,
|
97 |
-
model_name: str # 모델 이름 추가
|
98 |
-
):
|
99 |
"""
|
100 |
-
|
101 |
"""
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
temperature=temperature,
|
117 |
-
top_p=top_p,
|
118 |
-
)
|
119 |
-
assistant_message = response.choices[0].message['content']
|
120 |
-
return assistant_message
|
121 |
-
except Exception as e:
|
122 |
-
return f"오류가 발생했습니다: {str(e)}"
|
123 |
-
|
124 |
-
def respond_claude_qna(
|
125 |
-
question: str,
|
126 |
-
system_message: str,
|
127 |
-
max_tokens: int,
|
128 |
-
temperature: float,
|
129 |
-
top_p: float,
|
130 |
-
model_name: str # 모델 이름 파라미터 추가
|
131 |
-
) -> str:
|
132 |
-
"""
|
133 |
-
Claude API를 사용한 개선된 응답 생성 함수.
|
134 |
-
"""
|
135 |
-
claude_api_key = os.getenv("CLAUDE_TOKEN")
|
136 |
-
if not claude_api_key:
|
137 |
-
return "Claude API 토큰이 필요합니다."
|
138 |
-
try:
|
139 |
-
client = anthropic.Anthropic(api_key=claude_api_key)
|
140 |
-
message = client.messages.create(
|
141 |
-
model=model_name,
|
142 |
-
max_tokens=max_tokens,
|
143 |
-
temperature=temperature,
|
144 |
-
system=system_message,
|
145 |
-
messages=[
|
146 |
-
{"role": "user", "content": question}
|
147 |
-
]
|
148 |
-
)
|
149 |
-
return message.content[0].text
|
150 |
-
except anthropic.APIError as ae:
|
151 |
-
return f"Claude API 오류: {str(ae)}"
|
152 |
-
except anthropic.RateLimitError:
|
153 |
-
return "요청 한도를 초과했습니다. 잠시 후 다시 시도해주세요."
|
154 |
-
except Exception as e:
|
155 |
-
return f"예상치 못한 오류가 발생했습니다: {str(e)}"
|
156 |
-
|
157 |
-
def respond_o1mini_qna(
|
158 |
-
question: str,
|
159 |
-
system_message: str,
|
160 |
-
max_tokens: int,
|
161 |
-
temperature: float
|
162 |
-
):
|
163 |
-
"""
|
164 |
-
o1-mini 모델을 이용해 한 번의 질문(question)에 대한 답변을 반환하는 함수.
|
165 |
-
o1-mini에서는 'system' 메시지를 지원하지 않으므로 system_message와 question을 하나��� 'user' 메시지로 합쳐 전달합니다.
|
166 |
-
또한, o1-mini에서는 'max_tokens' 대신 'max_completion_tokens'를 사용하며, temperature는 고정값 1만 지원합니다.
|
167 |
-
"""
|
168 |
-
openai_token = os.getenv("OPENAI_TOKEN")
|
169 |
-
if not openai_token:
|
170 |
-
return "OpenAI API 토큰이 필요합니다."
|
171 |
-
openai.api_key = openai_token
|
172 |
-
combined_message = f"{system_message}\n\n{question}"
|
173 |
-
messages = [{"role": "user", "content": combined_message}]
|
174 |
-
try:
|
175 |
-
response = openai.ChatCompletion.create(
|
176 |
-
model="o1-mini",
|
177 |
-
messages=messages,
|
178 |
-
max_completion_tokens=max_tokens,
|
179 |
-
temperature=1, # 고정된 값 1 사용
|
180 |
-
)
|
181 |
-
assistant_message = response.choices[0].message['content']
|
182 |
-
return assistant_message
|
183 |
-
except Exception as e:
|
184 |
-
return f"오류가 발생했습니다: {str(e)}"
|
185 |
-
|
186 |
-
def respond_gemini_qna(
|
187 |
-
question: str,
|
188 |
-
system_message: str,
|
189 |
-
max_tokens: int,
|
190 |
-
temperature: float,
|
191 |
-
top_p: float, # top_p는 Gemini API에서 지원되면 전달됩니다.
|
192 |
-
model_id: str
|
193 |
-
):
|
194 |
-
"""
|
195 |
-
Gemini 모델(예: "gemini-2.0-flash", "gemini-2.0-flash-lite-preview-02-05")을 이용해
|
196 |
-
질문(question)에 대한 답변을 반환하는 함수.
|
197 |
-
최신 google-generativeai 라이브러리를 사용합니다.
|
198 |
-
"""
|
199 |
-
import os
|
200 |
-
try:
|
201 |
-
import google.generativeai as genai
|
202 |
-
except ModuleNotFoundError:
|
203 |
-
return ("오류가 발생했습니다: 'google-generativeai' 모듈을 찾을 수 없습니다. "
|
204 |
-
"해결 방법: 'pip install --upgrade google-generativeai' 를 실행하여 설치해주세요.")
|
205 |
-
|
206 |
-
gemini_api_key = os.getenv("GEMINI_API_KEY")
|
207 |
-
if not gemini_api_key:
|
208 |
-
return "Gemini API 토큰이 필요합니다."
|
209 |
-
|
210 |
-
# API 키 설정
|
211 |
-
genai.configure(api_key=gemini_api_key)
|
212 |
-
|
213 |
-
# system_message와 question을 하나의 프롬프트로 결합
|
214 |
-
prompt = f"{system_message}\n\n{question}"
|
215 |
-
|
216 |
-
try:
|
217 |
-
# 최신 SDK에서는 GenerativeModel 클래스를 사용합니다.
|
218 |
-
model = genai.GenerativeModel(model_name=model_id)
|
219 |
-
response = model.generate_content(prompt)
|
220 |
-
return response.text
|
221 |
-
except Exception as e:
|
222 |
-
return f"오류가 발생했습니다: {str(e)}"
|
223 |
-
|
224 |
-
#############################
|
225 |
-
# [기본코드] UI 부분 - 수정/삭제 불가 (탭 순서: OpenAI, Gemini, Claude, DeepSeek, Cohere Command R+)
|
226 |
-
#############################
|
227 |
|
228 |
with gr.Blocks() as demo:
|
229 |
-
gr.Markdown("#
|
230 |
-
|
231 |
-
|
232 |
-
|
233 |
-
|
234 |
-
|
235 |
-
|
236 |
-
|
237 |
-
|
238 |
-
|
239 |
-
|
240 |
-
|
241 |
-
|
242 |
-
|
243 |
-
|
244 |
-
|
245 |
-
|
246 |
-
|
247 |
-
|
248 |
-
|
249 |
-
value="""반드시 한글로 답변할 것.
|
250 |
-
너는 ChatGPT, OpenAI에서 개발한 언어 모델이다.
|
251 |
-
내가 요구하는 것을 최대한 자세하고 정확하게 답변하라.
|
252 |
-
""",
|
253 |
-
label="System Message",
|
254 |
-
lines=3
|
255 |
-
)
|
256 |
-
chatgpt_max_tokens_o = gr.Slider(minimum=100, maximum=4000, value=2000, step=100, label="Max Tokens")
|
257 |
-
chatgpt_temperature_o = gr.Slider(minimum=0.1, maximum=2.0, value=0.7, step=0.05, label="Temperature")
|
258 |
-
chatgpt_top_p_o = gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-P")
|
259 |
-
chatgpt_submit_button_o = gr.Button("전송")
|
260 |
-
|
261 |
-
def merge_and_call_chatgpt_o(i1, i2, i3, i4, i5, sys_msg, mt, temp, top_p_):
|
262 |
-
question = " ".join([i1, i2, i3, i4, i5])
|
263 |
-
return respond_chatgpt_qna(
|
264 |
-
question=question,
|
265 |
-
system_message=sys_msg,
|
266 |
-
max_tokens=mt,
|
267 |
-
temperature=temp,
|
268 |
-
top_p=top_p_
|
269 |
-
)
|
270 |
-
chatgpt_submit_button_o.click(
|
271 |
-
fn=merge_and_call_chatgpt_o,
|
272 |
-
inputs=[
|
273 |
-
chatgpt_input1_o, chatgpt_input2_o, chatgpt_input3_o, chatgpt_input4_o, chatgpt_input5_o,
|
274 |
-
chatgpt_system_message_o,
|
275 |
-
chatgpt_max_tokens_o,
|
276 |
-
chatgpt_temperature_o,
|
277 |
-
chatgpt_top_p_o
|
278 |
-
],
|
279 |
-
outputs=chatgpt_answer_output_o
|
280 |
-
)
|
281 |
-
|
282 |
-
with gr.Column(visible=False) as o1mini_ui:
|
283 |
-
o1mini_input1_o = gr.Textbox(label="입력1", lines=1)
|
284 |
-
o1mini_input2_o = gr.Textbox(label="입력2", lines=1)
|
285 |
-
o1mini_input3_o = gr.Textbox(label="입력3", lines=1)
|
286 |
-
o1mini_input4_o = gr.Textbox(label="입력4", lines=1)
|
287 |
-
o1mini_input5_o = gr.Textbox(label="입력5", lines=1)
|
288 |
-
o1mini_answer_output_o = gr.Textbox(label="결과", lines=5, interactive=False)
|
289 |
-
with gr.Accordion("고급 설정 (o1-mini)", open=False):
|
290 |
-
o1mini_system_message_o = gr.Textbox(
|
291 |
-
value="""반드시 한글로 답변할 것.
|
292 |
-
너는 o1-mini, OpenAI에서 개발한 경량 언어 모델이다.
|
293 |
-
내가 요구하는 것을 최대한 자세하고 정확하게 답변하라.
|
294 |
-
""",
|
295 |
-
label="System Message",
|
296 |
-
lines=3
|
297 |
-
)
|
298 |
-
o1mini_max_tokens_o = gr.Slider(minimum=100, maximum=4000, value=2000, step=100, label="Max Tokens")
|
299 |
-
o1mini_temperature_o = gr.Slider(minimum=0.1, maximum=2.0, value=0.7, step=0.05, label="Temperature")
|
300 |
-
o1mini_submit_button_o = gr.Button("전송")
|
301 |
-
|
302 |
-
def merge_and_call_o1mini_o(i1, i2, i3, i4, i5, sys_msg, mt, temp):
|
303 |
-
question = " ".join([i1, i2, i3, i4, i5])
|
304 |
-
return respond_o1mini_qna(
|
305 |
-
question=question,
|
306 |
-
system_message=sys_msg,
|
307 |
-
max_tokens=mt,
|
308 |
-
temperature=temp
|
309 |
-
)
|
310 |
-
o1mini_submit_button_o.click(
|
311 |
-
fn=merge_and_call_o1mini_o,
|
312 |
-
inputs=[
|
313 |
-
o1mini_input1_o, o1mini_input2_o, o1mini_input3_o, o1mini_input4_o, o1mini_input5_o,
|
314 |
-
o1mini_system_message_o,
|
315 |
-
o1mini_max_tokens_o,
|
316 |
-
o1mini_temperature_o
|
317 |
-
],
|
318 |
-
outputs=o1mini_answer_output_o
|
319 |
-
)
|
320 |
-
|
321 |
-
def update_openai_ui(model_choice):
|
322 |
-
if model_choice == "gpt-4o-mini":
|
323 |
-
return gr.update(visible=True), gr.update(visible=False)
|
324 |
-
else:
|
325 |
-
return gr.update(visible=False), gr.update(visible=True)
|
326 |
-
|
327 |
-
openai_model_radio.change(
|
328 |
-
fn=update_openai_ui,
|
329 |
-
inputs=openai_model_radio,
|
330 |
-
outputs=[chatgpt_ui, o1mini_ui]
|
331 |
-
)
|
332 |
-
|
333 |
-
#################
|
334 |
-
# Gemini 탭
|
335 |
-
#################
|
336 |
-
with gr.Tab("Gemini"):
|
337 |
-
gemini_model_radio = gr.Radio(
|
338 |
-
choices=["gemini-2.0-flash", "gemini-2.0-flash-lite-preview-02-05"],
|
339 |
-
label="모델 선택",
|
340 |
-
value="gemini-2.0-flash"
|
341 |
-
)
|
342 |
-
gemini_input1 = gr.Textbox(label="입력1", lines=1)
|
343 |
-
gemini_input2 = gr.Textbox(label="입력2", lines=1)
|
344 |
-
gemini_input3 = gr.Textbox(label="입력3", lines=1)
|
345 |
-
gemini_input4 = gr.Textbox(label="입력4", lines=1)
|
346 |
-
gemini_input5 = gr.Textbox(label="입력5", lines=1)
|
347 |
-
gemini_answer_output = gr.Textbox(label="결과", lines=5, interactive=False)
|
348 |
-
with gr.Accordion("고급 설정 (Gemini)", open=False):
|
349 |
-
gemini_system_message = gr.Textbox(
|
350 |
-
value="""반드시 한글로 답변할 것.
|
351 |
-
너는 Gemini 모델이다.
|
352 |
-
내가 요구하는 것을 최대한 자세하고 정확하게 답변하라.
|
353 |
-
""",
|
354 |
-
label="System Message",
|
355 |
-
lines=3
|
356 |
-
)
|
357 |
-
gemini_max_tokens = gr.Slider(minimum=100, maximum=4000, value=2000, step=100, label="Max Tokens")
|
358 |
-
gemini_temperature = gr.Slider(minimum=0.1, maximum=2.0, value=0.7, step=0.05, label="Temperature")
|
359 |
-
gemini_top_p = gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-P")
|
360 |
-
gemini_submit_button = gr.Button("전송")
|
361 |
-
|
362 |
-
def merge_and_call_gemini(i1, i2, i3, i4, i5, sys_msg, mt, temp, top_p_, model_radio):
|
363 |
-
question = " ".join([i1, i2, i3, i4, i5])
|
364 |
-
return respond_gemini_qna(
|
365 |
-
question=question,
|
366 |
-
system_message=sys_msg,
|
367 |
-
max_tokens=mt,
|
368 |
-
temperature=temp,
|
369 |
-
top_p=top_p_,
|
370 |
-
model_id=model_radio
|
371 |
-
)
|
372 |
-
gemini_submit_button.click(
|
373 |
-
fn=merge_and_call_gemini,
|
374 |
-
inputs=[
|
375 |
-
gemini_input1, gemini_input2, gemini_input3, gemini_input4, gemini_input5,
|
376 |
-
gemini_system_message,
|
377 |
-
gemini_max_tokens,
|
378 |
-
gemini_temperature,
|
379 |
-
gemini_top_p,
|
380 |
-
gemini_model_radio
|
381 |
-
],
|
382 |
-
outputs=gemini_answer_output
|
383 |
-
)
|
384 |
-
|
385 |
-
#################
|
386 |
-
# Claude 탭
|
387 |
-
#################
|
388 |
-
with gr.Tab("Claude"):
|
389 |
-
claude_model_radio = gr.Radio(
|
390 |
-
choices=[
|
391 |
-
"claude-3-haiku-20240307",
|
392 |
-
"claude-3-5-haiku-20241022",
|
393 |
-
"claude-3-5-sonnet-20241022"
|
394 |
-
],
|
395 |
-
label="모델 선택",
|
396 |
-
value="claude-3-5-sonnet-20241022"
|
397 |
-
)
|
398 |
-
claude_input1 = gr.Textbox(label="입력1", lines=1)
|
399 |
-
claude_input2 = gr.Textbox(label="입력2", lines=1)
|
400 |
-
claude_input3 = gr.Textbox(label="입력3", lines=1)
|
401 |
-
claude_input4 = gr.Textbox(label="입력4", lines=1)
|
402 |
-
claude_input5 = gr.Textbox(label="입력5", lines=1)
|
403 |
-
claude_answer_output = gr.Textbox(label="결과", interactive=False, lines=5)
|
404 |
-
with gr.Accordion("고급 설정 (Claude)", open=False):
|
405 |
-
claude_system_message = gr.Textbox(
|
406 |
-
label="System Message",
|
407 |
-
value="""반드시 한글로 답변할 것.
|
408 |
-
너는 Anthropic에서 개발한 클로드이다.
|
409 |
-
최대한 정확하고 친절하게 답변하라.
|
410 |
-
""",
|
411 |
-
lines=3
|
412 |
-
)
|
413 |
-
claude_max_tokens = gr.Slider(minimum=100, maximum=4000, value=2000, step=100, label="Max Tokens")
|
414 |
-
claude_temperature = gr.Slider(minimum=0.1, maximum=2.0, value=0.7, step=0.05, label="Temperature")
|
415 |
-
claude_top_p = gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p")
|
416 |
-
claude_submit_button = gr.Button("전송")
|
417 |
-
def merge_and_call_claude(i1, i2, i3, i4, i5, sys_msg, mt, temp, top_p_, model_radio):
|
418 |
-
question = " ".join([i1, i2, i3, i4, i5])
|
419 |
-
return respond_claude_qna(
|
420 |
-
question=question,
|
421 |
-
system_message=sys_msg,
|
422 |
-
max_tokens=mt,
|
423 |
-
temperature=temp,
|
424 |
-
top_p=top_p_,
|
425 |
-
model_name=model_radio
|
426 |
-
)
|
427 |
-
claude_submit_button.click(
|
428 |
-
fn=merge_and_call_claude,
|
429 |
-
inputs=[
|
430 |
-
claude_input1, claude_input2, claude_input3, claude_input4, claude_input5,
|
431 |
-
claude_system_message,
|
432 |
-
claude_max_tokens,
|
433 |
-
claude_temperature,
|
434 |
-
claude_top_p,
|
435 |
-
claude_model_radio
|
436 |
-
],
|
437 |
-
outputs=claude_answer_output
|
438 |
-
)
|
439 |
-
|
440 |
-
#################
|
441 |
-
# DeepSeek 탭
|
442 |
-
#################
|
443 |
-
with gr.Tab("DeepSeek"):
|
444 |
-
deepseek_model_radio = gr.Radio(
|
445 |
-
choices=["V3 (deepseek-chat)", "R1 (deepseek-reasoner)"],
|
446 |
-
label="모델 선택",
|
447 |
-
value="V3 (deepseek-chat)"
|
448 |
-
)
|
449 |
-
deepseek_input1 = gr.Textbox(label="입력1", lines=1)
|
450 |
-
deepseek_input2 = gr.Textbox(label="입력2", lines=1)
|
451 |
-
deepseek_input3 = gr.Textbox(label="입력3", lines=1)
|
452 |
-
deepseek_input4 = gr.Textbox(label="입력4", lines=1)
|
453 |
-
deepseek_input5 = gr.Textbox(label="입력5", lines=1)
|
454 |
-
deepseek_answer_output = gr.Textbox(label="결과", lines=5, interactive=False)
|
455 |
-
with gr.Accordion("고급 설정 (DeepSeek)", open=False):
|
456 |
-
deepseek_system_message = gr.Textbox(
|
457 |
-
value="""반드시 한글로 답변할 것.
|
458 |
-
너는 DeepSeek-V3, 최고의 언어 모델이다.
|
459 |
-
내가 요구하는 것을 최대한 자세하고 정확하게 답변하라.
|
460 |
-
""",
|
461 |
-
label="System Message",
|
462 |
-
lines=3
|
463 |
-
)
|
464 |
-
deepseek_max_tokens = gr.Slider(minimum=100, maximum=4000, value=2000, step=100, label="Max Tokens")
|
465 |
-
deepseek_temperature = gr.Slider(minimum=0.1, maximum=2.0, value=0.7, step=0.05, label="Temperature")
|
466 |
-
deepseek_top_p = gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-P")
|
467 |
-
deepseek_submit_button = gr.Button("전송")
|
468 |
-
def merge_and_call_deepseek(i1, i2, i3, i4, i5, sys_msg, mt, temp, top_p_, model_radio):
|
469 |
-
if model_radio == "V3 (deepseek-chat)":
|
470 |
-
model_name = "deepseek-chat"
|
471 |
-
else:
|
472 |
-
model_name = "deepseek-reasoner"
|
473 |
-
question = " ".join([i1, i2, i3, i4, i5])
|
474 |
-
return respond_deepseek_qna(
|
475 |
-
question=question,
|
476 |
-
system_message=sys_msg,
|
477 |
-
max_tokens=mt,
|
478 |
-
temperature=temp,
|
479 |
-
top_p=top_p_,
|
480 |
-
model_name=model_name
|
481 |
-
)
|
482 |
-
deepseek_submit_button.click(
|
483 |
-
fn=merge_and_call_deepseek,
|
484 |
-
inputs=[
|
485 |
-
deepseek_input1, deepseek_input2, deepseek_input3, deepseek_input4, deepseek_input5,
|
486 |
-
deepseek_system_message,
|
487 |
-
deepseek_max_tokens,
|
488 |
-
deepseek_temperature,
|
489 |
-
deepseek_top_p,
|
490 |
-
deepseek_model_radio
|
491 |
-
],
|
492 |
-
outputs=deepseek_answer_output
|
493 |
-
)
|
494 |
-
|
495 |
-
#################
|
496 |
-
# Cohere Command R+ 탭
|
497 |
-
#################
|
498 |
-
with gr.Tab("Cohere Command R+"):
|
499 |
-
cohere_input1 = gr.Textbox(label="입력1", lines=1)
|
500 |
-
cohere_input2 = gr.Textbox(label="입력2", lines=1)
|
501 |
-
cohere_input3 = gr.Textbox(label="입력3", lines=1)
|
502 |
-
cohere_input4 = gr.Textbox(label="입력4", lines=1)
|
503 |
-
cohere_input5 = gr.Textbox(label="입력5", lines=1)
|
504 |
-
cohere_answer_output = gr.Textbox(label="결과", lines=5, interactive=False)
|
505 |
-
with gr.Accordion("고급 설정 (Cohere)", open=False):
|
506 |
-
cohere_system_message = gr.Textbox(
|
507 |
-
value="""반드시 한글로 답변할 것.
|
508 |
-
너는 최고의 비서이다.
|
509 |
-
내가 요구하는것들을 최대한 자세하고 정확하게 답변하라.
|
510 |
-
""",
|
511 |
-
label="System Message",
|
512 |
-
lines=3
|
513 |
-
)
|
514 |
-
cohere_max_tokens = gr.Slider(minimum=100, maximum=10000, value=4000, step=100, label="Max Tokens")
|
515 |
-
cohere_temperature = gr.Slider(minimum=0.1, maximum=2.0, value=0.7, step=0.1, label="Temperature")
|
516 |
-
cohere_top_p = gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-P")
|
517 |
-
cohere_submit_button = gr.Button("전송")
|
518 |
-
def merge_and_call_cohere(i1, i2, i3, i4, i5, sys_msg, mt, temp, top_p_):
|
519 |
-
question = " ".join([i1, i2, i3, i4, i5])
|
520 |
-
return respond_cohere_qna(
|
521 |
-
question=question,
|
522 |
-
system_message=sys_msg,
|
523 |
-
max_tokens=mt,
|
524 |
-
temperature=temp,
|
525 |
-
top_p=top_p_
|
526 |
-
)
|
527 |
-
cohere_submit_button.click(
|
528 |
-
fn=merge_and_call_cohere,
|
529 |
-
inputs=[
|
530 |
-
cohere_input1, cohere_input2, cohere_input3, cohere_input4, cohere_input5,
|
531 |
-
cohere_system_message,
|
532 |
-
cohere_max_tokens,
|
533 |
-
cohere_temperature,
|
534 |
-
cohere_top_p
|
535 |
-
],
|
536 |
-
outputs=cohere_answer_output
|
537 |
-
)
|
538 |
|
539 |
-
#############################
|
540 |
-
# 메인 실행부
|
541 |
-
#############################
|
542 |
if __name__ == "__main__":
|
543 |
-
demo.launch()
|
|
|
1 |
import gradio as gr
|
|
|
2 |
import openai
|
|
|
3 |
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
4 |
|
5 |
def respond_chatgpt_qna(
|
6 |
question: str,
|
|
|
10 |
top_p: float
|
11 |
):
|
12 |
"""
|
13 |
+
OpenAI의 gpt-4o-mini 모델을 이용해 질문에 대한 답변을 반환하는 함수.
|
14 |
"""
|
15 |
openai_token = os.getenv("OPENAI_TOKEN")
|
16 |
if not openai_token:
|
|
|
33 |
except Exception as e:
|
34 |
return f"오류가 발생했습니다: {str(e)}"
|
35 |
|
36 |
+
def merge_and_call(tone: str, ref1: str, ref2: str, ref3: str):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
37 |
"""
|
38 |
+
사용자가 선택한 말투와 참조글들을 하나의 프롬프트로 합쳐 gpt-4o-mini 모델에 전달하는 함수.
|
39 |
"""
|
40 |
+
# 간단한 프롬프트 생성
|
41 |
+
question = f"말투: {tone}\n참조글 1: {ref1}\n참조글 2: {ref2}\n참조글 3: {ref3}"
|
42 |
+
# 고급 설정은 코드 내부에 기본값으로 지정 (UI에는 노출되지 않음)
|
43 |
+
system_message = "아래의 참조글들을 참고하여 블로그 글을 생성하라."
|
44 |
+
max_tokens = 2000
|
45 |
+
temperature = 0.7
|
46 |
+
top_p = 0.95
|
47 |
+
return respond_chatgpt_qna(
|
48 |
+
question=question,
|
49 |
+
system_message=system_message,
|
50 |
+
max_tokens=max_tokens,
|
51 |
+
temperature=temperature,
|
52 |
+
top_p=top_p
|
53 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
54 |
|
55 |
with gr.Blocks() as demo:
|
56 |
+
gr.Markdown("# 블로그 생성기")
|
57 |
+
|
58 |
+
# 입력 항목 구성
|
59 |
+
tone_radio = gr.Radio(
|
60 |
+
choices=["친근하게", "일반적인", "전문적인"],
|
61 |
+
label="말투바꾸기",
|
62 |
+
value="일반적인"
|
63 |
+
)
|
64 |
+
ref1_text = gr.Textbox(label="참조글 1", lines=5)
|
65 |
+
ref2_text = gr.Textbox(label="참조글 2", lines=5)
|
66 |
+
ref3_text = gr.Textbox(label="참조글 3", lines=5)
|
67 |
+
answer_output = gr.Textbox(label="결과", lines=10, interactive=False)
|
68 |
+
|
69 |
+
submit_button = gr.Button("전송")
|
70 |
+
|
71 |
+
submit_button.click(
|
72 |
+
fn=merge_and_call,
|
73 |
+
inputs=[tone_radio, ref1_text, ref2_text, ref3_text],
|
74 |
+
outputs=answer_output
|
75 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
76 |
|
|
|
|
|
|
|
77 |
if __name__ == "__main__":
|
78 |
+
demo.launch()
|