import streamlit as st import fitz # PyMuPDF = reads the content of the pdf file uploaded import openai from fpdf import FPDF import os import tempfile # function to extract text from pdf file def extract_text_from_pdf(pdf_file): # Save the uploaded file to a temporary location temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") temp_file.write(pdf_file.read()) temp_file.close() # Close the file to ensure it is saved # Open the saved pdf file doc = fitz.open(temp_file.name) text = "" # Extracted info saved here for page_num in range(len(doc)): page = doc.load_page(page_num) text += page.get_text() # Saving in text # Delete the temp file after reading os.remove(temp_file.name) return text # function to ensure the summary ends with a full stop def ensure_full_stop(text): text = text.strip() if not text.endswith(('.', '!', '?')): # Corrected tuple syntax text += '.' return text # function to summarize def summarize_text(api_key, text): openai.api_key = api_key response = openai.ChatCompletion.create( model="gpt-3.5-turbo", messages=[ {"role": "system", "content": "You are a helpful assistant."}, {"role": "user", "content": f"Summarize the following text:\n\n{text}"} ], max_tokens=500, temperature=0.5 # Fixed incorrect `temp` key ) summary = response.choices[0].message['content'].strip() return ensure_full_stop(summary) # function to understand the main gist of the pdf def predict_topic(api_key, text): openai.api_key = api_key response = openai.ChatCompletion.create( model="gpt-4o-mini", messages=[ {"role": "system", "content": "You are a helpful assistant."}, {"role": "user", "content": f"What is the main topic of the following text:\n\n{text}"} ], max_tokens=500, temperature=0.5 ) topic = response.choices[0].message['content'].strip() return ensure_full_stop(topic) def create_pdf(summary, topic, original_file_name): base_name = os.path.splitext(original_file_name)[0] pdf_file_name = f"{base_name} summary.pdf" pdf = FPDF() pdf.add_page() pdf.set_font("Arial", size=12) pdf.cell(200, 10, txt="Predicted Main Topic", ln=True, align='C') # Fixed `In=True` pdf.multi_cell(0, 10, txt=topic) pdf_file_path = f"/tmp/{pdf_file_name}" pdf.output(pdf_file_path) return pdf_file_path # Streamlit UI st.title("Research Paper Summarizer") api_key = st.text_input("Enter your API key:", type="password") # File upload uploaded_file = st.file_uploader("Upload your paper (PDF)", type=["pdf"]) if uploaded_file is not None: text = extract_text_from_pdf(uploaded_file) if len(text) > 1000: # Fixed missing colon summary = summarize_text(api_key, text) topic = predict_topic(api_key, text) st.subheader("Summary") st.write(summary) st.subheader("Predicted Topic") st.write(topic) if st.button("Get the Summary PDF"): pdf_path = create_pdf(summary, topic, uploaded_file.name) st.download_button( label="Download Summary PDF", data=open(pdf_path, "rb").read(), file_name=os.path.basename(pdf_path), mime="application/pdf" ) else: st.warning("The document is too short.") else: st.info("Please upload a PDF after entering your OpenAI API key.")