Team_skulk / app.py
mishrasahil934's picture
Create app.py (#11)
fdd3e13 verified
raw
history blame
2.95 kB
import os
import base64
import tempfile
import streamlit as st
import fitz # PyMuPDF
from transformers import pipeline
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
# Load the summarization model
tokenizer = AutoTokenizer.from_pretrained("MBZUAI/LaMini-Flan-T5-248M")
base_model = AutoModelForSeq2SeqLM.from_pretrained("MBZUAI/LaMini-Flan-T5-248M")
# Function to extract text from a PDF using PyMuPDF
def extract_text_from_pdf(pdf_path):
text = ""
doc = fitz.open(pdf_path)
for page_num in range(doc.page_count):
page = doc.load_page(page_num) # Get a page
text += page.get_text() # Extract text from the page
if text.strip():
return text
return None
# LLM pipeline for summarization
def llm_pipeline(input_text):
pipe_sum = pipeline(
'summarization',
model=base_model,
tokenizer=tokenizer,
max_length=500,
min_length=50,
)
result = pipe_sum(input_text)
return result[0]['summary_text']
@st.cache_data
# Function to display the PDF
def displayPDF(file_path):
with open(file_path, "rb") as f:
base64_pdf = base64.b64encode(f.read()).decode('utf-8')
pdf_display = f'<iframe src="data:application/pdf;base64,{base64_pdf}" width="100%" height="600" type="application/pdf"></iframe>'
st.markdown(pdf_display, unsafe_allow_html=True)
# Streamlit App
def main():
st.title('Content Summarizer')
# PDF Upload Section
uploaded_file = st.file_uploader("Upload your PDF file", type=['pdf'])
if uploaded_file is not None:
if st.button("Summarize PDF"):
col1, col2 = st.columns(2)
# Save the uploaded file to a temporary location
with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf", dir="/tmp/") as temp_file:
temp_file.write(uploaded_file.read())
temp_filepath = temp_file.name
with col1:
st.info("Uploaded PDF File")
displayPDF(temp_filepath)
with col2:
st.info("Summarization")
input_text = extract_text_from_pdf(temp_filepath)
if input_text: # Proceed only if text extraction was successful
summary = llm_pipeline(input_text)
st.success(summary)
# Text Input Section
st.header("Summarize Your Text")
user_input = st.text_area("Enter your content here:", height=200)
if st.button("Summarize Text"):
if user_input.strip():
col1, col2 = st.columns(2)
with col1:
st.info("Original Content")
st.write(user_input)
with col2:
st.info("Summarization")
summary = llm_pipeline(user_input)
st.success(summary)
else:
st.warning("Please enter some content to summarize.")
if __name__ == '__main__':
main()