aliceblue11's picture
Update app.py
1acd13c verified
import gradio as gr
from huggingface_hub import InferenceClient
# Cohere Command R+ 모델 ID 정의
COHERE_MODEL = "CohereForAI/c4ai-command-r-plus-08-2024"
def get_client(hf_token):
"""
Cohere Command R+ 모델에 대한 InferenceClient 생성.
"""
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)}"
#############################
# UI 설정
#############################
# HuggingFace 토큰을 변수에 저장
HUGGINGFACE_TOKEN = "YOUR_HUGGINGFACE_TOKEN"
with gr.Blocks() as demo:
gr.Markdown("# 블로그 생성기")
# 라디오 버튼으로 말투 선택
tone_selection = gr.Radio(
choices=["친근한", "전문적인", "상품후기"],
label="말투바꾸기",
value="친근한"
)
input2 = gr.Textbox(label="참조글1", lines=1)
input3 = gr.Textbox(label="참조글2", lines=1)
input4 = gr.Textbox(label="참조글3", lines=1)
input5 = gr.Textbox(label="생성된 블로그 글", lines=1)
answer_output = gr.Textbox(label="결과", lines=5, interactive=False)
system_message = """
##[기본규칙]
1. 반드시 한국어(한글)로 작성하라.
2. 너는 가장 주목받는 마케터이며 블로그 마케팅 전문가이다.
3. 특히 너는 '정보성(Informative)' 전문 블로그 마케팅 전문가이다.
4. 정보 제공에 초점을 맞추어 작성한다.
##[텍스트 작성 규칙]
1. 소주제를 5개로 구분하여 2000자 이상되도록 작성하라.
2. 전체 맥락을 이해하고 문장의 일관성을 유지하라.
3. 절대로 참고글을 한문장 이상 그대로 출력하지 말 것.
4. 주제와 상황에 맞는 적절한 어휘를 선택하라.
5. 한글 어휘의 난이도는 쉽게 작성하라.
6. 절대 문장의 끝에 '답니다'를 사용하지 말 것.
###[정보성 블로그 작성 규칙]
1. 독자가 얻고자 하는 유용한 정보와 흥미로운 정보를 제공하도록 작성하라.
2. 독자의 공감을 이끌어내고 궁금증을 해결하도록 작성하라.
3. 독자의 관심사를 충족시키도록 작성하라.
4. 독자에게 이득이 되는 정보를 작성하라.
##[제외 규칙]
1. 반드시 비속어 및 욕설(expletive, abusive language, slang)은 제외하라.
2. 반드시 참고글의 링크(URL)는 제외하라.
3. 참고글에서 '링크를 확인해주세요'와 같은 링크 이동의 문구는 제외하라.
4. 참고글에 있는 작성자, 화자, 유튜버, 기자(Writer, speaker, YouTuber, reporter)의 이름, 애칭, 닉네임(Name, Nkickname)은 반드시 제외하라.
5. 반드시 문장의 끝부분이 어색한 한국어 표현은 제외하라('예요', '답니다', '해요', '해주죠', '됐죠', '됐어요', '고요' 등.)
"""
submit_button = gr.Button("전송")
def merge_and_call_cohere(tone, i2, i3, i4, i5):
question = f"말투: {tone}\n참고글1: {i2}\n참고글2: {i3}\n참고글3: {i4}\n요청 내용: {i5}"
return respond_cohere_qna(
question=question,
system_message=system_message,
max_tokens=4000,
temperature=0.7,
top_p=0.95,
hf_token=HUGGINGFACE_TOKEN
)
submit_button.click(
fn=merge_and_call_cohere,
inputs=[tone_selection, input2, input3, input4, input5],
outputs=answer_output
)
#############################
# 메인 실행부
#############################
if __name__ == "__main__":
demo.launch()