Spaces:
Running
on
Zero
Running
on
Zero
cutechicken
commited on
Commit
โข
e468070
1
Parent(s):
0126cba
Update app.py
Browse files
app.py
CHANGED
@@ -281,67 +281,6 @@ def init_msg():
|
|
281 |
return "ํ์ผ์ ๋ถ์ํ๊ณ ์์ต๋๋ค..."
|
282 |
|
283 |
|
284 |
-
|
285 |
-
@spaces.GPU
|
286 |
-
def stream_chat(message: str, history: list, uploaded_file, temperature: float, max_new_tokens: int, top_p: float, top_k: int, penalty: float):
|
287 |
-
try:
|
288 |
-
print(f'message is - {message}')
|
289 |
-
print(f'history is - {history}')
|
290 |
-
|
291 |
-
# ํ์ผ ์
๋ก๋ ์ฒ๋ฆฌ
|
292 |
-
file_context = ""
|
293 |
-
if uploaded_file:
|
294 |
-
content, file_type = read_uploaded_file(uploaded_file)
|
295 |
-
if content:
|
296 |
-
file_context = f"\n\n์
๋ก๋๋ ํ์ผ ๋ด์ฉ:\n```\n{content}\n```"
|
297 |
-
|
298 |
-
# ๊ด๋ จ ์ปจํ
์คํธ ์ฐพ๊ธฐ
|
299 |
-
relevant_contexts = find_relevant_context(message)
|
300 |
-
wiki_context = "\n\n๊ด๋ จ ์ํคํผ๋์ ์ ๋ณด:\n"
|
301 |
-
for ctx in relevant_contexts:
|
302 |
-
wiki_context += f"Q: {ctx['question']}\nA: {ctx['answer']}\n์ ์ฌ๋: {ctx['similarity']:.3f}\n\n"
|
303 |
-
|
304 |
-
# ๋ํ ํ์คํ ๋ฆฌ ๊ตฌ์ฑ
|
305 |
-
conversation = []
|
306 |
-
for prompt, answer in history:
|
307 |
-
conversation.extend([
|
308 |
-
{"role": "user", "content": prompt},
|
309 |
-
{"role": "assistant", "content": answer}
|
310 |
-
])
|
311 |
-
|
312 |
-
# ์ต์ข
ํ๋กฌํํธ ๊ตฌ์ฑ
|
313 |
-
final_message = file_context + wiki_context + "\nํ์ฌ ์ง๋ฌธ: " + message
|
314 |
-
conversation.append({"role": "user", "content": final_message})
|
315 |
-
|
316 |
-
input_ids = tokenizer.apply_chat_template(conversation, tokenize=False, add_generation_prompt=True)
|
317 |
-
inputs = tokenizer(input_ids, return_tensors="pt").to(0)
|
318 |
-
|
319 |
-
streamer = TextIteratorStreamer(tokenizer, timeout=10., skip_prompt=True, skip_special_tokens=True)
|
320 |
-
|
321 |
-
generate_kwargs = dict(
|
322 |
-
inputs,
|
323 |
-
streamer=streamer,
|
324 |
-
top_k=top_k,
|
325 |
-
top_p=top_p,
|
326 |
-
repetition_penalty=penalty,
|
327 |
-
max_new_tokens=max_new_tokens,
|
328 |
-
do_sample=True,
|
329 |
-
temperature=temperature,
|
330 |
-
eos_token_id=[255001],
|
331 |
-
)
|
332 |
-
|
333 |
-
thread = Thread(target=model.generate, kwargs=generate_kwargs)
|
334 |
-
thread.start()
|
335 |
-
|
336 |
-
buffer = ""
|
337 |
-
for new_text in streamer:
|
338 |
-
buffer += new_text
|
339 |
-
yield "", history + [[message, buffer]]
|
340 |
-
|
341 |
-
except Exception as e:
|
342 |
-
error_message = f"์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค: {str(e)}"
|
343 |
-
yield "", history + [[message, error_message]]
|
344 |
-
|
345 |
CSS = """
|
346 |
/* 3D ์คํ์ผ CSS */
|
347 |
:root {
|
@@ -518,6 +457,80 @@ body {
|
|
518 |
}
|
519 |
"""
|
520 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
521 |
def create_demo():
|
522 |
with gr.Blocks(css=CSS) as demo:
|
523 |
with gr.Column():
|
@@ -554,6 +567,23 @@ def create_demo():
|
|
554 |
elem_classes="send-button custom-button",
|
555 |
scale=1
|
556 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
557 |
|
558 |
with gr.Accordion("๐ฎ ๊ณ ๊ธ ์ค์ ", open=False):
|
559 |
with gr.Row():
|
@@ -580,6 +610,9 @@ def create_demo():
|
|
580 |
label="๋ฐ๋ณต ์ต์ ๐"
|
581 |
)
|
582 |
|
|
|
|
|
|
|
583 |
# Examples ์์น ์์
|
584 |
gr.Examples(
|
585 |
examples=[
|
|
|
281 |
return "ํ์ผ์ ๋ถ์ํ๊ณ ์์ต๋๋ค..."
|
282 |
|
283 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
284 |
CSS = """
|
285 |
/* 3D ์คํ์ผ CSS */
|
286 |
:root {
|
|
|
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 == "ํ์ผ์ ๋ถ์ํ๊ณ ์์ต๋๋ค...":
|
469 |
+
try:
|
470 |
+
content, file_type = read_uploaded_file(uploaded_file)
|
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:
|
481 |
+
relevant_contexts = find_relevant_context(message)
|
482 |
+
wiki_context = "\n\n๊ด๋ จ ์ํคํผ๋์ ์ ๋ณด:\n"
|
483 |
+
for ctx in relevant_contexts:
|
484 |
+
wiki_context += f"Q: {ctx['question']}\nA: {ctx['answer']}\n์ ์ฌ๋: {ctx['similarity']:.3f}\n\n"
|
485 |
+
except Exception as e:
|
486 |
+
print(f"์ปจํ
์คํธ ๊ฒ์ ์ค๋ฅ: {str(e)}")
|
487 |
+
wiki_context = ""
|
488 |
+
|
489 |
+
# ๋ํ ํ์คํ ๋ฆฌ ๊ตฌ์ฑ
|
490 |
+
conversation = []
|
491 |
+
for prompt, answer in history:
|
492 |
+
conversation.extend([
|
493 |
+
{"role": "user", "content": prompt},
|
494 |
+
{"role": "assistant", "content": answer}
|
495 |
+
])
|
496 |
+
|
497 |
+
# ์ต์ข
ํ๋กฌํํธ ๊ตฌ์ฑ
|
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 |
+
inputs = tokenizer(input_ids, return_tensors="pt").to(0)
|
504 |
+
|
505 |
+
streamer = TextIteratorStreamer(tokenizer, timeout=10., skip_prompt=True, skip_special_tokens=True)
|
506 |
+
|
507 |
+
generate_kwargs = dict(
|
508 |
+
inputs,
|
509 |
+
streamer=streamer,
|
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 |
+
|
522 |
+
buffer = ""
|
523 |
+
for new_text in streamer:
|
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 |
with gr.Column():
|
|
|
567 |
elem_classes="send-button custom-button",
|
568 |
scale=1
|
569 |
)
|
570 |
+
|
571 |
+
# ์ด๋ฒคํธ ๋ฐ์ธ๋ฉ ์์
|
572 |
+
def init_msg():
|
573 |
+
return "ํ์ผ์ ๋ถ์ํ๊ณ ์์ต๋๋ค..."
|
574 |
+
|
575 |
+
file_upload.change(
|
576 |
+
fn=init_msg,
|
577 |
+
outputs=msg,
|
578 |
+
queue=False
|
579 |
+
).then(
|
580 |
+
fn=stream_chat,
|
581 |
+
inputs=[msg, chatbot, file_upload, temperature, max_new_tokens, top_p, top_k, penalty],
|
582 |
+
outputs=[msg, chatbot],
|
583 |
+
queue=True
|
584 |
+
)
|
585 |
+
|
586 |
+
|
587 |
|
588 |
with gr.Accordion("๐ฎ ๊ณ ๊ธ ์ค์ ", open=False):
|
589 |
with gr.Row():
|
|
|
610 |
label="๋ฐ๋ณต ์ต์ ๐"
|
611 |
)
|
612 |
|
613 |
+
|
614 |
+
|
615 |
+
|
616 |
# Examples ์์น ์์
|
617 |
gr.Examples(
|
618 |
examples=[
|