Update app.py
Browse files
app.py
CHANGED
|
@@ -3,14 +3,25 @@ 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(
|
| 9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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
|
|
@@ -25,10 +36,8 @@ 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 |
-
|
| 30 |
-
{"role": "user", "content": f"Summarize the following text:\n\n{text}"}
|
| 31 |
-
],
|
| 32 |
max_tokens=500,
|
| 33 |
temperature=0.5
|
| 34 |
)
|
|
@@ -40,10 +49,8 @@ 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 |
-
|
| 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 |
)
|
|
|
|
| 3 |
import openai
|
| 4 |
from fpdf import FPDF
|
| 5 |
import os
|
| 6 |
+
import tempfile
|
| 7 |
|
| 8 |
# Function to extract text from a PDF file
|
| 9 |
+
def extract_text_from_pdf(pdf_file):
|
| 10 |
+
# Save the uploaded file to a temporary location
|
| 11 |
+
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".pdf")
|
| 12 |
+
temp_file.write(pdf_file.read())
|
| 13 |
+
temp_file.close() # Close the file to ensure it's saved
|
| 14 |
+
|
| 15 |
+
# Open the saved PDF file
|
| 16 |
+
doc = fitz.open(temp_file.name)
|
| 17 |
text = ""
|
| 18 |
for page_num in range(len(doc)):
|
| 19 |
page = doc.load_page(page_num)
|
| 20 |
text += page.get_text()
|
| 21 |
+
|
| 22 |
+
# Delete the temporary file after reading (clean up)
|
| 23 |
+
os.remove(temp_file.name)
|
| 24 |
+
|
| 25 |
return text
|
| 26 |
|
| 27 |
# Function to ensure the summary ends with a full stop
|
|
|
|
| 36 |
openai.api_key = api_key
|
| 37 |
response = openai.ChatCompletion.create(
|
| 38 |
model="gpt-3.5-turbo", # Use "gpt-4" if you have access
|
| 39 |
+
messages=[{"role": "system", "content": "You are a helpful assistant."},
|
| 40 |
+
{"role": "user", "content": f"Summarize the following text:\n\n{text}"}],
|
|
|
|
|
|
|
| 41 |
max_tokens=500,
|
| 42 |
temperature=0.5
|
| 43 |
)
|
|
|
|
| 49 |
openai.api_key = api_key
|
| 50 |
response = openai.ChatCompletion.create(
|
| 51 |
model="gpt-3.5-turbo", # Use "gpt-4" if you have access
|
| 52 |
+
messages=[{"role": "system", "content": "You are a helpful assistant."},
|
| 53 |
+
{"role": "user", "content": f"What is the main topic of the following text?\n\n{text}"}],
|
|
|
|
|
|
|
| 54 |
max_tokens=500,
|
| 55 |
temperature=0.5
|
| 56 |
)
|