Kims12 commited on
Commit
44cf963
·
verified ·
1 Parent(s): 0089bb1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +1 -117
app.py CHANGED
@@ -4,8 +4,6 @@ import openai
4
  import anthropic
5
  import os
6
  from typing import Optional
7
- import transformers
8
- import torch
9
 
10
  #############################
11
  # [기본코드] - 수정/삭제 불가
@@ -172,62 +170,6 @@ def respond_claude_qna(
172
  except Exception as e:
173
  return f"예상치 못한 오류가 발생했습니다: {str(e)}"
174
 
175
- #############################
176
- # [추가코드] - Llama-3.3-70B-Instruct / Llama-3.2-3B-Instruct 적용 (transformers.pipeline 방식)
177
- #############################
178
-
179
- def get_llama_client(model_choice: str):
180
- """
181
- 선택된 Llama 모델에 맞춰 transformers의 text-generation 파이프라인을 생성.
182
- """
183
- if model_choice == "Llama-3.3-70B-Instruct":
184
- model_id = "meta-llama/Llama-3.3-70B-Instruct"
185
- elif model_choice == "Llama-3.2-3B-Instruct":
186
- model_id = "meta-llama/Llama-3.2-3B-Instruct"
187
- else:
188
- raise ValueError("유효하지 않은 모델 선택입니다.")
189
-
190
- pipeline_llama = transformers.pipeline(
191
- "text-generation",
192
- model=model_id,
193
- model_kwargs={"torch_dtype": torch.bfloat16},
194
- device_map="auto",
195
- )
196
- return pipeline_llama
197
-
198
- def respond_llama_qna(
199
- question: str,
200
- system_message: str,
201
- max_tokens: int,
202
- temperature: float,
203
- top_p: float,
204
- model_choice: str
205
- ):
206
- """
207
- 선택된 Llama 모델을 이용해 한 번의 질문(question)에 대한 답변을 transformers 파이프라인으로 반환하는 함수.
208
- system_message와 question을 하나의 프롬프트로 결합하여 생성합니다.
209
- """
210
- try:
211
- pipeline_llama = get_llama_client(model_choice)
212
- except ValueError as e:
213
- return f"오류: {str(e)}"
214
-
215
- # system_message와 question을 연결하여 프롬프트 생성
216
- prompt = system_message.strip() + "\n" + question.strip()
217
-
218
- try:
219
- outputs = pipeline_llama(
220
- prompt,
221
- max_new_tokens=max_tokens,
222
- temperature=temperature,
223
- top_p=top_p,
224
- )
225
- # 생성된 텍스트를 추출 (전체 프롬프트 이후의 텍스트만 반환할 수도 있음)
226
- generated_text = outputs[0]["generated_text"]
227
- return generated_text
228
- except Exception as e:
229
- return f"오류가 발생했습니다: {str(e)}"
230
-
231
  #############################
232
  # [기본코드] UI 부분 - 수정/삭제 불가
233
  #############################
@@ -463,66 +405,8 @@ with gr.Blocks() as demo:
463
  outputs=deepseek_answer_output
464
  )
465
 
466
- #################
467
- # Llama 탭 (추가)
468
- #################
469
- with gr.Tab("Llama"):
470
- # 라디오 버튼 추가: Llama-3.3-70B-Instruct (기본) / Llama-3.2-3B-Instruct
471
- llama_model_radio = gr.Radio(
472
- choices=["Llama-3.3-70B-Instruct", "Llama-3.2-3B-Instruct"],
473
- label="모델 선택",
474
- value="Llama-3.3-70B-Instruct"
475
- )
476
-
477
- llama_input1 = gr.Textbox(label="입력1", lines=1)
478
- llama_input2 = gr.Textbox(label="입력2", lines=1)
479
- llama_input3 = gr.Textbox(label="입력3", lines=1)
480
- llama_input4 = gr.Textbox(label="입력4", lines=1)
481
- llama_input5 = gr.Textbox(label="입력5", lines=1)
482
-
483
- llama_answer_output = gr.Textbox(label="결과", lines=5, interactive=False)
484
-
485
- with gr.Accordion("고급 설정 (Llama)", open=False):
486
- llama_system_message = gr.Textbox(
487
- value="""반드시 한글로 답변할 것.
488
- 너는 최고의 비서이다.
489
- 내가 요구하는 것을 최대한 자세하고 정확하게 답변하라.
490
- """,
491
- label="System Message",
492
- lines=3
493
- )
494
- llama_max_tokens = gr.Slider(minimum=100, maximum=10000, value=4000, step=100, label="Max Tokens")
495
- llama_temperature = gr.Slider(minimum=0.1, maximum=2.0, value=0.7, step=0.1, label="Temperature")
496
- llama_top_p = gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-P")
497
-
498
- llama_submit_button = gr.Button("전송")
499
-
500
- def merge_and_call_llama(i1, i2, i3, i4, i5, sys_msg, mt, temp, top_p_, model_choice):
501
- question = " ".join([i1, i2, i3, i4, i5])
502
- return respond_llama_qna(
503
- question=question,
504
- system_message=sys_msg,
505
- max_tokens=mt,
506
- temperature=temp,
507
- top_p=top_p_,
508
- model_choice=model_choice
509
- )
510
-
511
- llama_submit_button.click(
512
- fn=merge_and_call_llama,
513
- inputs=[
514
- llama_input1, llama_input2, llama_input3, llama_input4, llama_input5,
515
- llama_system_message,
516
- llama_max_tokens,
517
- llama_temperature,
518
- llama_top_p,
519
- llama_model_radio # 라디오 버튼 입력 추가
520
- ],
521
- outputs=llama_answer_output
522
- )
523
-
524
  #############################
525
  # 메인 실행부
526
  #############################
527
  if __name__ == "__main__":
528
- demo.launch()
 
4
  import anthropic
5
  import os
6
  from typing import Optional
 
 
7
 
8
  #############################
9
  # [기본코드] - 수정/삭제 불가
 
170
  except Exception as e:
171
  return f"예상치 못한 오류가 발생했습니다: {str(e)}"
172
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
173
  #############################
174
  # [기본코드] UI 부분 - 수정/삭제 불가
175
  #############################
 
405
  outputs=deepseek_answer_output
406
  )
407
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
408
  #############################
409
  # 메인 실행부
410
  #############################
411
  if __name__ == "__main__":
412
+ demo.launch()