Spaces:
Runtime error
Runtime error
Upload 2 files
Browse files- requirements.txt +3 -0
- use_model.py +34 -0
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
transformers
|
| 2 |
+
gradio
|
| 3 |
+
torch
|
use_model.py
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM, GenerationConfig
|
| 2 |
+
import gradio as gr
|
| 3 |
+
|
| 4 |
+
# Load model
|
| 5 |
+
tokenizer = AutoTokenizer.from_pretrained('/content/drive/MyDrive/TextSummarizer2/model_directory')
|
| 6 |
+
model = AutoModelForSeq2SeqLM.from_pretrained('/content/drive/MyDrive/TextSummarizer2/model_directory')
|
| 7 |
+
tokenizer.model_max_length = 1024
|
| 8 |
+
|
| 9 |
+
# Config
|
| 10 |
+
gen_config = GenerationConfig(
|
| 11 |
+
max_length=150,
|
| 12 |
+
min_length=40,
|
| 13 |
+
length_penalty=2.0,
|
| 14 |
+
num_beams=4,
|
| 15 |
+
early_stopping=True
|
| 16 |
+
)
|
| 17 |
+
|
| 18 |
+
# Summarization function
|
| 19 |
+
def summarize(blog_post):
|
| 20 |
+
input = tokenizer(blog_post, max_length=1024, truncation=True, return_tensors='pt')
|
| 21 |
+
summary_ids = model.generate(input['input_ids'], generation_config=gen_config)
|
| 22 |
+
summary = tokenizer.decode(summary_ids[0], skip_special_tokens=True)
|
| 23 |
+
return summary
|
| 24 |
+
|
| 25 |
+
# Gradio UI
|
| 26 |
+
iface = gr.Interface(
|
| 27 |
+
fn=summarize,
|
| 28 |
+
inputs=gr.Textbox(lines=15, label="Enter Text to Summarize"),
|
| 29 |
+
outputs=gr.Textbox(label="Summary"),
|
| 30 |
+
title="Text Summarizer",
|
| 31 |
+
description="Enter a long paragraph or blog post to get a summarized version."
|
| 32 |
+
)
|
| 33 |
+
|
| 34 |
+
iface.launch(share=True)
|