Spaces:
Sleeping
Sleeping
Commit ·
9cc8c20
1
Parent(s): 32312b0
fix: resolve sumy ObjectDocumentModel and ZeroGPU CUDA wrapper initialization
Browse files- app.py +5 -0
- extractive.py +19 -16
- requirements.txt +1 -3
- slm.py +8 -11
app.py
CHANGED
|
@@ -1,3 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
import time
|
| 3 |
import gradio as gr
|
|
|
|
| 1 |
+
try:
|
| 2 |
+
import spaces
|
| 3 |
+
except ImportError:
|
| 4 |
+
pass
|
| 5 |
+
|
| 6 |
import os
|
| 7 |
import time
|
| 8 |
import gradio as gr
|
extractive.py
CHANGED
|
@@ -59,6 +59,13 @@ def split_sentences(text: str, is_korean: bool) -> List[str]:
|
|
| 59 |
raw = re.split(r"(?<=[.!?])\s+", clean_text)
|
| 60 |
return [s.strip() for s in raw if s.strip()]
|
| 61 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
def lexrank_summary(text: str, sentence_count: int = 3) -> Tuple[str, float]:
|
| 63 |
t0 = time.perf_counter()
|
| 64 |
is_ko = is_korean_text(text)
|
|
@@ -68,38 +75,34 @@ def lexrank_summary(text: str, sentence_count: int = 3) -> Tuple[str, float]:
|
|
| 68 |
elapsed_ms = (time.perf_counter() - t0) * 1000
|
| 69 |
return " ".join(sentences), elapsed_ms
|
| 70 |
|
| 71 |
-
from sumy.models.dom
|
| 72 |
-
from sumy.models.dom._paragraph import Paragraph
|
| 73 |
-
from sumy.models.dom._document import Document
|
| 74 |
from sumy.summarizers.lex_rank import LexRankSummarizer
|
| 75 |
|
| 76 |
-
sumy_sentences = []
|
| 77 |
if is_ko:
|
| 78 |
kiwi = get_kiwi()
|
| 79 |
-
for
|
| 80 |
-
tokens = [token.form for token in kiwi.tokenize(s)]
|
| 81 |
-
sumy_sentences.append(Sentence(tokens, None))
|
| 82 |
else:
|
| 83 |
-
|
| 84 |
-
tokens = re.findall(r"\w+", s.lower())
|
| 85 |
-
sumy_sentences.append(Sentence(tokens, None))
|
| 86 |
|
| 87 |
-
|
|
|
|
| 88 |
summarizer = LexRankSummarizer()
|
| 89 |
selected_sumy = summarizer(doc, sentence_count)
|
| 90 |
|
| 91 |
-
# 선택된 sumy 문장의 원문 인덱스 추적
|
| 92 |
selected_texts = []
|
| 93 |
for s_obj in selected_sumy:
|
| 94 |
-
|
| 95 |
-
|
|
|
|
|
|
|
|
|
|
| 96 |
|
| 97 |
-
# 원문 순서대로 정렬 반환
|
| 98 |
selected_texts.sort(key=lambda x: x[0])
|
| 99 |
-
result = " ".join([t[1] for t in selected_texts])
|
| 100 |
elapsed_ms = (time.perf_counter() - t0) * 1000
|
| 101 |
return result, elapsed_ms
|
| 102 |
|
|
|
|
| 103 |
def embedding_summary(text: str, sentence_count: int = 3, model_name: str = "intfloat/multilingual-e5-small") -> Tuple[str, float]:
|
| 104 |
t0 = time.perf_counter()
|
| 105 |
is_ko = is_korean_text(text)
|
|
|
|
| 59 |
raw = re.split(r"(?<=[.!?])\s+", clean_text)
|
| 60 |
return [s.strip() for s in raw if s.strip()]
|
| 61 |
|
| 62 |
+
class SimpleTokenizer:
|
| 63 |
+
def __init__(self, tokenize_fn):
|
| 64 |
+
self._tokenize_fn = tokenize_fn
|
| 65 |
+
|
| 66 |
+
def to_words(self, text: str):
|
| 67 |
+
return self._tokenize_fn(text)
|
| 68 |
+
|
| 69 |
def lexrank_summary(text: str, sentence_count: int = 3) -> Tuple[str, float]:
|
| 70 |
t0 = time.perf_counter()
|
| 71 |
is_ko = is_korean_text(text)
|
|
|
|
| 75 |
elapsed_ms = (time.perf_counter() - t0) * 1000
|
| 76 |
return " ".join(sentences), elapsed_ms
|
| 77 |
|
| 78 |
+
from sumy.models.dom import ObjectDocumentModel, Paragraph, Sentence
|
|
|
|
|
|
|
| 79 |
from sumy.summarizers.lex_rank import LexRankSummarizer
|
| 80 |
|
|
|
|
| 81 |
if is_ko:
|
| 82 |
kiwi = get_kiwi()
|
| 83 |
+
tok = SimpleTokenizer(lambda t: [token.form for token in kiwi.tokenize(t)])
|
|
|
|
|
|
|
| 84 |
else:
|
| 85 |
+
tok = SimpleTokenizer(lambda t: re.findall(r"\w+", t.lower()))
|
|
|
|
|
|
|
| 86 |
|
| 87 |
+
sumy_sentences = [Sentence(s, tok) for s in sentences]
|
| 88 |
+
doc = ObjectDocumentModel([Paragraph(sumy_sentences)])
|
| 89 |
summarizer = LexRankSummarizer()
|
| 90 |
selected_sumy = summarizer(doc, sentence_count)
|
| 91 |
|
|
|
|
| 92 |
selected_texts = []
|
| 93 |
for s_obj in selected_sumy:
|
| 94 |
+
try:
|
| 95 |
+
idx = sumy_sentences.index(s_obj)
|
| 96 |
+
selected_texts.append((idx, sentences[idx]))
|
| 97 |
+
except ValueError:
|
| 98 |
+
pass
|
| 99 |
|
|
|
|
| 100 |
selected_texts.sort(key=lambda x: x[0])
|
| 101 |
+
result = " ".join([t[1] for t in selected_texts]) if selected_texts else " ".join(sentences[:sentence_count])
|
| 102 |
elapsed_ms = (time.perf_counter() - t0) * 1000
|
| 103 |
return result, elapsed_ms
|
| 104 |
|
| 105 |
+
|
| 106 |
def embedding_summary(text: str, sentence_count: int = 3, model_name: str = "intfloat/multilingual-e5-small") -> Tuple[str, float]:
|
| 107 |
t0 = time.perf_counter()
|
| 108 |
is_ko = is_korean_text(text)
|
requirements.txt
CHANGED
|
@@ -1,6 +1,5 @@
|
|
| 1 |
# Web & Presentation
|
| 2 |
gradio>=5.20.0
|
| 3 |
-
spaces>=0.30.0
|
| 4 |
pyyaml>=6.0
|
| 5 |
|
| 6 |
# Document Ingestion
|
|
@@ -16,14 +15,13 @@ sentence-transformers>=2.2.0
|
|
| 16 |
scikit-learn>=1.3.0
|
| 17 |
|
| 18 |
# Core Deep Learning & SLM Inference
|
| 19 |
-
torch>=2.8.0
|
| 20 |
transformers>=4.45.0
|
| 21 |
accelerate>=0.34.0
|
| 22 |
|
| 23 |
# Quantization & Compression Support
|
| 24 |
bitsandbytes>=0.43.0
|
| 25 |
-
compressed-tensors>=0.8.0
|
| 26 |
optimum>=1.20.0
|
| 27 |
|
|
|
|
| 28 |
# Testing
|
| 29 |
pytest>=8.0.0
|
|
|
|
| 1 |
# Web & Presentation
|
| 2 |
gradio>=5.20.0
|
|
|
|
| 3 |
pyyaml>=6.0
|
| 4 |
|
| 5 |
# Document Ingestion
|
|
|
|
| 15 |
scikit-learn>=1.3.0
|
| 16 |
|
| 17 |
# Core Deep Learning & SLM Inference
|
|
|
|
| 18 |
transformers>=4.45.0
|
| 19 |
accelerate>=0.34.0
|
| 20 |
|
| 21 |
# Quantization & Compression Support
|
| 22 |
bitsandbytes>=0.43.0
|
|
|
|
| 23 |
optimum>=1.20.0
|
| 24 |
|
| 25 |
+
|
| 26 |
# Testing
|
| 27 |
pytest>=8.0.0
|
slm.py
CHANGED
|
@@ -51,21 +51,14 @@ LOADED_MODELS = {}
|
|
| 51 |
LOADED_PROCESSORS = {}
|
| 52 |
RESULT_CACHE: Dict[str, Tuple[str, dict]] = {}
|
| 53 |
|
| 54 |
-
def get_device_map():
|
| 55 |
-
if torch.cuda.is_available():
|
| 56 |
-
return "auto"
|
| 57 |
-
return "cpu"
|
| 58 |
-
|
| 59 |
def get_torch_dtype(dtype_str: str):
|
| 60 |
-
if not torch.cuda.is_available():
|
| 61 |
-
return torch.float32
|
| 62 |
if dtype_str == "bfloat16":
|
| 63 |
return torch.bfloat16
|
| 64 |
if dtype_str == "float16":
|
| 65 |
return torch.float16
|
| 66 |
return "auto"
|
| 67 |
|
| 68 |
-
# ZeroGPU 가이드: 모듈 레벨에서 활성 모델 초기화
|
| 69 |
def initialize_active_models():
|
| 70 |
for key in ACTIVE_MODEL_KEYS:
|
| 71 |
if key in LOADED_MODELS:
|
|
@@ -74,7 +67,6 @@ def initialize_active_models():
|
|
| 74 |
repo_id = cfg["repo"]
|
| 75 |
loader_type = cfg.get("loader", "image-text")
|
| 76 |
dtype = get_torch_dtype(cfg.get("torch_dtype", "auto"))
|
| 77 |
-
dev_map = get_device_map()
|
| 78 |
|
| 79 |
print(f"[SLM 로드 중] {key} ({repo_id})...")
|
| 80 |
try:
|
|
@@ -86,7 +78,6 @@ def initialize_active_models():
|
|
| 86 |
model = AutoModelForImageTextToText.from_pretrained(
|
| 87 |
repo_id,
|
| 88 |
torch_dtype=dtype,
|
| 89 |
-
device_map=dev_map,
|
| 90 |
trust_remote_code=True
|
| 91 |
)
|
| 92 |
else:
|
|
@@ -94,16 +85,22 @@ def initialize_active_models():
|
|
| 94 |
model = AutoModelForCausalLM.from_pretrained(
|
| 95 |
repo_id,
|
| 96 |
torch_dtype=dtype,
|
| 97 |
-
device_map=dev_map,
|
| 98 |
trust_remote_code=True
|
| 99 |
)
|
| 100 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 101 |
LOADED_MODELS[key] = model
|
| 102 |
LOADED_PROCESSORS[key] = processor
|
| 103 |
print(f"[SLM 로드 완료] {key}")
|
| 104 |
except Exception as e:
|
| 105 |
print(f"[SLM 로드 실패] {key} ({repo_id}): {e}")
|
| 106 |
|
|
|
|
| 107 |
initialize_active_models()
|
| 108 |
|
| 109 |
def get_cache_key(model_key: str, text: str, max_new_tokens: int) -> str:
|
|
|
|
| 51 |
LOADED_PROCESSORS = {}
|
| 52 |
RESULT_CACHE: Dict[str, Tuple[str, dict]] = {}
|
| 53 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
def get_torch_dtype(dtype_str: str):
|
|
|
|
|
|
|
| 55 |
if dtype_str == "bfloat16":
|
| 56 |
return torch.bfloat16
|
| 57 |
if dtype_str == "float16":
|
| 58 |
return torch.float16
|
| 59 |
return "auto"
|
| 60 |
|
| 61 |
+
# ZeroGPU 가이드: 모듈 레벨에서 활성 모델 초기화 후 cuda 지정
|
| 62 |
def initialize_active_models():
|
| 63 |
for key in ACTIVE_MODEL_KEYS:
|
| 64 |
if key in LOADED_MODELS:
|
|
|
|
| 67 |
repo_id = cfg["repo"]
|
| 68 |
loader_type = cfg.get("loader", "image-text")
|
| 69 |
dtype = get_torch_dtype(cfg.get("torch_dtype", "auto"))
|
|
|
|
| 70 |
|
| 71 |
print(f"[SLM 로드 중] {key} ({repo_id})...")
|
| 72 |
try:
|
|
|
|
| 78 |
model = AutoModelForImageTextToText.from_pretrained(
|
| 79 |
repo_id,
|
| 80 |
torch_dtype=dtype,
|
|
|
|
| 81 |
trust_remote_code=True
|
| 82 |
)
|
| 83 |
else:
|
|
|
|
| 85 |
model = AutoModelForCausalLM.from_pretrained(
|
| 86 |
repo_id,
|
| 87 |
torch_dtype=dtype,
|
|
|
|
| 88 |
trust_remote_code=True
|
| 89 |
)
|
| 90 |
|
| 91 |
+
# ZeroGPU 모듈 레벨 배치: 가상화 레이어가 CPU 보관 후 GPU 함수 진입 시 자동 스왑
|
| 92 |
+
try:
|
| 93 |
+
model = model.to("cuda")
|
| 94 |
+
except Exception:
|
| 95 |
+
pass
|
| 96 |
+
|
| 97 |
LOADED_MODELS[key] = model
|
| 98 |
LOADED_PROCESSORS[key] = processor
|
| 99 |
print(f"[SLM 로드 완료] {key}")
|
| 100 |
except Exception as e:
|
| 101 |
print(f"[SLM 로드 실패] {key} ({repo_id}): {e}")
|
| 102 |
|
| 103 |
+
|
| 104 |
initialize_active_models()
|
| 105 |
|
| 106 |
def get_cache_key(model_key: str, text: str, max_new_tokens: int) -> str:
|