Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import LEDTokenizer, LEDForConditionalGeneration
|
| 3 |
+
import torch
|
| 4 |
+
|
| 5 |
+
# โหลดโมเดลและ tokenizer
|
| 6 |
+
model_name = "allenai/led-large-16384-arxiv"
|
| 7 |
+
tokenizer = LEDTokenizer.from_pretrained(model_name)
|
| 8 |
+
model = LEDForConditionalGeneration.from_pretrained(model_name)
|
| 9 |
+
|
| 10 |
+
# ฟังก์ชันสรุปข้อความ
|
| 11 |
+
def summarize_text(text, min_len=100, max_len=300):
|
| 12 |
+
inputs = tokenizer(text, return_tensors="pt", truncation=True, max_length=16384)
|
| 13 |
+
with torch.no_grad():
|
| 14 |
+
summary_ids = model.generate(
|
| 15 |
+
**inputs,
|
| 16 |
+
max_length=max_len,
|
| 17 |
+
min_length=min_len,
|
| 18 |
+
num_beams=4,
|
| 19 |
+
length_penalty=2.0
|
| 20 |
+
)
|
| 21 |
+
summary = tokenizer.decode(summary_ids[0], skip_special_tokens=True)
|
| 22 |
+
return summary
|
| 23 |
+
|
| 24 |
+
# UI Gradio
|
| 25 |
+
app = gr.Interface(
|
| 26 |
+
fn=summarize_text,
|
| 27 |
+
inputs=[
|
| 28 |
+
gr.Textbox(lines=15, placeholder="วางข้อความยาว ๆ ที่นี่...", label="Input Text"),
|
| 29 |
+
gr.Slider(50, 500, value=100, label="Minimum Summary Length"),
|
| 30 |
+
gr.Slider(100, 1000, value=300, label="Maximum Summary Length"),
|
| 31 |
+
],
|
| 32 |
+
outputs=gr.Textbox(label="Summarized Text"),
|
| 33 |
+
title="📄 Long Document Summarizer (LED 16k)",
|
| 34 |
+
description="ใช้โมเดล allenai/led-large-16384-arxiv สำหรับสรุปข้อความยาวมาก เช่น บทความ งานวิจัย หรือเอกสารหลายหน้า"
|
| 35 |
+
)
|
| 36 |
+
|
| 37 |
+
app.launch()
|