Spaces:
Sleeping
Sleeping
File size: 2,025 Bytes
76d8eeb d947d8e e3f1e79 c20a099 844c769 c20a099 c487204 4754e4d e3f1e79 c20a099 e3f1e79 c20a099 c487204 c20a099 519d900 c20a099 fb85da6 4754e4d c20a099 d947d8e c20a099 1ae8aab c20a099 519d900 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
import streamlit as st
from transformers import BartTokenizer, BartForConditionalGeneration, pipeline
import zipfile
import os
import nltk
# Download NLTK data
nltk.download('punkt')
from nltk.tokenize import sent_tokenize
# Define the path to the saved model zip file (ensure there is no extra space)
zip_model_path = 'bartzip.zip'
# Define the directory to extract the model
model_dir = './model'
# Unzip the model
with zipfile.ZipFile(zip_model_path, 'r') as zip_ref:
zip_ref.extractall(model_dir)
# After unzipping, the model should be in a specific directory, check the directory structure
model_path = os.path.join(model_dir, 'Bart_model')
# Print out the model_path for debugging
print("Model Path:", model_path)
# Verify that the directory exists and contains the necessary files
if not os.path.exists(model_path):
st.error(f"Model directory {model_path} does not exist or is incorrect.")
# Print out contents of model_dir for further debugging
# print("Contents of model_dir:", os.listdir(model_dir))
else:
# Load the tokenizer and model from the extracted directory
tokenizer = BartTokenizer.from_pretrained(model_path)
model = BartForConditionalGeneration.from_pretrained(model_path)
# Create a summarization pipeline
summarizer = pipeline("summarization", model=model, tokenizer=tokenizer)
# Set the title for the Streamlit app
st.title("BART Summary Generator")
# Text input for the user
text = st.text_area("Enter your text: ")
def generate_summary(input_text):
# Perform summarization
summary = summarizer(input_text, max_length=200, min_length=40, do_sample=False)
return summary[0]['summary_text']
if st.button("Generate"):
if text:
generated_summary = generate_summary(text)
# Display the generated summary
st.subheader("Generated Summary")
st.write(generated_summary)
else:
st.warning("Please enter some text to generate a summary.")
|