Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,14 +4,33 @@ from typing import List, Dict, Any, Optional
|
|
| 4 |
import hashlib
|
| 5 |
import json
|
| 6 |
from datetime import datetime
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
-
# PDF ์ฒ๋ฆฌ ๋ผ์ด๋ธ๋ฌ๋ฆฌ
|
| 9 |
-
import pymupdf # PyMuPDF
|
| 10 |
-
import chromadb
|
| 11 |
-
from chromadb.utils import embedding_functions
|
| 12 |
-
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
| 13 |
-
from sentence_transformers import SentenceTransformer
|
| 14 |
import numpy as np
|
|
|
|
| 15 |
|
| 16 |
# Custom CSS (๊ธฐ์กด CSS + ์ถ๊ฐ ์คํ์ผ)
|
| 17 |
custom_css = """
|
|
@@ -65,84 +84,96 @@ custom_css = """
|
|
| 65 |
border: 1px solid rgba(251, 191, 36, 0.5);
|
| 66 |
color: #f59e0b;
|
| 67 |
}
|
| 68 |
-
.document-card {
|
| 69 |
-
padding: 12px;
|
| 70 |
-
margin: 8px 0;
|
| 71 |
-
border-radius: 8px;
|
| 72 |
-
background: rgba(255, 255, 255, 0.1);
|
| 73 |
-
border: 1px solid rgba(255, 255, 255, 0.2);
|
| 74 |
-
cursor: pointer;
|
| 75 |
-
transition: all 0.3s ease;
|
| 76 |
-
}
|
| 77 |
-
.document-card:hover {
|
| 78 |
-
background: rgba(255, 255, 255, 0.2);
|
| 79 |
-
transform: translateX(5px);
|
| 80 |
-
}
|
| 81 |
"""
|
| 82 |
|
| 83 |
-
class
|
| 84 |
-
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
|
| 86 |
def __init__(self):
|
| 87 |
self.documents = {}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
self.embedder = None
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
)
|
| 96 |
-
self.initialize_vector_store()
|
| 97 |
-
|
| 98 |
-
def initialize_vector_store(self):
|
| 99 |
-
"""๋ฒกํฐ ์ ์ฅ์ ์ด๊ธฐํ"""
|
| 100 |
-
try:
|
| 101 |
-
# Sentence Transformer ๋ชจ๋ธ ๋ก๋
|
| 102 |
-
self.embedder = SentenceTransformer('sentence-transformers/all-MiniLM-L6-v2')
|
| 103 |
-
|
| 104 |
-
# ChromaDB ํด๋ผ์ด์ธํธ ์ด๊ธฐํ
|
| 105 |
-
self.chroma_client = chromadb.Client()
|
| 106 |
-
self.collection = self.chroma_client.create_collection(
|
| 107 |
-
name="pdf_documents",
|
| 108 |
-
metadata={"hnsw:space": "cosine"}
|
| 109 |
-
)
|
| 110 |
-
except Exception as e:
|
| 111 |
-
print(f"Vector store initialization error: {e}")
|
| 112 |
|
| 113 |
def extract_text_from_pdf(self, pdf_path: str) -> Dict[str, Any]:
|
| 114 |
"""PDF์์ ํ
์คํธ ์ถ์ถ"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 115 |
try:
|
| 116 |
-
doc =
|
| 117 |
text_content = []
|
| 118 |
metadata = {
|
| 119 |
"title": doc.metadata.get("title", "Untitled"),
|
| 120 |
"author": doc.metadata.get("author", "Unknown"),
|
| 121 |
"pages": len(doc),
|
| 122 |
-
"creation_date": doc.metadata.get("creationDate", ""),
|
| 123 |
"file_name": os.path.basename(pdf_path)
|
| 124 |
}
|
| 125 |
|
| 126 |
for page_num, page in enumerate(doc):
|
| 127 |
text = page.get_text()
|
| 128 |
if text.strip():
|
| 129 |
-
text_content.append(
|
| 130 |
-
"page": page_num + 1,
|
| 131 |
-
"content": text
|
| 132 |
-
})
|
| 133 |
|
| 134 |
doc.close()
|
| 135 |
|
| 136 |
return {
|
| 137 |
"metadata": metadata,
|
| 138 |
-
"
|
| 139 |
-
"full_text": "\n\n".join([p["content"] for p in text_content])
|
| 140 |
}
|
| 141 |
except Exception as e:
|
| 142 |
raise Exception(f"PDF ์ฒ๋ฆฌ ์ค๋ฅ: {str(e)}")
|
| 143 |
|
| 144 |
-
def
|
| 145 |
-
"""PDF ์ฒ๋ฆฌ ๋ฐ
|
| 146 |
try:
|
| 147 |
# PDF ํ
์คํธ ์ถ์ถ
|
| 148 |
pdf_data = self.extract_text_from_pdf(pdf_path)
|
|
@@ -150,33 +181,20 @@ class PDFRAGSystem:
|
|
| 150 |
# ํ
์คํธ๋ฅผ ์ฒญํฌ๋ก ๋ถํ
|
| 151 |
chunks = self.text_splitter.split_text(pdf_data["full_text"])
|
| 152 |
|
| 153 |
-
#
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
# ChromaDB์ ์ ์ฅ
|
| 157 |
-
ids = [f"{doc_id}_{i}" for i in range(len(chunks))]
|
| 158 |
-
metadatas = [
|
| 159 |
-
{
|
| 160 |
-
"doc_id": doc_id,
|
| 161 |
-
"chunk_index": i,
|
| 162 |
-
"source": pdf_data["metadata"]["file_name"],
|
| 163 |
-
"page_count": pdf_data["metadata"]["pages"]
|
| 164 |
-
}
|
| 165 |
-
for i in range(len(chunks))
|
| 166 |
-
]
|
| 167 |
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
embeddings=
|
| 171 |
-
|
| 172 |
-
metadatas=metadatas
|
| 173 |
-
)
|
| 174 |
|
| 175 |
# ๋ฌธ์ ์ ๋ณด ์ ์ฅ
|
| 176 |
self.documents[doc_id] = {
|
| 177 |
"metadata": pdf_data["metadata"],
|
| 178 |
"chunk_count": len(chunks),
|
| 179 |
-
"upload_time": datetime.now().isoformat()
|
|
|
|
| 180 |
}
|
| 181 |
|
| 182 |
return {
|
|
@@ -193,59 +211,92 @@ class PDFRAGSystem:
|
|
| 193 |
"error": str(e)
|
| 194 |
}
|
| 195 |
|
| 196 |
-
def search_relevant_chunks(self, query: str, top_k: int = 5) -> List[Dict]:
|
| 197 |
"""์ฟผ๋ฆฌ์ ๊ด๋ จ๋ ์ฒญํฌ ๊ฒ์"""
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
results = self.collection.query(
|
| 204 |
-
query_embeddings=query_embedding.tolist(),
|
| 205 |
-
n_results=top_k
|
| 206 |
-
)
|
| 207 |
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
chunks.
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 218 |
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 222 |
|
| 223 |
-
def
|
| 224 |
-
"""
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
]
|
|
|
|
|
|
|
|
|
|
|
|
|
| 229 |
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
|
| 233 |
|
| 234 |
-
|
| 235 |
{context}
|
| 236 |
|
| 237 |
-
|
|
|
|
| 238 |
|
| 239 |
-
|
|
|
|
| 240 |
|
| 241 |
-
return
|
| 242 |
|
| 243 |
# RAG ์์คํ
์ธ์คํด์ค ์์ฑ
|
| 244 |
-
rag_system =
|
| 245 |
|
| 246 |
# State variables
|
| 247 |
current_model = gr.State("openai/gpt-oss-120b")
|
| 248 |
-
uploaded_documents = gr.State({})
|
| 249 |
rag_enabled = gr.State(False)
|
| 250 |
|
| 251 |
def upload_pdf(file):
|
|
@@ -260,8 +311,8 @@ def upload_pdf(file):
|
|
| 260 |
|
| 261 |
doc_id = f"doc_{file_hash}"
|
| 262 |
|
| 263 |
-
# PDF ์ฒ๋ฆฌ ๋ฐ
|
| 264 |
-
result = rag_system.
|
| 265 |
|
| 266 |
if result["success"]:
|
| 267 |
status_html = f"""
|
|
@@ -300,49 +351,67 @@ def upload_pdf(file):
|
|
| 300 |
def clear_documents():
|
| 301 |
"""์
๋ก๋๋ ๋ฌธ์ ์ด๊ธฐํ"""
|
| 302 |
try:
|
| 303 |
-
# ChromaDB ์ปฌ๋ ์
์ฌ์์ฑ
|
| 304 |
-
rag_system.chroma_client.delete_collection("pdf_documents")
|
| 305 |
-
rag_system.collection = rag_system.chroma_client.create_collection(
|
| 306 |
-
name="pdf_documents",
|
| 307 |
-
metadata={"hnsw:space": "cosine"}
|
| 308 |
-
)
|
| 309 |
rag_system.documents = {}
|
|
|
|
|
|
|
| 310 |
|
| 311 |
return gr.update(value="<div class='pdf-status pdf-success'>โ
๋ชจ๋ ๋ฌธ์๊ฐ ์ญ์ ๋์์ต๋๋ค</div>"), gr.update(choices=[], value=[]), gr.update(value=False)
|
| 312 |
except Exception as e:
|
| 313 |
return gr.update(value=f"<div class='pdf-status pdf-error'>โ ์ญ์ ์คํจ: {str(e)}</div>"), gr.update(), gr.update()
|
| 314 |
|
| 315 |
-
def
|
| 316 |
-
"""
|
| 317 |
-
if
|
| 318 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 319 |
|
| 320 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 321 |
# ๊ด๋ จ ์ฒญํฌ ๊ฒ์
|
| 322 |
-
relevant_chunks = rag_system.search_relevant_chunks(message, top_k
|
| 323 |
|
| 324 |
if relevant_chunks:
|
| 325 |
-
#
|
| 326 |
-
|
| 327 |
-
filtered_chunks = [
|
| 328 |
-
chunk for chunk in relevant_chunks
|
| 329 |
-
if chunk['metadata']['doc_id'] in selected_doc_ids
|
| 330 |
-
]
|
| 331 |
|
| 332 |
-
|
| 333 |
-
|
| 334 |
-
rag_prompt = rag_system.generate_rag_prompt(message, filtered_chunks[:top_k])
|
| 335 |
-
return rag_prompt
|
| 336 |
-
|
| 337 |
-
return message
|
| 338 |
-
|
| 339 |
-
except Exception as e:
|
| 340 |
-
print(f"RAG processing error: {e}")
|
| 341 |
-
return message
|
| 342 |
|
| 343 |
-
|
| 344 |
-
|
| 345 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 346 |
|
| 347 |
# Gradio ์ธํฐํ์ด์ค
|
| 348 |
with gr.Blocks(fill_height=True, theme="Nymbo/Nymbo_Theme", css=custom_css) as demo:
|
|
@@ -403,14 +472,18 @@ with gr.Blocks(fill_height=True, theme="Nymbo/Nymbo_Theme", css=custom_css) as d
|
|
| 403 |
info="๋ต๋ณ ์์ฑ์ ์ฐธ๊ณ ํ ๋ฌธ์ ์ฒญํฌ์ ๊ฐ์"
|
| 404 |
)
|
| 405 |
|
| 406 |
-
|
| 407 |
-
|
| 408 |
-
|
| 409 |
-
|
| 410 |
-
|
| 411 |
-
|
| 412 |
-
|
| 413 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 414 |
|
| 415 |
# ๊ณ ๊ธ ์ต์
|
| 416 |
with gr.Accordion("โ๏ธ ๋ชจ๋ธ ์ค์ ", open=False):
|
|
@@ -443,7 +516,6 @@ with gr.Blocks(fill_height=True, theme="Nymbo/Nymbo_Theme", css=custom_css) as d
|
|
| 443 |
# ๋ชจ๋ธ ์ธํฐํ์ด์ค ์ปจํ
์ด๋
|
| 444 |
with gr.Column(visible=True) as model_120b_container:
|
| 445 |
gr.Markdown("### Model: openai/gpt-oss-120b")
|
| 446 |
-
# ์ค์ ๋ชจ๋ธ ๋ก๋๋ gr.load()๋ก ์ฒ๋ฆฌ
|
| 447 |
chatbot_120b = gr.Chatbot(height=400)
|
| 448 |
msg_box_120b = gr.Textbox(
|
| 449 |
label="๋ฉ์์ง ์
๋ ฅ",
|
|
@@ -501,31 +573,15 @@ with gr.Blocks(fill_height=True, theme="Nymbo/Nymbo_Theme", css=custom_css) as d
|
|
| 501 |
outputs=[]
|
| 502 |
)
|
| 503 |
|
| 504 |
-
# ์ฑํ
๊ธฐ๋ฅ (RAG ํตํฉ)
|
| 505 |
-
def chat_with_rag(message, history, enable_rag, selected_docs, top_k):
|
| 506 |
-
"""RAG๋ฅผ ํ์ฉํ ์ฑํ
"""
|
| 507 |
-
# RAG ์ฒ๋ฆฌ
|
| 508 |
-
processed_message = process_with_rag(message, enable_rag, selected_docs, top_k)
|
| 509 |
-
|
| 510 |
-
# ์ฌ๊ธฐ์ ์ค์ ๋ชจ๋ธ API ํธ์ถ ์ฝ๋๊ฐ ๋ค์ด๊ฐ์ผ ํจ
|
| 511 |
-
# ํ์ฌ๋ ์์ ์๋ต
|
| 512 |
-
if enable_rag and selected_docs:
|
| 513 |
-
response = f"[RAG ํ์ฑํ] ์ ํ๋ {len(selected_docs)}๊ฐ ๋ฌธ์๋ฅผ ์ฐธ๊ณ ํ์ฌ ๋ต๋ณํฉ๋๋ค:\n\n{processed_message[:200]}..."
|
| 514 |
-
else:
|
| 515 |
-
response = f"[์ผ๋ฐ ๋ชจ๋] {message}์ ๋ํ ๋ต๋ณ์
๋๋ค."
|
| 516 |
-
|
| 517 |
-
history.append((message, response))
|
| 518 |
-
return "", history
|
| 519 |
-
|
| 520 |
# 120b ๋ชจ๋ธ ์ฑํ
|
| 521 |
msg_box_120b.submit(
|
| 522 |
-
fn=
|
| 523 |
inputs=[msg_box_120b, chatbot_120b, enable_rag, document_list, top_k_chunks],
|
| 524 |
outputs=[msg_box_120b, chatbot_120b]
|
| 525 |
)
|
| 526 |
|
| 527 |
send_btn_120b.click(
|
| 528 |
-
fn=
|
| 529 |
inputs=[msg_box_120b, chatbot_120b, enable_rag, document_list, top_k_chunks],
|
| 530 |
outputs=[msg_box_120b, chatbot_120b]
|
| 531 |
)
|
|
@@ -537,13 +593,13 @@ with gr.Blocks(fill_height=True, theme="Nymbo/Nymbo_Theme", css=custom_css) as d
|
|
| 537 |
|
| 538 |
# 20b ๋ชจ๋ธ ์ฑํ
|
| 539 |
msg_box_20b.submit(
|
| 540 |
-
fn=
|
| 541 |
inputs=[msg_box_20b, chatbot_20b, enable_rag, document_list, top_k_chunks],
|
| 542 |
outputs=[msg_box_20b, chatbot_20b]
|
| 543 |
)
|
| 544 |
|
| 545 |
send_btn_20b.click(
|
| 546 |
-
fn=
|
| 547 |
inputs=[msg_box_20b, chatbot_20b, enable_rag, document_list, top_k_chunks],
|
| 548 |
outputs=[msg_box_20b, chatbot_20b]
|
| 549 |
)
|
|
|
|
| 4 |
import hashlib
|
| 5 |
import json
|
| 6 |
from datetime import datetime
|
| 7 |
+
import tempfile
|
| 8 |
+
|
| 9 |
+
# PDF ์ฒ๋ฆฌ ๋ผ์ด๋ธ๋ฌ๋ฆฌ (์ค์น ํ์ํ ๊ฒฝ์ฐ๋ฅผ ์ํ ๋์ฒด ๊ตฌํ ํฌํจ)
|
| 10 |
+
try:
|
| 11 |
+
import fitz # PyMuPDF
|
| 12 |
+
PDF_AVAILABLE = True
|
| 13 |
+
except ImportError:
|
| 14 |
+
PDF_AVAILABLE = False
|
| 15 |
+
print("PyMuPDF not installed. Install with: pip install pymupdf")
|
| 16 |
+
|
| 17 |
+
try:
|
| 18 |
+
import chromadb
|
| 19 |
+
from chromadb.utils import embedding_functions
|
| 20 |
+
CHROMA_AVAILABLE = True
|
| 21 |
+
except ImportError:
|
| 22 |
+
CHROMA_AVAILABLE = False
|
| 23 |
+
print("ChromaDB not installed. Install with: pip install chromadb")
|
| 24 |
+
|
| 25 |
+
try:
|
| 26 |
+
from sentence_transformers import SentenceTransformer
|
| 27 |
+
ST_AVAILABLE = True
|
| 28 |
+
except ImportError:
|
| 29 |
+
ST_AVAILABLE = False
|
| 30 |
+
print("Sentence Transformers not installed. Install with: pip install sentence-transformers")
|
| 31 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
import numpy as np
|
| 33 |
+
from typing import Tuple
|
| 34 |
|
| 35 |
# Custom CSS (๊ธฐ์กด CSS + ์ถ๊ฐ ์คํ์ผ)
|
| 36 |
custom_css = """
|
|
|
|
| 84 |
border: 1px solid rgba(251, 191, 36, 0.5);
|
| 85 |
color: #f59e0b;
|
| 86 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 87 |
"""
|
| 88 |
|
| 89 |
+
class SimpleTextSplitter:
|
| 90 |
+
"""๊ฐ๋จํ ํ
์คํธ ๋ถํ ๊ธฐ"""
|
| 91 |
+
def __init__(self, chunk_size=1000, chunk_overlap=200):
|
| 92 |
+
self.chunk_size = chunk_size
|
| 93 |
+
self.chunk_overlap = chunk_overlap
|
| 94 |
+
|
| 95 |
+
def split_text(self, text: str) -> List[str]:
|
| 96 |
+
"""ํ
์คํธ๋ฅผ ์ฒญํฌ๋ก ๋ถํ """
|
| 97 |
+
chunks = []
|
| 98 |
+
start = 0
|
| 99 |
+
text_len = len(text)
|
| 100 |
+
|
| 101 |
+
while start < text_len:
|
| 102 |
+
end = start + self.chunk_size
|
| 103 |
+
|
| 104 |
+
# ๋ฌธ์ฅ ๋์์ ์๋ฅด๊ธฐ ์ํด ๋ง์นจํ ์ฐพ๊ธฐ
|
| 105 |
+
if end < text_len:
|
| 106 |
+
last_period = text.rfind('.', start, end)
|
| 107 |
+
if last_period != -1 and last_period > start:
|
| 108 |
+
end = last_period + 1
|
| 109 |
+
|
| 110 |
+
chunk = text[start:end].strip()
|
| 111 |
+
if chunk:
|
| 112 |
+
chunks.append(chunk)
|
| 113 |
+
|
| 114 |
+
start = end - self.chunk_overlap
|
| 115 |
+
if start < 0:
|
| 116 |
+
start = 0
|
| 117 |
+
|
| 118 |
+
return chunks
|
| 119 |
+
|
| 120 |
+
class SimplePDFRAGSystem:
|
| 121 |
+
"""๊ฐ๋จํ PDF ๊ธฐ๋ฐ RAG ์์คํ
"""
|
| 122 |
|
| 123 |
def __init__(self):
|
| 124 |
self.documents = {}
|
| 125 |
+
self.document_chunks = {}
|
| 126 |
+
self.embeddings_store = {}
|
| 127 |
+
self.text_splitter = SimpleTextSplitter(chunk_size=1000, chunk_overlap=200)
|
| 128 |
+
|
| 129 |
+
# ์๋ฒ ๋ฉ ๋ชจ๋ธ ์ด๊ธฐํ (๊ฐ๋ฅํ ๊ฒฝ์ฐ)
|
| 130 |
self.embedder = None
|
| 131 |
+
if ST_AVAILABLE:
|
| 132 |
+
try:
|
| 133 |
+
self.embedder = SentenceTransformer('sentence-transformers/all-MiniLM-L6-v2')
|
| 134 |
+
print("Embedding model loaded successfully")
|
| 135 |
+
except Exception as e:
|
| 136 |
+
print(f"Failed to load embedding model: {e}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 137 |
|
| 138 |
def extract_text_from_pdf(self, pdf_path: str) -> Dict[str, Any]:
|
| 139 |
"""PDF์์ ํ
์คํธ ์ถ์ถ"""
|
| 140 |
+
if not PDF_AVAILABLE:
|
| 141 |
+
# PyMuPDF๊ฐ ์๋ ๊ฒฝ์ฐ ๋์ฒด ๋ฐฉ๋ฒ
|
| 142 |
+
return {
|
| 143 |
+
"metadata": {
|
| 144 |
+
"title": "PDF Reader Not Available",
|
| 145 |
+
"file_name": os.path.basename(pdf_path),
|
| 146 |
+
"pages": 0
|
| 147 |
+
},
|
| 148 |
+
"full_text": "PDF ์ฒ๋ฆฌ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๊ฐ ์ค์น๋์ง ์์์ต๋๋ค. 'pip install pymupdf'๋ฅผ ์คํํด์ฃผ์ธ์."
|
| 149 |
+
}
|
| 150 |
+
|
| 151 |
try:
|
| 152 |
+
doc = fitz.open(pdf_path)
|
| 153 |
text_content = []
|
| 154 |
metadata = {
|
| 155 |
"title": doc.metadata.get("title", "Untitled"),
|
| 156 |
"author": doc.metadata.get("author", "Unknown"),
|
| 157 |
"pages": len(doc),
|
|
|
|
| 158 |
"file_name": os.path.basename(pdf_path)
|
| 159 |
}
|
| 160 |
|
| 161 |
for page_num, page in enumerate(doc):
|
| 162 |
text = page.get_text()
|
| 163 |
if text.strip():
|
| 164 |
+
text_content.append(text)
|
|
|
|
|
|
|
|
|
|
| 165 |
|
| 166 |
doc.close()
|
| 167 |
|
| 168 |
return {
|
| 169 |
"metadata": metadata,
|
| 170 |
+
"full_text": "\n\n".join(text_content)
|
|
|
|
| 171 |
}
|
| 172 |
except Exception as e:
|
| 173 |
raise Exception(f"PDF ์ฒ๋ฆฌ ์ค๋ฅ: {str(e)}")
|
| 174 |
|
| 175 |
+
def process_and_store_pdf(self, pdf_path: str, doc_id: str) -> Dict[str, Any]:
|
| 176 |
+
"""PDF ์ฒ๋ฆฌ ๋ฐ ์ ์ฅ"""
|
| 177 |
try:
|
| 178 |
# PDF ํ
์คํธ ์ถ์ถ
|
| 179 |
pdf_data = self.extract_text_from_pdf(pdf_path)
|
|
|
|
| 181 |
# ํ
์คํธ๋ฅผ ์ฒญํฌ๋ก ๋ถํ
|
| 182 |
chunks = self.text_splitter.split_text(pdf_data["full_text"])
|
| 183 |
|
| 184 |
+
# ์ฒญํฌ ์ ์ฅ
|
| 185 |
+
self.document_chunks[doc_id] = chunks
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 186 |
|
| 187 |
+
# ์๋ฒ ๋ฉ ์์ฑ (๊ฐ๋ฅํ ๊ฒฝ์ฐ)
|
| 188 |
+
if self.embedder:
|
| 189 |
+
embeddings = self.embedder.encode(chunks)
|
| 190 |
+
self.embeddings_store[doc_id] = embeddings
|
|
|
|
|
|
|
| 191 |
|
| 192 |
# ๋ฌธ์ ์ ๋ณด ์ ์ฅ
|
| 193 |
self.documents[doc_id] = {
|
| 194 |
"metadata": pdf_data["metadata"],
|
| 195 |
"chunk_count": len(chunks),
|
| 196 |
+
"upload_time": datetime.now().isoformat(),
|
| 197 |
+
"full_text": pdf_data["full_text"][:500] # ์ฒ์ 500์ ์ ์ฅ
|
| 198 |
}
|
| 199 |
|
| 200 |
return {
|
|
|
|
| 211 |
"error": str(e)
|
| 212 |
}
|
| 213 |
|
| 214 |
+
def search_relevant_chunks(self, query: str, doc_ids: List[str], top_k: int = 5) -> List[Dict]:
|
| 215 |
"""์ฟผ๋ฆฌ์ ๊ด๋ จ๋ ์ฒญํฌ ๊ฒ์"""
|
| 216 |
+
all_relevant_chunks = []
|
| 217 |
+
|
| 218 |
+
if self.embedder and self.embeddings_store:
|
| 219 |
+
# ์๋ฒ ๋ฉ ๊ธฐ๋ฐ ๊ฒ์
|
| 220 |
+
query_embedding = self.embedder.encode([query])[0]
|
|
|
|
|
|
|
|
|
|
|
|
|
| 221 |
|
| 222 |
+
for doc_id in doc_ids:
|
| 223 |
+
if doc_id in self.embeddings_store and doc_id in self.document_chunks:
|
| 224 |
+
doc_embeddings = self.embeddings_store[doc_id]
|
| 225 |
+
chunks = self.document_chunks[doc_id]
|
| 226 |
+
|
| 227 |
+
# ์ฝ์ฌ์ธ ์ ์ฌ๋ ๊ณ์ฐ
|
| 228 |
+
similarities = []
|
| 229 |
+
for emb in doc_embeddings:
|
| 230 |
+
sim = np.dot(query_embedding, emb) / (np.linalg.norm(query_embedding) * np.linalg.norm(emb))
|
| 231 |
+
similarities.append(sim)
|
| 232 |
+
|
| 233 |
+
# ์์ k๊ฐ ์ ํ
|
| 234 |
+
top_indices = np.argsort(similarities)[-top_k:][::-1]
|
| 235 |
+
|
| 236 |
+
for idx in top_indices:
|
| 237 |
+
all_relevant_chunks.append({
|
| 238 |
+
"content": chunks[idx],
|
| 239 |
+
"doc_id": doc_id,
|
| 240 |
+
"doc_name": self.documents[doc_id]["metadata"]["file_name"],
|
| 241 |
+
"chunk_index": idx,
|
| 242 |
+
"similarity": similarities[idx]
|
| 243 |
+
})
|
| 244 |
+
else:
|
| 245 |
+
# ์๋ฒ ๋ฉ์ด ์๋ ๊ฒฝ์ฐ ํค์๋ ๊ธฐ๋ฐ ๊ฐ๋จํ ๊ฒ์
|
| 246 |
+
query_lower = query.lower()
|
| 247 |
+
query_words = set(query_lower.split())
|
| 248 |
|
| 249 |
+
for doc_id in doc_ids:
|
| 250 |
+
if doc_id in self.document_chunks:
|
| 251 |
+
chunks = self.document_chunks[doc_id]
|
| 252 |
+
for idx, chunk in enumerate(chunks):
|
| 253 |
+
chunk_lower = chunk.lower()
|
| 254 |
+
# ์ฟผ๋ฆฌ ๋จ์ด๊ฐ ์ฒญํฌ์ ํฌํจ๋์ด ์๋์ง ํ์ธ
|
| 255 |
+
matching_words = sum(1 for word in query_words if word in chunk_lower)
|
| 256 |
+
if matching_words > 0:
|
| 257 |
+
all_relevant_chunks.append({
|
| 258 |
+
"content": chunk,
|
| 259 |
+
"doc_id": doc_id,
|
| 260 |
+
"doc_name": self.documents[doc_id]["metadata"]["file_name"],
|
| 261 |
+
"chunk_index": idx,
|
| 262 |
+
"similarity": matching_words / len(query_words)
|
| 263 |
+
})
|
| 264 |
+
|
| 265 |
+
# ์ ์ฌ๋ ์์ผ๋ก ์ ๋ ฌํ๊ณ ์์ k๊ฐ ๋ฐํ
|
| 266 |
+
all_relevant_chunks.sort(key=lambda x: x.get('similarity', 0), reverse=True)
|
| 267 |
+
return all_relevant_chunks[:top_k]
|
| 268 |
|
| 269 |
+
def generate_context_prompt(self, query: str, chunks: List[Dict]) -> str:
|
| 270 |
+
"""์ปจํ
์คํธ๋ฅผ ํฌํจํ ํ๋กฌํํธ ์์ฑ"""
|
| 271 |
+
if not chunks:
|
| 272 |
+
return query
|
| 273 |
+
|
| 274 |
+
context_parts = []
|
| 275 |
+
for i, chunk in enumerate(chunks, 1):
|
| 276 |
+
context_parts.append(
|
| 277 |
+
f"[๋ฌธ์: {chunk['doc_name']}, ์น์
{chunk['chunk_index']+1}]\n{chunk['content']}\n"
|
| 278 |
+
)
|
| 279 |
|
| 280 |
+
context = "\n---\n".join(context_parts)
|
| 281 |
+
|
| 282 |
+
enhanced_prompt = f"""๋ค์ ๋ฌธ์ ๋ด์ฉ์ ์ฐธ๊ณ ํ์ฌ ์ง๋ฌธ์ ๋ต๋ณํด์ฃผ์ธ์.
|
| 283 |
|
| 284 |
+
## ์ฐธ๊ณ ๋ฌธ์:
|
| 285 |
{context}
|
| 286 |
|
| 287 |
+
## ์ง๋ฌธ:
|
| 288 |
+
{query}
|
| 289 |
|
| 290 |
+
## ๋ต๋ณ:
|
| 291 |
+
์ ๋ฌธ์ ๋ด์ฉ์ ๋ฐํ์ผ๋ก ์ง๋ฌธ์ ๋ํด ์์ธํ๊ณ ์ ํํ๊ฒ ๋ต๋ณํ๊ฒ ์ต๋๋ค."""
|
| 292 |
|
| 293 |
+
return enhanced_prompt
|
| 294 |
|
| 295 |
# RAG ์์คํ
์ธ์คํด์ค ์์ฑ
|
| 296 |
+
rag_system = SimplePDFRAGSystem()
|
| 297 |
|
| 298 |
# State variables
|
| 299 |
current_model = gr.State("openai/gpt-oss-120b")
|
|
|
|
| 300 |
rag_enabled = gr.State(False)
|
| 301 |
|
| 302 |
def upload_pdf(file):
|
|
|
|
| 311 |
|
| 312 |
doc_id = f"doc_{file_hash}"
|
| 313 |
|
| 314 |
+
# PDF ์ฒ๋ฆฌ ๋ฐ ์ ์ฅ
|
| 315 |
+
result = rag_system.process_and_store_pdf(file.name, doc_id)
|
| 316 |
|
| 317 |
if result["success"]:
|
| 318 |
status_html = f"""
|
|
|
|
| 351 |
def clear_documents():
|
| 352 |
"""์
๋ก๋๋ ๋ฌธ์ ์ด๊ธฐํ"""
|
| 353 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 354 |
rag_system.documents = {}
|
| 355 |
+
rag_system.document_chunks = {}
|
| 356 |
+
rag_system.embeddings_store = {}
|
| 357 |
|
| 358 |
return gr.update(value="<div class='pdf-status pdf-success'>โ
๋ชจ๋ ๋ฌธ์๊ฐ ์ญ์ ๋์์ต๋๋ค</div>"), gr.update(choices=[], value=[]), gr.update(value=False)
|
| 359 |
except Exception as e:
|
| 360 |
return gr.update(value=f"<div class='pdf-status pdf-error'>โ ์ญ์ ์คํจ: {str(e)}</div>"), gr.update(), gr.update()
|
| 361 |
|
| 362 |
+
def switch_model(model_choice):
|
| 363 |
+
"""๋ชจ๋ธ ์ ํ ํจ์"""
|
| 364 |
+
if model_choice == "openai/gpt-oss-120b":
|
| 365 |
+
return gr.update(visible=True), gr.update(visible=False), model_choice
|
| 366 |
+
else:
|
| 367 |
+
return gr.update(visible=False), gr.update(visible=True), model_choice
|
| 368 |
+
|
| 369 |
+
def chat_with_model(message: str, history: List[Tuple[str, str]], enable_rag: bool, selected_docs: List[str], top_k: int, model: str):
|
| 370 |
+
"""๋ชจ๋ธ๊ณผ ๋ํ (RAG ํฌํจ)"""
|
| 371 |
|
| 372 |
+
# RAG๊ฐ ํ์ฑํ๋๊ณ ๋ฌธ์๊ฐ ์ ํ๋ ๊ฒฝ์ฐ
|
| 373 |
+
if enable_rag and selected_docs:
|
| 374 |
+
# ์ ํ๋ ๋ฌธ์ ID ์ถ์ถ
|
| 375 |
+
doc_ids = [doc.split(":")[0] for doc in selected_docs]
|
| 376 |
+
|
| 377 |
# ๊ด๋ จ ์ฒญํฌ ๊ฒ์
|
| 378 |
+
relevant_chunks = rag_system.search_relevant_chunks(message, doc_ids, top_k)
|
| 379 |
|
| 380 |
if relevant_chunks:
|
| 381 |
+
# ์ปจํ
์คํธ๋ฅผ ํฌํจํ ํ๋กฌํํธ ์์ฑ
|
| 382 |
+
enhanced_message = rag_system.generate_context_prompt(message, relevant_chunks)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 383 |
|
| 384 |
+
# ๋๋ฒ๊ทธ ์ ๋ณด ํฌํจ ์๋ต (์ค์ ๊ตฌํ์ ๋ชจ๋ธ API ํธ์ถ๋ก ๋์ฒด)
|
| 385 |
+
response = f"""๐ RAG ๊ธฐ๋ฐ ๋ต๋ณ (๋ชจ๋ธ: {model})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 386 |
|
| 387 |
+
์ฐพ์ ๊ด๋ จ ๋ฌธ์ ์น์
: {len(relevant_chunks)}๊ฐ
|
| 388 |
+
|
| 389 |
+
์ง๋ฌธ: {message}
|
| 390 |
+
|
| 391 |
+
๋ต๋ณ:
|
| 392 |
+
{enhanced_message[:2000]}...
|
| 393 |
+
|
| 394 |
+
[์ฐธ๊ณ : ์ค์ ๊ตฌํ์ ์ฌ๊ธฐ์ ๋ชจ๋ธ API๋ฅผ ํธ์ถํ์ฌ enhanced_message๋ฅผ ์ ์กํ๊ณ ์๋ต์ ๋ฐ์์ผ ํฉ๋๋ค]
|
| 395 |
+
|
| 396 |
+
๊ด๋ จ ๋ฌธ์ ์น์
์์ฝ:
|
| 397 |
+
"""
|
| 398 |
+
for i, chunk in enumerate(relevant_chunks[:3], 1):
|
| 399 |
+
response += f"\n{i}. {chunk['doc_name']} - ์น์
{chunk['chunk_index']+1} (์ ์ฌ๋: {chunk['similarity']:.2f})"
|
| 400 |
+
response += f"\n ๋ด์ฉ: {chunk['content'][:200]}...\n"
|
| 401 |
+
else:
|
| 402 |
+
response = f"โ ๏ธ ์ ํ๋ ๋ฌธ์์์ '{message}'์ ๊ด๋ จ๋ ๋ด์ฉ์ ์ฐพ์ ์ ์์ต๋๋ค. ๋ค๋ฅธ ์ง๋ฌธ์ ์๋ํด๋ณด์ธ์."
|
| 403 |
+
else:
|
| 404 |
+
# RAG ๋นํ์ฑํ ์ํ
|
| 405 |
+
response = f"""์ผ๋ฐ ๋ต๋ณ ๋ชจ๋ (๋ชจ๋ธ: {model})
|
| 406 |
+
|
| 407 |
+
์ง๋ฌธ: {message}
|
| 408 |
+
|
| 409 |
+
[์ฐธ๊ณ : ์ค์ ๊ตฌํ์ ์ฌ๊ธฐ์ ๋ชจ๋ธ API๋ฅผ ํธ์ถํ์ฌ message๋ฅผ ์ ์กํ๊ณ ์๋ต์ ๋ฐ์์ผ ํฉ๋๋ค]
|
| 410 |
+
|
| 411 |
+
PDF ๋ฌธ์๋ฅผ ์
๋ก๋ํ๊ณ RAG๋ฅผ ํ์ฑํํ๋ฉด ๋ฌธ์ ๊ธฐ๋ฐ ๋ต๋ณ์ ๋ฐ์ ์ ์์ต๋๋ค."""
|
| 412 |
+
|
| 413 |
+
history.append((message, response))
|
| 414 |
+
return "", history
|
| 415 |
|
| 416 |
# Gradio ์ธํฐํ์ด์ค
|
| 417 |
with gr.Blocks(fill_height=True, theme="Nymbo/Nymbo_Theme", css=custom_css) as demo:
|
|
|
|
| 472 |
info="๋ต๋ณ ์์ฑ์ ์ฐธ๊ณ ํ ๋ฌธ์ ์ฒญํฌ์ ๊ฐ์"
|
| 473 |
)
|
| 474 |
|
| 475 |
+
gr.Markdown("""
|
| 476 |
+
### ๐ RAG ์ฌ์ฉ ํ:
|
| 477 |
+
1. PDF ํ์ผ์ ์
๋ก๋ํ์ธ์
|
| 478 |
+
2. ์
๋ก๋๋ ๋ฌธ์๋ฅผ ์ ํํ์ธ์
|
| 479 |
+
3. RAG๋ฅผ ํ์ฑํํ์ธ์
|
| 480 |
+
4. ๋ฌธ์ ๋ด์ฉ์ ๋ํด ์ง๋ฌธํ์ธ์
|
| 481 |
+
|
| 482 |
+
์์ ์ง๋ฌธ:
|
| 483 |
+
- "๋ฌธ์์ ์ฃผ์ ๋ด์ฉ์ ์์ฝํด์ฃผ์ธ์"
|
| 484 |
+
- "์ด ๋ฌธ์์์ ์ธ๊ธ๋ ๋ ์ง๋ ์ธ์ ์ธ๊ฐ์?"
|
| 485 |
+
- "์ฐธ๊ฐ ์๊ฒฉ ์กฐ๊ฑด์ ๋ฌด์์ธ๊ฐ์?"
|
| 486 |
+
""")
|
| 487 |
|
| 488 |
# ๊ณ ๊ธ ์ต์
|
| 489 |
with gr.Accordion("โ๏ธ ๋ชจ๋ธ ์ค์ ", open=False):
|
|
|
|
| 516 |
# ๋ชจ๋ธ ์ธํฐํ์ด์ค ์ปจํ
์ด๋
|
| 517 |
with gr.Column(visible=True) as model_120b_container:
|
| 518 |
gr.Markdown("### Model: openai/gpt-oss-120b")
|
|
|
|
| 519 |
chatbot_120b = gr.Chatbot(height=400)
|
| 520 |
msg_box_120b = gr.Textbox(
|
| 521 |
label="๋ฉ์์ง ์
๋ ฅ",
|
|
|
|
| 573 |
outputs=[]
|
| 574 |
)
|
| 575 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 576 |
# 120b ๋ชจ๋ธ ์ฑํ
|
| 577 |
msg_box_120b.submit(
|
| 578 |
+
fn=lambda msg, hist, rag, docs, k: chat_with_model(msg, hist, rag, docs, k, "openai/gpt-oss-120b"),
|
| 579 |
inputs=[msg_box_120b, chatbot_120b, enable_rag, document_list, top_k_chunks],
|
| 580 |
outputs=[msg_box_120b, chatbot_120b]
|
| 581 |
)
|
| 582 |
|
| 583 |
send_btn_120b.click(
|
| 584 |
+
fn=lambda msg, hist, rag, docs, k: chat_with_model(msg, hist, rag, docs, k, "openai/gpt-oss-120b"),
|
| 585 |
inputs=[msg_box_120b, chatbot_120b, enable_rag, document_list, top_k_chunks],
|
| 586 |
outputs=[msg_box_120b, chatbot_120b]
|
| 587 |
)
|
|
|
|
| 593 |
|
| 594 |
# 20b ๋ชจ๋ธ ์ฑํ
|
| 595 |
msg_box_20b.submit(
|
| 596 |
+
fn=lambda msg, hist, rag, docs, k: chat_with_model(msg, hist, rag, docs, k, "openai/gpt-oss-20b"),
|
| 597 |
inputs=[msg_box_20b, chatbot_20b, enable_rag, document_list, top_k_chunks],
|
| 598 |
outputs=[msg_box_20b, chatbot_20b]
|
| 599 |
)
|
| 600 |
|
| 601 |
send_btn_20b.click(
|
| 602 |
+
fn=lambda msg, hist, rag, docs, k: chat_with_model(msg, hist, rag, docs, k, "openai/gpt-oss-20b"),
|
| 603 |
inputs=[msg_box_20b, chatbot_20b, enable_rag, document_list, top_k_chunks],
|
| 604 |
outputs=[msg_box_20b, chatbot_20b]
|
| 605 |
)
|