File size: 3,190 Bytes
b34f0d5
7dcc8af
16edf41
b34f0d5
8144da3
c65ce97
 
16edf41
f1d1009
16edf41
 
f1d1009
7dcc8af
f1d1009
16edf41
c753d25
 
 
 
 
 
 
 
092cc1c
c753d25
 
 
092cc1c
16edf41
092cc1c
c753d25
092cc1c
c753d25
 
 
 
092cc1c
 
 
 
 
 
 
 
 
c753d25
092cc1c
c753d25
 
16edf41
 
 
 
 
 
 
 
f9b088b
b34f0d5
16edf41
f1d1009
16edf41
 
f1d1009
16edf41
 
 
 
 
 
c753d25
16edf41
 
 
 
 
 
 
 
 
 
bfcb9e7
 
 
 
 
 
16edf41
 
 
 
 
 
 
 
bfcb9e7
f9b088b
 
16edf41
 
bfcb9e7
16edf41
 
958e155
f9b088b
b34f0d5
958e155
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
import gradio as gr
from huggingface_hub import InferenceClient
import os

# Cohere Command R+ 모델 ID 정의
COHERE_MODEL = "CohereForAI/c4ai-command-r-plus-08-2024"

def get_client(hf_token):
    """
    Cohere Command R+ 모델을 위한 InferenceClient 생성.
    hf_token은 Gradio secrets를 통해 제공.
    """
    if not hf_token:
        raise ValueError("HuggingFace API 토큰이 필요합니다.")
    return InferenceClient(COHERE_MODEL, token=hf_token)

def respond_cohere_qna(
    question: str,
    system_message: str,
    max_tokens: int,
    temperature: float,
    top_p: float,
    hf_token: str
):
    """
    Cohere Command R+ 모델을 이용해 한 번의 질문(question)에 대한 답변을 반환하는 함수.
    """
    try:
        client = get_client(hf_token)
    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)}"

# 고급 설정 (코드 내에서만 정의)
SYSTEM_MESSAGE = """반드시 한글로 답변할 것.
너는 최고의 블로그 글 작성자이다.
내가 제공한 참조글을 바탕으로 블로그 글을 생성하라.
"""
MAX_TOKENS = 4000
TEMPERATURE = 0.7
TOP_P = 0.95

with gr.Blocks() as demo:
    gr.Markdown("# 블로그 생성기")

    # HuggingFace 토큰 (secrets 사용)
    hf_token = os.getenv("HF_TOKEN")

    # 입력 필드
    with gr.Row():
        tone = gr.Radio(
            choices=["친근하게", "일반적인", "전문적인"],
            label="말투바꾸기",
            value="일반적인"
        )
    ref1 = gr.Textbox(label="참조글 1", lines=3)
    ref2 = gr.Textbox(label="참조글 2", lines=3)
    ref3 = gr.Textbox(label="참조글 3", lines=3)

    # 결과 출력
    answer_output = gr.Textbox(label="생성된 블로그 글", lines=10, interactive=False)

    # 전송 버튼
    submit_button = gr.Button("생성")

    def generate_blog(tone, ref1, ref2, ref3):
        # HuggingFace 토큰은 함수 내부에서 직접 사용
        hf_token_value = os.getenv("HF_TOKEN")
        if not hf_token_value:
            return "HuggingFace 토큰이 설정되지 않았습니다."

        # 프롬프트 구성
        prompt = f"말투: {tone}\n참조글 1: {ref1}\n참조글 2: {ref2}\n참조글 3: {ref3}"
        return respond_cohere_qna(
            question=prompt,
            system_message=SYSTEM_MESSAGE,
            max_tokens=MAX_TOKENS,
            temperature=TEMPERATURE,
            top_p=TOP_P,
            hf_token=hf_token_value
        )

    submit_button.click(
        fn=generate_blog,
        inputs=[tone, ref1, ref2, ref3],  # hf_token은 제외
        outputs=answer_output
    )

# 메인 실행부
if __name__ == "__main__":
    demo.launch()