Spaces:
Sleeping
Sleeping
File size: 3,669 Bytes
b34f0d5 7dcc8af 16edf41 2bc2ba7 b34f0d5 8144da3 c65ce97 2bc2ba7 c231a45 5d429a8 7dcc8af 2bc2ba7 c753d25 c231a45 2bc2ba7 c231a45 c753d25 2bc2ba7 5d429a8 c231a45 2bc2ba7 092cc1c c231a45 2bc2ba7 c231a45 092cc1c c231a45 092cc1c c753d25 2bc2ba7 b34f0d5 16edf41 f1d1009 2bc2ba7 5d429a8 2bc2ba7 16edf41 2bc2ba7 16edf41 2bc2ba7 c231a45 2bc2ba7 16edf41 2bc2ba7 5d429a8 2bc2ba7 16edf41 958e155 b34f0d5 5d429a8 |
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 |
import gradio as gr
from huggingface_hub import InferenceClient
import os
from typing import Optional
#############################
# [기본코드] - Cohere 관련 부분만 남김
#############################
# Cohere Command R+ 모델 ID 정의
COHERE_MODEL = "CohereForAI/c4ai-command-r-plus-08-2024"
def get_client(model_name: str):
"""
모델 이름에 맞춰 InferenceClient 생성.
토큰은 환경 변수에서 가져옴.
"""
hf_token = os.getenv("HF_TOKEN")
if not hf_token:
raise ValueError("HuggingFace API 토큰(HF_TOKEN)이 설정되지 않았습니다.")
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)}"
#############################
# 고급 설정 (Cohere) - 코드에서만 정의 (UI에 노출 금지)
#############################
COHERE_SYSTEM_MESSAGE = """반드시 한글로 답변할 것.
너는 최고의 비서이다.
내가 요구하는 것들을 최대한 자세하고 정확하게 답변하라.
"""
COHERE_MAX_TOKENS = 4000
COHERE_TEMPERATURE = 0.7
COHERE_TOP_P = 0.95
#############################
# UI - 블로그 생성기
#############################
with gr.Blocks() as demo:
gr.Markdown("# 블로그 생성기")
# 말투바꾸기 (라디오 버튼)
tone_radio = gr.Radio(
label="말투바꾸기",
choices=["친근하게", "일반적인", "전문적인"],
value="일반적인" # 기본 선택
)
# 참조글 입력 (3개)
ref1 = gr.Textbox(label="참조글 1")
ref2 = gr.Textbox(label="참조글 2")
ref3 = gr.Textbox(label="참조글 3")
output_box = gr.Textbox(label="결과", lines=8, interactive=False)
def generate_blog(tone_value, ref1_value, ref2_value, ref3_value):
# 프롬프트: “~~”
# 말투와 참조글들을 하나로 합쳐 질문(프롬프트) 형식으로 구성
question = (
f"~~\n"
f"말투: {tone_value}\n"
f"참조글1: {ref1_value}\n"
f"참조글2: {ref2_value}\n"
f"참조글3: {ref3_value}\n"
)
# Cohere Command R+ 모델 호출
response = respond_cohere_qna(
question=question,
system_message=COHERE_SYSTEM_MESSAGE,
max_tokens=COHERE_MAX_TOKENS,
temperature=COHERE_TEMPERATURE,
top_p=COHERE_TOP_P
)
return response
generate_button = gr.Button("생성하기")
generate_button.click(
fn=generate_blog,
inputs=[tone_radio, ref1, ref2, ref3],
outputs=output_box
)
if __name__ == "__main__":
demo.launch()
|