Spaces:
Sleeping
Sleeping
File size: 3,324 Bytes
75dba65 35c8926 75dba65 35c8926 5d79a0f 35c8926 75dba65 35c8926 75dba65 35c8926 75dba65 35c8926 75dba65 35c8926 75dba65 35c8926 75dba65 67988e1 75dba65 35c8926 75dba65 35c8926 75dba65 9a8b617 35c8926 75dba65 35c8926 75dba65 67988e1 35c8926 67988e1 35c8926 67988e1 35c8926 46bf337 67988e1 35c8926 | 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 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 | 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)
|