Spaces:
Running
on
Zero
Running
on
Zero
cutechicken
commited on
Commit
โข
f4a0f87
1
Parent(s):
0b984ea
Update app.py
Browse files
app.py
CHANGED
@@ -12,34 +12,69 @@ from transformers import AutoModelForCausalLM, AutoTokenizer
|
|
12 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
13 |
MODEL_ID = "CohereForAI/c4ai-command-r7b-12-2024"
|
14 |
|
|
|
|
|
15 |
class ModelManager:
|
16 |
def __init__(self):
|
17 |
-
self.
|
18 |
-
self.
|
19 |
-
self.setup_model()
|
20 |
|
21 |
-
def
|
22 |
try:
|
23 |
-
print("
|
24 |
-
self.
|
25 |
-
|
26 |
-
|
27 |
-
trust_remote_code=True
|
28 |
-
)
|
29 |
-
print("ํ ํฌ๋์ด์ ๋ก๋ฉ ์๋ฃ")
|
30 |
-
|
31 |
-
print("๋ชจ๋ธ ๋ก๋ฉ ์์...")
|
32 |
-
self.model = AutoModelForCausalLM.from_pretrained(
|
33 |
-
MODEL_ID,
|
34 |
token=HF_TOKEN,
|
35 |
device_map="auto",
|
36 |
-
trust_remote_code=True,
|
37 |
torch_dtype=torch.float16
|
38 |
)
|
39 |
-
print("
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
40 |
except Exception as e:
|
41 |
-
|
42 |
-
raise Exception(f"๋ชจ๋ธ ๋ก๋ฉ ์คํจ: {e}")
|
43 |
|
44 |
class ChatHistory:
|
45 |
def __init__(self):
|
|
|
12 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
13 |
MODEL_ID = "CohereForAI/c4ai-command-r7b-12-2024"
|
14 |
|
15 |
+
from transformers import pipeline
|
16 |
+
|
17 |
class ModelManager:
|
18 |
def __init__(self):
|
19 |
+
self.pipe = None
|
20 |
+
self.setup_pipeline()
|
|
|
21 |
|
22 |
+
def setup_pipeline(self):
|
23 |
try:
|
24 |
+
print("ํ์ดํ๋ผ์ธ ์ด๊ธฐํ ์์...")
|
25 |
+
self.pipe = pipeline(
|
26 |
+
"text-generation",
|
27 |
+
model=MODEL_ID,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
28 |
token=HF_TOKEN,
|
29 |
device_map="auto",
|
|
|
30 |
torch_dtype=torch.float16
|
31 |
)
|
32 |
+
print("ํ์ดํ๋ผ์ธ ์ด๊ธฐํ ์๋ฃ")
|
33 |
+
except Exception as e:
|
34 |
+
print(f"ํ์ดํ๋ผ์ธ ์ด๊ธฐํ ์ค ์ค๋ฅ ๋ฐ์: {e}")
|
35 |
+
raise Exception(f"ํ์ดํ๋ผ์ธ ์ด๊ธฐํ ์คํจ: {e}")
|
36 |
+
|
37 |
+
def generate_response(self, messages, max_tokens=4000, temperature=0.7, top_p=0.9):
|
38 |
+
try:
|
39 |
+
# ๋ฉ์์ง ํ์ ๋ณํ
|
40 |
+
prompt = ""
|
41 |
+
for msg in messages:
|
42 |
+
role = msg["role"]
|
43 |
+
content = msg["content"]
|
44 |
+
if role == "system":
|
45 |
+
prompt += f"System: {content}\n"
|
46 |
+
elif role == "user":
|
47 |
+
prompt += f"User: {content}\n"
|
48 |
+
elif role == "assistant":
|
49 |
+
prompt += f"Assistant: {content}\n"
|
50 |
+
|
51 |
+
# ์๋ต ์์ฑ
|
52 |
+
response = self.pipe(
|
53 |
+
prompt,
|
54 |
+
max_new_tokens=max_tokens,
|
55 |
+
temperature=temperature,
|
56 |
+
top_p=top_p,
|
57 |
+
do_sample=True,
|
58 |
+
num_return_sequences=1,
|
59 |
+
pad_token_id=self.pipe.tokenizer.eos_token_id
|
60 |
+
)
|
61 |
+
|
62 |
+
# ์๋ต ํ
์คํธ ์ถ์ถ ๋ฐ ์คํธ๋ฆฌ๋ฐ ์๋ฎฌ๋ ์ด์
|
63 |
+
generated_text = response[0]['generated_text'][len(prompt):].strip()
|
64 |
+
words = generated_text.split()
|
65 |
+
|
66 |
+
# ๋จ์ด ๋จ์๋ก ์คํธ๋ฆฌ๋ฐ
|
67 |
+
partial_response = ""
|
68 |
+
for word in words:
|
69 |
+
partial_response += word + " "
|
70 |
+
yield type('Response', (), {
|
71 |
+
'choices': [type('Choice', (), {
|
72 |
+
'delta': {'content': word + " "}
|
73 |
+
})()]
|
74 |
+
})()
|
75 |
+
|
76 |
except Exception as e:
|
77 |
+
raise Exception(f"์๋ต ์์ฑ ์คํจ: {e}")
|
|
|
78 |
|
79 |
class ChatHistory:
|
80 |
def __init__(self):
|