Spaces:
Sleeping
Sleeping
Commit Β·
e0ed46b
1
Parent(s): 0e0b085
add
Browse files
app.py
CHANGED
|
@@ -2,17 +2,23 @@ import streamlit as st
|
|
| 2 |
import torch
|
| 3 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 4 |
|
| 5 |
-
# λͺ¨λΈ λ‘λ (
|
| 6 |
@st.cache_resource
|
| 7 |
-
def load_model(model_name="
|
| 8 |
tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
|
| 9 |
-
model = AutoModelForCausalLM.from_pretrained(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
return tokenizer, model
|
| 11 |
|
| 12 |
# μ± μ€ν ν¨μ
|
| 13 |
def main():
|
| 14 |
-
st.
|
| 15 |
-
st.
|
|
|
|
| 16 |
|
| 17 |
# μΈμ
μ€ν
μ΄νΈ μ΄κΈ°ν
|
| 18 |
if "chat_history_ids" not in st.session_state:
|
|
@@ -21,9 +27,10 @@ def main():
|
|
| 21 |
st.session_state["past_user_inputs"] = []
|
| 22 |
if "generated_responses" not in st.session_state:
|
| 23 |
st.session_state["generated_responses"] = []
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
|
|
|
| 27 |
# κΈ°μ‘΄ λν λ΄μ νμ
|
| 28 |
if st.session_state["past_user_inputs"]:
|
| 29 |
for user_text, bot_text in zip(st.session_state["past_user_inputs"], st.session_state["generated_responses"]):
|
|
@@ -33,40 +40,45 @@ def main():
|
|
| 33 |
# λ΄ λ©μμ§
|
| 34 |
with st.chat_message("assistant"):
|
| 35 |
st.write(bot_text)
|
| 36 |
-
|
| 37 |
# μ±ν
μ
λ ₯μ°½
|
| 38 |
user_input = st.chat_input("λ©μμ§λ₯Ό μ
λ ₯νμΈμ...")
|
| 39 |
-
|
| 40 |
if user_input:
|
| 41 |
# μ¬μ©μ λ©μμ§ νμ
|
| 42 |
with st.chat_message("user"):
|
| 43 |
st.write(user_input)
|
| 44 |
-
|
| 45 |
-
#
|
| 46 |
-
new_user_input_ids = tokenizer.encode(user_input + tokenizer.eos_token, return_tensors='pt')
|
| 47 |
-
|
| 48 |
if st.session_state["chat_history_ids"] is not None:
|
| 49 |
# κΈ°μ‘΄ νμ€ν 리μ μ΄μ΄ λΆμ΄κΈ°
|
| 50 |
bot_input_ids = torch.cat([st.session_state["chat_history_ids"], new_user_input_ids], dim=-1)
|
| 51 |
else:
|
| 52 |
bot_input_ids = new_user_input_ids
|
| 53 |
-
|
| 54 |
# λͺ¨λΈ μΆλ‘
|
| 55 |
with torch.no_grad():
|
| 56 |
chat_history_ids = model.generate(
|
| 57 |
bot_input_ids,
|
| 58 |
-
max_length=
|
| 59 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
)
|
| 61 |
-
|
| 62 |
-
#
|
| 63 |
-
|
| 64 |
-
|
|
|
|
| 65 |
# μΈμ
μ€ν
μ΄νΈμ λν λ΄μ© μ
λ°μ΄νΈ
|
| 66 |
st.session_state["past_user_inputs"].append(user_input)
|
| 67 |
st.session_state["generated_responses"].append(bot_text)
|
| 68 |
st.session_state["chat_history_ids"] = chat_history_ids
|
| 69 |
-
|
| 70 |
# λ΄ λ©μμ§ νμ
|
| 71 |
with st.chat_message("assistant"):
|
| 72 |
st.write(bot_text)
|
|
|
|
| 2 |
import torch
|
| 3 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 4 |
|
| 5 |
+
# λͺ¨λΈ λ‘λ (DeepSeek-R1-Distill-Qwen-1.5B μμ)
|
| 6 |
@st.cache_resource
|
| 7 |
+
def load_model(model_name="deepseek-ai/DeepSeek-R1-Distill-Qwen-1.5B"):
|
| 8 |
tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
|
| 9 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 10 |
+
model_name,
|
| 11 |
+
device_map="auto",
|
| 12 |
+
torch_dtype=torch.float16,
|
| 13 |
+
trust_remote_code=True # λ§μ½ 컀μ€ν
μ½λκ° νμν κ²½μ° νμ±ν
|
| 14 |
+
)
|
| 15 |
return tokenizer, model
|
| 16 |
|
| 17 |
# μ± μ€ν ν¨μ
|
| 18 |
def main():
|
| 19 |
+
st.set_page_config(page_title="DeepSeek-R1 Chatbot", page_icon="π€")
|
| 20 |
+
st.title("DeepSeek-R1 κΈ°λ° λνν μ±λ΄")
|
| 21 |
+
st.write("DeepSeek-R1-Distill-Qwen-1.5B λͺ¨λΈμ νμ©ν νκ΅μ΄ λν ν
μ€νΈμ© λ°λͺ¨μ
λλ€.")
|
| 22 |
|
| 23 |
# μΈμ
μ€ν
μ΄νΈ μ΄κΈ°ν
|
| 24 |
if "chat_history_ids" not in st.session_state:
|
|
|
|
| 27 |
st.session_state["past_user_inputs"] = []
|
| 28 |
if "generated_responses" not in st.session_state:
|
| 29 |
st.session_state["generated_responses"] = []
|
| 30 |
+
|
| 31 |
+
# λͺ¨λΈκ³Ό ν ν¬λμ΄μ λΆλ¬μ€κΈ°
|
| 32 |
+
tokenizer, model = load_model()
|
| 33 |
+
|
| 34 |
# κΈ°μ‘΄ λν λ΄μ νμ
|
| 35 |
if st.session_state["past_user_inputs"]:
|
| 36 |
for user_text, bot_text in zip(st.session_state["past_user_inputs"], st.session_state["generated_responses"]):
|
|
|
|
| 40 |
# λ΄ λ©μμ§
|
| 41 |
with st.chat_message("assistant"):
|
| 42 |
st.write(bot_text)
|
| 43 |
+
|
| 44 |
# μ±ν
μ
λ ₯μ°½
|
| 45 |
user_input = st.chat_input("λ©μμ§λ₯Ό μ
λ ₯νμΈμ...")
|
| 46 |
+
|
| 47 |
if user_input:
|
| 48 |
# μ¬μ©μ λ©μμ§ νμ
|
| 49 |
with st.chat_message("user"):
|
| 50 |
st.write(user_input)
|
| 51 |
+
|
| 52 |
+
# λͺ¨λΈ μ
λ ₯ μ μ²λ¦¬
|
| 53 |
+
new_user_input_ids = tokenizer.encode(user_input + tokenizer.eos_token, return_tensors='pt').to(model.device)
|
| 54 |
+
|
| 55 |
if st.session_state["chat_history_ids"] is not None:
|
| 56 |
# κΈ°μ‘΄ νμ€ν 리μ μ΄μ΄ λΆμ΄κΈ°
|
| 57 |
bot_input_ids = torch.cat([st.session_state["chat_history_ids"], new_user_input_ids], dim=-1)
|
| 58 |
else:
|
| 59 |
bot_input_ids = new_user_input_ids
|
| 60 |
+
|
| 61 |
# λͺ¨λΈ μΆλ‘
|
| 62 |
with torch.no_grad():
|
| 63 |
chat_history_ids = model.generate(
|
| 64 |
bot_input_ids,
|
| 65 |
+
max_length=32768, # λͺ¨λΈ μΉ΄λ κΆμ₯ μ΅λ κΈΈμ΄
|
| 66 |
+
temperature=0.6, # λͺ¨λΈ μΉ΄λ κΆμ₯ μ¨λ
|
| 67 |
+
top_p=0.95, # λͺ¨λΈ μΉ΄λ κΆμ₯ top-p
|
| 68 |
+
pad_token_id=tokenizer.eos_token_id,
|
| 69 |
+
do_sample=True,
|
| 70 |
+
num_return_sequences=1
|
| 71 |
)
|
| 72 |
+
|
| 73 |
+
# μλ‘ μμ±λ ν ν°λ§ λμ½λ©
|
| 74 |
+
bot_output_ids = chat_history_ids[:, bot_input_ids.shape[-1]:]
|
| 75 |
+
bot_text = tokenizer.decode(bot_output_ids[0], skip_special_tokens=True)
|
| 76 |
+
|
| 77 |
# μΈμ
μ€ν
μ΄νΈμ λν λ΄μ© μ
λ°μ΄νΈ
|
| 78 |
st.session_state["past_user_inputs"].append(user_input)
|
| 79 |
st.session_state["generated_responses"].append(bot_text)
|
| 80 |
st.session_state["chat_history_ids"] = chat_history_ids
|
| 81 |
+
|
| 82 |
# λ΄ λ©μμ§ νμ
|
| 83 |
with st.chat_message("assistant"):
|
| 84 |
st.write(bot_text)
|