File size: 4,819 Bytes
1dcf851
0f57722
 
 
1dcf851
0f57722
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1dcf851
0f57722
 
1dcf851
0f57722
 
 
 
 
 
 
 
 
 
 
 
1dcf851
0f57722
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1dcf851
 
0f57722
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import gradio as gr
from transformers import AutoTokenizer, AutoModelForCausalLM
import torch
import gc

class ModelManager:
    def __init__(self):
        self.model = None
        self.tokenizer = None
        self.model_name = "CohereForAI/c4ai-command-r-plus-4bit"
    
    def load_model(self):
        if self.model is None:
            try:
                print("๋ชจ๋ธ ๋กœ๋”ฉ ์ค‘... ์‹œ๊ฐ„์ด ๊ฑธ๋ฆด ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.")
                self.tokenizer = AutoTokenizer.from_pretrained(self.model_name)
                self.model = AutoModelForCausalLM.from_pretrained(
                    self.model_name,
                    torch_dtype=torch.float16,
                    device_map="auto",
                    trust_remote_code=True,
                    load_in_4bit=True,
                    low_cpu_mem_usage=True
                )
                print("๋ชจ๋ธ ๋กœ๋”ฉ ์™„๋ฃŒ!")
                return True
            except Exception as e:
                print(f"๋ชจ๋ธ ๋กœ๋”ฉ ์‹คํŒจ: {e}")
                return False
        return True
    
    def generate(self, message, history, max_tokens=1000, temperature=0.7):
        if not self.load_model():
            return "๋ชจ๋ธ ๋กœ๋”ฉ์— ์‹คํŒจํ–ˆ์Šต๋‹ˆ๋‹ค."
        
        try:
            # ์ฑ„ํŒ… ํžˆ์Šคํ† ๋ฆฌ ๊ตฌ์„ฑ
            conversation = []
            for human, assistant in history:
                conversation.append({"role": "user", "content": human})
                if assistant:
                    conversation.append({"role": "assistant", "content": assistant})
            conversation.append({"role": "user", "content": message})
            
            # ํ† ํฐํ™”
            input_ids = self.tokenizer.apply_chat_template(
                conversation,
                return_tensors="pt",
                add_generation_prompt=True
            )
            
            if torch.cuda.is_available():
                input_ids = input_ids.to("cuda")
            
            # ์ƒ์„ฑ
            with torch.no_grad():
                outputs = self.model.generate(
                    input_ids,
                    max_new_tokens=max_tokens,
                    temperature=temperature,
                    do_sample=True,
                    pad_token_id=self.tokenizer.eos_token_id,
                    eos_token_id=self.tokenizer.eos_token_id
                )
            
            response = self.tokenizer.decode(
                outputs[0][input_ids.shape[-1]:], 
                skip_special_tokens=True
            )
            
            return response
            
        except Exception as e:
            return f"์ƒ์„ฑ ์ค‘ ์˜ค๋ฅ˜ ๋ฐœ์ƒ: {str(e)}"
        finally:
            # ๋ฉ”๋ชจ๋ฆฌ ์ •๋ฆฌ
            if torch.cuda.is_available():
                torch.cuda.empty_cache()
            gc.collect()

# ๋ชจ๋ธ ๋งค๋‹ˆ์ € ์ธ์Šคํ„ด์Šค
model_manager = ModelManager()

def chat_fn(message, history, max_tokens, temperature):
    if not message.strip():
        return history, ""
    
    # ์‚ฌ์šฉ์ž ๋ฉ”์‹œ์ง€ ์ถ”๊ฐ€
    history.append([message, "์ƒ์„ฑ ์ค‘..."])
    
    # ๋ด‡ ์‘๋‹ต ์ƒ์„ฑ
    response = model_manager.generate(message, history[:-1], max_tokens, temperature)
    history[-1][1] = response
    
    return history, ""

# Gradio ์ธํ„ฐํŽ˜์ด์Šค
with gr.Blocks(title="Command R+ Chat") as demo:
    gr.Markdown("""
    # ๐Ÿค– Command R+ 4bit ์ฑ„ํŒ…๋ด‡
    
    Cohere์˜ Command R+ 4bit ์–‘์žํ™” ๋ชจ๋ธ๊ณผ ๋Œ€ํ™”ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
    โš ๏ธ ์ฒซ ์‹คํ–‰ ์‹œ ๋ชจ๋ธ ๋กœ๋”ฉ์— ์‹œ๊ฐ„์ด ๊ฑธ๋ฆด ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
    """)
    
    chatbot = gr.Chatbot(
        height=500,
        show_label=False,
        show_copy_button=True
    )
    
    with gr.Row():
        msg = gr.Textbox(
            label="๋ฉ”์‹œ์ง€ ์ž…๋ ฅ",
            placeholder="Command R+์—๊ฒŒ ์งˆ๋ฌธํ•˜์„ธ์š”...",
            lines=2,
            scale=4
        )
        submit = gr.Button("์ „์†ก ๐Ÿ“ค", variant="primary", scale=1)
        
    with gr.Row():
        clear = gr.Button("๋Œ€ํ™” ์ดˆ๊ธฐํ™” ๐Ÿ—‘๏ธ")
    
    with gr.Accordion("๊ณ ๊ธ‰ ์„ค์ •", open=False):
        max_tokens = gr.Slider(
            minimum=100, 
            maximum=2000, 
            value=1000, 
            step=100,
            label="์ตœ๋Œ€ ํ† ํฐ ์ˆ˜"
        )
        temperature = gr.Slider(
            minimum=0.1, 
            maximum=1.0, 
            value=0.7, 
            step=0.1,
            label="Temperature (์ฐฝ์˜์„ฑ)"
        )
    
    # ์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ
    msg.submit(
        chat_fn,
        [msg, chatbot, max_tokens, temperature],
        [chatbot, msg]
    )
    
    submit.click(
        chat_fn,
        [msg, chatbot, max_tokens, temperature],
        [chatbot, msg]
    )
    
    clear.click(lambda: ([], ""), outputs=[chatbot, msg])

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