-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
83 lines (69 loc) · 3.01 KB
/
app.py
File metadata and controls
83 lines (69 loc) · 3.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
from flask import Flask, render_template, request
import os
from werkzeug.utils import secure_filename
import fitz
from anthropic import Anthropic
from dotenv import load_dotenv
import markdown
load_dotenv()
app = Flask(__name__)
UPLOAD_FOLDER = 'uploads'
ALLOWED_EXTENSIONS = {'pdf'}
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
### Helper Functions ###
def allowed_file(filename):
# Splits the filename using the furthest right '.' as the delimiter.
# Files extensions are always at the end. So [1] will always return the file extension
return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
def extract_text(filepath):
# Stores the pdf in a document object which can be looped through per page
pdf_document = fitz.open(filepath)
extracted_text = ""
for page in pdf_document:
extracted_text += page.get_text()
return extracted_text
# Summarizing function utilizing Anthropic API
def summarize(text):
client = Anthropic(
api_key=os.environ.get("ANTHROPIC_API_KEY"),
)
# Building the message to deliver along with some system assumptions
message = client.messages.create(
max_tokens = 1024,
system = "You are a study assistant. Given the following text from a student's notes or textbook, produce: 1. A concise summary (3-5 sentences) 2. A bullet-point list of the 5-8 most important key points",
messages=[
{
"role": "user",
"content" : f"Here is the text to summarize: \n\n {text}",
}
],
model="claude-opus-4-6"
)
return message.content[0].text
@app.route("/")
def index():
return render_template('index.html')
@app.route("/upload", methods=["POST"])
def upload_post():
# Guard 1: Check if a file was uploaded or a file with a valid name.
# The string argument represents the name attribute of the HTML file input.
# Thet must match to establish a connection
file = request.files.get('pdf_upload')
if not file or file.filename == '':
return render_template('index.html', error="No file submitted")
# Guard 2: Ensure the uploaded file is of the correct extension
if not allowed_file(file.filename):
return render_template('index.html', error="Only PDF files are allowed")
# Builds the full path to save the file. Independent of OS
# secure_filename cleans potentially malicious file naming. Removes spaces, special characters, etc
filepath = os.path.join(app.config['UPLOAD_FOLDER'], secure_filename(file.filename))
file.save(filepath)
pdf_text = extract_text(filepath)
print(pdf_text)
ai_summary = summarize(pdf_text)
# Converts Markdown into HTML (since Anthropic reponse is in markdown)
ai_summary_html = markdown.markdown(ai_summary)
print(ai_summary)
# render_template is a Flask function tha takes an HTML file from templates/
# and sends it to the browser as a reponse
return render_template('index.html', summary = ai_summary_html)