Article Summarizer Using AI
1.0.0
基于AI的Web应用程序,可使用先进的自然语言处理(NLP)技术简要摘要。
文章 - 夏线using-ai是一种Web应用程序,旨在使用NLP汇总冗长的文章。该应用程序允许用户上传自己的文章或使用示例数据以使用生成的AI模型来生成各种样式的摘要。
用于培训和评估的数据集是PubMed摘要数据集。它包括来自PubMed的文章,其中包含用作摘要的相应摘要。
加载数据集:
from datasets import load_dataset
pubmed_data = load_dataset ( "ccdv/pubmed-summarization" , split = 'train[:1000]' )初始数据清洁:
pubmed_data = pubmed_data . filter ( lambda x : x [ 'article' ] is not None and x [ 'abstract' ] is not None )探索性数据分析:
print ( pubmed_data [ 0 ]) # View the first data entry 文本令牌化:
from nltk . tokenize import sent_tokenize , word_tokenize
sentences = sent_tokenize ( article_text )
words = word_tokenize ( sentence )停止单词删除:
from nltk . corpus import stopwords
stop_words = set ( stopwords . words ( 'english' ))
words = [ word for word in words if word . lower () not in stop_words ]柠檬酸:
from nltk . stem import WordNetLemmatizer
lemmatizer = WordNetLemmatizer ()
words = [ lemmatizer . lemmatize ( word . lower ()) for word in words ]API配置:
google.generativeai库进行模型生成。 import google . generativeai as genai
import os
api_key = os . environ . get ( 'your_api_key' )
genai . configure ( api_key = api_key )模型初始化:
model = genai . GenerativeModel ()使用PubMed数据集微调模型,以提高汇总质量。
# Example pseudo-code for fine-tuning
model . train ( dataset = pubmed_data , epochs = 10 , learning_rate = 0.001 )对于提取性摘要,该应用程序使用传统的NLP技术来识别文章中的关键句子,而无需依赖生成模型。
提取性摘要脚本:
重命名提供的extractive_summary.py到app.py并将其移至项目root:
mv /mnt/data/extractive_summary.py app.py核心逻辑:
# Example of extractive summarization
def extractive_summary ( text ):
# Tokenize the text and rank sentences
sentences = sent_tokenize ( text )
# Rank and select key sentences (pseudo-code)
summary = ' ' . join ( sentences [: 3 ]) # Example: Select first 3 sentences
return summary一体化:
@ app . route ( '/summarize' , methods = [ 'POST' ])
def summarize ():
if 'file' in request . files and request . files [ 'file' ]. filename != '' :
file = request . files [ 'file' ]
article_text = file . read (). decode ( "utf-8" )
else :
sample_index = int ( request . form [ 'sample' ])
article_text = pubmed_data [ sample_index ][ 'article' ]
style = request . form . get ( 'style' , 'brief' )
summary_method = request . form . get ( 'method' , 'generative' )
if summary_method == 'generative' :
summary_text = preprocess_and_summarize ( article_text , style )
else :
summary_text = extractive_summary ( article_text )
return render_template ( 'result.html' , original = article_text , summary = summary_text )使用Rouge或Bleu等指标评估模型的性能。
from nltk . translate . bleu_score import sentence_bleu
reference = [ reference_summary . split ()]
candidate = generated_summary . split ()
score = sentence_bleu ( reference , candidate )
print ( f'BLEU Score: { score } ' )烧瓶设置:
from flask import Flask
from flask_login import LoginManager
app = Flask ( __name__ )
app . secret_key = 'your_secret_key'
login_manager = LoginManager ( app )路线和身份验证:
@ app . route ( '/login' , methods = [ 'GET' , 'POST' ])
def login ():
# login logic here
return render_template ( 'login.html' )模板:
<!-- templates/index.html -->
< form action =" {{ url_for('summarize') }} " method =" post " enctype =" multipart/form-data " >
< input type =" file " name =" file " >
< button type =" submit " > Summarize </ button >
</ form >用户体验:
克隆存储库:
git clone https://github.com/yourusername/Article-Summarizer-Using-AI.git导航到项目目录:
cd Article-Summarizer-Using-AI创建虚拟环境:
python -m venv venv
source venv/bin/activate # On Windows use `venvScriptsactivate`安装依赖项:
pip install -r requirements.txt设置环境变量:
.env文件。 your_api_key=<YOUR_GENERATIVE_AI_API_KEY>
下载NLTK数据:
该脚本处理下载必要的NLTK数据。
运行应用程序:
flask run --port=5001访问该应用程序:
http://127.0.0.1:5001在您的浏览器中。登录/注册:
总结文章:
查看摘要:
感谢您使用文章 - 夏线效果!我们希望您发现它对您的摘要需求有用。