EnverLee commited on
Commit
96aaaae
·
verified ·
1 Parent(s): 2a16657

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -16
app.py CHANGED
@@ -7,12 +7,11 @@ from threading import Thread
7
  from sentence_transformers import SentenceTransformer
8
  import faiss
9
  import fitz # PyMuPDF
10
- import torchvision
11
 
12
  # 환경 변수에서 Hugging Face 토큰 가져오기
13
- token = os.environ.get("HF_TOKEN")
14
- if not token:
15
- raise ValueError("Hugging Face token is missing. Please set it in your environment variables.")
16
 
17
  # 임베딩 모델 로드
18
  ST = SentenceTransformer("mixedbread-ai/mxbai-embed-large-v1")
@@ -30,7 +29,7 @@ pdf_path = "laws.pdf" # 여기에 실제 PDF 경로를 입력하세요.
30
  law_text = extract_text_from_pdf(pdf_path)
31
 
32
  # 법률 문서 텍스트를 문장 단위로 나누고 임베딩
33
- law_sentences = law_text.split('\n')
34
  law_embeddings = ST.encode(law_sentences)
35
 
36
  # FAISS 인덱스 생성 및 임베딩 추가
@@ -46,7 +45,7 @@ data = data.map(lambda x: {"question_embedding": ST.encode(x["question"])}, batc
46
  data.add_faiss_index(column="question_embedding")
47
 
48
  # LLaMA 모델 설정
49
- model_id = "MLP-KTLim/llama-3-Korean-Bllossom-8B"
50
  bnb_config = BitsAndBytesConfig(
51
  load_in_4bit=True, bnb_4bit_use_double_quant=True, bnb_4bit_quant_type="nf4", bnb_4bit_compute_dtype=torch.bfloat16
52
  )
@@ -61,7 +60,8 @@ model = AutoModelForCausalLM.from_pretrained(
61
 
62
  SYS_PROMPT = """You are an assistant for answering legal questions.
63
  You are given the extracted parts of legal documents and a question. Provide a conversational answer.
64
- If you don't know the answer, just say "I do not know." Don't make up an answer."""
 
65
 
66
  # 법률 문서 검색 함수
67
  def search_law(query, k=5):
@@ -80,7 +80,7 @@ def search_qa(query, k=3):
80
  def format_prompt(prompt, law_docs, qa_docs):
81
  PROMPT = f"Question: {prompt}\n\nLegal Context:\n"
82
  for doc in law_docs:
83
- PROMPT += f"{doc[0]}\n"
84
  PROMPT += "\nLegal QA:\n"
85
  for doc in qa_docs:
86
  PROMPT += f"{doc}\n"
@@ -90,10 +90,11 @@ def format_prompt(prompt, law_docs, qa_docs):
90
  def talk(prompt, history):
91
  law_results = search_law(prompt, k=3)
92
  qa_results = search_qa(prompt, k=3)
93
-
94
  retrieved_law_docs = [result[0] for result in law_results]
95
  formatted_prompt = format_prompt(prompt, retrieved_law_docs, qa_results)
96
  formatted_prompt = formatted_prompt[:2000] # GPU 메모리 부족을 피하기 위해 프롬프트 제한
 
97
  messages = [{"role": "system", "content": SYS_PROMPT}, {"role": "user", "content": formatted_prompt}]
98
 
99
  # 모델에게 생성 지시
@@ -116,6 +117,7 @@ def talk(prompt, history):
116
  temperature=0.75,
117
  eos_token_id=tokenizer.eos_token_id,
118
  )
 
119
  t = Thread(target=model.generate, kwargs=generate_kwargs)
120
  t.start()
121
 
@@ -126,11 +128,8 @@ def talk(prompt, history):
126
 
127
  # Gradio 인터페이스 설정
128
  TITLE = "Legal RAG Chatbot"
129
-
130
- DESCRIPTION = """
131
- A chatbot that uses Retrieval-Augmented Generation (RAG) for legal consultation.
132
- This chatbot can search legal documents and previous legal QA pairs to provide answers.
133
- """
134
 
135
  demo = gr.ChatInterface(
136
  fn=talk,
@@ -149,5 +148,4 @@ demo = gr.ChatInterface(
149
  )
150
 
151
  # Gradio 데모 실행
152
- demo.launch(debug=True)
153
-
 
7
  from sentence_transformers import SentenceTransformer
8
  import faiss
9
  import fitz # PyMuPDF
10
+ from google.colab import userdata
11
 
12
  # 환경 변수에서 Hugging Face 토큰 가져오기
13
+ token = userdata.get("HF_TOKEN")
14
+
 
15
 
16
  # 임베딩 모델 로드
17
  ST = SentenceTransformer("mixedbread-ai/mxbai-embed-large-v1")
 
29
  law_text = extract_text_from_pdf(pdf_path)
30
 
31
  # 법률 문서 텍스트를 문장 단위로 나누고 임베딩
32
+ law_sentences = law_text.split('\n') # Adjust splitting based on your PDF structure
33
  law_embeddings = ST.encode(law_sentences)
34
 
35
  # FAISS 인덱스 생성 및 임베딩 추가
 
45
  data.add_faiss_index(column="question_embedding")
46
 
47
  # LLaMA 모델 설정
48
+ model_id = "meta-llama/Meta-Llama-3-8B-Instruct"
49
  bnb_config = BitsAndBytesConfig(
50
  load_in_4bit=True, bnb_4bit_use_double_quant=True, bnb_4bit_quant_type="nf4", bnb_4bit_compute_dtype=torch.bfloat16
51
  )
 
60
 
61
  SYS_PROMPT = """You are an assistant for answering legal questions.
62
  You are given the extracted parts of legal documents and a question. Provide a conversational answer.
63
+ If you don't know the answer, just say "I do not know." Don't make up an answer.
64
+ you must answer korean."""
65
 
66
  # 법률 문서 검색 함수
67
  def search_law(query, k=5):
 
80
  def format_prompt(prompt, law_docs, qa_docs):
81
  PROMPT = f"Question: {prompt}\n\nLegal Context:\n"
82
  for doc in law_docs:
83
+ PROMPT += f"{doc[0]}\n" # Assuming doc[0] contains the relevant text
84
  PROMPT += "\nLegal QA:\n"
85
  for doc in qa_docs:
86
  PROMPT += f"{doc}\n"
 
90
  def talk(prompt, history):
91
  law_results = search_law(prompt, k=3)
92
  qa_results = search_qa(prompt, k=3)
93
+
94
  retrieved_law_docs = [result[0] for result in law_results]
95
  formatted_prompt = format_prompt(prompt, retrieved_law_docs, qa_results)
96
  formatted_prompt = formatted_prompt[:2000] # GPU 메모리 부족을 피하기 위해 프롬프트 제한
97
+
98
  messages = [{"role": "system", "content": SYS_PROMPT}, {"role": "user", "content": formatted_prompt}]
99
 
100
  # 모델에게 생성 지시
 
117
  temperature=0.75,
118
  eos_token_id=tokenizer.eos_token_id,
119
  )
120
+
121
  t = Thread(target=model.generate, kwargs=generate_kwargs)
122
  t.start()
123
 
 
128
 
129
  # Gradio 인터페이스 설정
130
  TITLE = "Legal RAG Chatbot"
131
+ DESCRIPTION = """A chatbot that uses Retrieval-Augmented Generation (RAG) for legal consultation.
132
+ This chatbot can search legal documents and previous legal QA pairs to provide answers."""
 
 
 
133
 
134
  demo = gr.ChatInterface(
135
  fn=talk,
 
148
  )
149
 
150
  # Gradio 데모 실행
151
+ demo.launch(debug=True)