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