Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,144 +1,50 @@
|
|
| 1 |
-
import nltk
|
| 2 |
-
nltk.download("punkt")
|
| 3 |
-
|
| 4 |
import gradio as gr
|
| 5 |
-
import trafilatura
|
| 6 |
-
import requests
|
| 7 |
-
from markdownify import markdownify as md
|
| 8 |
-
from sumy.parsers.plaintext import PlaintextParser
|
| 9 |
-
from sumy.nlp.tokenizers import Tokenizer
|
| 10 |
-
from sumy.summarizers.text_rank import TextRankSummarizer
|
| 11 |
-
import re
|
| 12 |
import torch
|
| 13 |
-
from transformers import
|
| 14 |
|
| 15 |
-
# =====
|
| 16 |
MODEL_OPTIONS = {
|
| 17 |
"Qwen2.5-1.5B-Instruct": "Qwen/Qwen2.5-1.5B-Instruct",
|
| 18 |
"Gemma-3-4B-it": "google/gemma-3-4b-it",
|
| 19 |
-
"
|
| 20 |
}
|
| 21 |
|
| 22 |
# ===== ๋ชจ๋ธ ๋ก๋ =====
|
| 23 |
def load_model(model_name):
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
model_name
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
).to("cpu")
|
| 30 |
-
return pipeline("text-generation", model=model, tokenizer=tokenizer, device=-1)
|
| 31 |
-
|
| 32 |
-
# ===== ํ
์คํธ ์ ์ฒ๋ฆฌ =====
|
| 33 |
-
def clean_text(text: str) -> str:
|
| 34 |
-
return re.sub(r'\s+', ' ', text).strip()
|
| 35 |
-
|
| 36 |
-
def remove_duplicates(sentences):
|
| 37 |
-
seen, result = set(), []
|
| 38 |
-
for s in sentences:
|
| 39 |
-
s_clean = s.strip()
|
| 40 |
-
if s_clean and s_clean not in seen:
|
| 41 |
-
seen.add(s_clean)
|
| 42 |
-
result.append(s_clean)
|
| 43 |
-
return result
|
| 44 |
-
|
| 45 |
-
# ===== ์๋ ์์ฝ =====
|
| 46 |
-
def summarize_text(text):
|
| 47 |
-
text = clean_text(text)
|
| 48 |
-
length = len(text)
|
| 49 |
-
if length < 300:
|
| 50 |
-
sentence_count = 1
|
| 51 |
-
elif length < 800:
|
| 52 |
-
sentence_count = 2
|
| 53 |
-
elif length < 1500:
|
| 54 |
-
sentence_count = 3
|
| 55 |
else:
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
summarizer = TextRankSummarizer()
|
| 72 |
-
summary_sentences = summarizer(parser.document, sentence_count)
|
| 73 |
-
summary_list = [str(sentence) for sentence in summary_sentences]
|
| 74 |
-
summary_list = remove_duplicates(summary_list)
|
| 75 |
-
summary_list.sort(key=lambda s: text.find(s))
|
| 76 |
-
return summary_list
|
| 77 |
-
|
| 78 |
-
# ===== LLM ์ฌ์์ฑ =====
|
| 79 |
-
def rewrite_with_llm(sentences, model_choice):
|
| 80 |
-
model_name = MODEL_OPTIONS[model_choice]
|
| 81 |
-
llm_pipeline = load_model(model_name)
|
| 82 |
-
|
| 83 |
-
joined_text = "\n".join(sentences)
|
| 84 |
-
prompt = f"""๋ค์ ๋ฌธ์ฅ์ ์๋ฏธ๋ ์ ์งํ๋, ์๋ฌธ์ ์๋ ๋ด์ฉ์ ์ ๋ ์ถ๊ฐํ์ง ๋ง๊ณ ,
|
| 85 |
-
๋ฌธ์ฅ๋ง ๋ ์์ฐ์ค๋ฝ๊ฒ ๋ฐ๊ฟ์ฃผ์ธ์. ๋ค๋ฅธ ์ค๋ช
์ด๋ ๋ถ์ฐ ๋ฌธ์ฅ์ ์ฐ์ง ๋ง์ธ์.
|
| 86 |
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
""
|
| 90 |
-
result = llm_pipeline(prompt, max_new_tokens=150, do_sample=False, temperature=0)
|
| 91 |
-
return result[0]["generated_text"].replace(prompt, "").strip()
|
| 92 |
-
|
| 93 |
-
# ===== ์ ์ฒด ํ์ดํ๋ผ์ธ =====
|
| 94 |
-
def extract_summarize_paraphrase(url, model_choice):
|
| 95 |
-
headers = {"User-Agent": "Mozilla/5.0"}
|
| 96 |
-
try:
|
| 97 |
-
r = requests.get(url, headers=headers, timeout=10)
|
| 98 |
-
r.raise_for_status()
|
| 99 |
-
|
| 100 |
-
html_content = trafilatura.extract(
|
| 101 |
-
r.text,
|
| 102 |
-
output_format="html",
|
| 103 |
-
include_tables=False,
|
| 104 |
-
favor_recall=True
|
| 105 |
-
)
|
| 106 |
-
|
| 107 |
-
if not html_content:
|
| 108 |
-
markdown_text = md(r.text, heading_style="ATX")
|
| 109 |
-
else:
|
| 110 |
-
markdown_text = md(html_content, heading_style="ATX")
|
| 111 |
-
|
| 112 |
-
summary_sentences = summarize_text(markdown_text)
|
| 113 |
-
if not summary_sentences:
|
| 114 |
-
summary_sentences = ["์์ฝ ์์"]
|
| 115 |
-
|
| 116 |
-
paraphrased_text = rewrite_with_llm(summary_sentences, model_choice)
|
| 117 |
-
|
| 118 |
-
return (
|
| 119 |
-
markdown_text or "๋ณธ๋ฌธ ์์",
|
| 120 |
-
"\n".join(summary_sentences),
|
| 121 |
-
paraphrased_text
|
| 122 |
-
)
|
| 123 |
|
| 124 |
-
|
| 125 |
-
|
|
|
|
|
|
|
|
|
|
| 126 |
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
gr.Textbox(label="URL ์
๋ ฅ", placeholder="https://example.com"),
|
| 132 |
-
gr.Dropdown(choices=list(MODEL_OPTIONS.keys()), value="Qwen2.5-1.5B-Instruct", label="์ฌ์์ฑ ๋ชจ๋ธ ์ ํ")
|
| 133 |
-
],
|
| 134 |
-
outputs=[
|
| 135 |
-
gr.Markdown(label="์ถ์ถ๋ ๋ณธ๋ฌธ"),
|
| 136 |
-
gr.Textbox(label="์๋ ์์ฝ", lines=5),
|
| 137 |
-
gr.Textbox(label="์๋ ์ฌ์์ฑ (LLM)", lines=5)
|
| 138 |
-
],
|
| 139 |
-
title="ํ๊ตญ์ด ๋ณธ๋ฌธ ์ถ์ถ + ์๋ ์์ฝ + LLM ์ฌ์์ฑ",
|
| 140 |
-
description="Qwen 1.5B, Gemma 3 E4B, HyperCLOVA-X-Seed-3B ์ค ์ ํํ์ฌ ์ฌ์์ฑ"
|
| 141 |
-
)
|
| 142 |
|
| 143 |
-
|
| 144 |
-
iface.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
import torch
|
| 3 |
+
from transformers import pipeline, AutoTokenizer, AutoModelForVision2Seq
|
| 4 |
|
| 5 |
+
# ===== ๋ชจ๋ธ ๋ชฉ๋ก =====
|
| 6 |
MODEL_OPTIONS = {
|
| 7 |
"Qwen2.5-1.5B-Instruct": "Qwen/Qwen2.5-1.5B-Instruct",
|
| 8 |
"Gemma-3-4B-it": "google/gemma-3-4b-it",
|
| 9 |
+
"CLOVA-Donut-CORDv2": "naver-clova-ix/donut-base-finetuned-cord-v2"
|
| 10 |
}
|
| 11 |
|
| 12 |
# ===== ๋ชจ๋ธ ๋ก๋ =====
|
| 13 |
def load_model(model_name):
|
| 14 |
+
if model_name == "naver-clova-ix/donut-base-finetuned-cord-v2":
|
| 15 |
+
# Vision2Seq ๋ชจ๋ธ ๋ก๋
|
| 16 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 17 |
+
model = AutoModelForVision2Seq.from_pretrained(model_name)
|
| 18 |
+
return pipeline("image-to-text", model=model, tokenizer=tokenizer)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
else:
|
| 20 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 21 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
|
| 22 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 23 |
+
model_name,
|
| 24 |
+
torch_dtype=torch.float32,
|
| 25 |
+
trust_remote_code=True
|
| 26 |
+
).to("cpu")
|
| 27 |
+
return pipeline("text-generation", model=model, tokenizer=tokenizer, device=-1)
|
| 28 |
+
|
| 29 |
+
# ===== CLOVA ์ด๋ฏธ์ง ์ฒ๋ฆฌ =====
|
| 30 |
+
def process_image_with_clova(image):
|
| 31 |
+
pipe = load_model("naver-clova-ix/donut-base-finetuned-cord-v2")
|
| 32 |
+
result = pipe(image)
|
| 33 |
+
return result[0]["generated_text"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
+
# ===== Gradio UI =====
|
| 36 |
+
with gr.Blocks() as iface:
|
| 37 |
+
gr.Markdown("## Qwen / Gemma / CLOVA Donut ํ
์คํธ")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
+
with gr.Tab("ํ
์คํธ URL ์์ฝ/์ฌ์์ฑ"):
|
| 40 |
+
url_input = gr.Textbox(label="URL ์
๋ ฅ")
|
| 41 |
+
model_choice = gr.Dropdown(choices=list(MODEL_OPTIONS.keys()), value="Qwen2.5-1.5B-Instruct")
|
| 42 |
+
output_text = gr.Textbox(label="์ถ๋ ฅ")
|
| 43 |
+
# ์ฌ๊ธฐ์ ๊ธฐ์กด URL ์ฒ๋ฆฌ ํจ์ ์ฐ๊ฒฐ
|
| 44 |
|
| 45 |
+
with gr.Tab("CLOVA ์ด๋ฏธ์ง โ ํ
์คํธ"):
|
| 46 |
+
image_input = gr.Image(type="pil", label="์ด๋ฏธ์ง ์
๋ก๋")
|
| 47 |
+
clova_output = gr.Textbox(label="์ธ์ ๊ฒฐ๊ณผ")
|
| 48 |
+
image_input.change(process_image_with_clova, inputs=image_input, outputs=clova_output)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
|
| 50 |
+
iface.launch()
|
|
|