Spaces:
Running
Running
from dotenv import load_dotenv | |
load_dotenv() | |
from tempfile import NamedTemporaryFile | |
import os | |
import streamlit as st | |
from langchain.text_splitter import RecursiveCharacterTextSplitter | |
from langchain.document_loaders import PyPDFLoader,DirectoryLoader | |
from langchain.chains.summarize import load_summarize_chain | |
from transformers import pipeline | |
import torch | |
import base64 | |
# Load model directly | |
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM | |
tokenizer = AutoTokenizer.from_pretrained("MBZUAI/LaMini-Flan-T5-248M") | |
base_model = AutoModelForSeq2SeqLM.from_pretrained("MBZUAI/LaMini-Flan-T5-248M") | |
#file loader and processing | |
def file_preprocessing(file): | |
loader = PyPDFLoader(file) | |
pages = loader.load_and_split() | |
text_splitter = RecursiveCharacterTextSplitter(chunk_size=300, chunk_overlap=50) | |
texts = text_splitter.split_documents(pages) | |
final_texts = "" | |
for text in texts: | |
print(text) | |
final_texts = final_texts + text.page_content | |
return final_texts | |
#lm pipeline | |
def llm_pipleline(filepath): | |
pipe_sum = pipeline( | |
'summarization', | |
model = base_model, | |
tokenizer = tokenizer, | |
max_length = 500, | |
min_length = 50 | |
) | |
input_text = file_preprocessing(filepath) | |
result = pipe_sum(input_text) | |
result = result[0]['summary_text'] | |
return result | |
def llm_pipleline1(ans): | |
pipe_sum = pipeline( | |
'summarization', | |
model = base_model, | |
tokenizer = tokenizer, | |
max_length = 500, | |
min_length = 50 | |
) | |
input_text =""+ ans | |
result = pipe_sum(input_text) | |
result = result[0]['summary_text'] | |
return result | |
# Function to display the PDF file | |
def displayPDF(file): | |
# Opening file from file path | |
with open(file, "rb") as f: | |
base_pdf = base64.b64encode(f.read()).decode('utf-8') # Corrected function name and variable | |
# Embedding PDF in HTML | |
pdf_display = f'<iframe src="data:application/pdf;base64,{base_pdf}" width="100%" height="600" type="application/pdf"></iframe>' | |
# Displaying the file | |
st.markdown(pdf_display, unsafe_allow_html=True) | |
#streamlit code | |
st.set_page_config(layout='wide') | |
def main(): | |
st.title('Content Summarizer') | |
uploaded_file = st.file_uploader("Upload your PDF file", type=['pdf']) | |
if uploaded_file is not None: | |
if st.button("Summarize"): | |
col1, col2 = st.columns(2) | |
# Save the uploaded file to a temporary location | |
with NamedTemporaryFile(delete=False, suffix=".pdf") as temp_file: | |
temp_file.write(uploaded_file.read()) | |
temp_filepath = temp_file.name | |
with col1: | |
st.info("Uploaded PDF File") | |
pdf_viewer = displayPDF(temp_filepath) | |
with col2: | |
st.info("Summarization is below") | |
summary = llm_pipleline(temp_filepath) | |
st.success(summary) | |
# New Section for Text Input Summarization | |
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 is below") | |
summary = llm_pipleline1(user_input) | |
st.success(summary) | |
else: | |
st.warning("Please enter some content to summarize.") | |
if __name__ == '__main__': | |
main() | |