Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,27 +1,28 @@
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
from transformers import BartTokenizer, BartForConditionalGeneration
|
| 4 |
-
from huggingface_hub import hf_hub_download
|
| 5 |
|
| 6 |
-
hf_token = os.getenv("HF_TOKEN")
|
| 7 |
|
| 8 |
# Load model and tokenizer from Hugging Face hub using the provided model name
|
| 9 |
model_name = "iimran/SAM-TheSummariserV2"
|
| 10 |
-
|
| 11 |
-
|
|
|
|
|
|
|
| 12 |
|
| 13 |
def summarize(input_text):
|
| 14 |
# Tokenize the input text with truncation
|
| 15 |
inputs = tokenizer(input_text, max_length=1024, truncation=True, return_tensors="pt")
|
| 16 |
-
|
| 17 |
# Generate the summary using beam search
|
| 18 |
summary_ids = model.generate(
|
| 19 |
inputs["input_ids"],
|
| 20 |
-
num_beams=4,
|
| 21 |
-
max_length=128,
|
| 22 |
-
early_stopping=True
|
| 23 |
)
|
| 24 |
-
|
| 25 |
# Decode the generated summary tokens to a string
|
| 26 |
summary = tokenizer.decode(summary_ids[0], skip_special_tokens=True)
|
| 27 |
return summary
|
|
@@ -44,4 +45,4 @@ iface = gr.Interface(
|
|
| 44 |
)
|
| 45 |
|
| 46 |
# Launch the Gradio interface
|
| 47 |
-
iface.launch()
|
|
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
from transformers import BartTokenizer, BartForConditionalGeneration
|
|
|
|
| 4 |
|
| 5 |
+
hf_token = os.getenv("HF_TOKEN") # optional unless model is private/gated
|
| 6 |
|
| 7 |
# Load model and tokenizer from Hugging Face hub using the provided model name
|
| 8 |
model_name = "iimran/SAM-TheSummariserV2"
|
| 9 |
+
|
| 10 |
+
# FIX: use `token=` instead of `use_auth_token=`
|
| 11 |
+
tokenizer = BartTokenizer.from_pretrained(model_name, token=hf_token)
|
| 12 |
+
model = BartForConditionalGeneration.from_pretrained(model_name, token=hf_token)
|
| 13 |
|
| 14 |
def summarize(input_text):
|
| 15 |
# Tokenize the input text with truncation
|
| 16 |
inputs = tokenizer(input_text, max_length=1024, truncation=True, return_tensors="pt")
|
| 17 |
+
|
| 18 |
# Generate the summary using beam search
|
| 19 |
summary_ids = model.generate(
|
| 20 |
inputs["input_ids"],
|
| 21 |
+
num_beams=4, # Use beam search with 4 beams for quality summaries
|
| 22 |
+
max_length=128, # Set maximum length for the generated summary
|
| 23 |
+
early_stopping=True # Enable early stopping if all beams finish
|
| 24 |
)
|
| 25 |
+
|
| 26 |
# Decode the generated summary tokens to a string
|
| 27 |
summary = tokenizer.decode(summary_ids[0], skip_special_tokens=True)
|
| 28 |
return summary
|
|
|
|
| 45 |
)
|
| 46 |
|
| 47 |
# Launch the Gradio interface
|
| 48 |
+
iface.launch()
|