Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,31 +1,79 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from transformers import pipeline, AutoTokenizer, AutoModelForCausalLM
|
| 3 |
import torch
|
|
|
|
|
|
|
| 4 |
|
| 5 |
-
|
|
|
|
|
|
|
| 6 |
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
def summarize(text):
|
| 15 |
-
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
response = tokenizer.decode(outputs[0][input_ids.shape[1]:], skip_special_tokens=True)
|
| 21 |
-
return response
|
| 22 |
|
| 23 |
-
|
|
|
|
| 24 |
fn=summarize,
|
| 25 |
-
inputs=gr.Textbox(
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
)
|
| 30 |
|
| 31 |
-
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
import torch
|
| 3 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 4 |
+
from peft import PeftModel
|
| 5 |
|
| 6 |
+
# 1. ๋ชจ๋ธ ID ์ค์
|
| 7 |
+
base_id = "Qwen/Qwen2.5-7B-Instruct"
|
| 8 |
+
adapter_id = "epinfomax/BizFlow-Summarizer-Ko"
|
| 9 |
|
| 10 |
+
# 2. ํ๋์จ์ด ์ค์ (GPU/CPU ์๋ ๊ฐ์ง)
|
| 11 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 12 |
+
dtype = torch.float16 if device == "cuda" else torch.float32
|
| 13 |
+
|
| 14 |
+
print(f"๐ ๋ชจ๋ธ ๋ก๋ฉ ์ค... (Device: {device})")
|
| 15 |
+
|
| 16 |
+
# 3. ๋ชจ๋ธ ๋ก๋
|
| 17 |
+
tokenizer = AutoTokenizer.from_pretrained(base_id)
|
| 18 |
+
model = AutoModelForCausalLM.from_pretrained(base_id, torch_dtype=dtype)
|
| 19 |
+
model = PeftModel.from_pretrained(model, adapter_id)
|
| 20 |
+
model.to(device)
|
| 21 |
+
model.eval()
|
| 22 |
|
| 23 |
def summarize(text):
|
| 24 |
+
# โ
์์ ๋ ๋ถ๋ถ: messages ๋ฆฌ์คํธ ๊ตฌ์กฐํ
|
| 25 |
+
# ์์คํ
ํ๋กฌํํธ(์ง์์ฌํญ)์ ์ฌ์ฉ์ ์
๋ ฅ(text)์ ๋์
๋๋ฆฌ ๋ฆฌ์คํธ๋ก ๋ง๋ญ๋๋ค.
|
| 26 |
+
messages = [
|
| 27 |
+
{
|
| 28 |
+
"role": "system",
|
| 29 |
+
"content": "๋น์ ์ ๋น์ฆ๋์ค ๋ฌธ์๋ฅผ ์ ๋ฌธ์ ์ผ๋ก ์์ฝํ๋ AI ์ด์์คํดํธ์
๋๋ค. ํต์ฌ ๋ด์ฉ์ ๋ช
ํํ๊ฒ ์์ฝํด ์ฃผ์ธ์."
|
| 30 |
+
},
|
| 31 |
+
{
|
| 32 |
+
"role": "user",
|
| 33 |
+
"content": text
|
| 34 |
+
}
|
| 35 |
+
]
|
| 36 |
+
|
| 37 |
+
# ์
๋ ฅ ํ
์คํธ ํฌ๋งทํ
(Chat Template ์ ์ฉ)
|
| 38 |
+
input_text = tokenizer.apply_chat_template(
|
| 39 |
+
messages,
|
| 40 |
+
tokenize=False,
|
| 41 |
+
add_generation_prompt=True
|
| 42 |
+
)
|
| 43 |
+
|
| 44 |
+
# ํ ํฌ๋์ด์ง ๋ฐ GPU ์ด๋
|
| 45 |
+
inputs = tokenizer([input_text], return_tensors="pt").to(device)
|
| 46 |
+
|
| 47 |
+
# ์ถ๋ก
|
| 48 |
+
with torch.no_grad():
|
| 49 |
+
outputs = model.generate(
|
| 50 |
+
**inputs,
|
| 51 |
+
max_new_tokens=512,
|
| 52 |
+
temperature=0.3,
|
| 53 |
+
repetition_penalty=1.1
|
| 54 |
+
)
|
| 55 |
+
|
| 56 |
+
# ๊ฒฐ๊ณผ ๋์ฝ๋ฉ (์
๋ ฅ ํ๋กฌํํธ ์ ์ธ)
|
| 57 |
+
generated_tokens = outputs[:, inputs.input_ids.shape[1]:]
|
| 58 |
+
result = tokenizer.batch_decode(generated_tokens, skip_special_tokens=True)
|
| 59 |
|
| 60 |
+
# batch_decode๋ ๋ฆฌ์คํธ๋ฅผ ๋ฐํํ๋ฏ๋ก ์ฒซ ๋ฒ์งธ ์์๋ง ๋ฐํํ์ฌ ๊น๋ํ๊ฒ ์ถ๋ ฅ
|
| 61 |
+
return result[0]
|
|
|
|
|
|
|
| 62 |
|
| 63 |
+
# 4. ์น ์ธํฐํ์ด์ค ์ ์
|
| 64 |
+
iface = gr.Interface(
|
| 65 |
fn=summarize,
|
| 66 |
+
inputs=gr.Textbox(
|
| 67 |
+
lines=15,
|
| 68 |
+
placeholder="์์ฝํ ๋ฌธ์๋ฅผ ์ฌ๊ธฐ์ ๋ถ์ฌ๋ฃ์ผ์ธ์...",
|
| 69 |
+
label="์
๋ ฅ ๋ฌธ์"
|
| 70 |
+
),
|
| 71 |
+
outputs=gr.Textbox(label="์์ฝ ๊ฒฐ๊ณผ"),
|
| 72 |
+
title="BizFlow ๋ฌธ์ ์์ฝ๊ธฐ",
|
| 73 |
+
description="Qwen2.5-7B + ํ์ธํ๋(LoRA) ๋ชจ๋ธ ํ
์คํธ ๋ฐ๋ชจ์
๋๋ค.",
|
| 74 |
+
examples=[["์ผ์ฑ์ ์๊ฐ ์ค๋ ์ปจํผ๋ฐ์ค์ฝ์ ํตํด ์ง๋ํด 4๋ถ๊ธฐ ํ์ ์ค์ ์ ๋ฐํํ๋ค. ์ฐ๊ฒฐ ๊ธฐ์ค ๋งค์ถ์ 67์กฐ 7800์ต ์์ผ๋ก ์ ๋
๋๊ธฐ ๋๋น 3.8% ๊ฐ์ํ์ผ๋, ์์
์ด์ต์ 2์กฐ 8200์ต ์์ผ๋ก..."]]
|
| 75 |
)
|
| 76 |
|
| 77 |
+
# ์ฑ ์คํ
|
| 78 |
+
if __name__ == "__main__":
|
| 79 |
+
iface.launch()
|