Instructions to use Kabil007/Mr.Summarizer with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use Kabil007/Mr.Summarizer with Transformers:
# Use a pipeline as a high-level helper # Warning: Pipeline type "summarization" is no longer supported in transformers v5. # You must load the model directly (see below) or downgrade to v4.x with: # 'pip install "transformers<5.0.0' from transformers import pipeline pipe = pipeline("summarization", model="Kabil007/Mr.Summarizer")# Load model directly from transformers import AutoModel model = AutoModel.from_pretrained("Kabil007/Mr.Summarizer", device_map="auto") - Notebooks
- Google Colab
- Kaggle
| from flask import Flask, render_template, request, redirect, url_for, flash, jsonify | |
| from PyPDF2 import PdfReader | |
| from transformers import AutoTokenizer, AutoModelForSeq2SeqLM | |
| import os | |
| app = Flask(__name__) | |
| app.secret_key = "supersecretkey" | |
| UPLOAD_FOLDER = 'uploads' | |
| app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER | |
| if not os.path.exists(UPLOAD_FOLDER): | |
| os.makedirs(UPLOAD_FOLDER) | |
| # Load the pre-trained BART tokenizer and model | |
| tokenizer = AutoTokenizer.from_pretrained("facebook/bart-large-cnn") | |
| model = AutoModelForSeq2SeqLM.from_pretrained("facebook/bart-large-cnn") | |
| def extract_text_from_pdf(pdf_path): | |
| reader = PdfReader(pdf_path) | |
| text = "" | |
| for page in reader.pages: | |
| text += page.extract_text() | |
| return text | |
| def index(): | |
| return render_template('index.html') | |
| def upload_file(): | |
| if 'file' not in request.files: | |
| flash("No File Path") | |
| return redirect(url_for('index')) | |
| file = request.files['file'] | |
| if file.filename == '': | |
| flash("Not Selected File") | |
| return redirect(url_for('index')) | |
| if file and file.filename.endswith('.pdf'): | |
| file.save(os.path.join(app.config['UPLOAD_FOLDER'], file.filename)) | |
| flash("File Successfully Uploaded") | |
| file.save(file) | |
| text = extract_text_from_pdf(file) | |
| inputs = tokenizer(text, max_length=1024, return_tensors="pt", truncation=True) | |
| summary_ids = model.generate(inputs["input_ids"], num_beams=4, max_length=300, early_stopping=True) | |
| summary = tokenizer.decode(summary_ids[0], skip_special_tokens=True) | |
| return render_template('index.html', summary=summary) | |
| else: | |
| flash("Only PDF file are alllowed") | |
| return redirect(url_for('index')) | |
| def summarize_text(): | |
| data = request.json | |
| text = data.get('text', '') | |
| if text: | |
| inputs = tokenizer(text, max_length=1024, return_tensors="pt", truncation=True) | |
| summary_ids = model.generate(inputs["input_ids"], num_beams=4, max_length=300, early_stopping=True) | |
| summary = tokenizer.decode(summary_ids[0], skip_special_tokens=True) | |
| return jsonify({'summary': summary}) | |
| return jsonify({'summary': ''}), 400 | |
| if __name__ == "__main__": | |
| app.run(debug=True) |