Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,16 +1,12 @@
|
|
1 |
import gradio as gr
|
2 |
from datasets import load_dataset
|
3 |
-
import
|
4 |
-
from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer, BitsAndBytesConfig
|
5 |
import torch
|
6 |
from threading import Thread
|
7 |
from sentence_transformers import SentenceTransformer
|
8 |
import faiss
|
9 |
import fitz # PyMuPDF
|
10 |
-
|
11 |
-
# ํ๊ฒฝ ๋ณ์์์ Hugging Face ํ ํฐ ๊ฐ์ ธ์ค๊ธฐ
|
12 |
-
token = os.environ.get("HF_TOKEN")
|
13 |
-
|
14 |
|
15 |
# ์๋ฒ ๋ฉ ๋ชจ๋ธ ๋ก๋
|
16 |
ST = SentenceTransformer("mixedbread-ai/mxbai-embed-large-v1")
|
@@ -23,17 +19,36 @@ def extract_text_from_pdf(pdf_path):
|
|
23 |
text += page.get_text()
|
24 |
return text
|
25 |
|
26 |
-
#
|
27 |
-
|
28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
|
30 |
-
#
|
31 |
-
|
32 |
-
law_embeddings = ST.encode(law_sentences)
|
33 |
|
34 |
-
#
|
35 |
-
|
36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
37 |
|
38 |
# Hugging Face์์ ๋ฒ๋ฅ ์๋ด ๋ฐ์ดํฐ์
๋ก๋
|
39 |
dataset = load_dataset("jihye-moon/LawQA-Ko")
|
@@ -44,29 +59,21 @@ data = data.map(lambda x: {"question_embedding": ST.encode(x["question"])}, batc
|
|
44 |
data.add_faiss_index(column="question_embedding")
|
45 |
|
46 |
# LLaMA ๋ชจ๋ธ ์ค์
|
47 |
-
model_id = "
|
48 |
-
|
49 |
-
load_in_4bit=True, bnb_4bit_use_double_quant=True, bnb_4bit_quant_type="nf4", bnb_4bit_compute_dtype=torch.bfloat16
|
50 |
-
)
|
51 |
-
tokenizer = AutoTokenizer.from_pretrained(model_id, token=token)
|
52 |
model = AutoModelForCausalLM.from_pretrained(
|
53 |
model_id,
|
54 |
torch_dtype=torch.bfloat16,
|
55 |
device_map="auto",
|
56 |
-
quantization_config=bnb_config,
|
57 |
-
token=token
|
58 |
)
|
59 |
|
60 |
SYS_PROMPT = """You are an assistant for answering legal questions.
|
61 |
You are given the extracted parts of legal documents and a question. Provide a conversational answer.
|
62 |
If you don't know the answer, just say "I do not know." Don't make up an answer.
|
63 |
-
you must answer korean.
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
query_embedding = ST.encode([query])
|
68 |
-
D, I = index.search(query_embedding, k)
|
69 |
-
return [(law_sentences[i], D[0][idx]) for idx, i in enumerate(I[0])]
|
70 |
|
71 |
# ๋ฒ๋ฅ ์๋ด ๋ฐ์ดํฐ ๊ฒ์ ํจ์
|
72 |
def search_qa(query, k=3):
|
@@ -97,11 +104,7 @@ def talk(prompt, history):
|
|
97 |
messages = [{"role": "system", "content": SYS_PROMPT}, {"role": "user", "content": formatted_prompt}]
|
98 |
|
99 |
# ๋ชจ๋ธ์๊ฒ ์์ฑ ์ง์
|
100 |
-
input_ids = tokenizer.
|
101 |
-
messages,
|
102 |
-
add_generation_prompt=True,
|
103 |
-
return_tensors="pt"
|
104 |
-
).to(model.device)
|
105 |
|
106 |
streamer = TextIteratorStreamer(
|
107 |
tokenizer, timeout=10.0, skip_prompt=True, skip_special_tokens=True
|
@@ -147,4 +150,4 @@ demo = gr.ChatInterface(
|
|
147 |
)
|
148 |
|
149 |
# Gradio ๋ฐ๋ชจ ์คํ
|
150 |
-
demo.launch(debug=True)
|
|
|
1 |
import gradio as gr
|
2 |
from datasets import load_dataset
|
3 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer
|
|
|
4 |
import torch
|
5 |
from threading import Thread
|
6 |
from sentence_transformers import SentenceTransformer
|
7 |
import faiss
|
8 |
import fitz # PyMuPDF
|
9 |
+
import os
|
|
|
|
|
|
|
10 |
|
11 |
# ์๋ฒ ๋ฉ ๋ชจ๋ธ ๋ก๋
|
12 |
ST = SentenceTransformer("mixedbread-ai/mxbai-embed-large-v1")
|
|
|
19 |
text += page.get_text()
|
20 |
return text
|
21 |
|
22 |
+
# ๊ธฐ๋ณธ ์ ๊ณต PDF ํ์ผ ๊ฒฝ๋ก
|
23 |
+
default_pdf_path = "laws.pdf"
|
24 |
+
|
25 |
+
# FAISS ์ธ๋ฑ์ค ์ด๊ธฐํ
|
26 |
+
index = None
|
27 |
+
law_sentences = []
|
28 |
+
|
29 |
+
# ๊ธฐ๋ณธ ์ ๊ณต PDF ํ์ผ ์ฒ๋ฆฌ ํจ์
|
30 |
+
def process_default_pdf():
|
31 |
+
global index, law_sentences
|
32 |
|
33 |
+
# PDF์์ ํ
์คํธ ์ถ์ถ
|
34 |
+
law_text = extract_text_from_pdf(default_pdf_path)
|
|
|
35 |
|
36 |
+
# ๋ฌธ์ฅ์ ๋๋๊ณ ์๋ฒ ๋ฉ ์์ฑ
|
37 |
+
law_sentences = law_text.split('\n')
|
38 |
+
law_embeddings = ST.encode(law_sentences)
|
39 |
+
|
40 |
+
# FAISS ์ธ๋ฑ์ค ์์ฑ ๋ฐ ์๋ฒ ๋ฉ ์ถ๊ฐ
|
41 |
+
index = faiss.IndexFlatL2(law_embeddings.shape[1])
|
42 |
+
index.add(law_embeddings)
|
43 |
+
|
44 |
+
# ์ฒ์์ ๊ธฐ๋ณธ PDF ํ์ผ ์ฒ๋ฆฌ
|
45 |
+
process_default_pdf()
|
46 |
+
|
47 |
+
# ๋ฒ๋ฅ ๋ฌธ์ ๊ฒ์ ํจ์
|
48 |
+
def search_law(query, k=5):
|
49 |
+
query_embedding = ST.encode([query])
|
50 |
+
D, I = index.search(query_embedding, k)
|
51 |
+
return [(law_sentences[i], D[0][idx]) for idx, i in enumerate(I[0])]
|
52 |
|
53 |
# Hugging Face์์ ๋ฒ๋ฅ ์๋ด ๋ฐ์ดํฐ์
๋ก๋
|
54 |
dataset = load_dataset("jihye-moon/LawQA-Ko")
|
|
|
59 |
data.add_faiss_index(column="question_embedding")
|
60 |
|
61 |
# LLaMA ๋ชจ๋ธ ์ค์
|
62 |
+
model_id = "google/gemma-2-2b-it"
|
63 |
+
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
|
|
|
|
|
|
64 |
model = AutoModelForCausalLM.from_pretrained(
|
65 |
model_id,
|
66 |
torch_dtype=torch.bfloat16,
|
67 |
device_map="auto",
|
|
|
|
|
68 |
)
|
69 |
|
70 |
SYS_PROMPT = """You are an assistant for answering legal questions.
|
71 |
You are given the extracted parts of legal documents and a question. Provide a conversational answer.
|
72 |
If you don't know the answer, just say "I do not know." Don't make up an answer.
|
73 |
+
you must answer korean.
|
74 |
+
You're a LAWEYE legal advisor bot. Your job is to provide korean legal assistance by asking questions to korean speaker, then offering advice or guidance based on the information and law provisions provided. Make sure you only respond with one question at a time.
|
75 |
+
...
|
76 |
+
"""
|
|
|
|
|
|
|
77 |
|
78 |
# ๋ฒ๋ฅ ์๋ด ๋ฐ์ดํฐ ๊ฒ์ ํจ์
|
79 |
def search_qa(query, k=3):
|
|
|
104 |
messages = [{"role": "system", "content": SYS_PROMPT}, {"role": "user", "content": formatted_prompt}]
|
105 |
|
106 |
# ๋ชจ๋ธ์๊ฒ ์์ฑ ์ง์
|
107 |
+
input_ids = tokenizer(messages, return_tensors="pt").to(model.device).input_ids
|
|
|
|
|
|
|
|
|
108 |
|
109 |
streamer = TextIteratorStreamer(
|
110 |
tokenizer, timeout=10.0, skip_prompt=True, skip_special_tokens=True
|
|
|
150 |
)
|
151 |
|
152 |
# Gradio ๋ฐ๋ชจ ์คํ
|
153 |
+
demo.launch(debug=True)
|