import os import openai import sys import gradio as gr from langchain import OpenAI, PromptTemplate from langchain.text_splitter import CharacterTextSplitter from langchain.chains.summarize import load_summarize_chain from langchain.document_loaders import PyPDFLoader from langchain.chains import RetrievalQA sys.path.append('../..') from dotenv import load_dotenv, find_dotenv _ = load_dotenv(find_dotenv()) # read local .env file openai.api_key = os.environ['OPENAI_API_KEY'] llm = OpenAI(temperature=0) text_splitter = CharacterTextSplitter() def summarize_pdf(pdf_file): pdf_file = pdf_file.name loader = PyPDFLoader(pdf_file) docs = loader.load_and_split() chain = load_summarize_chain(llm, chain_type="map_reduce") summary = chain.run(docs) return summary def main(): output_summary = gr.Textbox(label="Summary") iface = gr.Interface( fn=summarize_pdf, inputs=["file"], outputs=[output_summary], title="PDF Summarizer", description="Langchain based summarization application that's given a PDF file, then create a summary of the text content.
Enter the PDF file and get its summary.", ) iface.launch(share=True) if __name__ == "__main__": main()