import gradio as gr import os import openai from dotenv import load_dotenv, find_dotenv from langchain import OpenAI, PromptTemplate from langchain.chains.summarize import load_summarize_chain from langchain.document_loaders import PyPDFLoader _ = load_dotenv(find_dotenv()) openai.api_key = os.environ['OPENAI_API_KEY'] llm = OpenAI(temperature=0) def pdf_summarizer(pdf): loader = PyPDFLoader(pdf.name) docs = loader.load_and_split() chain = load_summarize_chain(llm, chain_type="map_reduce") summary = chain.run(docs) return summary inputs = gr.File(label="Upload only a PDF file") outputs = gr.Textbox(label="PDF Summary") iface = gr.Interface( fn=pdf_summarizer, inputs=inputs, outputs=outputs, title="PDF Summarizer", ) iface.launch()