Update app.py
Browse files
app.py
CHANGED
@@ -22,7 +22,7 @@ index = None
|
|
22 |
data = None
|
23 |
|
24 |
# μλ² λ© λͺ¨λΈ Lazy Loading
|
25 |
-
@spaces.GPU
|
26 |
def load_embedding_model():
|
27 |
global ST
|
28 |
if ST is None:
|
@@ -30,7 +30,7 @@ def load_embedding_model():
|
|
30 |
return ST
|
31 |
|
32 |
# LLaMA λͺ¨λΈ λ° ν ν¬λμ΄μ Lazy Loading
|
33 |
-
@spaces.GPU
|
34 |
def load_model():
|
35 |
global model, tokenizer
|
36 |
if model is None or tokenizer is None:
|
@@ -45,7 +45,6 @@ def load_model():
|
|
45 |
return model, tokenizer
|
46 |
|
47 |
# PDFμμ ν
μ€νΈ μΆμΆ λ° μλ² λ© Lazy Loading
|
48 |
-
@spaces.GPU
|
49 |
def load_law_data():
|
50 |
global law_sentences, law_embeddings, index
|
51 |
if law_sentences is None or law_embeddings is None or index is None:
|
@@ -63,7 +62,6 @@ def load_law_data():
|
|
63 |
index.add(law_embeddings)
|
64 |
|
65 |
# Hugging Faceμμ λ²λ₯ μλ΄ λ°μ΄ν°μ
λ‘λ (Lazy Loading)
|
66 |
-
@spaces.GPU
|
67 |
def load_dataset_data():
|
68 |
global data
|
69 |
if data is None:
|
@@ -75,7 +73,6 @@ def load_dataset_data():
|
|
75 |
|
76 |
|
77 |
# λ²λ₯ λ¬Έμ κ²μ ν¨μ
|
78 |
-
@spaces.GPU
|
79 |
def search_law(query, k=5):
|
80 |
load_law_data() # PDF ν
μ€νΈμ μλ² λ© Lazy Loading
|
81 |
query_embedding = load_embedding_model().encode([query])
|
@@ -83,7 +80,6 @@ def search_law(query, k=5):
|
|
83 |
return [(law_sentences[i], D[0][idx]) for idx, i in enumerate(I[0])]
|
84 |
|
85 |
# λ²λ₯ μλ΄ λ°μ΄ν° κ²μ ν¨μ
|
86 |
-
@spaces.GPU
|
87 |
def search_qa(query, k=3):
|
88 |
dataset_data = load_dataset_data()
|
89 |
scores, retrieved_examples = dataset_data.get_nearest_examples(
|
@@ -92,7 +88,6 @@ def search_qa(query, k=3):
|
|
92 |
return [retrieved_examples["answer"][i] for i in range(k)]
|
93 |
|
94 |
# μ΅μ’
ν둬ννΈ μμ±
|
95 |
-
@spaces.GPU
|
96 |
def format_prompt(prompt, law_docs, qa_docs):
|
97 |
PROMPT = f"Question: {prompt}\n\nLegal Context:\n"
|
98 |
for doc in law_docs:
|
@@ -103,7 +98,7 @@ def format_prompt(prompt, law_docs, qa_docs):
|
|
103 |
return PROMPT
|
104 |
|
105 |
# μ±λ΄ μλ΅ ν¨μ
|
106 |
-
@spaces.GPU
|
107 |
def talk(prompt, history):
|
108 |
law_results = search_law(prompt, k=3)
|
109 |
qa_results = search_qa(prompt, k=3)
|
|
|
22 |
data = None
|
23 |
|
24 |
# μλ² λ© λͺ¨λΈ Lazy Loading
|
25 |
+
@spaces.GPU(duration=120)
|
26 |
def load_embedding_model():
|
27 |
global ST
|
28 |
if ST is None:
|
|
|
30 |
return ST
|
31 |
|
32 |
# LLaMA λͺ¨λΈ λ° ν ν¬λμ΄μ Lazy Loading
|
33 |
+
@spaces.GPU(duration=120)
|
34 |
def load_model():
|
35 |
global model, tokenizer
|
36 |
if model is None or tokenizer is None:
|
|
|
45 |
return model, tokenizer
|
46 |
|
47 |
# PDFμμ ν
μ€νΈ μΆμΆ λ° μλ² λ© Lazy Loading
|
|
|
48 |
def load_law_data():
|
49 |
global law_sentences, law_embeddings, index
|
50 |
if law_sentences is None or law_embeddings is None or index is None:
|
|
|
62 |
index.add(law_embeddings)
|
63 |
|
64 |
# Hugging Faceμμ λ²λ₯ μλ΄ λ°μ΄ν°μ
λ‘λ (Lazy Loading)
|
|
|
65 |
def load_dataset_data():
|
66 |
global data
|
67 |
if data is None:
|
|
|
73 |
|
74 |
|
75 |
# λ²λ₯ λ¬Έμ κ²μ ν¨μ
|
|
|
76 |
def search_law(query, k=5):
|
77 |
load_law_data() # PDF ν
μ€νΈμ μλ² λ© Lazy Loading
|
78 |
query_embedding = load_embedding_model().encode([query])
|
|
|
80 |
return [(law_sentences[i], D[0][idx]) for idx, i in enumerate(I[0])]
|
81 |
|
82 |
# λ²λ₯ μλ΄ λ°μ΄ν° κ²μ ν¨μ
|
|
|
83 |
def search_qa(query, k=3):
|
84 |
dataset_data = load_dataset_data()
|
85 |
scores, retrieved_examples = dataset_data.get_nearest_examples(
|
|
|
88 |
return [retrieved_examples["answer"][i] for i in range(k)]
|
89 |
|
90 |
# μ΅μ’
ν둬ννΈ μμ±
|
|
|
91 |
def format_prompt(prompt, law_docs, qa_docs):
|
92 |
PROMPT = f"Question: {prompt}\n\nLegal Context:\n"
|
93 |
for doc in law_docs:
|
|
|
98 |
return PROMPT
|
99 |
|
100 |
# μ±λ΄ μλ΅ ν¨μ
|
101 |
+
@spaces.GPU(duration=120)
|
102 |
def talk(prompt, history):
|
103 |
law_results = search_law(prompt, k=3)
|
104 |
qa_results = search_qa(prompt, k=3)
|