|
|
|
|
|
import gradio as gr |
|
import PyPDF2 |
|
import tempfile |
|
from langchain.prompts import PromptTemplate |
|
from langchain_huggingface.llms import HuggingFacePipeline |
|
|
|
|
|
class TextSummarizer: |
|
def __init__(self): |
|
|
|
self.model_id = "facebook/bart-large-cnn" |
|
|
|
def summarize_text(self, article_text, max_length=150, min_length=30): |
|
|
|
llm = HuggingFacePipeline.from_model_id( |
|
model_id=self.model_id, |
|
task="summarization", |
|
pipeline_kwargs={ |
|
"max_length": max_length, |
|
"min_length": min_length, |
|
"do_sample": False |
|
} |
|
) |
|
"""pipeline_kwargs = { |
|
"max_length": 250, |
|
"do_sample": True, |
|
"temperature": 0.7, # More creative |
|
"top_k": 50, # Limit to top 50 tokens |
|
"top_p": 0.95 # Use nucleus sampling |
|
}""" |
|
|
|
prompt = PromptTemplate(input_variables=["document"], template="""{document}""") |
|
|
|
|
|
prompt_input = prompt.format(document=article_text) |
|
|
|
|
|
summary = llm.__call__(prompt_input) |
|
|
|
|
|
if isinstance(summary, list): |
|
return summary[0]['summary_text'] if 'summary_text' in summary[0] else str(summary[0]) |
|
return str(summary) |
|
|
|
|
|
def pdf_to_text(pdf_file): |
|
try: |
|
|
|
with tempfile.NamedTemporaryFile(delete=False) as tmp: |
|
tmp.write(pdf_file) |
|
tmp.flush() |
|
|
|
|
|
reader = PyPDF2.PdfReader(tmp.name) |
|
text = "\n".join(page.extract_text() or "" for page in reader.pages) |
|
|
|
|
|
return text.strip() if text.strip() else "No extractable text found in the PDF." |
|
except Exception as e: |
|
return f"Error reading PDF: {str(e)}" |
|
|
|
|
|
summarizer = TextSummarizer() |
|
|
|
|
|
def summarize_input(text, max_words): |
|
if not text.strip(): |
|
return "Please enter or extract some text first." |
|
|
|
try: |
|
|
|
max_length = int(max_words) |
|
|
|
min_length = max(30, max_length // 4) |
|
|
|
|
|
return summarizer.summarize_text(text, max_length=max_length, min_length=min_length) |
|
except Exception as e: |
|
return f"Error during summarization: {str(e)}" |
|
|
|
|
|
with gr.Blocks() as demo: |
|
gr.Markdown("## π Text & PDF Summarizer with Length Control") |
|
|
|
with gr.Row(): |
|
|
|
text_input = gr.Textbox(label="Enter article text", lines=15, placeholder="Paste your article here...") |
|
|
|
|
|
pdf_file = gr.File(label="Or upload PDF", file_types=[".pdf"], type="binary") |
|
|
|
|
|
max_words = gr.Number(label="Max summary word count", value=150, precision=0) |
|
|
|
with gr.Row(): |
|
|
|
convert_btn = gr.Button("Convert PDF to Text") |
|
|
|
summary_btn = gr.Button("Summarize Text") |
|
|
|
|
|
output_text = gr.Textbox(label="Summary", lines=10) |
|
|
|
|
|
convert_btn.click(fn=pdf_to_text, inputs=pdf_file, outputs=text_input) |
|
summary_btn.click(fn=summarize_input, inputs=[text_input, max_words], outputs=output_text) |
|
|
|
|
|
if __name__ == "__main__": |
|
demo.launch() |
|
|