File size: 2,837 Bytes
747ccea
 
fe67895
91c1d45
f779047
54a4802
e74c3bc
91c1d45
0e5afe0
6da265e
 
 
 
 
 
 
 
 
 
 
747ccea
 
 
 
 
 
 
 
6da265e
 
91c1d45
6da265e
 
 
 
 
747ccea
6da265e
747ccea
 
 
 
 
6da265e
747ccea
 
91c1d45
 
 
 
 
 
 
 
 
 
 
6da265e
91c1d45
 
 
3176ef0
6da265e
37b20ad
 
6da265e
 
 
3176ef0
91c1d45
6da265e
747ccea
 
 
91c1d45
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
import gradio as gr
from huggingface_hub import InferenceClient
import os
import requests

# μΆ”λ‘  API ν΄λΌμ΄μ–ΈνŠΈ μ„€μ •
hf_client = InferenceClient("CohereForAI/c4ai-command-r-plus-08-2024", token=os.getenv("HF_TOKEN"))
#hf_client = InferenceClient("CohereForAI/aya-23-35B", token=os.getenv("HF_TOKEN"))

def load_fashion_code():
    try:
        with open('fashion.cod', 'r', encoding='utf-8') as file:
            return file.read()
    except FileNotFoundError:
        return "fashion.cod νŒŒμΌμ„ 찾을 수 μ—†μŠ΅λ‹ˆλ‹€."
    except Exception as e:
        return f"νŒŒμΌμ„ μ½λŠ” 쀑 였λ₯˜κ°€ λ°œμƒν–ˆμŠ΅λ‹ˆλ‹€: {str(e)}"

fashion_code = load_fashion_code()

def respond(
    message,
    history: list[tuple[str, str]],
    system_message,
    max_tokens,
    temperature,
    top_p,
):
    global fashion_code
    system_prefix = """λ°˜λ“œμ‹œ ν•œκΈ€λ‘œ 닡변할것. λ„ˆλŠ” μ£Όμ–΄μ§„ μ†ŒμŠ€μ½”λ“œλ₯Ό 기반으둜 "μ„œλΉ„μŠ€ μ‚¬μš© μ„€λͺ… 및 μ•ˆλ‚΄, qnaλ₯Ό ν•˜λŠ” 역할이닀". μ½”λ“œλ₯Ό 기반으둜 μ‚¬μš© μ„€λͺ… 및 질의 응닡을 μ§„ν–‰ν•˜λ©°, μ΄μš©μžμ—κ²Œ 도움을 μ£Όμ–΄μ•Ό ν•œλ‹€. μ΄μš©μžκ°€ κΆκΈˆν•΄ ν•  만 ν•œ λ‚΄μš©μ— μΉœμ ˆν•˜κ²Œ μ•Œλ €μ£Όλ„λ‘ ν•˜λΌ. μ½”λ“œ 전체 λ‚΄μš©μ— λŒ€ν•΄μ„œλŠ” λ³΄μ•ˆμ„ μœ μ§€ν•˜κ³ , ν‚€ κ°’ 및 μ—”λ“œν¬μΈνŠΈμ™€ ꡬ체적인 λͺ¨λΈμ€ κ³΅κ°œν•˜μ§€ 마라. """

    if message.lower() == "νŒ¨μ…˜ μ½”λ“œ μ‹€ν–‰":
        system_message += f"\n\nνŒ¨μ…˜ μ½”λ“œ λ‚΄μš©:\n{fashion_code}"
        message = "νŒ¨μ…˜ μ½”λ“œμ— λŒ€ν•΄ μ„€λͺ…ν•΄μ£Όμ„Έμš”."

    messages = [{"role": "system", "content": f"{system_prefix} {system_message}"}]

    # prefix μΆ”κ°€
    for val in history:
        if val[0]:
            messages.append({"role": "user", "content": val[0]})
        if val[1]:
            messages.append({"role": "assistant", "content": val[1]})
    
    messages.append({"role": "user", "content": message})

    response = ""
    for message in hf_client.chat_completion(
        messages,
        max_tokens=max_tokens,
        stream=True,
        temperature=temperature,
        top_p=top_p,
    ):
        token = message.choices[0].delta.content
        if token is not None:
            response += token.strip("")  # 토큰 제거
    yield response

demo = gr.ChatInterface(
    respond,
    examples=[
        ["νŒ¨μ…˜ μ½”λ“œ μ‹€ν–‰"],        
        ["μ‚¬μš© 방법을 μžμ„Ένžˆ μ„€λͺ…ν•˜λΌ"],
        ["μ‚¬μš© 방법을 유튜브 μ˜μƒ 슀크립트 ν˜•νƒœλ‘œ μž‘μ„±ν•˜λΌ"],
        ["μ‚¬μš© 방법을 SEO μ΅œμ ν™”ν•˜μ—¬ λΈ”λ‘œκ·Έ 포슀트둜 4000 토큰 이상 μž‘μ„±ν•˜λΌ"],
        ["계속 μ΄μ–΄μ„œ λ‹΅λ³€ν•˜λΌ"],
        ["νŒ¨μ…˜ μ½”λ“œ μ‹€ν–‰"],
    ],
    cache_examples=False,  # 캐싱 λΉ„ν™œμ„±ν™” μ„€μ •
    # css="""footer {visibility: hidden}""",  # 이곳에 CSSλ₯Ό μΆ”κ°€
)

if __name__ == "__main__":
    demo.launch()