TextSummarizer / app.py
SheemaMasood's picture
Update app.py
46bf337 verified
Raw
History Blame Contribute Delete
3.32 kB
import gradio as gr
from transformers import pipeline, AutoTokenizer
import pdfplumber
from docx import Document
# βœ… Load tokenizer and summarization model
MODEL_NAME = "facebook/bart-large-cnn"
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME, local_files_only=False, force_download=True)
summarizer = pipeline("summarization", model=MODEL_NAME, tokenizer=MODEL_NAME, device=-1)
# βœ… Function to extract text from different file formats
def extract_text(file):
if file is None:
return "No file uploaded."
file_name = file.name.lower()
try:
if file_name.endswith(".pdf"):
with pdfplumber.open(file.name) as pdf:
text = "\n".join([page.extract_text() for page in pdf.pages if page.extract_text()])
elif file_name.endswith(".docx"):
doc = Document(file.name)
text = "\n".join([para.text for para in doc.paragraphs])
elif file_name.endswith(".txt"):
text = file.read().decode("utf-8")
else:
return "❌ Unsupported file format. Please upload a PDF, DOCX, or TXT file."
return text if text.strip() else "⚠ No readable text found in the file."
except Exception as e:
return f"❌ Error reading file: {str(e)}"
# βœ… Function to summarize text
def summarize_text(text, file):
# If file uploaded, extract text
if file is not None:
text = extract_text(file)
# Ensure valid text and truncate to 1024 tokens
if text.strip() and "Error" not in text:
inputs = tokenizer(text, return_tensors="pt", truncation=True, max_length=1024)
summary = summarizer(tokenizer.decode(inputs["input_ids"][0]), max_length=150, min_length=50, do_sample=False)
return summary[0]["summary_text"]
else:
return "⚠ No valid text found to summarize."
# βœ… Gradio Interface with Footer
app = gr.Blocks()
with app:
gr.Markdown("## πŸ“ AI-Powered Text Summarization")
gr.Markdown("πŸ“„ Upload a document or enter text to get a concise AI-generated summary.")
with gr.Row():
text_input = gr.Textbox(lines=10, placeholder="πŸ“Œ Enter text here or upload a file below ⬇")
file_input = gr.File(label="πŸ“‚ Upload File (PDF, DOCX, TXT)")
output_text = gr.Textbox(label="πŸ“ƒ Summarized Text")
summarize_button = gr.Button("✨ Summarize")
summarize_button.click(summarize_text, inputs=[text_input, file_input], outputs=output_text)
# βœ… Footer
gr.Markdown("---")
# βœ… Fixed Footer with Clickable Links
gr.HTML(
"""
<div class='footer' style="text-align: center; padding: 10px; font-size: 16px;">
🌟 Developed by <b>Sheema Masood</b> | Powered By Gradio 🌌 <br>
πŸ”— Connect with me:
<a href="https://github.com/SheemaMasood381" target="_blank" style="color: #f4d03f; text-decoration: none;">GitHub</a> |
<a href="https://www.linkedin.com/in/sheema-masood/" target="_blank" style="color: #3498db; text-decoration: none;">LinkedIn</a> |
<a href="https://www.kaggle.com/sheemamasood" target="_blank" style="color: #e74c3c; text-decoration: none;">Kaggle</a>
</div>
"""
)
# βœ… Launch App
if __name__ == "__main__":
app.launch(debug=True)