EnverLee commited on
Commit
ac5d990
Β·
verified Β·
1 Parent(s): 6af4f49

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +3 -8
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)