Spaces:
Running
on
Zero
Running
on
Zero
File size: 20,403 Bytes
9b2f298 ee0cb34 61279f3 ee0cb34 6ed5f50 225994f 6ed5f50 225994f 6ed5f50 225994f 6ed5f50 9b2f298 61279f3 9b2f298 61279f3 9b2f298 61279f3 9b2f298 61279f3 9b2f298 61279f3 9b2f298 61279f3 ee0cb34 61279f3 ee0cb34 61279f3 ee0cb34 61279f3 ee0cb34 61279f3 ee0cb34 61279f3 ee0cb34 61279f3 6ed5f50 61279f3 6ed5f50 61279f3 6ed5f50 97286e7 61279f3 9b2f298 225994f 6ed5f50 225994f 6ed5f50 225994f 6ed5f50 225994f 6ed5f50 ee0cb34 225994f 6ed5f50 225994f 6ed5f50 ee0cb34 225994f 6ed5f50 225994f 6ed5f50 ee0cb34 225994f 6ed5f50 225994f ee0cb34 225994f ee0cb34 6ed5f50 ee0cb34 225994f ee0cb34 225994f ee0cb34 225994f ee0cb34 225994f ee0cb34 6ed5f50 225994f ee0cb34 6ed5f50 ee0cb34 6ed5f50 ee0cb34 6ed5f50 225994f ee0cb34 225994f 6ed5f50 225994f 6ed5f50 225994f 6ed5f50 ee0cb34 225994f 97286e7 225994f 6ed5f50 225994f 61279f3 225994f 6ed5f50 225994f 6ed5f50 225994f ee0cb34 6ed5f50 225994f 6ed5f50 97286e7 ee0cb34 6ed5f50 97286e7 6ed5f50 225994f 97286e7 6ed5f50 ee0cb34 6ed5f50 ee0cb34 6ed5f50 ee0cb34 97286e7 99986b4 97286e7 ee0cb34 6ed5f50 97286e7 6ed5f50 ee0cb34 225994f ee0cb34 97286e7 ee0cb34 6ed5f50 ee0cb34 6ed5f50 ee0cb34 97286e7 ee0cb34 6ed5f50 ee0cb34 6ed5f50 61279f3 6ed5f50 ee0cb34 225994f 6ed5f50 97286e7 225994f 97286e7 61279f3 97286e7 61279f3 97286e7 6ed5f50 97286e7 9b2f298 61279f3 6ed5f50 9b2f298 6ed5f50 9b2f298 99986b4 97286e7 9b2f298 97286e7 9b2f298 6ed5f50 9b2f298 ee0cb34 9b2f298 97286e7 9b2f298 97286e7 6ed5f50 97286e7 6ed5f50 97286e7 ee0cb34 97286e7 ee0cb34 97286e7 ee0cb34 97286e7 ee0cb34 97286e7 ee0cb34 97286e7 ee0cb34 97286e7 ee0cb34 6ed5f50 97286e7 6ed5f50 9b2f298 6ed5f50 97286e7 9b2f298 97286e7 9b2f298 97286e7 9b2f298 7d0296f 6ed5f50 7d0296f 97286e7 6ed5f50 97286e7 6ed5f50 ee0cb34 97286e7 99986b4 97286e7 61279f3 97286e7 61279f3 6ed5f50 97286e7 99986b4 97286e7 61279f3 97286e7 61279f3 ee0cb34 6ed5f50 ee0cb34 97286e7 ee0cb34 97286e7 ee0cb34 97286e7 ee0cb34 97286e7 ee0cb34 7d0296f 97286e7 99986b4 97286e7 99986b4 7d0296f 97286e7 ee0cb34 97286e7 7d0296f 61279f3 97286e7 61279f3 6ed5f50 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 |
import gradio as gr
import os
from typing import List, Dict, Any, Optional, Tuple
import hashlib
from datetime import datetime
import numpy as np
# PDF ์ฒ๋ฆฌ ๋ผ์ด๋ธ๋ฌ๋ฆฌ
try:
import fitz # PyMuPDF
PDF_AVAILABLE = True
except ImportError:
PDF_AVAILABLE = False
print("โ ๏ธ PyMuPDF not installed. Install with: pip install pymupdf")
try:
from sentence_transformers import SentenceTransformer
ST_AVAILABLE = True
except ImportError:
ST_AVAILABLE = False
print("โ ๏ธ Sentence Transformers not installed. Install with: pip install sentence-transformers")
# Soft and bright custom CSS
custom_css = """
.gradio-container {
background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
min-height: 100vh;
font-family: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif;
}
.main-container {
background: rgba(255, 255, 255, 0.98);
border-radius: 16px;
padding: 24px;
box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -1px rgba(0, 0, 0, 0.06);
border: 1px solid rgba(0, 0, 0, 0.05);
margin: 12px;
}
/* Status messages styling */
.pdf-status {
padding: 12px 16px;
border-radius: 12px;
margin: 12px 0;
font-size: 0.95rem;
font-weight: 500;
}
.pdf-success {
background: linear-gradient(135deg, #d4edda 0%, #c3e6cb 100%);
border: 1px solid #b1dfbb;
color: #155724;
}
.pdf-error {
background: linear-gradient(135deg, #f8d7da 0%, #f5c6cb 100%);
border: 1px solid #f1aeb5;
color: #721c24;
}
.pdf-info {
background: linear-gradient(135deg, #d1ecf1 0%, #bee5eb 100%);
border: 1px solid #9ec5d8;
color: #0c5460;
}
.rag-context {
background: linear-gradient(135deg, #fef3c7 0%, #fde68a 100%);
border-left: 4px solid #f59e0b;
padding: 12px;
margin: 12px 0;
border-radius: 8px;
font-size: 0.9rem;
}
"""
class SimpleTextSplitter:
"""ํ
์คํธ ๋ถํ ๊ธฐ"""
def __init__(self, chunk_size=800, chunk_overlap=100):
self.chunk_size = chunk_size
self.chunk_overlap = chunk_overlap
def split_text(self, text: str) -> List[str]:
"""ํ
์คํธ๋ฅผ ์ฒญํฌ๋ก ๋ถํ """
chunks = []
sentences = text.split('. ')
current_chunk = ""
for sentence in sentences:
if len(current_chunk) + len(sentence) < self.chunk_size:
current_chunk += sentence + ". "
else:
if current_chunk:
chunks.append(current_chunk.strip())
current_chunk = sentence + ". "
if current_chunk:
chunks.append(current_chunk.strip())
return chunks
class PDFRAGSystem:
"""PDF ๊ธฐ๋ฐ RAG ์์คํ
"""
def __init__(self):
self.documents = {}
self.document_chunks = {}
self.embeddings_store = {}
self.text_splitter = SimpleTextSplitter(chunk_size=800, chunk_overlap=100)
# ์๋ฒ ๋ฉ ๋ชจ๋ธ ์ด๊ธฐํ
self.embedder = None
if ST_AVAILABLE:
try:
self.embedder = SentenceTransformer('sentence-transformers/all-MiniLM-L6-v2')
print("โ
์๋ฒ ๋ฉ ๋ชจ๋ธ ๋ก๋ ์ฑ๊ณต")
except Exception as e:
print(f"โ ๏ธ ์๋ฒ ๋ฉ ๋ชจ๋ธ ๋ก๋ ์คํจ: {e}")
def extract_text_from_pdf(self, pdf_path: str) -> Dict[str, Any]:
"""PDF์์ ํ
์คํธ ์ถ์ถ"""
if not PDF_AVAILABLE:
return {
"metadata": {
"title": "PDF Reader Not Available",
"file_name": os.path.basename(pdf_path),
"pages": 0
},
"full_text": "PDF ์ฒ๋ฆฌ๋ฅผ ์ํด 'pip install pymupdf'๋ฅผ ์คํํด์ฃผ์ธ์."
}
try:
doc = fitz.open(pdf_path)
text_content = []
metadata = {
"title": doc.metadata.get("title", os.path.basename(pdf_path)),
"pages": len(doc),
"file_name": os.path.basename(pdf_path)
}
for page_num, page in enumerate(doc):
text = page.get_text()
if text.strip():
text_content.append(text)
doc.close()
return {
"metadata": metadata,
"full_text": "\n\n".join(text_content)
}
except Exception as e:
raise Exception(f"PDF ์ฒ๋ฆฌ ์ค๋ฅ: {str(e)}")
def process_and_store_pdf(self, pdf_path: str, doc_id: str) -> Dict[str, Any]:
"""PDF ์ฒ๋ฆฌ ๋ฐ ์ ์ฅ"""
try:
# PDF ํ
์คํธ ์ถ์ถ
pdf_data = self.extract_text_from_pdf(pdf_path)
# ํ
์คํธ๋ฅผ ์ฒญํฌ๋ก ๋ถํ
chunks = self.text_splitter.split_text(pdf_data["full_text"])
# ์ฒญํฌ ์ ์ฅ
self.document_chunks[doc_id] = chunks
# ์๋ฒ ๋ฉ ์์ฑ
if self.embedder:
embeddings = self.embedder.encode(chunks)
self.embeddings_store[doc_id] = embeddings
# ๋ฌธ์ ์ ๋ณด ์ ์ฅ
self.documents[doc_id] = {
"metadata": pdf_data["metadata"],
"chunk_count": len(chunks),
"upload_time": datetime.now().isoformat()
}
return {
"success": True,
"doc_id": doc_id,
"chunks": len(chunks),
"pages": pdf_data["metadata"]["pages"],
"title": pdf_data["metadata"]["title"]
}
except Exception as e:
return {"success": False, "error": str(e)}
def search_relevant_chunks(self, query: str, doc_ids: List[str], top_k: int = 3) -> List[Dict]:
"""๊ด๋ จ ์ฒญํฌ ๊ฒ์"""
all_relevant_chunks = []
if self.embedder and self.embeddings_store:
# ์๋ฒ ๋ฉ ๊ธฐ๋ฐ ๊ฒ์
query_embedding = self.embedder.encode([query])[0]
for doc_id in doc_ids:
if doc_id in self.embeddings_store and doc_id in self.document_chunks:
doc_embeddings = self.embeddings_store[doc_id]
chunks = self.document_chunks[doc_id]
# ์ฝ์ฌ์ธ ์ ์ฌ๋ ๊ณ์ฐ
similarities = []
for emb in doc_embeddings:
sim = np.dot(query_embedding, emb) / (np.linalg.norm(query_embedding) * np.linalg.norm(emb))
similarities.append(sim)
# ์์ ์ฒญํฌ ์ ํ
top_indices = np.argsort(similarities)[-top_k:][::-1]
for idx in top_indices:
if similarities[idx] > 0.2:
all_relevant_chunks.append({
"content": chunks[idx],
"doc_name": self.documents[doc_id]["metadata"]["file_name"],
"similarity": similarities[idx]
})
else:
# ํค์๋ ๊ธฐ๋ฐ ๊ฒ์
query_keywords = set(query.lower().split())
for doc_id in doc_ids:
if doc_id in self.document_chunks:
chunks = self.document_chunks[doc_id]
for i, chunk in enumerate(chunks[:5]): # ์ฒ์ 5๊ฐ๋ง
chunk_lower = chunk.lower()
score = sum(1 for keyword in query_keywords if keyword in chunk_lower)
if score > 0:
all_relevant_chunks.append({
"content": chunk[:500],
"doc_name": self.documents[doc_id]["metadata"]["file_name"],
"similarity": score / len(query_keywords) if query_keywords else 0
})
# ์ ๋ ฌ ๋ฐ ๋ฐํ
all_relevant_chunks.sort(key=lambda x: x.get('similarity', 0), reverse=True)
return all_relevant_chunks[:top_k]
def create_rag_prompt(self, query: str, doc_ids: List[str], top_k: int = 3) -> str:
"""RAG ํ๋กฌํํธ ์์ฑ"""
relevant_chunks = self.search_relevant_chunks(query, doc_ids, top_k)
if not relevant_chunks:
return query
# ํ๋กฌํํธ ๊ตฌ์ฑ
prompt_parts = []
prompt_parts.append("๋ค์ ๋ฌธ์ ๋ด์ฉ์ ์ฐธ๊ณ ํ์ฌ ๋ต๋ณํด์ฃผ์ธ์:\n")
prompt_parts.append("=" * 40)
for i, chunk in enumerate(relevant_chunks, 1):
prompt_parts.append(f"\n[์ฐธ๊ณ {i} - {chunk['doc_name']}]")
content = chunk['content'][:300] if len(chunk['content']) > 300 else chunk['content']
prompt_parts.append(content)
prompt_parts.append("\n" + "=" * 40)
prompt_parts.append(f"\n์ง๋ฌธ: {query}")
return "\n".join(prompt_parts)
# RAG ์์คํ
์ธ์คํด์ค ์์ฑ
rag_system = PDFRAGSystem()
# State variable to track current model and RAG settings
current_model = gr.State("openai/gpt-oss-120b")
rag_enabled_state = gr.State(False)
selected_docs_state = gr.State([])
top_k_state = gr.State(3)
def upload_pdf(file):
"""PDF ํ์ผ ์
๋ก๋ ์ฒ๋ฆฌ"""
if file is None:
return (
gr.update(value="<div class='pdf-status pdf-info'>๐ ํ์ผ์ ์ ํํด์ฃผ์ธ์</div>"),
gr.update(choices=[]),
gr.update(value=False)
)
try:
# ํ์ผ ํด์๋ฅผ ID๋ก ์ฌ์ฉ
with open(file.name, 'rb') as f:
file_hash = hashlib.md5(f.read()).hexdigest()[:8]
doc_id = f"doc_{file_hash}"
# PDF ์ฒ๋ฆฌ ๋ฐ ์ ์ฅ
result = rag_system.process_and_store_pdf(file.name, doc_id)
if result["success"]:
status_html = f"""
<div class="pdf-status pdf-success">
โ
PDF ์
๋ก๋ ์๋ฃ!<br>
๐ {result['title']}<br>
๐ {result['pages']} ํ์ด์ง | ๐ {result['chunks']} ์ฒญํฌ
</div>
"""
# ๋ฌธ์ ๋ชฉ๋ก ์
๋ฐ์ดํธ
doc_choices = [f"{doc_id}: {rag_system.documents[doc_id]['metadata']['file_name']}"
for doc_id in rag_system.documents.keys()]
return (
status_html,
gr.update(choices=doc_choices, value=doc_choices),
gr.update(value=True)
)
else:
return (
f"<div class='pdf-status pdf-error'>โ ์ค๋ฅ: {result['error']}</div>",
gr.update(),
gr.update(value=False)
)
except Exception as e:
return (
f"<div class='pdf-status pdf-error'>โ ์ค๋ฅ: {str(e)}</div>",
gr.update(),
gr.update(value=False)
)
def clear_documents():
"""๋ฌธ์ ์ด๊ธฐํ"""
rag_system.documents = {}
rag_system.document_chunks = {}
rag_system.embeddings_store = {}
return (
gr.update(value="<div class='pdf-status pdf-info'>๐๏ธ ๋ชจ๋ ๋ฌธ์๊ฐ ์ญ์ ๋์์ต๋๋ค</div>"),
gr.update(choices=[], value=[]),
gr.update(value=False)
)
def switch_model(model_choice):
"""Function to switch between models"""
return gr.update(visible=False), gr.update(visible=True), model_choice
def create_rag_wrapper(original_fn, model_name):
"""์๋ณธ ๋ชจ๋ธ ํจ์๋ฅผ RAG๋ก ๊ฐ์ธ๋ ๋ํผ ์์ฑ"""
def wrapped_fn(message, history=None):
# RAG ์ค์ ๊ฐ์ ธ์ค๊ธฐ
if rag_enabled_state.value and selected_docs_state.value:
doc_ids = [doc.split(":")[0] for doc in selected_docs_state.value]
enhanced_message = rag_system.create_rag_prompt(message, doc_ids, top_k_state.value)
# RAG ์ ์ฉ ์๋ฆผ
print(f"๐ RAG ์ ์ฉ: {len(message)}์ โ {len(enhanced_message)}์")
# ์๋ณธ ๋ชจ๋ธ์ ๊ฐํ๋ ๋ฉ์์ง ์ ๋ฌ
if history is not None:
return original_fn(enhanced_message, history)
else:
return original_fn(enhanced_message)
else:
# RAG ๋ฏธ์ ์ฉ์ ์๋ณธ ๋ฉ์์ง ๊ทธ๋๋ก ์ ๋ฌ
if history is not None:
return original_fn(message, history)
else:
return original_fn(message)
return wrapped_fn
# Main interface with soft theme
with gr.Blocks(fill_height=True, theme=gr.themes.Soft(), css=custom_css) as demo:
with gr.Row():
# Sidebar
with gr.Column(scale=1):
with gr.Group(elem_classes="main-container"):
gr.Markdown("# ๐ Inference Provider + RAG")
gr.Markdown(
"OpenAI GPT-OSS models served by Cerebras API. "
"Upload PDF documents for context-aware responses."
)
# Model selection
model_dropdown = gr.Dropdown(
choices=["openai/gpt-oss-120b", "openai/gpt-oss-20b"],
value="openai/gpt-oss-120b",
label="๐ Select Model",
info="Choose between different model sizes"
)
# Login button
login_button = gr.LoginButton("Sign in with Hugging Face", size="lg")
# Reload button to apply model change
reload_btn = gr.Button("๐ Apply Model Change", variant="primary", size="lg")
# RAG Settings
with gr.Accordion("๐ PDF RAG Settings", open=True):
pdf_upload = gr.File(
label="Upload PDF",
file_types=[".pdf"],
type="filepath"
)
upload_status = gr.HTML(
value="<div class='pdf-status pdf-info'>๐ค Upload a PDF to enable document-based answers</div>"
)
document_list = gr.CheckboxGroup(
choices=[],
label="๐ Uploaded Documents",
info="Select documents to use as context"
)
clear_btn = gr.Button("๐๏ธ Clear All Documents", size="sm", variant="secondary")
enable_rag = gr.Checkbox(
label="โจ Enable RAG",
value=False,
info="Use documents for context-aware responses"
)
top_k_chunks = gr.Slider(
minimum=1,
maximum=5,
value=3,
step=1,
label="Context Chunks",
info="Number of document chunks to use"
)
# Additional options
with gr.Accordion("โ๏ธ Advanced Options", open=False):
gr.Markdown("*These options will be available after model implementation*")
temperature = gr.Slider(
minimum=0,
maximum=2,
value=0.7,
step=0.1,
label="Temperature"
)
max_tokens = gr.Slider(
minimum=1,
maximum=4096,
value=512,
step=1,
label="Max Tokens"
)
# Main chat area
with gr.Column(scale=3):
with gr.Group(elem_classes="main-container"):
gr.Markdown("## ๐ฌ Chat Interface")
# RAG status
rag_status = gr.HTML(
value="<div class='pdf-status pdf-info'>๐ RAG: <strong>Disabled</strong></div>"
)
# RAG context preview
context_preview = gr.HTML(value="", visible=False)
# Container for model interfaces
with gr.Column(visible=True) as model_120b_container:
gr.Markdown("### Model: openai/gpt-oss-120b")
# Load the original model and wrap it with RAG
original_interface_120b = gr.load(
"models/openai/gpt-oss-120b",
accept_token=login_button,
provider="fireworks-ai"
)
# Note: The loaded interface will have its own chat components
# We'll intercept the messages through our wrapper function
with gr.Column(visible=False) as model_20b_container:
gr.Markdown("### Model: openai/gpt-oss-20b")
# Load the original model
original_interface_20b = gr.load(
"models/openai/gpt-oss-20b",
accept_token=login_button,
provider="fireworks-ai"
)
# Event Handlers
# PDF upload
pdf_upload.upload(
fn=upload_pdf,
inputs=[pdf_upload],
outputs=[upload_status, document_list, enable_rag]
)
# Clear documents
clear_btn.click(
fn=clear_documents,
outputs=[upload_status, document_list, enable_rag]
)
# Update RAG state when settings change
def update_rag_state(enabled, docs, k):
rag_enabled_state.value = enabled
selected_docs_state.value = docs if docs else []
top_k_state.value = k
status = "โ
Enabled" if enabled and docs else "โญ Disabled"
status_html = f"<div class='pdf-status pdf-info'>๐ RAG: <strong>{status}</strong></div>"
# Show context preview if RAG is enabled
if enabled and docs:
preview = f"<div class='rag-context'>๐ Using {len(docs)} document(s) with {k} chunks per query</div>"
return gr.update(value=status_html), gr.update(value=preview, visible=True)
else:
return gr.update(value=status_html), gr.update(value="", visible=False)
# Connect RAG state updates
enable_rag.change(
fn=update_rag_state,
inputs=[enable_rag, document_list, top_k_chunks],
outputs=[rag_status, context_preview]
)
document_list.change(
fn=update_rag_state,
inputs=[enable_rag, document_list, top_k_chunks],
outputs=[rag_status, context_preview]
)
top_k_chunks.change(
fn=update_rag_state,
inputs=[enable_rag, document_list, top_k_chunks],
outputs=[rag_status, context_preview]
)
# Handle model switching
reload_btn.click(
fn=switch_model,
inputs=[model_dropdown],
outputs=[model_120b_container, model_20b_container, current_model]
).then(
fn=lambda: gr.Info("Model switched successfully!"),
inputs=[],
outputs=[]
)
# Update visibility based on dropdown selection
def update_visibility(model_choice):
if model_choice == "openai/gpt-oss-120b":
return gr.update(visible=True), gr.update(visible=False)
else:
return gr.update(visible=False), gr.update(visible=True)
model_dropdown.change(
fn=update_visibility,
inputs=[model_dropdown],
outputs=[model_120b_container, model_20b_container]
)
# Monkey-patch the loaded interfaces to add RAG support
# This is done after the interface is loaded
demo.load = lambda: print("๐ RAG System Ready!")
demo.launch() |