Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -1,9 +1,10 @@
|
|
1 |
import gradio as gr
|
2 |
import os
|
3 |
-
from typing import List, Dict, Any, Optional
|
4 |
import hashlib
|
5 |
from datetime import datetime
|
6 |
import numpy as np
|
|
|
7 |
|
8 |
# PDF ์ฒ๋ฆฌ ๋ผ์ด๋ธ๋ฌ๋ฆฌ
|
9 |
try:
|
@@ -20,64 +21,89 @@ except ImportError:
|
|
20 |
ST_AVAILABLE = False
|
21 |
print("โ ๏ธ Sentence Transformers not installed. Install with: pip install sentence-transformers")
|
22 |
|
23 |
-
#
|
24 |
custom_css = """
|
25 |
.gradio-container {
|
26 |
-
background: linear-gradient(135deg, #
|
27 |
-
background-size: 400% 400%;
|
28 |
-
animation: gradient-animation 15s ease infinite;
|
29 |
min-height: 100vh;
|
|
|
30 |
}
|
31 |
-
|
32 |
-
0% { background-position: 0% 50%; }
|
33 |
-
50% { background-position: 100% 50%; }
|
34 |
-
100% { background-position: 0% 50%; }
|
35 |
-
}
|
36 |
-
.dark .gradio-container {
|
37 |
-
background: linear-gradient(135deg, #1a1a2e 0%, #16213e 25%, #0f3460 50%, #533483 75%, #e94560 100%);
|
38 |
-
background-size: 400% 400%;
|
39 |
-
animation: gradient-animation 15s ease infinite;
|
40 |
-
}
|
41 |
.main-container {
|
42 |
-
background
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
margin: 10px;
|
49 |
}
|
50 |
-
|
51 |
-
|
52 |
-
|
|
|
53 |
}
|
|
|
|
|
54 |
.pdf-status {
|
55 |
-
padding:
|
56 |
-
border-radius:
|
57 |
-
margin:
|
58 |
-
font-size: 0.
|
|
|
|
|
59 |
}
|
|
|
60 |
.pdf-success {
|
61 |
-
background
|
62 |
-
border: 1px solid
|
63 |
-
color: #
|
64 |
}
|
|
|
65 |
.pdf-error {
|
66 |
-
background
|
67 |
-
border: 1px solid
|
68 |
-
color: #
|
69 |
}
|
|
|
70 |
.pdf-info {
|
71 |
-
background
|
72 |
-
border: 1px solid
|
73 |
-
color: #
|
|
|
|
|
|
|
|
|
|
|
|
|
74 |
}
|
|
|
|
|
75 |
.rag-context {
|
76 |
-
background
|
77 |
border-left: 4px solid #f59e0b;
|
78 |
-
padding:
|
79 |
-
margin:
|
80 |
-
border-radius:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
81 |
}
|
82 |
"""
|
83 |
|
@@ -230,12 +256,12 @@ class PDFRAGSystem:
|
|
230 |
for doc_id in doc_ids:
|
231 |
if doc_id in self.document_chunks:
|
232 |
chunks = self.document_chunks[doc_id]
|
233 |
-
for chunk in chunks[:top_k]:
|
234 |
chunk_lower = chunk.lower()
|
235 |
score = sum(1 for keyword in query_keywords if keyword in chunk_lower)
|
236 |
if score > 0:
|
237 |
all_relevant_chunks.append({
|
238 |
-
"content": chunk[:500],
|
239 |
"doc_name": self.documents[doc_id]["metadata"]["file_name"],
|
240 |
"similarity": score / len(query_keywords) if query_keywords else 0
|
241 |
})
|
@@ -253,7 +279,7 @@ class PDFRAGSystem:
|
|
253 |
|
254 |
# ํ๋กฌํํธ ๊ตฌ์ฑ
|
255 |
prompt_parts = []
|
256 |
-
prompt_parts.append("
|
257 |
prompt_parts.append("=" * 50)
|
258 |
|
259 |
for i, chunk in enumerate(relevant_chunks, 1):
|
@@ -264,21 +290,22 @@ class PDFRAGSystem:
|
|
264 |
|
265 |
prompt_parts.append("=" * 50)
|
266 |
prompt_parts.append(f"\n์ง๋ฌธ: {query}")
|
267 |
-
prompt_parts.append("\n์
|
268 |
|
269 |
return "\n".join(prompt_parts)
|
270 |
|
271 |
# RAG ์์คํ
์ธ์คํด์ค ์์ฑ
|
272 |
rag_system = PDFRAGSystem()
|
273 |
|
274 |
-
# State
|
275 |
current_model = gr.State("openai/gpt-oss-120b")
|
|
|
276 |
|
277 |
def upload_pdf(file):
|
278 |
"""PDF ํ์ผ ์
๋ก๋ ์ฒ๋ฆฌ"""
|
279 |
if file is None:
|
280 |
return (
|
281 |
-
gr.update(value="<div class='pdf-status pdf-
|
282 |
gr.update(choices=[]),
|
283 |
gr.update(value=False)
|
284 |
)
|
@@ -296,7 +323,7 @@ def upload_pdf(file):
|
|
296 |
if result["success"]:
|
297 |
status_html = f"""
|
298 |
<div class="pdf-status pdf-success">
|
299 |
-
โ
PDF ์
๋ก๋
|
300 |
๐ ํ์ผ: {result['title']}<br>
|
301 |
๐ ํ์ด์ง: {result['pages']}ํ์ด์ง<br>
|
302 |
๐ ์ฒญํฌ: {result['chunks']}๊ฐ ์์ฑ
|
@@ -334,80 +361,99 @@ def clear_documents():
|
|
334 |
rag_system.embeddings_store = {}
|
335 |
|
336 |
return (
|
337 |
-
gr.update(value="<div class='pdf-status pdf-
|
338 |
gr.update(choices=[], value=[]),
|
339 |
gr.update(value=False)
|
340 |
)
|
341 |
|
342 |
def switch_model(model_choice):
|
343 |
"""Function to switch between models"""
|
344 |
-
|
|
|
|
|
|
|
345 |
|
346 |
-
def
|
347 |
-
"""RAG
|
348 |
-
if not selected_docs:
|
349 |
-
return ""
|
350 |
|
351 |
-
|
352 |
-
|
353 |
|
354 |
-
|
355 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
356 |
|
357 |
-
|
358 |
-
|
359 |
-
|
360 |
-
|
361 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
362 |
|
363 |
-
return
|
364 |
|
365 |
-
# Main interface
|
366 |
-
with gr.Blocks(fill_height=True, theme=
|
367 |
-
# JavaScript to handle message passing
|
368 |
-
gr.HTML("""
|
369 |
-
<script>
|
370 |
-
function sendToModel(processedMsg) {
|
371 |
-
// This function would send the processed message to the model
|
372 |
-
console.log("Sending to model:", processedMsg);
|
373 |
-
}
|
374 |
-
</script>
|
375 |
-
""")
|
376 |
|
377 |
with gr.Row():
|
378 |
# Sidebar
|
379 |
with gr.Column(scale=1):
|
380 |
with gr.Group(elem_classes="main-container"):
|
381 |
-
gr.Markdown("#
|
382 |
gr.Markdown(
|
383 |
-
"OpenAI GPT-OSS
|
384 |
-
"Sign in with your Hugging Face account to use this API."
|
385 |
)
|
386 |
|
|
|
|
|
|
|
387 |
# Model selection
|
388 |
model_dropdown = gr.Dropdown(
|
389 |
choices=["openai/gpt-oss-120b", "openai/gpt-oss-20b"],
|
390 |
value="openai/gpt-oss-120b",
|
391 |
-
label="๐
|
392 |
-
info="
|
393 |
)
|
394 |
|
395 |
-
# Login button
|
396 |
-
login_button = gr.LoginButton("Sign in with Hugging Face", size="lg")
|
397 |
-
|
398 |
# Reload button to apply model change
|
399 |
-
reload_btn = gr.Button("๐
|
400 |
|
401 |
# RAG Settings
|
402 |
-
with gr.Accordion("๐ PDF RAG
|
403 |
pdf_upload = gr.File(
|
404 |
-
label="
|
405 |
file_types=[".pdf"],
|
406 |
type="filepath"
|
407 |
)
|
408 |
|
409 |
upload_status = gr.HTML(
|
410 |
-
value="<div class='pdf-status pdf-info'
|
411 |
)
|
412 |
|
413 |
document_list = gr.CheckboxGroup(
|
@@ -416,10 +462,11 @@ with gr.Blocks(fill_height=True, theme="Nymbo/Nymbo_Theme", css=custom_css) as d
|
|
416 |
info="์ฐธ๊ณ ํ ๋ฌธ์๋ฅผ ์ ํํ์ธ์"
|
417 |
)
|
418 |
|
419 |
-
|
|
|
420 |
|
421 |
enable_rag = gr.Checkbox(
|
422 |
-
label="RAG ํ์ฑํ",
|
423 |
value=False,
|
424 |
info="์ ํํ ๋ฌธ์๋ฅผ ์ฐธ๊ณ ํ์ฌ ๋ต๋ณ ์์ฑ"
|
425 |
)
|
@@ -430,25 +477,26 @@ with gr.Blocks(fill_height=True, theme="Nymbo/Nymbo_Theme", css=custom_css) as d
|
|
430 |
value=3,
|
431 |
step=1,
|
432 |
label="์ฐธ์กฐ ์ฒญํฌ ์",
|
433 |
-
info="
|
434 |
)
|
435 |
|
436 |
# Additional options
|
437 |
-
with gr.Accordion("
|
438 |
-
gr.Markdown("*These options will be available after model implementation*")
|
439 |
temperature = gr.Slider(
|
440 |
minimum=0,
|
441 |
maximum=2,
|
442 |
value=0.7,
|
443 |
step=0.1,
|
444 |
-
label="Temperature"
|
|
|
445 |
)
|
446 |
max_tokens = gr.Slider(
|
447 |
minimum=1,
|
448 |
maximum=4096,
|
449 |
value=512,
|
450 |
step=1,
|
451 |
-
label="Max Tokens"
|
|
|
452 |
)
|
453 |
|
454 |
# Main chat area
|
@@ -461,53 +509,67 @@ with gr.Blocks(fill_height=True, theme="Nymbo/Nymbo_Theme", css=custom_css) as d
|
|
461 |
value="<div class='pdf-status pdf-info'>๐ RAG: <strong>๋นํ์ฑํ</strong></div>"
|
462 |
)
|
463 |
|
464 |
-
#
|
465 |
-
rag_context_display = gr.HTML(value="", visible=False)
|
466 |
-
|
467 |
-
# Container for model interfaces
|
468 |
with gr.Column(visible=True) as model_120b_container:
|
469 |
-
gr.Markdown("### Model: openai/gpt-oss-120b")
|
|
|
|
|
|
|
|
|
|
|
|
|
470 |
|
471 |
-
|
472 |
-
|
473 |
-
|
474 |
-
|
475 |
-
|
476 |
-
|
477 |
-
lines=2
|
478 |
)
|
|
|
|
|
|
|
|
|
479 |
|
480 |
-
|
481 |
-
|
482 |
-
|
483 |
-
|
484 |
-
|
485 |
-
|
486 |
-
|
487 |
-
|
488 |
-
provider="fireworks-ai"
|
489 |
)
|
490 |
|
491 |
with gr.Column(visible=False) as model_20b_container:
|
492 |
-
gr.Markdown("### Model: openai/gpt-oss-20b")
|
|
|
|
|
|
|
|
|
|
|
|
|
493 |
|
494 |
-
with gr.
|
495 |
-
|
496 |
-
|
497 |
-
|
498 |
-
|
499 |
-
|
500 |
)
|
|
|
|
|
|
|
|
|
501 |
|
502 |
-
|
503 |
-
|
504 |
-
|
505 |
-
|
506 |
-
|
507 |
-
|
508 |
-
|
509 |
-
|
510 |
-
provider="fireworks-ai"
|
511 |
)
|
512 |
|
513 |
# Event Handlers
|
@@ -528,7 +590,7 @@ with gr.Blocks(fill_height=True, theme="Nymbo/Nymbo_Theme", css=custom_css) as d
|
|
528 |
# RAG ์ํ ์
๋ฐ์ดํธ
|
529 |
enable_rag.change(
|
530 |
fn=lambda x: gr.update(
|
531 |
-
value=f"<div class='pdf-status pdf-info'>๐ RAG: <strong>{'ํ์ฑํ' if x else '๋นํ์ฑํ'}</strong></div>"
|
532 |
),
|
533 |
inputs=[enable_rag],
|
534 |
outputs=[rag_status]
|
@@ -540,59 +602,89 @@ with gr.Blocks(fill_height=True, theme="Nymbo/Nymbo_Theme", css=custom_css) as d
|
|
540 |
inputs=[model_dropdown],
|
541 |
outputs=[model_120b_container, model_20b_container, current_model]
|
542 |
).then(
|
543 |
-
fn=lambda: gr.Info("
|
544 |
inputs=[],
|
545 |
outputs=[]
|
546 |
)
|
547 |
|
548 |
-
#
|
549 |
-
|
550 |
-
|
551 |
-
|
552 |
-
|
553 |
-
|
554 |
-
|
555 |
-
|
556 |
-
|
557 |
-
|
558 |
-
|
|
|
559 |
)
|
560 |
|
561 |
-
|
562 |
-
|
563 |
-
|
564 |
-
|
565 |
-
|
566 |
-
|
567 |
-
|
568 |
-
|
569 |
-
|
570 |
-
|
|
|
|
|
571 |
|
572 |
-
|
573 |
-
|
574 |
-
|
575 |
-
inputs=[user_input, enable_rag, document_list, top_k_chunks],
|
576 |
-
outputs=[user_input, rag_context_display]
|
577 |
)
|
578 |
|
579 |
-
|
580 |
-
|
581 |
-
|
582 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
583 |
)
|
584 |
|
585 |
-
# 20b ๋ชจ๋ธ์ฉ ์ด๋ฒคํธ
|
586 |
send_btn_20b.click(
|
587 |
-
fn=
|
588 |
-
|
589 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
590 |
)
|
591 |
|
592 |
-
|
593 |
-
fn=
|
594 |
-
|
595 |
-
outputs=[user_input_20b, rag_context_display]
|
596 |
)
|
597 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
598 |
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
import os
|
3 |
+
from typing import List, Dict, Any, Optional, Tuple
|
4 |
import hashlib
|
5 |
from datetime import datetime
|
6 |
import numpy as np
|
7 |
+
from gradio_client import Client
|
8 |
|
9 |
# PDF ์ฒ๋ฆฌ ๋ผ์ด๋ธ๋ฌ๋ฆฌ
|
10 |
try:
|
|
|
21 |
ST_AVAILABLE = False
|
22 |
print("โ ๏ธ Sentence Transformers not installed. Install with: pip install sentence-transformers")
|
23 |
|
24 |
+
# Soft and bright custom CSS
|
25 |
custom_css = """
|
26 |
.gradio-container {
|
27 |
+
background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
|
|
|
|
|
28 |
min-height: 100vh;
|
29 |
+
font-family: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif;
|
30 |
}
|
31 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
32 |
.main-container {
|
33 |
+
background: rgba(255, 255, 255, 0.98);
|
34 |
+
border-radius: 16px;
|
35 |
+
padding: 24px;
|
36 |
+
box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -1px rgba(0, 0, 0, 0.06);
|
37 |
+
border: 1px solid rgba(0, 0, 0, 0.05);
|
38 |
+
margin: 12px;
|
|
|
39 |
}
|
40 |
+
|
41 |
+
.main-container:hover {
|
42 |
+
box-shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.1), 0 4px 6px -2px rgba(0, 0, 0, 0.05);
|
43 |
+
transition: all 0.3s ease;
|
44 |
}
|
45 |
+
|
46 |
+
/* Status messages styling */
|
47 |
.pdf-status {
|
48 |
+
padding: 12px 16px;
|
49 |
+
border-radius: 12px;
|
50 |
+
margin: 12px 0;
|
51 |
+
font-size: 0.95rem;
|
52 |
+
font-weight: 500;
|
53 |
+
backdrop-filter: blur(10px);
|
54 |
}
|
55 |
+
|
56 |
.pdf-success {
|
57 |
+
background: linear-gradient(135deg, #d4edda 0%, #c3e6cb 100%);
|
58 |
+
border: 1px solid #b1dfbb;
|
59 |
+
color: #155724;
|
60 |
}
|
61 |
+
|
62 |
.pdf-error {
|
63 |
+
background: linear-gradient(135deg, #f8d7da 0%, #f5c6cb 100%);
|
64 |
+
border: 1px solid #f1aeb5;
|
65 |
+
color: #721c24;
|
66 |
}
|
67 |
+
|
68 |
.pdf-info {
|
69 |
+
background: linear-gradient(135deg, #d1ecf1 0%, #bee5eb 100%);
|
70 |
+
border: 1px solid #9ec5d8;
|
71 |
+
color: #0c5460;
|
72 |
+
}
|
73 |
+
|
74 |
+
.pdf-warning {
|
75 |
+
background: linear-gradient(135deg, #fff3cd 0%, #ffeeba 100%);
|
76 |
+
border: 1px solid #ffeaa7;
|
77 |
+
color: #856404;
|
78 |
}
|
79 |
+
|
80 |
+
/* RAG context display */
|
81 |
.rag-context {
|
82 |
+
background: linear-gradient(135deg, #fef3c7 0%, #fde68a 100%);
|
83 |
border-left: 4px solid #f59e0b;
|
84 |
+
padding: 16px;
|
85 |
+
margin: 16px 0;
|
86 |
+
border-radius: 8px;
|
87 |
+
font-size: 0.9rem;
|
88 |
+
}
|
89 |
+
|
90 |
+
/* Chat message styling */
|
91 |
+
.message {
|
92 |
+
padding: 12px 16px;
|
93 |
+
margin: 8px 4px;
|
94 |
+
border-radius: 12px;
|
95 |
+
max-width: 80%;
|
96 |
+
}
|
97 |
+
|
98 |
+
.user-message {
|
99 |
+
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
100 |
+
color: white;
|
101 |
+
margin-left: auto;
|
102 |
+
}
|
103 |
+
|
104 |
+
.bot-message {
|
105 |
+
background: #f3f4f6;
|
106 |
+
color: #1f2937;
|
107 |
}
|
108 |
"""
|
109 |
|
|
|
256 |
for doc_id in doc_ids:
|
257 |
if doc_id in self.document_chunks:
|
258 |
chunks = self.document_chunks[doc_id]
|
259 |
+
for chunk in chunks[:top_k]:
|
260 |
chunk_lower = chunk.lower()
|
261 |
score = sum(1 for keyword in query_keywords if keyword in chunk_lower)
|
262 |
if score > 0:
|
263 |
all_relevant_chunks.append({
|
264 |
+
"content": chunk[:500],
|
265 |
"doc_name": self.documents[doc_id]["metadata"]["file_name"],
|
266 |
"similarity": score / len(query_keywords) if query_keywords else 0
|
267 |
})
|
|
|
279 |
|
280 |
# ํ๋กฌํํธ ๊ตฌ์ฑ
|
281 |
prompt_parts = []
|
282 |
+
prompt_parts.append("์๋ ์ฐธ๊ณ ๋ฌธ์๋ฅผ ๋ฐํ์ผ๋ก ์ง๋ฌธ์ ๋ต๋ณํด์ฃผ์ธ์.\n")
|
283 |
prompt_parts.append("=" * 50)
|
284 |
|
285 |
for i, chunk in enumerate(relevant_chunks, 1):
|
|
|
290 |
|
291 |
prompt_parts.append("=" * 50)
|
292 |
prompt_parts.append(f"\n์ง๋ฌธ: {query}")
|
293 |
+
prompt_parts.append("\n์ ์ฐธ๊ณ ๋ฌธ์์ ๋ด์ฉ์ ๋ฐํ์ผ๋ก ์ ํํ๊ณ ์์ธํ๊ฒ ๋ต๋ณํด์ฃผ์ธ์:")
|
294 |
|
295 |
return "\n".join(prompt_parts)
|
296 |
|
297 |
# RAG ์์คํ
์ธ์คํด์ค ์์ฑ
|
298 |
rag_system = PDFRAGSystem()
|
299 |
|
300 |
+
# State variables
|
301 |
current_model = gr.State("openai/gpt-oss-120b")
|
302 |
+
conversation_history = gr.State([])
|
303 |
|
304 |
def upload_pdf(file):
|
305 |
"""PDF ํ์ผ ์
๋ก๋ ์ฒ๋ฆฌ"""
|
306 |
if file is None:
|
307 |
return (
|
308 |
+
gr.update(value="<div class='pdf-status pdf-warning'>๐ ํ์ผ์ ์ ํํด์ฃผ์ธ์</div>"),
|
309 |
gr.update(choices=[]),
|
310 |
gr.update(value=False)
|
311 |
)
|
|
|
323 |
if result["success"]:
|
324 |
status_html = f"""
|
325 |
<div class="pdf-status pdf-success">
|
326 |
+
โ
PDF ์
๋ก๋ ์๋ฃ<br>
|
327 |
๐ ํ์ผ: {result['title']}<br>
|
328 |
๐ ํ์ด์ง: {result['pages']}ํ์ด์ง<br>
|
329 |
๐ ์ฒญํฌ: {result['chunks']}๊ฐ ์์ฑ
|
|
|
361 |
rag_system.embeddings_store = {}
|
362 |
|
363 |
return (
|
364 |
+
gr.update(value="<div class='pdf-status pdf-info'>๐๏ธ ๋ชจ๋ ๋ฌธ์๊ฐ ์ญ์ ๋์์ต๋๋ค</div>"),
|
365 |
gr.update(choices=[], value=[]),
|
366 |
gr.update(value=False)
|
367 |
)
|
368 |
|
369 |
def switch_model(model_choice):
|
370 |
"""Function to switch between models"""
|
371 |
+
if model_choice == "openai/gpt-oss-120b":
|
372 |
+
return gr.update(visible=True), gr.update(visible=False), model_choice
|
373 |
+
else:
|
374 |
+
return gr.update(visible=False), gr.update(visible=True), model_choice
|
375 |
|
376 |
+
def chat_with_rag(message, history, model_name, enable_rag, selected_docs, top_k, temperature, max_tokens):
|
377 |
+
"""RAG๋ฅผ ์ ์ฉํ ์ฑํ
ํจ์"""
|
|
|
|
|
378 |
|
379 |
+
if not message:
|
380 |
+
return history
|
381 |
|
382 |
+
# RAG ์ ์ฉ
|
383 |
+
if enable_rag and selected_docs:
|
384 |
+
doc_ids = [doc.split(":")[0] for doc in selected_docs]
|
385 |
+
enhanced_message = rag_system.create_rag_prompt(message, doc_ids, top_k)
|
386 |
+
|
387 |
+
# ๋๋ฒ๊ทธ: RAG ์ ์ฉ ํ์ธ
|
388 |
+
print(f"RAG ์ ์ฉ๋จ - ์๋ณธ: {len(message)}์, ๊ฐํ: {len(enhanced_message)}์")
|
389 |
+
else:
|
390 |
+
enhanced_message = message
|
391 |
|
392 |
+
try:
|
393 |
+
# ์ฌ๊ธฐ์ ์ค์ ๋ชจ๋ธ API๋ฅผ ํธ์ถํด์ผ ํฉ๋๋ค
|
394 |
+
# ์์๋ก ๋ชจ์ ์๋ต ์์ฑ
|
395 |
+
if enable_rag and selected_docs:
|
396 |
+
response = f"""๐ [RAG ๊ธฐ๋ฐ ๋ต๋ณ]
|
397 |
+
|
398 |
+
๋ฌธ์๋ฅผ ์ฐธ๊ณ ํ์ฌ ๋ต๋ณ๋๋ฆฝ๋๋ค:
|
399 |
+
|
400 |
+
{enhanced_message[:500]}...
|
401 |
+
|
402 |
+
[์ฐธ๊ณ : ์ค์ ๋ชจ๋ธ API ์ฐ๊ฒฐ ํ์]
|
403 |
+
"""
|
404 |
+
else:
|
405 |
+
response = f"""๐ฌ [์ผ๋ฐ ๋ต๋ณ]
|
406 |
+
|
407 |
+
์ง๋ฌธ: {message}
|
408 |
+
|
409 |
+
[์ฐธ๊ณ : ์ค์ ๋ชจ๋ธ API ์ฐ๊ฒฐ ํ์]
|
410 |
+
"""
|
411 |
+
|
412 |
+
# ๋ํ ๊ธฐ๋ก์ ์ถ๊ฐ
|
413 |
+
history.append([message, response])
|
414 |
+
|
415 |
+
except Exception as e:
|
416 |
+
response = f"โ ์ค๋ฅ ๋ฐ์: {str(e)}"
|
417 |
+
history.append([message, response])
|
418 |
|
419 |
+
return history
|
420 |
|
421 |
+
# Main interface with soft theme
|
422 |
+
with gr.Blocks(fill_height=True, theme=gr.themes.Soft(), css=custom_css) as demo:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
423 |
|
424 |
with gr.Row():
|
425 |
# Sidebar
|
426 |
with gr.Column(scale=1):
|
427 |
with gr.Group(elem_classes="main-container"):
|
428 |
+
gr.Markdown("# ๐ค AI Chat + RAG")
|
429 |
gr.Markdown(
|
430 |
+
"OpenAI GPT-OSS ๋ชจ๋ธ๊ณผ PDF ๋ฌธ์ ๊ธฐ๋ฐ ๋ต๋ณ ์์คํ
์
๋๋ค."
|
|
|
431 |
)
|
432 |
|
433 |
+
# Login button
|
434 |
+
login_button = gr.LoginButton("๐ Hugging Face ๋ก๊ทธ์ธ", size="lg")
|
435 |
+
|
436 |
# Model selection
|
437 |
model_dropdown = gr.Dropdown(
|
438 |
choices=["openai/gpt-oss-120b", "openai/gpt-oss-20b"],
|
439 |
value="openai/gpt-oss-120b",
|
440 |
+
label="๐ ๋ชจ๋ธ ์ ํ",
|
441 |
+
info="์ํ๋ ๋ชจ๋ธ ํฌ๊ธฐ๋ฅผ ์ ํํ์ธ์"
|
442 |
)
|
443 |
|
|
|
|
|
|
|
444 |
# Reload button to apply model change
|
445 |
+
reload_btn = gr.Button("๐ ๋ชจ๋ธ ๋ณ๊ฒฝ ์ ์ฉ", variant="primary", size="lg")
|
446 |
|
447 |
# RAG Settings
|
448 |
+
with gr.Accordion("๐ PDF RAG ์ค์ ", open=True):
|
449 |
pdf_upload = gr.File(
|
450 |
+
label="๐ค PDF ์
๋ก๋",
|
451 |
file_types=[".pdf"],
|
452 |
type="filepath"
|
453 |
)
|
454 |
|
455 |
upload_status = gr.HTML(
|
456 |
+
value="<div class='pdf-status pdf-info'>๐ PDF๋ฅผ ์
๋ก๋ํ์ฌ ๋ฌธ์ ๊ธฐ๋ฐ ๋ต๋ณ์ ๋ฐ์ผ์ธ์</div>"
|
457 |
)
|
458 |
|
459 |
document_list = gr.CheckboxGroup(
|
|
|
462 |
info="์ฐธ๊ณ ํ ๋ฌธ์๋ฅผ ์ ํํ์ธ์"
|
463 |
)
|
464 |
|
465 |
+
with gr.Row():
|
466 |
+
clear_btn = gr.Button("๐๏ธ ๋ชจ๋ ๋ฌธ์ ์ญ์ ", size="sm", variant="secondary")
|
467 |
|
468 |
enable_rag = gr.Checkbox(
|
469 |
+
label="โจ RAG ํ์ฑํ",
|
470 |
value=False,
|
471 |
info="์ ํํ ๋ฌธ์๋ฅผ ์ฐธ๊ณ ํ์ฌ ๋ต๋ณ ์์ฑ"
|
472 |
)
|
|
|
477 |
value=3,
|
478 |
step=1,
|
479 |
label="์ฐธ์กฐ ์ฒญํฌ ์",
|
480 |
+
info="๋ต๋ณ์ ์ฐธ๊ณ ํ ๋ฌธ์ ์กฐ๊ฐ ๊ฐ์"
|
481 |
)
|
482 |
|
483 |
# Additional options
|
484 |
+
with gr.Accordion("๐๏ธ ๋ชจ๋ธ ์ต์
", open=False):
|
|
|
485 |
temperature = gr.Slider(
|
486 |
minimum=0,
|
487 |
maximum=2,
|
488 |
value=0.7,
|
489 |
step=0.1,
|
490 |
+
label="Temperature",
|
491 |
+
info="๋ฎ์์๋ก ์ผ๊ด์ฑ ์๊ณ , ๋์์๋ก ์ฐฝ์์ ์
๋๋ค"
|
492 |
)
|
493 |
max_tokens = gr.Slider(
|
494 |
minimum=1,
|
495 |
maximum=4096,
|
496 |
value=512,
|
497 |
step=1,
|
498 |
+
label="Max Tokens",
|
499 |
+
info="์์ฑํ ์ต๋ ํ ํฐ ์"
|
500 |
)
|
501 |
|
502 |
# Main chat area
|
|
|
509 |
value="<div class='pdf-status pdf-info'>๐ RAG: <strong>๋นํ์ฑํ</strong></div>"
|
510 |
)
|
511 |
|
512 |
+
# ํตํฉ๋ ์ฑํ
์ธํฐํ์ด์ค (๋ชจ๋ธ๋ณ๋ก ํ๋์ฉ)
|
|
|
|
|
|
|
513 |
with gr.Column(visible=True) as model_120b_container:
|
514 |
+
gr.Markdown("### ๐ Model: openai/gpt-oss-120b")
|
515 |
+
|
516 |
+
chatbot_120b = gr.Chatbot(
|
517 |
+
height=400,
|
518 |
+
show_label=False,
|
519 |
+
elem_classes="chatbot"
|
520 |
+
)
|
521 |
|
522 |
+
with gr.Row():
|
523 |
+
msg_120b = gr.Textbox(
|
524 |
+
placeholder="๋ฉ์์ง๋ฅผ ์
๋ ฅํ์ธ์... (Enter๋ก ์ ์ก)",
|
525 |
+
show_label=False,
|
526 |
+
scale=4,
|
527 |
+
container=False
|
|
|
528 |
)
|
529 |
+
send_btn_120b = gr.Button("๐ค ์ ์ก", variant="primary", scale=1)
|
530 |
+
|
531 |
+
with gr.Row():
|
532 |
+
clear_btn_120b = gr.Button("๐๏ธ ๋ํ ์ด๊ธฐํ", variant="secondary", size="sm")
|
533 |
|
534 |
+
# ์์ ์ง๋ฌธ๋ค
|
535 |
+
gr.Examples(
|
536 |
+
examples=[
|
537 |
+
"๋ฌธ์์ ์ฃผ์ ๋ด์ฉ์ ์์ฝํด์ฃผ์ธ์",
|
538 |
+
"์ด ๋ฌธ์์์ ๊ฐ์ฅ ์ค์ํ ํฌ์ธํธ๋ ๋ฌด์์ธ๊ฐ์?",
|
539 |
+
"๋ฌธ์์ ์ธ๊ธ๋ ๋ ์ง์ ์ผ์ ์ ์๋ ค์ฃผ์ธ์"
|
540 |
+
],
|
541 |
+
inputs=msg_120b
|
|
|
542 |
)
|
543 |
|
544 |
with gr.Column(visible=False) as model_20b_container:
|
545 |
+
gr.Markdown("### ๐ Model: openai/gpt-oss-20b")
|
546 |
+
|
547 |
+
chatbot_20b = gr.Chatbot(
|
548 |
+
height=400,
|
549 |
+
show_label=False,
|
550 |
+
elem_classes="chatbot"
|
551 |
+
)
|
552 |
|
553 |
+
with gr.Row():
|
554 |
+
msg_20b = gr.Textbox(
|
555 |
+
placeholder="๋ฉ์์ง๋ฅผ ์
๋ ฅํ์ธ์... (Enter๋ก ์ ์ก)",
|
556 |
+
show_label=False,
|
557 |
+
scale=4,
|
558 |
+
container=False
|
559 |
)
|
560 |
+
send_btn_20b = gr.Button("๐ค ์ ์ก", variant="primary", scale=1)
|
561 |
+
|
562 |
+
with gr.Row():
|
563 |
+
clear_btn_20b = gr.Button("๐๏ธ ๋ํ ์ด๊ธฐํ", variant="secondary", size="sm")
|
564 |
|
565 |
+
# ์์ ์ง๋ฌธ๋ค
|
566 |
+
gr.Examples(
|
567 |
+
examples=[
|
568 |
+
"๋ฌธ์์ ์ฃผ์ ๋ด์ฉ์ ์์ฝํด์ฃผ์ธ์",
|
569 |
+
"์ด ๋ฌธ์์์ ๊ฐ์ฅ ์ค์ํ ํฌ์ธํธ๋ ๋ฌด์์ธ๊ฐ์?",
|
570 |
+
"๋ฌธ์์ ์ธ๊ธ๋ ๋ ์ง์ ์ผ์ ์ ์๋ ค์ฃผ์ธ์"
|
571 |
+
],
|
572 |
+
inputs=msg_20b
|
|
|
573 |
)
|
574 |
|
575 |
# Event Handlers
|
|
|
590 |
# RAG ์ํ ์
๋ฐ์ดํธ
|
591 |
enable_rag.change(
|
592 |
fn=lambda x: gr.update(
|
593 |
+
value=f"<div class='pdf-status pdf-info'>๐ RAG: <strong>{'โ
ํ์ฑํ' if x else 'โญ ๋นํ์ฑํ'}</strong></div>"
|
594 |
),
|
595 |
inputs=[enable_rag],
|
596 |
outputs=[rag_status]
|
|
|
602 |
inputs=[model_dropdown],
|
603 |
outputs=[model_120b_container, model_20b_container, current_model]
|
604 |
).then(
|
605 |
+
fn=lambda: gr.Info("โ
๋ชจ๋ธ์ด ์ฑ๊ณต์ ์ผ๋ก ์ ํ๋์์ต๋๋ค!"),
|
606 |
inputs=[],
|
607 |
outputs=[]
|
608 |
)
|
609 |
|
610 |
+
# 120b ๋ชจ๋ธ ์ฑํ
์ด๋ฒคํธ
|
611 |
+
msg_120b.submit(
|
612 |
+
fn=lambda msg, hist: chat_with_rag(
|
613 |
+
msg, hist, "openai/gpt-oss-120b",
|
614 |
+
enable_rag.value, document_list.value, top_k_chunks.value,
|
615 |
+
temperature.value, max_tokens.value
|
616 |
+
),
|
617 |
+
inputs=[msg_120b, chatbot_120b],
|
618 |
+
outputs=[chatbot_120b]
|
619 |
+
).then(
|
620 |
+
fn=lambda: "",
|
621 |
+
outputs=[msg_120b]
|
622 |
)
|
623 |
|
624 |
+
send_btn_120b.click(
|
625 |
+
fn=lambda msg, hist: chat_with_rag(
|
626 |
+
msg, hist, "openai/gpt-oss-120b",
|
627 |
+
enable_rag.value, document_list.value, top_k_chunks.value,
|
628 |
+
temperature.value, max_tokens.value
|
629 |
+
),
|
630 |
+
inputs=[msg_120b, chatbot_120b],
|
631 |
+
outputs=[chatbot_120b]
|
632 |
+
).then(
|
633 |
+
fn=lambda: "",
|
634 |
+
outputs=[msg_120b]
|
635 |
+
)
|
636 |
|
637 |
+
clear_btn_120b.click(
|
638 |
+
fn=lambda: [],
|
639 |
+
outputs=[chatbot_120b]
|
|
|
|
|
640 |
)
|
641 |
|
642 |
+
# 20b ๋ชจ๋ธ ์ฑํ
์ด๋ฒคํธ
|
643 |
+
msg_20b.submit(
|
644 |
+
fn=lambda msg, hist: chat_with_rag(
|
645 |
+
msg, hist, "openai/gpt-oss-20b",
|
646 |
+
enable_rag.value, document_list.value, top_k_chunks.value,
|
647 |
+
temperature.value, max_tokens.value
|
648 |
+
),
|
649 |
+
inputs=[msg_20b, chatbot_20b],
|
650 |
+
outputs=[chatbot_20b]
|
651 |
+
).then(
|
652 |
+
fn=lambda: "",
|
653 |
+
outputs=[msg_20b]
|
654 |
)
|
655 |
|
|
|
656 |
send_btn_20b.click(
|
657 |
+
fn=lambda msg, hist: chat_with_rag(
|
658 |
+
msg, hist, "openai/gpt-oss-20b",
|
659 |
+
enable_rag.value, document_list.value, top_k_chunks.value,
|
660 |
+
temperature.value, max_tokens.value
|
661 |
+
),
|
662 |
+
inputs=[msg_20b, chatbot_20b],
|
663 |
+
outputs=[chatbot_20b]
|
664 |
+
).then(
|
665 |
+
fn=lambda: "",
|
666 |
+
outputs=[msg_20b]
|
667 |
)
|
668 |
|
669 |
+
clear_btn_20b.click(
|
670 |
+
fn=lambda: [],
|
671 |
+
outputs=[chatbot_20b]
|
|
|
672 |
)
|
673 |
|
674 |
+
# ์ค์ ๋ชจ๋ธ API ์ฐ๊ฒฐ์ ์ํ ํจ์ (๊ตฌํ ํ์)
|
675 |
+
def connect_to_model_api(model_name, message, temperature, max_tokens):
|
676 |
+
"""
|
677 |
+
์ค์ ๋ชจ๋ธ API์ ์ฐ๊ฒฐํ๋ ํจ์
|
678 |
+
TODO: ์ฌ๊ธฐ์ ์ค์ API ํธ์ถ ์ฝ๋๋ฅผ ๊ตฌํํด์ผ ํฉ๋๋ค
|
679 |
+
|
680 |
+
์์:
|
681 |
+
- OpenAI API
|
682 |
+
- Hugging Face Inference API
|
683 |
+
- Custom model endpoint
|
684 |
+
"""
|
685 |
+
# client = Client(f"models/{model_name}")
|
686 |
+
# response = client.predict(message, temperature=temperature, max_tokens=max_tokens)
|
687 |
+
# return response
|
688 |
+
pass
|
689 |
+
|
690 |
demo.launch()
|