Kims12 commited on
Commit
86de29b
·
verified ·
1 Parent(s): 895c50f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +104 -43
app.py CHANGED
@@ -175,13 +175,14 @@ def respond_o1mini_qna(
175
  question: str,
176
  system_message: str,
177
  max_tokens: int,
178
- temperature: float # 입력값은 있으나 사용하지 않습니다.
179
  ):
180
  """
181
  o1-mini 모델을 이용해 한 번의 질문(question)에 대한 답변을 반환하는 함수.
182
  o1-mini에서는 'system' 역할 메시지를 지원하지 않으므로,
183
  system_message와 question을 하나의 'user' 메시지로 합쳐 전달합니다.
184
- 또한, 온도(temperature)는 고정값 1만 지원하므로 해당 값으로 호출합니다.
 
185
  """
186
  openai_token = os.getenv("OPENAI_TOKEN")
187
  if not openai_token:
@@ -189,7 +190,6 @@ def respond_o1mini_qna(
189
 
190
  openai.api_key = openai_token
191
 
192
- # system_message와 question을 하나의 메시지로 합침
193
  combined_message = f"{system_message}\n\n{question}"
194
  messages = [
195
  {"role": "user", "content": combined_message}
@@ -208,7 +208,7 @@ def respond_o1mini_qna(
208
  return f"오류가 발생했습니다: {str(e)}"
209
 
210
  #############################
211
- # [기본코드] UI 부분 - 수정/삭제 불가
212
  #############################
213
 
214
  with gr.Blocks() as demo:
@@ -456,50 +456,111 @@ with gr.Blocks() as demo:
456
  )
457
 
458
  #################
459
- # o1-mini
460
  #################
461
- with gr.Tab("o1-mini"):
462
- o1mini_input1 = gr.Textbox(label="입력1", lines=1)
463
- o1mini_input2 = gr.Textbox(label="입력2", lines=1)
464
- o1mini_input3 = gr.Textbox(label="입력3", lines=1)
465
- o1mini_input4 = gr.Textbox(label="입력4", lines=1)
466
- o1mini_input5 = gr.Textbox(label="입력5", lines=1)
467
-
468
- o1mini_answer_output = gr.Textbox(label="결과", lines=5, interactive=False)
469
-
470
- with gr.Accordion("고급 설정 (o1-mini)", open=False):
471
- o1mini_system_message = gr.Textbox(
472
- value="""반드시 한글로 답변할 것.
473
- 너는 o1-mini, OpenAI에서 개발한 경량 언어 모델이다.
 
 
 
 
 
 
 
474
  내가 요구하는 것을 최대한 자세하고 정확하게 답변하라.
475
  """,
476
- label="System Message",
477
- lines=3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
478
  )
479
- o1mini_max_tokens = gr.Slider(minimum=100, maximum=4000, value=2000, step=100, label="Max Tokens")
480
- o1mini_temperature = gr.Slider(minimum=0.1, maximum=2.0, value=0.7, step=0.05, label="Temperature")
481
- # o1-mini는 top_p 지원이 없으므로 해당 옵션은 UI에서 제외함
482
-
483
- o1mini_submit_button = gr.Button("전송")
484
-
485
- def merge_and_call_o1mini(i1, i2, i3, i4, i5, sys_msg, mt, temp):
486
- question = " ".join([i1, i2, i3, i4, i5])
487
- return respond_o1mini_qna(
488
- question=question,
489
- system_message=sys_msg,
490
- max_tokens=mt,
491
- temperature=temp
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
492
  )
493
-
494
- o1mini_submit_button.click(
495
- fn=merge_and_call_o1mini,
496
- inputs=[
497
- o1mini_input1, o1mini_input2, o1mini_input3, o1mini_input4, o1mini_input5,
498
- o1mini_system_message,
499
- o1mini_max_tokens,
500
- o1mini_temperature
501
- ],
502
- outputs=o1mini_answer_output
 
 
503
  )
504
 
505
  #############################
 
175
  question: str,
176
  system_message: str,
177
  max_tokens: int,
178
+ temperature: float
179
  ):
180
  """
181
  o1-mini 모델을 이용해 한 번의 질문(question)에 대한 답변을 반환하는 함수.
182
  o1-mini에서는 'system' 역할 메시지를 지원하지 않으므로,
183
  system_message와 question을 하나의 'user' 메시지로 합쳐 전달합니다.
184
+ 또한, o1-mini에서는 'max_tokens' 대신 'max_completion_tokens' 파라미터를 사용하며,
185
+ 온도(temperature)는 고정값 1만 지원합니다.
186
  """
187
  openai_token = os.getenv("OPENAI_TOKEN")
188
  if not openai_token:
 
190
 
191
  openai.api_key = openai_token
192
 
 
193
  combined_message = f"{system_message}\n\n{question}"
194
  messages = [
195
  {"role": "user", "content": combined_message}
 
208
  return f"오류가 발생했습니다: {str(e)}"
209
 
210
  #############################
211
+ # [기본코드] UI 부분 - 수정/삭제 불가 (단, OpenAI 탭 추가)
212
  #############################
213
 
214
  with gr.Blocks() as demo:
 
456
  )
457
 
458
  #################
459
+ # OpenAI 탭 (gpt-4o-mini / o1-mini 통합)
460
  #################
461
+ with gr.Tab("OpenAI"):
462
+ # 모델 선택 라디오 버튼 (gpt-4o-mini와 o1-mini)
463
+ openai_model_radio = gr.Radio(
464
+ choices=["gpt-4o-mini", "o1-mini"],
465
+ label="모델 선택",
466
+ value="gpt-4o-mini"
467
+ )
468
+
469
+ # gpt-4o-mini 전용 UI 그룹 (초기 visible)
470
+ with gr.Column(visible=True) as chatgpt_ui:
471
+ chatgpt_input1_o = gr.Textbox(label="입력1", lines=1)
472
+ chatgpt_input2_o = gr.Textbox(label="입력2", lines=1)
473
+ chatgpt_input3_o = gr.Textbox(label="입력3", lines=1)
474
+ chatgpt_input4_o = gr.Textbox(label="입력4", lines=1)
475
+ chatgpt_input5_o = gr.Textbox(label="입력5", lines=1)
476
+ chatgpt_answer_output_o = gr.Textbox(label="결과", lines=5, interactive=False)
477
+ with gr.Accordion("고급 설정 (gpt-4o-mini)", open=False):
478
+ chatgpt_system_message_o = gr.Textbox(
479
+ value="""반드시 한글로 답변할 것.
480
+ 너는 ChatGPT, OpenAI에서 개발한 언어 모델이다.
481
  내가 요구하는 것을 최대한 자세하고 정확하게 답변하라.
482
  """,
483
+ label="System Message",
484
+ lines=3
485
+ )
486
+ chatgpt_max_tokens_o = gr.Slider(minimum=100, maximum=4000, value=2000, step=100, label="Max Tokens")
487
+ chatgpt_temperature_o = gr.Slider(minimum=0.1, maximum=2.0, value=0.7, step=0.05, label="Temperature")
488
+ chatgpt_top_p_o = gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-P")
489
+ chatgpt_submit_button_o = gr.Button("전송")
490
+
491
+ def merge_and_call_chatgpt_o(i1, i2, i3, i4, i5, sys_msg, mt, temp, top_p_):
492
+ question = " ".join([i1, i2, i3, i4, i5])
493
+ return respond_chatgpt_qna(
494
+ question=question,
495
+ system_message=sys_msg,
496
+ max_tokens=mt,
497
+ temperature=temp,
498
+ top_p=top_p_
499
+ )
500
+ chatgpt_submit_button_o.click(
501
+ fn=merge_and_call_chatgpt_o,
502
+ inputs=[
503
+ chatgpt_input1_o, chatgpt_input2_o, chatgpt_input3_o, chatgpt_input4_o, chatgpt_input5_o,
504
+ chatgpt_system_message_o,
505
+ chatgpt_max_tokens_o,
506
+ chatgpt_temperature_o,
507
+ chatgpt_top_p_o
508
+ ],
509
+ outputs=chatgpt_answer_output_o
510
  )
511
+
512
+ # o1-mini 전용 UI 그룹 (초기 hidden)
513
+ with gr.Column(visible=False) as o1mini_ui:
514
+ o1mini_input1_o = gr.Textbox(label="입력1", lines=1)
515
+ o1mini_input2_o = gr.Textbox(label="입력2", lines=1)
516
+ o1mini_input3_o = gr.Textbox(label="입력3", lines=1)
517
+ o1mini_input4_o = gr.Textbox(label="입력4", lines=1)
518
+ o1mini_input5_o = gr.Textbox(label="입력5", lines=1)
519
+ o1mini_answer_output_o = gr.Textbox(label="결과", lines=5, interactive=False)
520
+ with gr.Accordion("고급 설정 (o1-mini)", open=False):
521
+ o1mini_system_message_o = gr.Textbox(
522
+ value="""반드시 한글로 답변할 것.
523
+ 너는 o1-mini, OpenAI에서 개발한 경량 언어 모델이다.
524
+ 내가 요구하는 것을 최대한 자세하고 정확하게 답변하라.
525
+ """,
526
+ label="System Message",
527
+ lines=3
528
+ )
529
+ o1mini_max_tokens_o = gr.Slider(minimum=100, maximum=4000, value=2000, step=100, label="Max Tokens")
530
+ o1mini_temperature_o = gr.Slider(minimum=0.1, maximum=2.0, value=0.7, step=0.05, label="Temperature")
531
+ # o1-mini는 top_p 지원이 없으므로 해당 옵션은 UI에서 제외함
532
+ o1mini_submit_button_o = gr.Button("전송")
533
+
534
+ def merge_and_call_o1mini_o(i1, i2, i3, i4, i5, sys_msg, mt, temp):
535
+ question = " ".join([i1, i2, i3, i4, i5])
536
+ return respond_o1mini_qna(
537
+ question=question,
538
+ system_message=sys_msg,
539
+ max_tokens=mt,
540
+ temperature=temp
541
+ )
542
+ o1mini_submit_button_o.click(
543
+ fn=merge_and_call_o1mini_o,
544
+ inputs=[
545
+ o1mini_input1_o, o1mini_input2_o, o1mini_input3_o, o1mini_input4_o, o1mini_input5_o,
546
+ o1mini_system_message_o,
547
+ o1mini_max_tokens_o,
548
+ o1mini_temperature_o
549
+ ],
550
+ outputs=o1mini_answer_output_o
551
  )
552
+
553
+ # UI 업데이트: 라디오 버튼 선택에 따라 gpt-4o-mini / o1-mini UI 전환
554
+ def update_openai_ui(model_choice):
555
+ if model_choice == "gpt-4o-mini":
556
+ return gr.update(visible=True), gr.update(visible=False)
557
+ else:
558
+ return gr.update(visible=False), gr.update(visible=True)
559
+
560
+ openai_model_radio.change(
561
+ fn=update_openai_ui,
562
+ inputs=openai_model_radio,
563
+ outputs=[chatgpt_ui, o1mini_ui]
564
  )
565
 
566
  #############################