-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.py
More file actions
222 lines (169 loc) · 6.07 KB
/
Copy pathapp.py
File metadata and controls
222 lines (169 loc) · 6.07 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
import os
import uuid
import json
import hashlib
import sqlite3
import fitz # PyMuPDF
import requests
from flask import Flask, render_template, request, jsonify, session, Response
app = Flask(__name__)
app.secret_key = os.urandom(24)
OLLAMA_URL = "http://localhost:11434/api/chat"
MODEL = os.environ.get("OLLAMA_MODEL", "qwen3.5:35b-a3b")
UPLOAD_FOLDER = os.path.join(os.path.dirname(__file__), "uploads")
DB_PATH = os.path.join(os.path.dirname(__file__), "cache.db")
MAX_CONTEXT_CHARS = 120_000 # ~60K tokens, leaves room for conversation
os.makedirs(UPLOAD_FOLDER, exist_ok=True)
# In-memory storage: session_id -> {"text": str, "history": list}
store = {}
def init_db():
conn = sqlite3.connect(DB_PATH)
conn.execute(
"""CREATE TABLE IF NOT EXISTS pdf_cache (
hash TEXT PRIMARY KEY,
filename TEXT,
text TEXT NOT NULL,
summary TEXT NOT NULL
)"""
)
conn.commit()
conn.close()
init_db()
def get_cache(pdf_hash):
conn = sqlite3.connect(DB_PATH)
row = conn.execute(
"SELECT text, summary FROM pdf_cache WHERE hash = ?", (pdf_hash,)
).fetchone()
conn.close()
if row:
return {"text": row[0], "summary": row[1]}
return None
def set_cache(pdf_hash, filename, text, summary):
conn = sqlite3.connect(DB_PATH)
conn.execute(
"INSERT OR REPLACE INTO pdf_cache (hash, filename, text, summary) VALUES (?, ?, ?, ?)",
(pdf_hash, filename, text, summary),
)
conn.commit()
conn.close()
def extract_text(pdf_path):
doc = fitz.open(pdf_path)
text = ""
for page in doc:
text += page.get_text()
doc.close()
return text
def hash_file(filepath):
h = hashlib.sha256()
with open(filepath, "rb") as f:
for chunk in iter(lambda: f.read(8192), b""):
h.update(chunk)
return h.hexdigest()
def ollama_stream(messages):
resp = requests.post(
OLLAMA_URL,
json={"model": MODEL, "messages": messages, "stream": True},
stream=True,
timeout=300,
)
resp.raise_for_status()
for line in resp.iter_lines():
if line:
data = json.loads(line)
content = data.get("message", {}).get("content", "")
if content:
yield content
if data.get("done"):
break
def get_session_id():
if "sid" not in session:
session["sid"] = str(uuid.uuid4())
return session["sid"]
@app.route("/")
def index():
return render_template("index.html")
@app.route("/upload", methods=["POST"])
def upload():
if "file" not in request.files:
return jsonify({"error": "No file provided"}), 400
file = request.files["file"]
if not file.filename.lower().endswith(".pdf"):
return jsonify({"error": "Only PDF files are supported"}), 400
sid = get_session_id()
filepath = os.path.join(UPLOAD_FOLDER, f"{sid}.pdf")
file.save(filepath)
pdf_hash = hash_file(filepath)
cached = get_cache(pdf_hash)
if cached:
os.remove(filepath)
truncated = cached["text"]
summary = cached["summary"]
store[sid] = {
"text": truncated,
"history": [{"role": "assistant", "content": summary}],
}
def generate_cached():
yield f"data: {json.dumps({'stage': 'cache_hit'})}\n\n"
yield f"data: {json.dumps({'content': summary})}\n\n"
yield "data: [DONE]\n\n"
return Response(generate_cached(), mimetype="text/event-stream")
# Not cached — extract and summarize
def generate():
yield f"data: {json.dumps({'stage': 'extracting'})}\n\n"
text = extract_text(filepath)
os.remove(filepath)
if not text.strip():
yield f"data: {json.dumps({'error': 'Could not extract text from this PDF. It may be scanned/image-based.'})}\n\n"
return
truncated = text[:MAX_CONTEXT_CHARS]
store[sid] = {"text": truncated, "history": []}
yield f"data: {json.dumps({'stage': 'summarizing'})}\n\n"
messages = [
{
"role": "system",
"content": "You are a helpful assistant. The user has uploaded a PDF document. Provide a clear, well-structured summary of its contents.",
},
{
"role": "user",
"content": f"Please summarize this document:\n\n{truncated}",
},
]
full_response = []
for chunk in ollama_stream(messages):
full_response.append(chunk)
yield f"data: {json.dumps({'content': chunk})}\n\n"
summary = "".join(full_response)
store[sid]["history"] = [
{"role": "assistant", "content": summary},
]
set_cache(pdf_hash, file.filename, truncated, summary)
yield "data: [DONE]\n\n"
return Response(generate(), mimetype="text/event-stream")
@app.route("/chat", methods=["POST"])
def chat():
sid = get_session_id()
data = request.get_json()
user_message = data.get("message", "").strip()
if not user_message:
return jsonify({"error": "Empty message"}), 400
if sid not in store:
return jsonify({"error": "No PDF uploaded yet. Please upload a PDF first."}), 400
pdf_text = store[sid]["text"]
history = store[sid]["history"]
history.append({"role": "user", "content": user_message})
messages = [
{
"role": "system",
"content": f"You are a helpful assistant answering questions about the following document. Base your answers on the document content.\n\n--- DOCUMENT ---\n{pdf_text}\n--- END DOCUMENT ---",
},
] + history
def generate():
full_response = []
for chunk in ollama_stream(messages):
full_response.append(chunk)
yield f"data: {json.dumps({'content': chunk})}\n\n"
history.append({"role": "assistant", "content": "".join(full_response)})
yield "data: [DONE]\n\n"
return Response(generate(), mimetype="text/event-stream")
if __name__ == "__main__":
app.run(debug=True, port=5005)