PDF_Summarizer / app.py
Sayeed95's picture
Update app.py
9c27b2f
import gradio as gr
import warnings
import json
#import openai
import os
import tiktoken
import gradio as gr
from langchain import OpenAI, PromptTemplate
from langchain.chains.summarize import load_summarize_chain
from langchain.document_loaders import PyPDFLoader
warnings.filterwarnings('ignore')
#openai.api_key = "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"
def num_tokens_from_string(string: str, encoding_name: str) -> int:
"""Returns the number of tokens in a text string."""
encoding = tiktoken.get_encoding(encoding_name)
num_tokens = len(encoding.encode(string))
return num_tokens
num_tokens_from_string("tiktoken is great!", "cl100k_base")
llm = OpenAI(temperature=0)
def summarize_pdf(pdf_file_path):
loader = PyPDFLoader(pdf_file_path)
docs = loader.load_and_split()
chain = load_summarize_chain(llm, chain_type="map_reduce")
summary = chain.run(docs)
return summary
#summarize = summarize_pdf("Assignment 1 - AIDI.pdf")
#summarize
input_pdf_path = gr.components.Textbox(label="Provide the PDF file path")
output_summary = gr.components.Textbox(label="Summary")
interface = gr.Interface(
fn=summarize_pdf,
inputs=input_pdf_path,
outputs=output_summary,
title="PDF Summarizer",
description="Provide PDF file path to get the summary.",
).launch(share=True)