Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -170,6 +170,60 @@ def respond_claude_qna(
|
|
| 170 |
except Exception as e:
|
| 171 |
return f"예상치 못한 오류가 발생했습니다: {str(e)}"
|
| 172 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 173 |
#############################
|
| 174 |
# [기본코드] UI 부분 - 수정/삭제 불가
|
| 175 |
#############################
|
|
@@ -405,8 +459,66 @@ with gr.Blocks() as demo:
|
|
| 405 |
outputs=deepseek_answer_output
|
| 406 |
)
|
| 407 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 408 |
#############################
|
| 409 |
# 메인 실행부
|
| 410 |
#############################
|
| 411 |
if __name__ == "__main__":
|
| 412 |
-
demo.launch()
|
|
|
|
| 170 |
except Exception as e:
|
| 171 |
return f"예상치 못한 오류가 발생했습니다: {str(e)}"
|
| 172 |
|
| 173 |
+
#############################
|
| 174 |
+
# [추가코드] - Llama-3.3-70B-Instruct / Llama-3.2-3B-Instruct 적용
|
| 175 |
+
#############################
|
| 176 |
+
|
| 177 |
+
def get_llama_client(model_choice: str):
|
| 178 |
+
"""
|
| 179 |
+
선택된 Llama 모델에 맞춰 InferenceClient 생성.
|
| 180 |
+
토큰은 환경 변수에서 가져옴.
|
| 181 |
+
"""
|
| 182 |
+
hf_token = os.getenv("HF_TOKEN")
|
| 183 |
+
if not hf_token:
|
| 184 |
+
raise ValueError("HuggingFace API 토큰이 필요합니다.")
|
| 185 |
+
|
| 186 |
+
if model_choice == "Llama-3.3-70B-Instruct":
|
| 187 |
+
model_id = "Llama-3.3-70B-Instruct"
|
| 188 |
+
elif model_choice == "Llama-3.2-3B-Instruct":
|
| 189 |
+
model_id = "Llama-3.2-3B-Instruct"
|
| 190 |
+
else:
|
| 191 |
+
raise ValueError("유효하지 않은 모델 선택입니다.")
|
| 192 |
+
return InferenceClient(model_id, token=hf_token)
|
| 193 |
+
|
| 194 |
+
def respond_llama_qna(
|
| 195 |
+
question: str,
|
| 196 |
+
system_message: str,
|
| 197 |
+
max_tokens: int,
|
| 198 |
+
temperature: float,
|
| 199 |
+
top_p: float,
|
| 200 |
+
model_choice: str
|
| 201 |
+
):
|
| 202 |
+
"""
|
| 203 |
+
선택된 Llama 모델을 이용해 한 번의 질문(question)에 대한 답변을 반환하는 함수.
|
| 204 |
+
"""
|
| 205 |
+
try:
|
| 206 |
+
client = get_llama_client(model_choice)
|
| 207 |
+
except ValueError as e:
|
| 208 |
+
return f"오류: {str(e)}"
|
| 209 |
+
|
| 210 |
+
messages = [
|
| 211 |
+
{"role": "system", "content": system_message},
|
| 212 |
+
{"role": "user", "content": question}
|
| 213 |
+
]
|
| 214 |
+
|
| 215 |
+
try:
|
| 216 |
+
response_full = client.chat_completion(
|
| 217 |
+
messages,
|
| 218 |
+
max_tokens=max_tokens,
|
| 219 |
+
temperature=temperature,
|
| 220 |
+
top_p=top_p,
|
| 221 |
+
)
|
| 222 |
+
assistant_message = response_full.choices[0].message.content
|
| 223 |
+
return assistant_message
|
| 224 |
+
except Exception as e:
|
| 225 |
+
return f"오류가 발생했습니다: {str(e)}"
|
| 226 |
+
|
| 227 |
#############################
|
| 228 |
# [기본코드] UI 부분 - 수정/삭제 불가
|
| 229 |
#############################
|
|
|
|
| 459 |
outputs=deepseek_answer_output
|
| 460 |
)
|
| 461 |
|
| 462 |
+
#################
|
| 463 |
+
# Llama 탭 (추가)
|
| 464 |
+
#################
|
| 465 |
+
with gr.Tab("Llama"):
|
| 466 |
+
# 라디오 버튼 추가: Llama-3.3-70B-Instruct (기본) / Llama-3.2-3B-Instruct
|
| 467 |
+
llama_model_radio = gr.Radio(
|
| 468 |
+
choices=["Llama-3.3-70B-Instruct", "Llama-3.2-3B-Instruct"],
|
| 469 |
+
label="모델 선택",
|
| 470 |
+
value="Llama-3.3-70B-Instruct"
|
| 471 |
+
)
|
| 472 |
+
|
| 473 |
+
llama_input1 = gr.Textbox(label="입력1", lines=1)
|
| 474 |
+
llama_input2 = gr.Textbox(label="입력2", lines=1)
|
| 475 |
+
llama_input3 = gr.Textbox(label="입력3", lines=1)
|
| 476 |
+
llama_input4 = gr.Textbox(label="입력4", lines=1)
|
| 477 |
+
llama_input5 = gr.Textbox(label="입력5", lines=1)
|
| 478 |
+
|
| 479 |
+
llama_answer_output = gr.Textbox(label="결과", lines=5, interactive=False)
|
| 480 |
+
|
| 481 |
+
with gr.Accordion("고급 설정 (Llama)", open=False):
|
| 482 |
+
llama_system_message = gr.Textbox(
|
| 483 |
+
value="""반드시 한글로 답변할 것.
|
| 484 |
+
너는 최고의 비서이다.
|
| 485 |
+
내가 요구하는 것을 최대한 자세하고 정확하게 답변하라.
|
| 486 |
+
""",
|
| 487 |
+
label="System Message",
|
| 488 |
+
lines=3
|
| 489 |
+
)
|
| 490 |
+
llama_max_tokens = gr.Slider(minimum=100, maximum=10000, value=4000, step=100, label="Max Tokens")
|
| 491 |
+
llama_temperature = gr.Slider(minimum=0.1, maximum=2.0, value=0.7, step=0.1, label="Temperature")
|
| 492 |
+
llama_top_p = gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-P")
|
| 493 |
+
|
| 494 |
+
llama_submit_button = gr.Button("전송")
|
| 495 |
+
|
| 496 |
+
def merge_and_call_llama(i1, i2, i3, i4, i5, sys_msg, mt, temp, top_p_, model_choice):
|
| 497 |
+
question = " ".join([i1, i2, i3, i4, i5])
|
| 498 |
+
return respond_llama_qna(
|
| 499 |
+
question=question,
|
| 500 |
+
system_message=sys_msg,
|
| 501 |
+
max_tokens=mt,
|
| 502 |
+
temperature=temp,
|
| 503 |
+
top_p=top_p_,
|
| 504 |
+
model_choice=model_choice
|
| 505 |
+
)
|
| 506 |
+
|
| 507 |
+
llama_submit_button.click(
|
| 508 |
+
fn=merge_and_call_llama,
|
| 509 |
+
inputs=[
|
| 510 |
+
llama_input1, llama_input2, llama_input3, llama_input4, llama_input5,
|
| 511 |
+
llama_system_message,
|
| 512 |
+
llama_max_tokens,
|
| 513 |
+
llama_temperature,
|
| 514 |
+
llama_top_p,
|
| 515 |
+
llama_model_radio # 라디오 버튼 입력 추가
|
| 516 |
+
],
|
| 517 |
+
outputs=llama_answer_output
|
| 518 |
+
)
|
| 519 |
+
|
| 520 |
#############################
|
| 521 |
# 메인 실행부
|
| 522 |
#############################
|
| 523 |
if __name__ == "__main__":
|
| 524 |
+
demo.launch()
|