Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,113 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import fitz # PyMuPDF
|
3 |
+
import openai
|
4 |
+
from fpdf import FPDF
|
5 |
+
import os
|
6 |
+
|
7 |
+
# Function to extract text from a PDF file
|
8 |
+
def extract_text_from_pdf(pdf_path):
|
9 |
+
doc = fitz.open(pdf_path)
|
10 |
+
text = ""
|
11 |
+
for page_num in range(len(doc)):
|
12 |
+
page = doc.load_page(page_num)
|
13 |
+
text += page.get_text()
|
14 |
+
return text
|
15 |
+
|
16 |
+
# Function to ensure the summary ends with a full stop
|
17 |
+
def ensure_full_stop(text):
|
18 |
+
text = text.strip()
|
19 |
+
if not text.endswith(('.', '!', '?')):
|
20 |
+
text += '.'
|
21 |
+
return text
|
22 |
+
|
23 |
+
# Function to summarize text using OpenAI GPT model
|
24 |
+
def summarize_text(api_key, text):
|
25 |
+
openai.api_key = api_key
|
26 |
+
response = openai.ChatCompletion.create(
|
27 |
+
model="gpt-3.5-turbo", # Use "gpt-4" if you have access
|
28 |
+
messages=[
|
29 |
+
{"role": "system", "content": "You are a helpful assistant."},
|
30 |
+
{"role": "user", "content": f"Summarize the following text:\n\n{text}"}
|
31 |
+
],
|
32 |
+
max_tokens=500,
|
33 |
+
temperature=0.5
|
34 |
+
)
|
35 |
+
summary = response.choices[0].message['content'].strip()
|
36 |
+
return ensure_full_stop(summary)
|
37 |
+
|
38 |
+
# Function to predict the main topic of the text
|
39 |
+
def predict_topic(api_key, text):
|
40 |
+
openai.api_key = api_key
|
41 |
+
response = openai.ChatCompletion.create(
|
42 |
+
model="gpt-3.5-turbo", # Use "gpt-4" if you have access
|
43 |
+
messages=[
|
44 |
+
{"role": "system", "content": "You are a helpful assistant."},
|
45 |
+
{"role": "user", "content": f"What is the main topic of the following text?\n\n{text}"}
|
46 |
+
],
|
47 |
+
max_tokens=500,
|
48 |
+
temperature=0.5
|
49 |
+
)
|
50 |
+
topic = response.choices[0].message['content'].strip()
|
51 |
+
return topic
|
52 |
+
|
53 |
+
# Function to generate a PDF with summary and topic
|
54 |
+
def create_pdf(summary, topic, original_file_name):
|
55 |
+
base_name = os.path.splitext(original_file_name)[0] # Remove the .pdf extension
|
56 |
+
pdf_file_name = f"{base_name} summary.pdf" # Create the new filename
|
57 |
+
|
58 |
+
pdf = FPDF()
|
59 |
+
pdf.add_page()
|
60 |
+
pdf.set_font("Arial", size=12)
|
61 |
+
|
62 |
+
pdf.cell(200, 10, txt="Summary", ln=True, align='C')
|
63 |
+
pdf.multi_cell(0, 10, txt=summary)
|
64 |
+
|
65 |
+
pdf.cell(200, 10, txt="Predicted Main Topic", ln=True, align='C')
|
66 |
+
pdf.multi_cell(0, 10, txt=topic)
|
67 |
+
|
68 |
+
# Save the PDF to a file in memory
|
69 |
+
pdf_file_path = f"/tmp/{pdf_file_name}"
|
70 |
+
pdf.output(pdf_file_path)
|
71 |
+
|
72 |
+
return pdf_file_path
|
73 |
+
|
74 |
+
# Streamlit UI
|
75 |
+
st.title("Research Paper Summarizer")
|
76 |
+
|
77 |
+
# API Key input
|
78 |
+
api_key = st.text_input("Enter your OpenAI API Key:", type="password")
|
79 |
+
|
80 |
+
# File upload
|
81 |
+
uploaded_file = st.file_uploader("Upload your research paper (PDF)", type=["pdf"])
|
82 |
+
|
83 |
+
if uploaded_file is not None:
|
84 |
+
# Extract text from the uploaded PDF
|
85 |
+
text = extract_text_from_pdf(uploaded_file)
|
86 |
+
|
87 |
+
if len(text) > 1000:
|
88 |
+
# Summarize the text
|
89 |
+
summary = summarize_text(api_key, text)
|
90 |
+
|
91 |
+
# Predict the main topic
|
92 |
+
topic = predict_topic(api_key, text)
|
93 |
+
|
94 |
+
# Display the results
|
95 |
+
st.subheader("Summary")
|
96 |
+
st.write(summary)
|
97 |
+
|
98 |
+
st.subheader("Predicted Main Topic")
|
99 |
+
st.write(topic)
|
100 |
+
|
101 |
+
# Button to download results as a PDF
|
102 |
+
if st.button("Download Summary as PDF"):
|
103 |
+
pdf_path = create_pdf(summary, topic, uploaded_file.name)
|
104 |
+
st.download_button(
|
105 |
+
label="Download Summary PDF",
|
106 |
+
data=open(pdf_path, "rb").read(),
|
107 |
+
file_name=os.path.basename(pdf_path),
|
108 |
+
mime="application/pdf"
|
109 |
+
)
|
110 |
+
else:
|
111 |
+
st.warning("The document is too short for meaningful analysis.")
|
112 |
+
else:
|
113 |
+
st.info("Please upload a valid PDF file to proceed.")
|