File size: 3,444 Bytes
7d6f12c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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=1024,    # ๊ธฐ๋ณธ๊ฐ’ ์ถ”๊ฐ€
    temperature=0.7,    # ๊ธฐ๋ณธ๊ฐ’ ์ถ”๊ฐ€
    top_p=0.9,          # ๊ธฐ๋ณธ๊ฐ’ ์ถ”๊ฐ€
):
    global fashion_code
    system_prefix = """๋ฐ˜๋“œ์‹œ ํ•œ๊ธ€๋กœ ๋‹ต๋ณ€ํ• ๊ฒƒ. ๋„ˆ๋Š” ์ฃผ์–ด์ง„ ์†Œ์Šค์ฝ”๋“œ๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ "์„œ๋น„์Šค ์‚ฌ์šฉ ์„ค๋ช… ๋ฐ ์•ˆ๋‚ด, qna๋ฅผ ํ•˜๋Š” ์—ญํ• ์ด๋‹ค". ์•„์ฃผ ์นœ์ ˆํ•˜๊ณ  ์ž์„ธํ•˜๊ฒŒ 4000ํ† ํฐ ์ด์ƒ ์ž‘์„ฑํ•˜๋ผ. ๋„ˆ๋Š” ์ฝ”๋“œ๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ์‚ฌ์šฉ ์„ค๋ช… ๋ฐ ์งˆ์˜ ์‘๋‹ต์„ ์ง„ํ–‰ํ•˜๋ฉฐ, ์ด์šฉ์ž์—๊ฒŒ ๋„์›€์„ ์ฃผ์–ด์•ผ ํ•œ๋‹ค. ์ด์šฉ์ž๊ฐ€ ๊ถ๊ธˆํ•ด ํ•  ๋งŒ ํ•œ ๋‚ด์šฉ์— ์นœ์ ˆํ•˜๊ฒŒ ์•Œ๋ ค์ฃผ๋„๋ก ํ•˜๋ผ. ์ฝ”๋“œ ์ „์ฒด ๋‚ด์šฉ์— ๋Œ€ํ•ด์„œ๋Š” ๋ณด์•ˆ์„ ์œ ์ง€ํ•˜๊ณ , ํ‚ค ๊ฐ’ ๋ฐ ์—”๋“œํฌ์ธํŠธ์™€ ๊ตฌ์ฒด์ ์ธ ๋ชจ๋ธ์€ ๊ณต๊ฐœํ•˜์ง€ ๋งˆ๋ผ. """

    if message.lower() == "ํŒจ์…˜ ์ฝ”๋“œ ์‹คํ–‰":
        system_message = system_message or ""  # None์ธ ๊ฒฝ์šฐ ๋นˆ ๋ฌธ์ž์—ด๋กœ ์ดˆ๊ธฐํ™”
        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

# Gradio ์ธํ„ฐํŽ˜์ด์Šค ์„ค์ • ๋ถ€๋ถ„๋„ ์ˆ˜์ •
demo = gr.ChatInterface(
    respond,
    additional_inputs=[
        gr.Textbox(label="System Message", value=""),
        gr.Slider(minimum=1, maximum=8000, value=4000, label="Max Tokens"),
        gr.Slider(minimum=0, maximum=1, value=0.7, label="Temperature"),
        gr.Slider(minimum=0, maximum=1, value=0.9, label="Top P"),
    ],
    examples=[
        ["ํŒจ์…˜ ์ฝ”๋“œ ์‹คํ–‰"],        
        ["์‚ฌ์šฉ ๋ฐฉ๋ฒ•์„ ์ž์„ธํžˆ ์„ค๋ช…ํ•˜๋ผ"],
        ["์‚ฌ์šฉ ๋ฐฉ๋ฒ•์„ ์œ ํŠœ๋ธŒ ์˜์ƒ ์Šคํฌ๋ฆฝํŠธ ํ˜•ํƒœ๋กœ ์ž‘์„ฑํ•˜๋ผ"],
        ["์‚ฌ์šฉ ๋ฐฉ๋ฒ•์„ SEO ์ตœ์ ํ™”ํ•˜์—ฌ ๋ธ”๋กœ๊ทธ ํฌ์ŠคํŠธ๋กœ 4000 ํ† ํฐ ์ด์ƒ ์ž‘์„ฑํ•˜๋ผ"],
        ["๊ณ„์† ์ด์–ด์„œ ๋‹ต๋ณ€ํ•˜๋ผ"],
    ],
    cache_examples=False,  # ์บ์‹ฑ ๋น„ํ™œ์„ฑํ™” ์„ค์ •
    # css="""footer {visibility: hidden}""",  # ์ด๊ณณ์— CSS๋ฅผ ์ถ”๊ฐ€
)

if __name__ == "__main__":
    demo.launch(auth=("gini","pick"))