Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,118 +1,138 @@
|
|
1 |
-
import os
|
2 |
-
import zipfile
|
3 |
-
import pandas as pd
|
4 |
import gradio as gr
|
5 |
from langchain_community.vectorstores import FAISS
|
6 |
from langchain_community.embeddings import HuggingFaceEmbeddings
|
7 |
-
from langchain.chains import RetrievalQA
|
8 |
-
from langchain.prompts import PromptTemplate
|
9 |
-
from langchain_community.llms import HuggingFacePipeline
|
10 |
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
|
11 |
import torch
|
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 |
model_name = "kakaocorp/kanana-nano-2.1b-instruct"
|
53 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
54 |
-
model = AutoModelForCausalLM.from_pretrained(
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
# ====== QA ์ฒด์ธ ======
|
59 |
-
custom_prompt = PromptTemplate(
|
60 |
-
input_variables=["context", "question"],
|
61 |
-
template="๋ค์ ๋ฌธ๋งฅ์ ์ฐธ๊ณ ํ์ฌ ์ง๋ฌธ์ ๋ตํ์ธ์.\n\n๋ฌธ๋งฅ:\n{context}\n\n์ง๋ฌธ:\n{question}\n\n๋ต๋ณ:"
|
62 |
)
|
63 |
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
|
|
|
|
|
|
|
|
70 |
)
|
|
|
71 |
|
72 |
-
#
|
73 |
choices = [
|
74 |
-
"1
|
75 |
-
"2
|
76 |
-
"3
|
77 |
-
"4
|
78 |
]
|
79 |
|
80 |
-
|
|
|
81 |
try:
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
retrieved_context = "\n".join([doc.page_content for doc in result["source_documents"]])
|
91 |
-
|
92 |
prompt = f"""
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
#
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
111 |
demo = gr.ChatInterface(
|
112 |
-
|
113 |
-
title="
|
114 |
-
description=
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
115 |
)
|
116 |
|
|
|
117 |
if __name__ == "__main__":
|
|
|
|
|
|
|
118 |
demo.launch()
|
|
|
1 |
+
import os, zipfile, shutil, glob
|
|
|
|
|
2 |
import gradio as gr
|
3 |
from langchain_community.vectorstores import FAISS
|
4 |
from langchain_community.embeddings import HuggingFaceEmbeddings
|
|
|
|
|
|
|
5 |
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
|
6 |
import torch
|
7 |
+
import langchain
|
8 |
+
|
9 |
+
ZIP_NAME = "solo_leveling_faiss_ko.zip"
|
10 |
+
TARGET_DIR = "solo_leveling_faiss_ko"
|
11 |
+
|
12 |
+
def ensure_faiss_dir() -> str:
|
13 |
+
"""FAISS index๊ฐ ์ด๋ ์๋ ๋ก๋ ๊ฐ๋ฅํ ์์น๋ฅผ ๋ณด์ฅํฉ๋๋ค."""
|
14 |
+
if os.path.exists(os.path.join(TARGET_DIR, "index.faiss")) and \
|
15 |
+
os.path.exists(os.path.join(TARGET_DIR, "index.pkl")):
|
16 |
+
return TARGET_DIR
|
17 |
+
|
18 |
+
if os.path.exists("index.faiss") and os.path.exists("index.pkl"):
|
19 |
+
os.makedirs(TARGET_DIR, exist_ok=True)
|
20 |
+
if not os.path.exists(os.path.join(TARGET_DIR, "index.faiss")):
|
21 |
+
shutil.move("index.faiss", os.path.join(TARGET_DIR, "index.faiss"))
|
22 |
+
if not os.path.exists(os.path.join(TARGET_DIR, "index.pkl")):
|
23 |
+
shutil.move("index.pkl", os.path.join(TARGET_DIR, "index.pkl"))
|
24 |
+
return TARGET_DIR
|
25 |
+
|
26 |
+
if os.path.exists(ZIP_NAME):
|
27 |
+
with zipfile.ZipFile(ZIP_NAME, 'r') as z:
|
28 |
+
z.extractall(".")
|
29 |
+
if os.path.exists(os.path.join(TARGET_DIR, "index.faiss")) and \
|
30 |
+
os.path.exists(os.path.join(TARGET_DIR, "index.pkl")):
|
31 |
+
return TARGET_DIR
|
32 |
+
faiss_cand = glob.glob("**/index.faiss", recursive=True)
|
33 |
+
pkl_cand = glob.glob("**/index.pkl", recursive=True)
|
34 |
+
if faiss_cand and pkl_cand:
|
35 |
+
os.makedirs(TARGET_DIR, exist_ok=True)
|
36 |
+
shutil.copy2(faiss_cand[0], os.path.join(TARGET_DIR, "index.faiss"))
|
37 |
+
shutil.copy2(pkl_cand[0], os.path.join(TARGET_DIR, "index.pkl"))
|
38 |
+
return TARGET_DIR
|
39 |
+
|
40 |
+
raise FileNotFoundError("FAISS index files not found (index.faiss / index.pkl).")
|
41 |
+
|
42 |
+
# 0) FAISS ์ธ๋ฑ์ค ์์น ํ๋ณด
|
43 |
+
base_dir = ensure_faiss_dir()
|
44 |
+
|
45 |
+
# 1) ๋ฒกํฐ DB
|
46 |
+
embeddings = HuggingFaceEmbeddings(model_name="jhgan/ko-sroberta-multitask")
|
47 |
+
vectorstore = FAISS.load_local(base_dir, embeddings, allow_dangerous_deserialization=True)
|
48 |
+
|
49 |
+
# 2) ๋ชจ๋ธ ๋ก๋ฉ (CPU ํ๊ฒฝ ์์ ์ต์
)
|
50 |
model_name = "kakaocorp/kanana-nano-2.1b-instruct"
|
51 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
52 |
+
model = AutoModelForCausalLM.from_pretrained(
|
53 |
+
model_name,
|
54 |
+
torch_dtype=torch.float32,
|
55 |
+
device_map=None
|
|
|
|
|
|
|
|
|
56 |
)
|
57 |
|
58 |
+
# 3) ํ
์คํธ ์์ฑ ํ์ดํ๋ผ์ธ
|
59 |
+
pipe = pipeline(
|
60 |
+
"text-generation",
|
61 |
+
model=model,
|
62 |
+
tokenizer=tokenizer,
|
63 |
+
max_new_tokens=100,
|
64 |
+
temperature=0.6,
|
65 |
+
do_sample=True,
|
66 |
+
top_p=0.9,
|
67 |
+
return_full_text=False
|
68 |
)
|
69 |
+
lm = pipe
|
70 |
|
71 |
+
# ์ ํ์ง
|
72 |
choices = [
|
73 |
+
"1: ํฉ๋์ ๋ฌด๋ฆฌ๋ฅผ ๋ชจ๋ ์ฒ์นํ๋ค.",
|
74 |
+
"2: ์งํธ๋ฅผ ํฌํจํ ํฉ๋์ ๋ฌด๋ฆฌ๋ฅผ ๋ชจ๋ ์ฒ์นํ๋ค.",
|
75 |
+
"3: ์ ๋ถ ๊ธฐ์ ์ํค๊ณ ์ด๋ ค๋๋ค.",
|
76 |
+
"4: ์์คํ
์ ๊ฑฐ๋ถํ๊ณ ๊ทธ๋ฅ ๋๋ง์น๋ค."
|
77 |
]
|
78 |
|
79 |
+
# RAG + ๋์ฌ ์์ฑ ํจ์
|
80 |
+
def rag_answer(message, history):
|
81 |
try:
|
82 |
+
user_idx = int(message.strip()) - 1
|
83 |
+
user_choice = choices[user_idx]
|
84 |
+
except:
|
85 |
+
return "โ์ฌ๋ฐ๋ฅธ ๋ฒํธ๋ฅผ ์
๋ ฅํด์ฃผ์ธ์. (์: 1 ~ 4)"
|
86 |
+
|
87 |
+
# FAISS ๊ฒ์
|
88 |
+
docs = vectorstore.similarity_search(user_choice, k=3)
|
89 |
+
context = "\n".join([doc.page_content for doc in docs])
|
|
|
|
|
90 |
prompt = f"""
|
91 |
+
๋น์ ์ ์นํฐ '๋ ํผ์๋ง ๋ ๋ฒจ์
'์ ์ฑ์ง์ฐ์
๋๋ค.
|
92 |
+
ํ์ฌ ์ํฉ:
|
93 |
+
{context}
|
94 |
+
์ฌ์ฉ์ ์ ํ: {user_choice}
|
95 |
+
์ฑ์ง์ฐ์ ๋งํฌ๋ก ๊ฐ๊ฒฐํ๊ณ ์์ฐ์ค๋ฌ์ด ๋์ฌ๋ฅผ 1~2๋ฌธ์ฅ ์์ฑํ์ธ์.
|
96 |
+
์ค๋ณต๋ ๋ด์ฉ์ด๋ ๋น์ทํ ๋ฌธ์ฅ์ ๋ง๋ค์ง ๋ง์ธ์.
|
97 |
+
"""
|
98 |
+
response = lm(prompt)[0]["generated_text"]
|
99 |
+
only_dialogue = response.strip().split("\n")[-1]
|
100 |
+
|
101 |
+
# "๋์ฌ:" ์ค๋ณต ๋ฐฉ์ง
|
102 |
+
if not only_dialogue.startswith("๋์ฌ:"):
|
103 |
+
only_dialogue = "๋์ฌ: " + only_dialogue
|
104 |
+
|
105 |
+
return only_dialogue
|
106 |
+
|
107 |
+
# ๋ฐฐ๊ฒฝ ์ด๋ฏธ์ง CSS
|
108 |
+
css_code = """
|
109 |
+
body {
|
110 |
+
background-image: url('https://huggingface.co/spaces/min24ss/r-story-test/resolve/main/jinwoo.png');
|
111 |
+
background-size: cover;
|
112 |
+
background-position: center;
|
113 |
+
}
|
114 |
+
"""
|
115 |
+
|
116 |
+
# Gradio UI
|
117 |
demo = gr.ChatInterface(
|
118 |
+
fn=rag_answer,
|
119 |
+
title="[๊ธด๊ธ ํ์คํธ: ์ ์ ์ฒ์นํ๋ผ!]",
|
120 |
+
description=(
|
121 |
+
"'ํ๋ ์ด์ด'์๊ฒ ์ด์๋ฅผ ๊ฐ์ง ์ด๋ค์ด ์ฃผ์์ ์์ต๋๋ค. ์ด๋ค์ ๋ชจ๋ ์ฒ์นํ์ฌ ์์ ์ ํ๋ณดํ์ญ์์ค.<br>"
|
122 |
+
"์ง์์ ๋ฐ๋ฅด์ง ์์ผ๋ฉด ๋น์ ์ ์ฌ์ฅ์ ์ ์ง(!)ํ๊ฒ ๋ฉ๋๋ค.<br>"
|
123 |
+
"์ฒ์นํด์ผ ํ ์ ์ ์ซ์: 8๋ช
/ ์ฒ์นํ ์ ์ ์ซ์: 0๋ช
<br><br>"
|
124 |
+
"๐ฌ ์ ํ์ง๋ฅผ ์
๋ ฅํ์ธ์:<br>"
|
125 |
+
"1: ํฉ๋์ ๋ฌด๋ฆฌ๋ฅผ ๋ชจ๋ ์ฒ์นํ๋ค.<br>"
|
126 |
+
"2: ํฉ๋์ ๋ฌด๋ฆฌ์ ์งํธ๋ฅผ ํฌํจํ์ฌ ๋ชจ๋ ์ฒ์นํ๋ค.<br>"
|
127 |
+
"3: ์ ๋ถ ๊ธฐ์ ์ํค๊ณ ์ด๋ ค๋๋ค.<br>"
|
128 |
+
"4: ์์คํ
์ ๊ฑฐ๋ถํ๊ณ ๊ทธ๋ฅ ๋๋ง์น๋ค."
|
129 |
+
),
|
130 |
+
css=css_code
|
131 |
)
|
132 |
|
133 |
+
# ์คํ
|
134 |
if __name__ == "__main__":
|
135 |
+
print("Torch:", torch.__version__)
|
136 |
+
print("Transformers:", __import__('transformers').__version__)
|
137 |
+
print("LangChain:", langchain.__version__)
|
138 |
demo.launch()
|