CoolFace
Apppublic

ranjangoel/GPT-PDF

sourceHugging Faceupdated 4y agoView on Hugging Face
5likes
pdf_reader.cpython-39.pyc32 linesDownload Raw Back to __pycache__
1a

2�Qd2�@sNddlmZddlZddlmZmZddlmZddlm	Z	Gdd�d�Z3dS)	�)�	PdfReaderN�)�BASE_POINTS�READING_PROMT_V2)�Paper)�OpenAIModelc@s`eZdZdZddedfdd�Zdd�Zd	d4�Zdd�Ze	d
�dd�Z5dd�Zdd�Zdd�Z
dS)�PaperReadera�6    A class for summarizing research papers using the OpenAI API.7 8    Attributes:9        openai_key (str): The API key to use the OpenAI API.10        token_length (int): The length of text to send to the API at a time.11        model (str): The GPT model to use for summarization.12        points_to_focus (str): The key points to focus on while summarizing.13        verbose (bool): A flag to enable/disable verbose logging.14 15    i�z
gpt-3.5-turboFcCsN|t_t�|�|_d|_g|_g|_||_d|_	||_16||_t||d�|_17dS)NzXYou are a researcher helper bot. Now you need to read the summaries of a research paper.�)�api_key�model)
�openair18r�format�init_prompt�summary_prompt�messages�summary_msgZ	token_len�19keep_roundr�verboser)�selfZ20openai_keyZtoken_lengthr�points_to_focusr�r�;/Users/alpaca/ChatGPT-Paper-Reader/gpt_reader/pdf_reader.py�__init__szPaperReader.__init__cCsRt|�|jdddkrJ|dg}tdt|��D]}|�||�q2|S|SdS)Nrr	r�)�lenr�range�append)r�msg�new_msg�irrr�drop_conversation)s21zPaperReader.drop_conversationcCs|j�|�S�N)r�send_msg)rrrrrr"3szPaperReader.send_msgcCsB|j�d|d��|�|j�}|j�d|d��|�|j�|_|S�N�user��role�content�system)rrr"r )r�message�responserrr�_chat6s22zPaperReader._chat)�papercCs�d|jd�g|_d|jd�g|_|��D]T\}}}td�||��|�d�|||��}|jrdt|�|j�	dd�|�d��q(|j�	ddd��|�23|j�}|S)Nr(r%zpage: {}, part: {}u$now I send you page {}, part {}:{}r$z{}z,Now please make a summary of the whole paper)rrrrZ24iter_pages�printr
r+rrr")rr,Zpage_idxZpart_idx�text�summary�resultrrr�	summarizeDs25�zPaperReader.summarizecCs&t|�}t|�}td�|�|�}|S)Nzreading pdf finished)rrr-r1)rZpdf_path�26pdf_readerr,r/rrr�read_pdf_and_summarize`s2728z"PaperReader.read_pdf_and_summarizecCs|jSr!)r)rrrr�get_summary_of_each_partksz$PaperReader.get_summary_of_each_partcCs4|j�d|d��|�|j�}|j�d|d��|Sr#)rrr")r�questionr*rrrr5oszPaperReader.questionN)�__name__�29__module__�__qualname__�__doc__rrr r"r+rr1r3r4r5rrrrr	s�3031r)ZPyPDF2rr�promptrrr,rZmodel_interfacerrrrrr�<module>s32