Spaces:
Running
on
Zero
Running
on
Zero
cutechicken
commited on
Commit
โข
3c893d2
1
Parent(s):
861ff06
Update app.py
Browse files
app.py
CHANGED
@@ -2,6 +2,7 @@ import torch
|
|
2 |
import gradio as gr
|
3 |
import spaces
|
4 |
from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer
|
|
|
5 |
import os
|
6 |
from threading import Thread
|
7 |
import random
|
@@ -12,9 +13,20 @@ import pandas as pd
|
|
12 |
from typing import List, Tuple
|
13 |
import json
|
14 |
from datetime import datetime
|
15 |
-
|
16 |
-
|
17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
|
19 |
# ํ๊ฒฝ ๋ณ์ ์ค์
|
20 |
HF_TOKEN = os.environ.get("HF_TOKEN", None)
|
@@ -22,12 +34,7 @@ MODEL_ID = "CohereForAI/c4ai-command-r7b-12-2024"
|
|
22 |
MODELS = os.environ.get("MODELS")
|
23 |
MODEL_NAME = MODEL_ID.split("/")[-1]
|
24 |
|
25 |
-
#
|
26 |
-
model = AutoModelForCausalLM.from_pretrained(
|
27 |
-
MODEL_ID,
|
28 |
-
torch_dtype=torch.bfloat16,
|
29 |
-
device_map="auto",
|
30 |
-
)
|
31 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
|
32 |
|
33 |
# ์ํคํผ๋์ ๋ฐ์ดํฐ์
๋ก๋
|
@@ -147,51 +154,45 @@ def analyze_file_content(content, file_type):
|
|
147 |
words = len(content.split())
|
148 |
return f"๐ ๋ฌธ์ ๊ตฌ์กฐ: {total_lines}์ค, {paragraphs}๋จ๋ฝ, ์ฝ {words}๋จ์ด"
|
149 |
|
150 |
-
|
151 |
-
|
152 |
-
return "", ""
|
153 |
try:
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
df = pd.read_parquet(file.name, engine='pyarrow')
|
158 |
-
content = df.head(10).to_markdown(index=False)
|
159 |
-
return content, "parquet"
|
160 |
-
elif file_ext == '.csv':
|
161 |
-
encodings = ['utf-8', 'cp949', 'euc-kr', 'latin1']
|
162 |
-
for encoding in encodings:
|
163 |
-
try:
|
164 |
-
df = pd.read_csv(file.name, encoding=encoding)
|
165 |
-
content = f"๐ ๋ฐ์ดํฐ ๋ฏธ๋ฆฌ๋ณด๊ธฐ:\n{df.head(10).to_markdown(index=False)}\n\n"
|
166 |
-
content += f"\n๐ ๋ฐ์ดํฐ ์ ๋ณด:\n"
|
167 |
-
content += f"- ์ ์ฒด ํ ์: {len(df)}\n"
|
168 |
-
content += f"- ์ ์ฒด ์ด ์: {len(df.columns)}\n"
|
169 |
-
content += f"- ์ปฌ๋ผ ๋ชฉ๋ก: {', '.join(df.columns)}\n"
|
170 |
-
content += f"\n๐ ์ปฌ๋ผ ๋ฐ์ดํฐ ํ์
:\n"
|
171 |
-
for col, dtype in df.dtypes.items():
|
172 |
-
content += f"- {col}: {dtype}\n"
|
173 |
-
null_counts = df.isnull().sum()
|
174 |
-
if null_counts.any():
|
175 |
-
content += f"\nโ ๏ธ ๊ฒฐ์ธก์น:\n"
|
176 |
-
for col, null_count in null_counts[null_counts > 0].items():
|
177 |
-
content += f"- {col}: {null_count}๊ฐ ๋๋ฝ\n"
|
178 |
-
return content, "csv"
|
179 |
-
except UnicodeDecodeError:
|
180 |
-
continue
|
181 |
-
raise UnicodeDecodeError(f"โ ์ง์๋๋ ์ธ์ฝ๋ฉ์ผ๋ก ํ์ผ์ ์ฝ์ ์ ์์ต๋๋ค ({', '.join(encodings)})")
|
182 |
else:
|
183 |
-
|
184 |
-
|
185 |
-
|
186 |
-
|
187 |
-
|
188 |
-
|
189 |
-
|
190 |
-
|
191 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
192 |
except Exception as e:
|
193 |
-
return f"
|
194 |
-
|
195 |
|
196 |
def read_uploaded_file(file):
|
197 |
if file is None:
|
@@ -199,76 +200,173 @@ def read_uploaded_file(file):
|
|
199 |
try:
|
200 |
file_ext = os.path.splitext(file.name)[1].lower()
|
201 |
|
|
|
|
|
|
|
202 |
if file_ext == '.parquet':
|
203 |
-
|
204 |
-
|
205 |
-
|
206 |
-
|
207 |
-
|
208 |
-
|
209 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
210 |
|
|
|
|
|
211 |
elif file_ext == '.csv':
|
212 |
encodings = ['utf-8', 'cp949', 'euc-kr', 'latin1']
|
213 |
for encoding in encodings:
|
214 |
try:
|
215 |
df = pd.read_csv(file.name, encoding=encoding)
|
216 |
-
content = f"๐
|
217 |
-
content += f"
|
218 |
-
content += f"- ์ ์ฒด ํ ์: {len(df)}
|
219 |
-
content += f"- ์ ์ฒด ์ด ์: {len(df.columns)}
|
220 |
-
content += f"-
|
221 |
-
|
222 |
-
|
223 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
224 |
null_counts = df.isnull().sum()
|
225 |
-
|
226 |
-
content += f"\n
|
227 |
-
|
228 |
-
content += f"- {col}: {null_count}๊ฐ ๋๋ฝ\n"
|
229 |
return content, "csv"
|
230 |
except UnicodeDecodeError:
|
231 |
continue
|
232 |
raise UnicodeDecodeError(f"์ง์๋๋ ์ธ์ฝ๋ฉ์ผ๋ก ํ์ผ์ ์ฝ์ ์ ์์ต๋๋ค ({', '.join(encodings)})")
|
233 |
-
|
234 |
-
|
|
|
235 |
encodings = ['utf-8', 'cp949', 'euc-kr', 'latin1']
|
236 |
for encoding in encodings:
|
237 |
try:
|
238 |
with open(file.name, 'r', encoding=encoding) as f:
|
239 |
content = f.read()
|
240 |
|
241 |
-
|
242 |
-
|
243 |
-
|
244 |
-
|
245 |
-
|
246 |
-
|
247 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
248 |
|
249 |
-
|
250 |
-
|
251 |
-
|
252 |
-
|
253 |
-
|
254 |
-
|
255 |
-
|
256 |
-
|
257 |
-
|
258 |
-
analysis += f"- ํด๋์ค ์: {classes}\n"
|
259 |
-
analysis += f"- import ๋ฌธ ์: {imports}\n"
|
260 |
-
else:
|
261 |
-
# ์ผ๋ฐ ํ
์คํธ ํ์ผ ๋ถ์
|
262 |
-
words = len(content.split())
|
263 |
-
chars = len(content)
|
264 |
-
|
265 |
-
analysis = f"\n๐ ํ
์คํธ ๋ถ์:\n"
|
266 |
-
analysis += f"- ์ ์ฒด ๋ผ์ธ ์: {total_lines}\n"
|
267 |
-
analysis += f"- ์ค์ ๋ด์ฉ์ด ์๋ ๋ผ์ธ ์: {non_empty_lines}\n"
|
268 |
-
analysis += f"- ๋จ์ด ์: {words}\n"
|
269 |
-
analysis += f"- ๋ฌธ์ ์: {chars}\n"
|
270 |
|
271 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
272 |
except UnicodeDecodeError:
|
273 |
continue
|
274 |
raise UnicodeDecodeError(f"์ง์๋๋ ์ธ์ฝ๋ฉ์ผ๋ก ํ์ผ์ ์ฝ์ ์ ์์ต๋๋ค ({', '.join(encodings)})")
|
@@ -276,6 +374,9 @@ def read_uploaded_file(file):
|
|
276 |
except Exception as e:
|
277 |
return f"ํ์ผ ์ฝ๊ธฐ ์ค๋ฅ: {str(e)}", "error"
|
278 |
|
|
|
|
|
|
|
279 |
# ํ์ผ ์
๋ก๋ ์ด๋ฒคํธ ํธ๋ค๋ง ์์
|
280 |
def init_msg():
|
281 |
return "ํ์ผ์ ๋ถ์ํ๊ณ ์์ต๋๋ค..."
|
@@ -291,18 +392,15 @@ CSS = """
|
|
291 |
--text-color: #333333;
|
292 |
--shadow-color: rgba(0, 0, 0, 0.1);
|
293 |
}
|
294 |
-
|
295 |
body {
|
296 |
background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
|
297 |
min-height: 100vh;
|
298 |
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
|
299 |
}
|
300 |
-
|
301 |
.container {
|
302 |
transform-style: preserve-3d;
|
303 |
perspective: 1000px;
|
304 |
}
|
305 |
-
|
306 |
.chatbot {
|
307 |
background: var(--card-background);
|
308 |
border-radius: 20px;
|
@@ -313,11 +411,9 @@ body {
|
|
313 |
transition: transform 0.3s ease;
|
314 |
backdrop-filter: blur(10px);
|
315 |
}
|
316 |
-
|
317 |
.chatbot:hover {
|
318 |
transform: translateZ(10px);
|
319 |
}
|
320 |
-
|
321 |
/* ๋ฉ์์ง ์
๋ ฅ ์์ญ */
|
322 |
.input-area {
|
323 |
background: var(--card-background);
|
@@ -333,11 +429,9 @@ body {
|
|
333 |
align-items: center;
|
334 |
gap: 10px;
|
335 |
}
|
336 |
-
|
337 |
.input-area:hover {
|
338 |
transform: translateZ(5px);
|
339 |
}
|
340 |
-
|
341 |
/* ๋ฒํผ ์คํ์ผ */
|
342 |
.custom-button {
|
343 |
background: linear-gradient(145deg, var(--primary-color), var(--secondary-color));
|
@@ -353,14 +447,12 @@ body {
|
|
353 |
0 4px 6px var(--shadow-color),
|
354 |
0 1px 3px var(--shadow-color);
|
355 |
}
|
356 |
-
|
357 |
.custom-button:hover {
|
358 |
transform: translateZ(5px) translateY(-2px);
|
359 |
box-shadow:
|
360 |
0 7px 14px var(--shadow-color),
|
361 |
0 3px 6px var(--shadow-color);
|
362 |
}
|
363 |
-
|
364 |
/* ํ์ผ ์
๋ก๋ ๋ฒํผ */
|
365 |
.file-upload-icon {
|
366 |
background: linear-gradient(145deg, #64b5f6, #42a5f5);
|
@@ -376,12 +468,10 @@ body {
|
|
376 |
transition: all 0.3s ease;
|
377 |
box-shadow: 0 2px 5px rgba(0,0,0,0.1);
|
378 |
}
|
379 |
-
|
380 |
.file-upload-icon:hover {
|
381 |
transform: translateY(-2px);
|
382 |
box-shadow: 0 4px 8px rgba(0,0,0,0.2);
|
383 |
}
|
384 |
-
|
385 |
/* ํ์ผ ์
๋ก๋ ๋ฒํผ ๋ด๋ถ ์์ ์คํ์ผ๋ง */
|
386 |
.file-upload-icon > .wrap {
|
387 |
display: flex !important;
|
@@ -390,17 +480,14 @@ body {
|
|
390 |
width: 100%;
|
391 |
height: 100%;
|
392 |
}
|
393 |
-
|
394 |
.file-upload-icon > .wrap > p {
|
395 |
display: none !important;
|
396 |
}
|
397 |
-
|
398 |
.file-upload-icon > .wrap::before {
|
399 |
content: "๐";
|
400 |
font-size: 2em;
|
401 |
display: block;
|
402 |
}
|
403 |
-
|
404 |
/* ๋ฉ์์ง ์คํ์ผ */
|
405 |
.message {
|
406 |
background: var(--card-background);
|
@@ -413,16 +500,13 @@ body {
|
|
413 |
transform: translateZ(0);
|
414 |
transition: all 0.3s ease;
|
415 |
}
|
416 |
-
|
417 |
.message:hover {
|
418 |
transform: translateZ(5px);
|
419 |
}
|
420 |
-
|
421 |
.chat-container {
|
422 |
height: 600px !important;
|
423 |
margin-bottom: 10px;
|
424 |
}
|
425 |
-
|
426 |
.input-container {
|
427 |
height: 70px !important;
|
428 |
display: flex;
|
@@ -430,7 +514,6 @@ body {
|
|
430 |
gap: 10px;
|
431 |
margin-top: 5px;
|
432 |
}
|
433 |
-
|
434 |
.input-textbox {
|
435 |
height: 70px !important;
|
436 |
border-radius: 8px !important;
|
@@ -439,17 +522,14 @@ body {
|
|
439 |
display: flex !important;
|
440 |
align-items: flex-start !important; /* ํ
์คํธ ์
๋ ฅ ์์น๋ฅผ ์๋ก ์กฐ์ */
|
441 |
}
|
442 |
-
|
443 |
.input-textbox textarea {
|
444 |
padding-top: 5px !important; /* ํ
์คํธ ์๋จ ์ฌ๋ฐฑ ์กฐ์ */
|
445 |
}
|
446 |
-
|
447 |
.send-button {
|
448 |
height: 70px !important;
|
449 |
min-width: 70px !important;
|
450 |
font-size: 1.1em !important;
|
451 |
}
|
452 |
-
|
453 |
/* ์ค์ ํจ๋ ๊ธฐ๋ณธ ์คํ์ผ */
|
454 |
.settings-panel {
|
455 |
padding: 20px;
|
@@ -457,12 +537,37 @@ body {
|
|
457 |
}
|
458 |
"""
|
459 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
460 |
@spaces.GPU
|
461 |
def stream_chat(message: str, history: list, uploaded_file, temperature: float, max_new_tokens: int, top_p: float, top_k: int, penalty: float):
|
|
|
|
|
462 |
try:
|
|
|
|
|
|
|
463 |
print(f'message is - {message}')
|
464 |
print(f'history is - {history}')
|
465 |
-
|
466 |
# ํ์ผ ์
๋ก๋ ์ฒ๋ฆฌ
|
467 |
file_context = ""
|
468 |
if uploaded_file and message == "ํ์ผ์ ๋ถ์ํ๊ณ ์์ต๋๋ค...":
|
@@ -471,10 +576,23 @@ def stream_chat(message: str, history: list, uploaded_file, temperature: float,
|
|
471 |
if content:
|
472 |
file_analysis = analyze_file_content(content, file_type)
|
473 |
file_context = f"\n\n๐ ํ์ผ ๋ถ์ ๊ฒฐ๊ณผ:\n{file_analysis}\n\nํ์ผ ๋ด์ฉ:\n```\n{content}\n```"
|
|
|
474 |
message = "์
๋ก๋๋ ํ์ผ์ ๋ถ์ํด์ฃผ์ธ์."
|
475 |
except Exception as e:
|
476 |
print(f"ํ์ผ ๋ถ์ ์ค๋ฅ: {str(e)}")
|
477 |
file_context = f"\n\nโ ํ์ผ ๋ถ์ ์ค ์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค: {str(e)}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
478 |
|
479 |
# ๊ด๋ จ ์ปจํ
์คํธ ์ฐพ๊ธฐ
|
480 |
try:
|
@@ -498,9 +616,18 @@ def stream_chat(message: str, history: list, uploaded_file, temperature: float,
|
|
498 |
final_message = file_context + wiki_context + "\nํ์ฌ ์ง๋ฌธ: " + message
|
499 |
conversation.append({"role": "user", "content": final_message})
|
500 |
|
501 |
-
#
|
502 |
input_ids = tokenizer.apply_chat_template(conversation, tokenize=False, add_generation_prompt=True)
|
503 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
504 |
|
505 |
streamer = TextIteratorStreamer(tokenizer, timeout=10., skip_prompt=True, skip_special_tokens=True)
|
506 |
|
@@ -510,12 +637,15 @@ def stream_chat(message: str, history: list, uploaded_file, temperature: float,
|
|
510 |
top_k=top_k,
|
511 |
top_p=top_p,
|
512 |
repetition_penalty=penalty,
|
513 |
-
max_new_tokens=max_new_tokens,
|
514 |
do_sample=True,
|
515 |
temperature=temperature,
|
516 |
eos_token_id=[255001],
|
517 |
)
|
518 |
|
|
|
|
|
|
|
519 |
thread = Thread(target=model.generate, kwargs=generate_kwargs)
|
520 |
thread.start()
|
521 |
|
@@ -524,15 +654,26 @@ def stream_chat(message: str, history: list, uploaded_file, temperature: float,
|
|
524 |
buffer += new_text
|
525 |
yield "", history + [[message, buffer]]
|
526 |
|
|
|
|
|
|
|
527 |
except Exception as e:
|
528 |
error_message = f"์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค: {str(e)}"
|
529 |
print(f"Stream chat ์ค๋ฅ: {error_message}")
|
|
|
|
|
530 |
yield "", history + [[message, error_message]]
|
531 |
|
532 |
|
533 |
|
534 |
def create_demo():
|
535 |
with gr.Blocks(css=CSS) as demo:
|
|
|
|
|
|
|
|
|
|
|
|
|
536 |
chatbot = gr.Chatbot(
|
537 |
value=[],
|
538 |
height=600,
|
@@ -551,7 +692,7 @@ def create_demo():
|
|
551 |
show_label=False
|
552 |
)
|
553 |
|
554 |
-
with gr.Column(scale=
|
555 |
msg = gr.Textbox(
|
556 |
show_label=False,
|
557 |
placeholder="๋ฉ์์ง๋ฅผ ์
๋ ฅํ์ธ์... ๐ญ",
|
@@ -566,6 +707,13 @@ def create_demo():
|
|
566 |
elem_classes="send-button custom-button",
|
567 |
scale=1
|
568 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
569 |
|
570 |
with gr.Accordion("๐ฎ ๊ณ ๊ธ ์ค์ ", open=False):
|
571 |
with gr.Row():
|
@@ -602,6 +750,12 @@ def create_demo():
|
|
602 |
inputs=msg
|
603 |
)
|
604 |
|
|
|
|
|
|
|
|
|
|
|
|
|
605 |
# ์ด๋ฒคํธ ๋ฐ์ธ๋ฉ
|
606 |
msg.submit(
|
607 |
stream_chat,
|
@@ -626,6 +780,13 @@ def create_demo():
|
|
626 |
queue=True
|
627 |
)
|
628 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
629 |
return demo
|
630 |
|
631 |
if __name__ == "__main__":
|
|
|
2 |
import gradio as gr
|
3 |
import spaces
|
4 |
from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer
|
5 |
+
|
6 |
import os
|
7 |
from threading import Thread
|
8 |
import random
|
|
|
13 |
from typing import List, Tuple
|
14 |
import json
|
15 |
from datetime import datetime
|
16 |
+
import pyarrow.parquet as pq
|
17 |
+
import pypdf
|
18 |
+
import io
|
19 |
+
import pyarrow.parquet as pq
|
20 |
+
from pdfminer.high_level import extract_text
|
21 |
+
from pdfminer.layout import LAParams
|
22 |
+
from tabulate import tabulate # tabulate ์ถ๊ฐ
|
23 |
+
import platform
|
24 |
+
import subprocess
|
25 |
+
import pytesseract
|
26 |
+
from pdf2image import convert_from_path
|
27 |
+
|
28 |
+
# ์ ์ญ ๋ณ์ ์ถ๊ฐ
|
29 |
+
current_file_context = None
|
30 |
|
31 |
# ํ๊ฒฝ ๋ณ์ ์ค์
|
32 |
HF_TOKEN = os.environ.get("HF_TOKEN", None)
|
|
|
34 |
MODELS = os.environ.get("MODELS")
|
35 |
MODEL_NAME = MODEL_ID.split("/")[-1]
|
36 |
|
37 |
+
model = None # ์ ์ญ ๋ณ์๋ก ์ ์ธ
|
|
|
|
|
|
|
|
|
|
|
38 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
|
39 |
|
40 |
# ์ํคํผ๋์ ๋ฐ์ดํฐ์
๋ก๋
|
|
|
154 |
words = len(content.split())
|
155 |
return f"๐ ๋ฌธ์ ๊ตฌ์กฐ: {total_lines}์ค, {paragraphs}๋จ๋ฝ, ์ฝ {words}๋จ์ด"
|
156 |
|
157 |
+
|
158 |
+
def extract_pdf_text_with_ocr(file_path):
|
|
|
159 |
try:
|
160 |
+
# Poppler ๊ฒฝ๋ก ์ค์
|
161 |
+
if platform.system() == 'Windows':
|
162 |
+
poppler_path = r"C:\Program Files\poppler-0.68.0\bin"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
163 |
else:
|
164 |
+
poppler_path = None # Linux์ ๊ฒฝ์ฐ ๊ธฐ๋ณธ ๊ฒฝ๋ก ์ฌ์ฉ
|
165 |
+
|
166 |
+
# PDF๋ฅผ ์ด๋ฏธ์ง๋ก ๋ณํ
|
167 |
+
images = convert_from_path(
|
168 |
+
file_path,
|
169 |
+
poppler_path=poppler_path,
|
170 |
+
fmt='jpeg',
|
171 |
+
grayscale=False,
|
172 |
+
size=(1700, None) # ํด์๋ ํฅ์
|
173 |
+
)
|
174 |
+
|
175 |
+
# ์ ์ฒด ํ
์คํธ ์ ์ฅ
|
176 |
+
text = ""
|
177 |
+
|
178 |
+
# ๊ฐ ํ์ด์ง์ ๋ํด OCR ์ํ
|
179 |
+
for i, image in enumerate(images):
|
180 |
+
try:
|
181 |
+
# OCR ์ค์
|
182 |
+
custom_config = r'--oem 3 --psm 6 -l kor+eng'
|
183 |
+
# OCR ์ํ
|
184 |
+
page_text = pytesseract.image_to_string(
|
185 |
+
image,
|
186 |
+
config=custom_config
|
187 |
+
)
|
188 |
+
text += f"\n--- ํ์ด์ง {i+1} ---\n{page_text}\n"
|
189 |
+
except Exception as e:
|
190 |
+
print(f"ํ์ด์ง {i+1} OCR ์ค๋ฅ: {str(e)}")
|
191 |
+
continue
|
192 |
+
|
193 |
+
return text
|
194 |
except Exception as e:
|
195 |
+
return f"PDF ํ
์คํธ ์ถ์ถ ์ค๋ฅ: {str(e)}"
|
|
|
196 |
|
197 |
def read_uploaded_file(file):
|
198 |
if file is None:
|
|
|
200 |
try:
|
201 |
file_ext = os.path.splitext(file.name)[1].lower()
|
202 |
|
203 |
+
|
204 |
+
|
205 |
+
# Parquet ํ์ผ ์ฒ๋ฆฌ
|
206 |
if file_ext == '.parquet':
|
207 |
+
try:
|
208 |
+
table = pq.read_table(file.name)
|
209 |
+
df = table.to_pandas()
|
210 |
+
|
211 |
+
content = f"๐ Parquet ํ์ผ ๋ถ์:\n\n"
|
212 |
+
content += f"1. ๊ธฐ๋ณธ ์ ๋ณด:\n"
|
213 |
+
content += f"- ์ ์ฒด ํ ์: {len(df):,}๊ฐ\n"
|
214 |
+
content += f"- ์ ์ฒด ์ด ์: {len(df.columns)}๊ฐ\n"
|
215 |
+
content += f"- ๋ฉ๋ชจ๋ฆฌ ์ฌ์ฉ๋: {df.memory_usage(deep=True).sum() / 1024 / 1024:.2f} MB\n\n"
|
216 |
+
|
217 |
+
content += f"2. ์ปฌ๋ผ ์ ๋ณด:\n"
|
218 |
+
for col in df.columns:
|
219 |
+
content += f"- {col} ({df[col].dtype})\n"
|
220 |
+
|
221 |
+
content += f"\n3. ๋ฐ์ดํฐ ๋ฏธ๋ฆฌ๋ณด๊ธฐ:\n"
|
222 |
+
# tabulate ์ฌ์ฉํ์ฌ ํ
์ด๋ธ ํ์์ผ๋ก ์ถ๋ ฅ
|
223 |
+
content += tabulate(df.head(5), headers='keys', tablefmt='pipe', showindex=False)
|
224 |
+
|
225 |
+
content += f"\n\n4. ๊ฒฐ์ธก์น ์ ๋ณด:\n"
|
226 |
+
null_counts = df.isnull().sum()
|
227 |
+
for col, count in null_counts[null_counts > 0].items():
|
228 |
+
content += f"- {col}: {count:,}๊ฐ ({count/len(df)*100:.1f}%)\n"
|
229 |
+
|
230 |
+
# ์์นํ ์ปฌ๋ผ์ ๋ํ ๊ธฐ๋ณธ ํต๊ณ
|
231 |
+
numeric_cols = df.select_dtypes(include=['int64', 'float64']).columns
|
232 |
+
if len(numeric_cols) > 0:
|
233 |
+
content += f"\n5. ์์นํ ์ปฌ๋ผ ํต๊ณ:\n"
|
234 |
+
stats_df = df[numeric_cols].describe()
|
235 |
+
content += tabulate(stats_df, headers='keys', tablefmt='pipe')
|
236 |
+
|
237 |
+
return content, "parquet"
|
238 |
+
except Exception as e:
|
239 |
+
return f"Parquet ํ์ผ ์ฝ๊ธฐ ์ค๋ฅ: {str(e)}", "error"
|
240 |
+
|
241 |
+
# PDF ํ์ผ ์ฒ๋ฆฌ
|
242 |
+
if file_ext == '.pdf':
|
243 |
+
try:
|
244 |
+
pdf_reader = pypdf.PdfReader(file.name)
|
245 |
+
total_pages = len(pdf_reader.pages)
|
246 |
+
|
247 |
+
content = f"๐ PDF ๋ฌธ์ ๋ถ์:\n\n"
|
248 |
+
content += f"1. ๊ธฐ๋ณธ ์ ๋ณด:\n"
|
249 |
+
content += f"- ์ด ํ์ด์ง ์: {total_pages}ํ์ด์ง\n"
|
250 |
+
|
251 |
+
# ๋ฉํ๋ฐ์ดํฐ ์ถ์ถ
|
252 |
+
if pdf_reader.metadata:
|
253 |
+
content += "\n2. ๋ฉํ๋ฐ์ดํฐ:\n"
|
254 |
+
for key, value in pdf_reader.metadata.items():
|
255 |
+
if value and str(key).startswith('/'):
|
256 |
+
content += f"- {key[1:]}: {value}\n"
|
257 |
+
|
258 |
+
# ๋จผ์ pdfminer๋ก ํ
์คํธ ์ถ์ถ ์๋
|
259 |
+
try:
|
260 |
+
text = extract_text(
|
261 |
+
file.name,
|
262 |
+
laparams=LAParams(
|
263 |
+
line_margin=0.5,
|
264 |
+
word_margin=0.1,
|
265 |
+
char_margin=2.0,
|
266 |
+
all_texts=True
|
267 |
+
)
|
268 |
+
)
|
269 |
+
except:
|
270 |
+
text = ""
|
271 |
+
|
272 |
+
# pdfminer๋ก ์ถ์ถ ์คํจ์ OCR ์๋
|
273 |
+
if not text.strip():
|
274 |
+
text = extract_pdf_text_with_ocr(file.name)
|
275 |
+
|
276 |
+
# ํ
์คํธ ๋ถ์
|
277 |
+
if text:
|
278 |
+
words = text.split()
|
279 |
+
lines = text.split('\n')
|
280 |
+
content += f"\n3. ํ
์คํธ ๋ถ์:\n"
|
281 |
+
content += f"- ์ด ๋จ์ด ์: {len(words):,}๊ฐ\n"
|
282 |
+
content += f"- ๊ณ ์ ๋จ์ด ์: {len(set(words)):,}๊ฐ\n"
|
283 |
+
content += f"- ์ด ๋ผ์ธ ์: {len(lines):,}๊ฐ\n"
|
284 |
+
|
285 |
+
# ๋ณธ๋ฌธ ๋ด์ฉ
|
286 |
+
content += f"\n4. ๋ณธ๋ฌธ ๋ด์ฉ:\n"
|
287 |
+
preview_length = min(2000, len(text)) # ๋ฏธ๋ฆฌ๋ณด๊ธฐ ๊ธธ์ด ์ฆ๊ฐ
|
288 |
+
content += f"--- ์ฒ์ {preview_length}์ ---\n"
|
289 |
+
content += text[:preview_length]
|
290 |
+
if len(text) > preview_length:
|
291 |
+
content += f"\n... (์ด {len(text):,}์ ์ค ์ผ๋ถ ํ์)\n"
|
292 |
+
else:
|
293 |
+
content += "\nโ ๏ธ ํ
์คํธ ์ถ์ถ ์คํจ"
|
294 |
+
|
295 |
+
return content, "pdf"
|
296 |
+
except Exception as e:
|
297 |
+
return f"PDF ํ์ผ ์ฝ๊ธฐ ์ค๋ฅ: {str(e)}", "error"
|
298 |
+
|
299 |
|
300 |
+
|
301 |
+
# CSV ํ์ผ ์ฒ๋ฆฌ
|
302 |
elif file_ext == '.csv':
|
303 |
encodings = ['utf-8', 'cp949', 'euc-kr', 'latin1']
|
304 |
for encoding in encodings:
|
305 |
try:
|
306 |
df = pd.read_csv(file.name, encoding=encoding)
|
307 |
+
content = f"๐ CSV ํ์ผ ๋ถ์:\n\n"
|
308 |
+
content += f"1. ๊ธฐ๋ณธ ์ ๋ณด:\n"
|
309 |
+
content += f"- ์ ์ฒด ํ ์: {len(df):,}๊ฐ\n"
|
310 |
+
content += f"- ์ ์ฒด ์ด ์: {len(df.columns)}๊ฐ\n"
|
311 |
+
content += f"- ๋ฉ๋ชจ๋ฆฌ ์ฌ์ฉ๋: {df.memory_usage(deep=True).sum() / 1024 / 1024:.2f} MB\n\n"
|
312 |
+
|
313 |
+
content += f"2. ์ปฌ๋ผ ์ ๋ณด:\n"
|
314 |
+
for col in df.columns:
|
315 |
+
content += f"- {col} ({df[col].dtype})\n"
|
316 |
+
|
317 |
+
content += f"\n3. ๋ฐ์ดํฐ ๋ฏธ๋ฆฌ๋ณด๊ธฐ:\n"
|
318 |
+
content += df.head(5).to_markdown(index=False)
|
319 |
+
|
320 |
+
content += f"\n\n4. ๊ฒฐ์ธก์น ์ ๋ณด:\n"
|
321 |
null_counts = df.isnull().sum()
|
322 |
+
for col, count in null_counts[null_counts > 0].items():
|
323 |
+
content += f"- {col}: {count:,}๊ฐ ({count/len(df)*100:.1f}%)\n"
|
324 |
+
|
|
|
325 |
return content, "csv"
|
326 |
except UnicodeDecodeError:
|
327 |
continue
|
328 |
raise UnicodeDecodeError(f"์ง์๋๋ ์ธ์ฝ๋ฉ์ผ๋ก ํ์ผ์ ์ฝ์ ์ ์์ต๋๋ค ({', '.join(encodings)})")
|
329 |
+
|
330 |
+
# ํ
์คํธ ํ์ผ ์ฒ๋ฆฌ
|
331 |
+
else:
|
332 |
encodings = ['utf-8', 'cp949', 'euc-kr', 'latin1']
|
333 |
for encoding in encodings:
|
334 |
try:
|
335 |
with open(file.name, 'r', encoding=encoding) as f:
|
336 |
content = f.read()
|
337 |
|
338 |
+
# ํ์ผ ๋ด์ฉ ๋ถ์
|
339 |
+
lines = content.split('\n')
|
340 |
+
total_lines = len(lines)
|
341 |
+
non_empty_lines = len([line for line in lines if line.strip()])
|
342 |
+
|
343 |
+
# ์ฝ๋ ํ์ผ ์ฌ๋ถ ํ์ธ
|
344 |
+
is_code = any(keyword in content.lower() for keyword in ['def ', 'class ', 'import ', 'function'])
|
345 |
+
|
346 |
+
analysis = f"\n๐ ํ์ผ ๋ถ์:\n"
|
347 |
+
if is_code:
|
348 |
+
# ์ฝ๋ ํ์ผ ๋ถ์
|
349 |
+
functions = len([line for line in lines if 'def ' in line])
|
350 |
+
classes = len([line for line in lines if 'class ' in line])
|
351 |
+
imports = len([line for line in lines if 'import ' in line or 'from ' in line])
|
352 |
|
353 |
+
analysis += f"- ํ์ผ ์ ํ: ์ฝ๋\n"
|
354 |
+
analysis += f"- ์ ์ฒด ๋ผ์ธ ์: {total_lines:,}์ค\n"
|
355 |
+
analysis += f"- ํจ์ ์: {functions}๊ฐ\n"
|
356 |
+
analysis += f"- ํด๋์ค ์: {classes}๊ฐ\n"
|
357 |
+
analysis += f"- import ๋ฌธ ์: {imports}๊ฐ\n"
|
358 |
+
else:
|
359 |
+
# ์ผ๋ฐ ํ
์คํธ ํ์ผ ๋ถ์
|
360 |
+
words = len(content.split())
|
361 |
+
chars = len(content)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
362 |
|
363 |
+
analysis += f"- ํ์ผ ์ ํ: ํ
์คํธ\n"
|
364 |
+
analysis += f"- ์ ์ฒด ๋ผ์ธ ์: {total_lines:,}์ค\n"
|
365 |
+
analysis += f"- ์ค์ ๋ด์ฉ์ด ์๋ ๋ผ์ธ ์: {non_empty_lines:,}์ค\n"
|
366 |
+
analysis += f"- ๋จ์ด ์: {words:,}๊ฐ\n"
|
367 |
+
analysis += f"- ๋ฌธ์ ์: {chars:,}๊ฐ\n"
|
368 |
+
|
369 |
+
return content + analysis, "text"
|
370 |
except UnicodeDecodeError:
|
371 |
continue
|
372 |
raise UnicodeDecodeError(f"์ง์๋๋ ์ธ์ฝ๋ฉ์ผ๋ก ํ์ผ์ ์ฝ์ ์ ์์ต๋๋ค ({', '.join(encodings)})")
|
|
|
374 |
except Exception as e:
|
375 |
return f"ํ์ผ ์ฝ๊ธฐ ์ค๋ฅ: {str(e)}", "error"
|
376 |
|
377 |
+
|
378 |
+
|
379 |
+
|
380 |
# ํ์ผ ์
๋ก๋ ์ด๋ฒคํธ ํธ๋ค๋ง ์์
|
381 |
def init_msg():
|
382 |
return "ํ์ผ์ ๋ถ์ํ๊ณ ์์ต๋๋ค..."
|
|
|
392 |
--text-color: #333333;
|
393 |
--shadow-color: rgba(0, 0, 0, 0.1);
|
394 |
}
|
|
|
395 |
body {
|
396 |
background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
|
397 |
min-height: 100vh;
|
398 |
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
|
399 |
}
|
|
|
400 |
.container {
|
401 |
transform-style: preserve-3d;
|
402 |
perspective: 1000px;
|
403 |
}
|
|
|
404 |
.chatbot {
|
405 |
background: var(--card-background);
|
406 |
border-radius: 20px;
|
|
|
411 |
transition: transform 0.3s ease;
|
412 |
backdrop-filter: blur(10px);
|
413 |
}
|
|
|
414 |
.chatbot:hover {
|
415 |
transform: translateZ(10px);
|
416 |
}
|
|
|
417 |
/* ๋ฉ์์ง ์
๋ ฅ ์์ญ */
|
418 |
.input-area {
|
419 |
background: var(--card-background);
|
|
|
429 |
align-items: center;
|
430 |
gap: 10px;
|
431 |
}
|
|
|
432 |
.input-area:hover {
|
433 |
transform: translateZ(5px);
|
434 |
}
|
|
|
435 |
/* ๋ฒํผ ์คํ์ผ */
|
436 |
.custom-button {
|
437 |
background: linear-gradient(145deg, var(--primary-color), var(--secondary-color));
|
|
|
447 |
0 4px 6px var(--shadow-color),
|
448 |
0 1px 3px var(--shadow-color);
|
449 |
}
|
|
|
450 |
.custom-button:hover {
|
451 |
transform: translateZ(5px) translateY(-2px);
|
452 |
box-shadow:
|
453 |
0 7px 14px var(--shadow-color),
|
454 |
0 3px 6px var(--shadow-color);
|
455 |
}
|
|
|
456 |
/* ํ์ผ ์
๋ก๋ ๋ฒํผ */
|
457 |
.file-upload-icon {
|
458 |
background: linear-gradient(145deg, #64b5f6, #42a5f5);
|
|
|
468 |
transition: all 0.3s ease;
|
469 |
box-shadow: 0 2px 5px rgba(0,0,0,0.1);
|
470 |
}
|
|
|
471 |
.file-upload-icon:hover {
|
472 |
transform: translateY(-2px);
|
473 |
box-shadow: 0 4px 8px rgba(0,0,0,0.2);
|
474 |
}
|
|
|
475 |
/* ํ์ผ ์
๋ก๋ ๋ฒํผ ๋ด๋ถ ์์ ์คํ์ผ๋ง */
|
476 |
.file-upload-icon > .wrap {
|
477 |
display: flex !important;
|
|
|
480 |
width: 100%;
|
481 |
height: 100%;
|
482 |
}
|
|
|
483 |
.file-upload-icon > .wrap > p {
|
484 |
display: none !important;
|
485 |
}
|
|
|
486 |
.file-upload-icon > .wrap::before {
|
487 |
content: "๐";
|
488 |
font-size: 2em;
|
489 |
display: block;
|
490 |
}
|
|
|
491 |
/* ๋ฉ์์ง ์คํ์ผ */
|
492 |
.message {
|
493 |
background: var(--card-background);
|
|
|
500 |
transform: translateZ(0);
|
501 |
transition: all 0.3s ease;
|
502 |
}
|
|
|
503 |
.message:hover {
|
504 |
transform: translateZ(5px);
|
505 |
}
|
|
|
506 |
.chat-container {
|
507 |
height: 600px !important;
|
508 |
margin-bottom: 10px;
|
509 |
}
|
|
|
510 |
.input-container {
|
511 |
height: 70px !important;
|
512 |
display: flex;
|
|
|
514 |
gap: 10px;
|
515 |
margin-top: 5px;
|
516 |
}
|
|
|
517 |
.input-textbox {
|
518 |
height: 70px !important;
|
519 |
border-radius: 8px !important;
|
|
|
522 |
display: flex !important;
|
523 |
align-items: flex-start !important; /* ํ
์คํธ ์
๋ ฅ ์์น๋ฅผ ์๋ก ์กฐ์ */
|
524 |
}
|
|
|
525 |
.input-textbox textarea {
|
526 |
padding-top: 5px !important; /* ํ
์คํธ ์๋จ ์ฌ๋ฐฑ ์กฐ์ */
|
527 |
}
|
|
|
528 |
.send-button {
|
529 |
height: 70px !important;
|
530 |
min-width: 70px !important;
|
531 |
font-size: 1.1em !important;
|
532 |
}
|
|
|
533 |
/* ์ค์ ํจ๋ ๊ธฐ๋ณธ ์คํ์ผ */
|
534 |
.settings-panel {
|
535 |
padding: 20px;
|
|
|
537 |
}
|
538 |
"""
|
539 |
|
540 |
+
# GPU ๋ฉ๋ชจ๋ฆฌ ๊ด๋ฆฌ ํจ์ ์์
|
541 |
+
def clear_cuda_memory():
|
542 |
+
if hasattr(torch.cuda, 'empty_cache'):
|
543 |
+
with torch.cuda.device('cuda'):
|
544 |
+
torch.cuda.empty_cache()
|
545 |
+
|
546 |
+
# ๋ชจ๋ธ ๋ก๋ ํจ์ ์์
|
547 |
+
@spaces.GPU
|
548 |
+
def load_model():
|
549 |
+
try:
|
550 |
+
model = AutoModelForCausalLM.from_pretrained(
|
551 |
+
MODEL_ID,
|
552 |
+
torch_dtype=torch.bfloat16,
|
553 |
+
device_map="auto",
|
554 |
+
)
|
555 |
+
return model
|
556 |
+
except Exception as e:
|
557 |
+
print(f"๋ชจ๋ธ ๋ก๋ ์ค๋ฅ: {str(e)}")
|
558 |
+
raise
|
559 |
+
|
560 |
@spaces.GPU
|
561 |
def stream_chat(message: str, history: list, uploaded_file, temperature: float, max_new_tokens: int, top_p: float, top_k: int, penalty: float):
|
562 |
+
global model, current_file_context
|
563 |
+
|
564 |
try:
|
565 |
+
if model is None:
|
566 |
+
model = load_model()
|
567 |
+
|
568 |
print(f'message is - {message}')
|
569 |
print(f'history is - {history}')
|
570 |
+
|
571 |
# ํ์ผ ์
๋ก๋ ์ฒ๋ฆฌ
|
572 |
file_context = ""
|
573 |
if uploaded_file and message == "ํ์ผ์ ๋ถ์ํ๊ณ ์์ต๋๋ค...":
|
|
|
576 |
if content:
|
577 |
file_analysis = analyze_file_content(content, file_type)
|
578 |
file_context = f"\n\n๐ ํ์ผ ๋ถ์ ๊ฒฐ๊ณผ:\n{file_analysis}\n\nํ์ผ ๋ด์ฉ:\n```\n{content}\n```"
|
579 |
+
current_file_context = file_context # ํ์ผ ์ปจํ
์คํธ ์ ์ฅ
|
580 |
message = "์
๋ก๋๋ ํ์ผ์ ๋ถ์ํด์ฃผ์ธ์."
|
581 |
except Exception as e:
|
582 |
print(f"ํ์ผ ๋ถ์ ์ค๋ฅ: {str(e)}")
|
583 |
file_context = f"\n\nโ ํ์ผ ๋ถ์ ์ค ์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค: {str(e)}"
|
584 |
+
elif current_file_context: # ์ ์ฅ๋ ํ์ผ ์ปจํ
์คํธ๊ฐ ์์ผ๋ฉด ์ฌ์ฉ
|
585 |
+
file_context = current_file_context
|
586 |
+
|
587 |
+
|
588 |
+
# ๋ฉ๋ชจ๋ฆฌ ์ฌ์ฉ๋ ๋ชจ๋ํฐ๋ง
|
589 |
+
if torch.cuda.is_available():
|
590 |
+
print(f"CUDA ๋ฉ๋ชจ๋ฆฌ ์ฌ์ฉ๋: {torch.cuda.memory_allocated() / 1024**2:.2f} MB")
|
591 |
+
|
592 |
+
# ๋ํ ํ์คํ ๋ฆฌ๊ฐ ๋๋ฌด ๊ธธ๋ฉด ์๋ผ๋ด๊ธฐ
|
593 |
+
max_history_length = 10 # ์ต๋ ํ์คํ ๋ฆฌ ๊ธธ์ด ์ค์
|
594 |
+
if len(history) > max_history_length:
|
595 |
+
history = history[-max_history_length:]
|
596 |
|
597 |
# ๊ด๋ จ ์ปจํ
์คํธ ์ฐพ๊ธฐ
|
598 |
try:
|
|
|
616 |
final_message = file_context + wiki_context + "\nํ์ฌ ์ง๋ฌธ: " + message
|
617 |
conversation.append({"role": "user", "content": final_message})
|
618 |
|
619 |
+
# ํ ํฐ ์ ์ ํ
|
620 |
input_ids = tokenizer.apply_chat_template(conversation, tokenize=False, add_generation_prompt=True)
|
621 |
+
max_length = 4096 # ๋๋ ๋ชจ๋ธ์ ์ต๋ ์ปจํ
์คํธ ๊ธธ์ด
|
622 |
+
if len(input_ids.split()) > max_length:
|
623 |
+
# ์ปจํ
์คํธ๊ฐ ๋๋ฌด ๊ธธ๋ฉด ์๋ผ๋ด๊ธฐ
|
624 |
+
input_ids = " ".join(input_ids.split()[-max_length:])
|
625 |
+
|
626 |
+
inputs = tokenizer(input_ids, return_tensors="pt").to("cuda")
|
627 |
+
|
628 |
+
# ๋ฉ๋ชจ๋ฆฌ ์ฌ์ฉ๋ ์ฒดํฌ
|
629 |
+
if torch.cuda.is_available():
|
630 |
+
print(f"์
๋ ฅ ํ
์ ์์ฑ ํ CUDA ๋ฉ๋ชจ๋ฆฌ: {torch.cuda.memory_allocated() / 1024**2:.2f} MB")
|
631 |
|
632 |
streamer = TextIteratorStreamer(tokenizer, timeout=10., skip_prompt=True, skip_special_tokens=True)
|
633 |
|
|
|
637 |
top_k=top_k,
|
638 |
top_p=top_p,
|
639 |
repetition_penalty=penalty,
|
640 |
+
max_new_tokens=min(max_new_tokens, 2048), # ์ต๋ ํ ํฐ ์ ์ ํ
|
641 |
do_sample=True,
|
642 |
temperature=temperature,
|
643 |
eos_token_id=[255001],
|
644 |
)
|
645 |
|
646 |
+
# ์์ฑ ์์ ์ ๋ฉ๋ชจ๋ฆฌ ์ ๋ฆฌ
|
647 |
+
clear_cuda_memory()
|
648 |
+
|
649 |
thread = Thread(target=model.generate, kwargs=generate_kwargs)
|
650 |
thread.start()
|
651 |
|
|
|
654 |
buffer += new_text
|
655 |
yield "", history + [[message, buffer]]
|
656 |
|
657 |
+
# ์์ฑ ์๋ฃ ํ ๋ฉ๋ชจ๋ฆฌ ์ ๋ฆฌ
|
658 |
+
clear_cuda_memory()
|
659 |
+
|
660 |
except Exception as e:
|
661 |
error_message = f"์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค: {str(e)}"
|
662 |
print(f"Stream chat ์ค๋ฅ: {error_message}")
|
663 |
+
# ์ค๋ฅ ๋ฐ์ ์์๋ ๋ฉ๋ชจ๋ฆฌ ์ ๋ฆฌ
|
664 |
+
clear_cuda_memory()
|
665 |
yield "", history + [[message, error_message]]
|
666 |
|
667 |
|
668 |
|
669 |
def create_demo():
|
670 |
with gr.Blocks(css=CSS) as demo:
|
671 |
+
with gr.Column(elem_classes="markdown-style"):
|
672 |
+
gr.Markdown("""
|
673 |
+
# ๐ค OnDevice AI RAG
|
674 |
+
#### ๐ RAG: ๋ฐ์ดํฐ ๋ถ์/ํ์ต ํ์ผ ์
๋ก๋(TXT, CSV, PDF, Parquet ํ์ผ)
|
675 |
+
""")
|
676 |
+
|
677 |
chatbot = gr.Chatbot(
|
678 |
value=[],
|
679 |
height=600,
|
|
|
692 |
show_label=False
|
693 |
)
|
694 |
|
695 |
+
with gr.Column(scale=3):
|
696 |
msg = gr.Textbox(
|
697 |
show_label=False,
|
698 |
placeholder="๋ฉ์์ง๋ฅผ ์
๋ ฅํ์ธ์... ๐ญ",
|
|
|
707 |
elem_classes="send-button custom-button",
|
708 |
scale=1
|
709 |
)
|
710 |
+
|
711 |
+
with gr.Column(scale=1, min_width=70):
|
712 |
+
clear = gr.Button(
|
713 |
+
"Clear",
|
714 |
+
elem_classes="clear-button custom-button",
|
715 |
+
scale=1
|
716 |
+
)
|
717 |
|
718 |
with gr.Accordion("๐ฎ ๊ณ ๊ธ ์ค์ ", open=False):
|
719 |
with gr.Row():
|
|
|
750 |
inputs=msg
|
751 |
)
|
752 |
|
753 |
+
# Clear ํจ์ ์ ์
|
754 |
+
def clear_conversation():
|
755 |
+
global current_file_context
|
756 |
+
current_file_context = None
|
757 |
+
return [], None, "์๋ก์ด ๋ํ๋ฅผ ์์ํ์ธ์..."
|
758 |
+
|
759 |
# ์ด๋ฒคํธ ๋ฐ์ธ๋ฉ
|
760 |
msg.submit(
|
761 |
stream_chat,
|
|
|
780 |
queue=True
|
781 |
)
|
782 |
|
783 |
+
# Clear ๋ฒํผ ์ด๋ฒคํธ ๋ฐ์ธ๋ฉ
|
784 |
+
clear.click(
|
785 |
+
fn=clear_conversation,
|
786 |
+
outputs=[chatbot, file_upload, msg],
|
787 |
+
queue=False
|
788 |
+
)
|
789 |
+
|
790 |
return demo
|
791 |
|
792 |
if __name__ == "__main__":
|