Spaces:
Running
Running
Upload r_story_test.py
Browse files- r_story_test.py +133 -0
r_story_test.py
ADDED
@@ -0,0 +1,133 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#!/usr/bin/env python
|
2 |
+
# coding: utf-8
|
3 |
+
|
4 |
+
# ## 1. tsv full data load
|
5 |
+
import pandas as pd
|
6 |
+
|
7 |
+
df = pd.read_csv("sl_webtoon_full_data_sequential.tsv", sep="\t")
|
8 |
+
|
9 |
+
print(df.head())
|
10 |
+
print("์ ์ฒด ๋ฌธ์ฅ ์:", len(df))
|
11 |
+
print("์ปฌ๋ผ ๋ชฉ๋ก:", df.columns.tolist())
|
12 |
+
|
13 |
+
df['row_id'] = df.index # ์ธ๋ฑ์ค ์ปฌ๋ผ ์ถ๊ฐ
|
14 |
+
df['text'] = df.apply(
|
15 |
+
lambda x: f"[{x['์ํผ์๋']}] #{x['row_id']} {x['type']} {x['scene_text']}",
|
16 |
+
axis=1
|
17 |
+
)
|
18 |
+
texts = df['text'].tolist()
|
19 |
+
print("์ต์ข
๋ฌธ์ฅ ์:", len(texts))
|
20 |
+
|
21 |
+
# ## 2. RAG ๋ฌธ์ฅ ์์ฑ
|
22 |
+
print("์์ 5๊ฐ:")
|
23 |
+
for t in df['text'].head(5).tolist():
|
24 |
+
print("-", t)
|
25 |
+
|
26 |
+
# ## 3. ํ๊ตญ์ด ์๋ฒ ๋ฉ ๋ชจ๋ธ ๋ก๋, ๋ฒกํฐ db
|
27 |
+
from langchain.vectorstores import FAISS
|
28 |
+
from langchain.embeddings import HuggingFaceEmbeddings
|
29 |
+
|
30 |
+
embedding_model = HuggingFaceEmbeddings(model_name='jhgan/ko-sroberta-multitask')
|
31 |
+
|
32 |
+
db = FAISS.from_texts(texts, embedding_model)
|
33 |
+
print(" ๋ฒกํฐDB ์์ฑ ์๋ฃ. ์ด ๋ฌธ์ฅ ์:", len(texts))
|
34 |
+
db.save_local("solo_leveling_faiss_ko")
|
35 |
+
|
36 |
+
db = FAISS.load_local("solo_leveling_faiss_ko", embedding_model, allow_dangerous_deserialization=True)
|
37 |
+
|
38 |
+
# ๊ฒ์ ํ
์คํธ
|
39 |
+
query = "๋ง๋์์ด ๋ญ์ง?"
|
40 |
+
docs = db.similarity_search(query, k=5)
|
41 |
+
for i, doc in enumerate(docs, 1):
|
42 |
+
print(f"[{i}] {doc.page_content}")
|
43 |
+
|
44 |
+
# ## 4. LLM ๋ก๋ (CPU ์ ์ฉ)
|
45 |
+
from transformers import pipeline, AutoTokenizer, AutoModelForCausalLM
|
46 |
+
from langchain.chains import RetrievalQA
|
47 |
+
from langchain.prompts import PromptTemplate
|
48 |
+
from langchain_community.llms import HuggingFacePipeline
|
49 |
+
import torch
|
50 |
+
|
51 |
+
# CPU๋ก ๊ฐ์
|
52 |
+
generator = pipeline(
|
53 |
+
"text-generation",
|
54 |
+
model="kakaocorp/kanana-nano-2.1b-instruct",
|
55 |
+
device=-1 # โ
CPU ์ฌ์ฉ
|
56 |
+
)
|
57 |
+
|
58 |
+
embedding_model = HuggingFaceEmbeddings(model_name='jhgan/ko-sroberta-multitask')
|
59 |
+
vectorstore = FAISS.load_local("solo_leveling_faiss_ko", embedding_model, allow_dangerous_deserialization=True)
|
60 |
+
|
61 |
+
model_name = "kakaocorp/kanana-nano-2.1b-instruct"
|
62 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
63 |
+
model = AutoModelForCausalLM.from_pretrained(
|
64 |
+
model_name,
|
65 |
+
torch_dtype=torch.float32 # โ
CPU์์๋ float32
|
66 |
+
).to("cpu") # โ
CPU ์ฌ์ฉ
|
67 |
+
|
68 |
+
llm_pipeline = pipeline("text-generation", model=model, tokenizer=tokenizer, max_new_tokens=128)
|
69 |
+
llm = HuggingFacePipeline(pipeline=llm_pipeline)
|
70 |
+
|
71 |
+
custom_prompt = PromptTemplate(
|
72 |
+
input_variables=["context", "question"],
|
73 |
+
template="๋ค์ ๋ฌธ๋งฅ์ ์ฐธ๊ณ ํ์ฌ ์ง๋ฌธ์ ๋ตํ์ธ์.\n\n๋ฌธ๋งฅ:\n{context}\n\n์ง๋ฌธ:\n{question}\n\n๋ต๋ณ:"
|
74 |
+
)
|
75 |
+
|
76 |
+
qa_chain = RetrievalQA.from_chain_type(
|
77 |
+
llm=llm,
|
78 |
+
retriever=vectorstore.as_retriever(search_kwargs={"k": 5}),
|
79 |
+
chain_type="stuff",
|
80 |
+
return_source_documents=True,
|
81 |
+
chain_type_kwargs={"prompt": custom_prompt}
|
82 |
+
)
|
83 |
+
|
84 |
+
# ์ง๋ฌธ ํ
์คํธ
|
85 |
+
query = "์ฑ์ง์ฐ๋ ๋ช ๊ธ ํํฐ์ง?"
|
86 |
+
result = qa_chain({"query": query})
|
87 |
+
print("๋ต๋ณ:", result["result"])
|
88 |
+
print("\n์ฐธ์กฐ ๋ฌธ์:")
|
89 |
+
for doc in result["source_documents"]:
|
90 |
+
print(doc.page_content)
|
91 |
+
|
92 |
+
# ## 5. ํฉ๋์ ์ํผ์๋
|
93 |
+
choices = [
|
94 |
+
"1: ํฉ๋์ ๋ฌด๋ฆฌ๋ฅผ ๋ชจ๋ ์ฒ์นํ๋ค.",
|
95 |
+
"2: ์งํธ๋ฅผ ํฌํจํ ํฉ๋์ ๋ฌด๋ฆฌ๋ฅผ ๋ชจ๋ ์ฒ์นํ๋ค.",
|
96 |
+
"3: ์ ๋ถ ๊ธฐ์ ์ํค๊ณ ์ด๋ ค๋๋ค.",
|
97 |
+
"4: ์์คํ
์ ๊ฑฐ๋ถํ๊ณ ๊ทธ๋ฅ ๋๋ง์น๋ค."
|
98 |
+
]
|
99 |
+
|
100 |
+
print("\n[์ ํ์ง]")
|
101 |
+
for idx, choice in enumerate(choices, start=1):
|
102 |
+
print(f"{idx}. {choice}")
|
103 |
+
|
104 |
+
user_idx = int(input("\n์ ํ ๋ฒํธ ์
๋ ฅ: ")) - 1
|
105 |
+
user_choice = choices[user_idx]
|
106 |
+
print(f"\n[์ฌ์ฉ์ ์ ํ]: {user_choice}")
|
107 |
+
|
108 |
+
result = qa_chain({"query": user_choice})
|
109 |
+
retrieved_context = "\n".join([doc.page_content for doc in result["source_documents"]])
|
110 |
+
|
111 |
+
print("\n[๊ฒ์๋ ๊ทผ๊ฑฐ ๋ฌธ์ ์์]")
|
112 |
+
print(retrieved_context[:600], "...")
|
113 |
+
|
114 |
+
prompt = f"""
|
115 |
+
๋น์ ์ ์นํฐ '๋ ํผ์๋ง ๋ ๋ฒจ์
'์ ์ฑ์ง์ฐ์
๋๋ค.
|
116 |
+
ํ์ฌ ์ํฉ:
|
117 |
+
{retrieved_context}
|
118 |
+
์ฌ์ฉ์ ์ ํ: {user_choice}
|
119 |
+
์ฑ์ง์ฐ์ ๋งํฌ๋ก ๊ฐ๊ฒฐํ๊ณ ์์ฐ์ค๋ฌ์ด ๋์ฌ๋ฅผ 1~2๋ฌธ์ฅ ์์ฑํ์ธ์.
|
120 |
+
์ค๋ณต๋ ๋ด์ฉ์ด๋ ๋น์ทํ ๋ฌธ์ฅ์ ๋ง๋ค์ง ๋ง์ธ์.
|
121 |
+
"""
|
122 |
+
|
123 |
+
response = generator(
|
124 |
+
prompt,
|
125 |
+
max_new_tokens=200,
|
126 |
+
do_sample=True,
|
127 |
+
temperature=0.6,
|
128 |
+
top_p=0.9,
|
129 |
+
return_full_text=False
|
130 |
+
)[0]["generated_text"]
|
131 |
+
|
132 |
+
print("\n[์ฑ์ง์ฐ ์๋ต]")
|
133 |
+
print(response)
|