Spaces:
Sleeping
Sleeping
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() |