Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,66 +1,31 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
import torch
|
| 3 |
-
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 4 |
-
from peft import PeftModel
|
| 5 |
|
| 6 |
-
|
| 7 |
-
base_id = "Qwen/Qwen2.5-7B-Instruct"
|
| 8 |
-
adapter_id = "epinfomax/BizFlow-Summarizer-Ko"
|
| 9 |
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 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 |
-
|
| 25 |
-
messages =
|
| 26 |
-
|
| 27 |
-
# Qwen ์ฑํ
ํ
ํ๋ฆฟ ์ ์ฉ
|
| 28 |
-
input_text = tokenizer.apply_chat_template(
|
| 29 |
-
messages,
|
| 30 |
-
tokenize=False,
|
| 31 |
-
add_generation_prompt=True
|
| 32 |
-
)
|
| 33 |
-
|
| 34 |
-
# ์
๋ ฅ ํ ํฐํ
|
| 35 |
-
inputs = tokenizer([input_text], return_tensors="pt").to(device)
|
| 36 |
-
|
| 37 |
-
# ์ถ๋ก (์์ฝ ์์ฑ)
|
| 38 |
-
with torch.no_grad():
|
| 39 |
-
outputs = model.generate(
|
| 40 |
-
**inputs,
|
| 41 |
-
max_new_tokens=512, # ์์ฑํ ์ต๋ ๊ธธ์ด
|
| 42 |
-
temperature=0.3, # ๊ฐ์ด ๋ฎ์์๋ก ์ฌ์ค์ ์ธ ์์ฝ
|
| 43 |
-
repetition_penalty=1.1
|
| 44 |
-
)
|
| 45 |
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
|
|
|
| 49 |
|
| 50 |
-
|
| 51 |
-
iface = gr.Interface(
|
| 52 |
fn=summarize,
|
| 53 |
-
inputs=gr.Textbox(
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
),
|
| 58 |
-
outputs=gr.Textbox(label="์์ฝ ๊ฒฐ๊ณผ"),
|
| 59 |
-
title="BizFlow ๋ฌธ์ ์์ฝ ์์ด์ ํธ",
|
| 60 |
-
description="Qwen2.5-7B ๋ชจ๋ธ์ ํ์ธํ๋ํ์ฌ ๋ง๋ ํ๊ตญ์ด ์ ๋ฌธ ์์ฝ๊ธฐ์
๋๋ค.",
|
| 61 |
-
examples=["์ผ์ฑ์ ์๊ฐ ์ค๋ ์ปจํผ๋ฐ์ค์ฝ์ ํตํด ์ง๋ํด 4๋ถ๊ธฐ ํ์ ์ค์ ์ ๋ฐํํ๋ค. ์ฐ๊ฒฐ ๊ธฐ์ค ๋งค์ถ์ 67์กฐ 7800์ต ์์ผ๋ก ์ ๋
๋๊ธฐ ๋๋น 3.8% ๊ฐ์ํ์ผ๋, ์์
์ด์ต์ 2์กฐ 8200์ต ์์ผ๋ก..."]
|
| 62 |
)
|
| 63 |
|
| 64 |
-
|
| 65 |
-
if __name__ == "__main__":
|
| 66 |
-
iface.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from transformers import pipeline, AutoTokenizer, AutoModelForCausalLM
|
| 3 |
import torch
|
|
|
|
|
|
|
| 4 |
|
| 5 |
+
model_id = "epinfomax/BizFlow-Summarizer-Ko"
|
|
|
|
|
|
|
| 6 |
|
| 7 |
+
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
| 8 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 9 |
+
model_id,
|
| 10 |
+
torch_dtype=torch.float16,
|
| 11 |
+
device_map="auto"
|
| 12 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
def summarize(text):
|
| 15 |
+
prompt = f"๋ค์ ๊ธ์ ์์ฝํด์ฃผ์ธ์:\n\n{text}"
|
| 16 |
+
messages = [{"role": "user", "content": prompt}]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
+
input_ids = tokenizer.apply_chat_template(messages, return_tensors="pt").to(model.device)
|
| 19 |
+
outputs = model.generate(input_ids, max_new_tokens=512, do_sample=True, temperature=0.7)
|
| 20 |
+
response = tokenizer.decode(outputs[0][input_ids.shape[1]:], skip_special_tokens=True)
|
| 21 |
+
return response
|
| 22 |
|
| 23 |
+
demo = gr.Interface(
|
|
|
|
| 24 |
fn=summarize,
|
| 25 |
+
inputs=gr.Textbox(lines=10, label="์๋ฌธ", placeholder="์์ฝํ ํ
์คํธ๋ฅผ ์
๋ ฅํ์ธ์..."),
|
| 26 |
+
outputs=gr.Textbox(lines=5, label="์์ฝ"),
|
| 27 |
+
title="BizFlow Summarizer Ko",
|
| 28 |
+
description="ํ๊ตญ์ด ๋ด์ค/๋ฌธ์ ์์ฝ ๋ชจ๋ธ"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
)
|
| 30 |
|
| 31 |
+
demo.launch()
|
|
|
|
|
|