-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathapp.py
More file actions
481 lines (405 loc) · 17.6 KB
/
app.py
File metadata and controls
481 lines (405 loc) · 17.6 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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
# Google NotebookLM REST API wrapper
# Namhyeon Go <gnh1201@catswords.re.kr>
# https://github.com/gnh1201/notebooklm-rest-api
import os
import uuid
import tempfile
from typing import Any, Optional, Literal, Dict
from fastapi import FastAPI, UploadFile, File, Form, HTTPException, Depends
from fastapi.responses import FileResponse
from pydantic import BaseModel
from notebooklm import NotebookLMClient, RPCError # notebooklm-py :contentReference[oaicite:2]{index=2}
# ----------------------------
# Config / Security
# ----------------------------
API_KEY = os.environ.get("NOTEBOOKLM_REST_API_KEY", "") # set this in production
AUTH_STORAGE_PATH = os.environ.get("NOTEBOOKLM_STORAGE_PATH") # optional override
def require_api_key(x_api_key: Optional[str] = None):
# Minimal API-key gate. Put this behind a real gateway (Cloudflare, Nginx, etc.) for production.
if API_KEY:
# FastAPI header parsing without extra imports (keep simple):
# Prefer: from fastapi import Header; def require_api_key(x_api_key: str = Header(None)) ...
# but we keep it minimal and rely on query param fallback too.
if x_api_key != API_KEY:
raise HTTPException(status_code=401, detail="Invalid API key")
async def get_client() -> NotebookLMClient:
"""
Creates a client using notebooklm-py's supported auth precedence:
- explicit path to from_storage()
- NOTEBOOKLM_AUTH_JSON
- NOTEBOOKLM_HOME/storage_state.json
- ~/.notebooklm/storage_state.json
:contentReference[oaicite:3]{index=3}
"""
try:
if AUTH_STORAGE_PATH:
return await NotebookLMClient.from_storage(AUTH_STORAGE_PATH)
return await NotebookLMClient.from_storage()
except Exception as e:
raise HTTPException(status_code=500, detail=f"Failed to initialize NotebookLM client: {e}")
def map_rpc_error(e: RPCError) -> HTTPException:
# notebooklm-py raises RPCError for API failures :contentReference[oaicite:4]{index=4}
msg = str(e)
if "401" in msg or "403" in msg or "auth" in msg.lower():
return HTTPException(status_code=401, detail=msg)
if "rate" in msg.lower() or "429" in msg:
return HTTPException(status_code=429, detail=msg)
return HTTPException(status_code=502, detail=msg)
# ----------------------------
# Models
# ----------------------------
class NotebookCreateReq(BaseModel):
title: str
class NotebookRenameReq(BaseModel):
new_title: str
class SourceAddUrlReq(BaseModel):
url: str
wait: bool = True
class SourceAddTextReq(BaseModel):
title: str
content: str
class SourceAddYoutubeReq(BaseModel):
url: str
wait: bool = True
class ChatAskReq(BaseModel):
question: str
# optional persona fields could be added if you want
class ArtifactGenerateReq(BaseModel):
# A simple unified generator:
# audio/video/report/quiz/flashcards/slide_deck/infographic/data_table/mind_map
type: Literal[
"audio",
"video",
"report",
"quiz",
"flashcards",
"slide_deck",
"infographic",
"data_table",
"mind_map",
]
# Options are passed through as-is to the underlying generate_* calls where applicable.
# (The library supports many per-type options; keep this generic.)
options: Dict[str, Any] = {}
class TaskPollResp(BaseModel):
ok: bool
status: Any
# ----------------------------
# App
# ----------------------------
app = FastAPI(title="NotebookLM REST API (powered by notebooklm-py)")
@app.get("/health")
async def health():
return {"ok": True}
# ----------------------------
# Notebooks
# ----------------------------
@app.get("/v1/notebooks")
async def list_notebooks():
client = await get_client()
async with client:
try:
nbs = await client.notebooks.list()
return {"ok": True, "items": [nb.model_dump() if hasattr(nb, "model_dump") else nb.__dict__ for nb in nbs]}
except RPCError as e:
raise map_rpc_error(e)
@app.post("/v1/notebooks")
async def create_notebook(req: NotebookCreateReq):
client = await get_client()
async with client:
try:
nb = await client.notebooks.create(req.title)
return {"ok": True, "notebook": nb.model_dump() if hasattr(nb, "model_dump") else nb.__dict__}
except RPCError as e:
raise map_rpc_error(e)
@app.get("/v1/notebooks/{notebook_id}")
async def get_notebook(notebook_id: str):
client = await get_client()
async with client:
try:
nb = await client.notebooks.get(notebook_id)
return {"ok": True, "notebook": nb.model_dump() if hasattr(nb, "model_dump") else nb.__dict__}
except RPCError as e:
raise map_rpc_error(e)
@app.delete("/v1/notebooks/{notebook_id}")
async def delete_notebook(notebook_id: str):
client = await get_client()
async with client:
try:
ok = await client.notebooks.delete(notebook_id)
return {"ok": True, "deleted": bool(ok)}
except RPCError as e:
raise map_rpc_error(e)
@app.patch("/v1/notebooks/{notebook_id}/rename")
async def rename_notebook(notebook_id: str, req: NotebookRenameReq):
client = await get_client()
async with client:
try:
nb = await client.notebooks.rename(notebook_id, req.new_title)
return {"ok": True, "notebook": nb.model_dump() if hasattr(nb, "model_dump") else nb.__dict__}
except RPCError as e:
raise map_rpc_error(e)
@app.get("/v1/notebooks/{notebook_id}/summary")
async def get_notebook_summary(notebook_id: str):
client = await get_client()
async with client:
try:
summary = await client.notebooks.get_summary(notebook_id)
return {"ok": True, "summary": summary}
except RPCError as e:
raise map_rpc_error(e)
@app.get("/v1/notebooks/{notebook_id}/description")
async def get_notebook_description(notebook_id: str):
client = await get_client()
async with client:
try:
desc = await client.notebooks.get_description(notebook_id)
return {"ok": True, "description": desc.model_dump() if hasattr(desc, "model_dump") else desc.__dict__}
except RPCError as e:
raise map_rpc_error(e)
# ----------------------------
# Sources
# ----------------------------
@app.get("/v1/notebooks/{notebook_id}/sources")
async def list_sources(notebook_id: str):
client = await get_client()
async with client:
try:
items = await client.sources.list(notebook_id)
return {"ok": True, "items": [s.model_dump() if hasattr(s, "model_dump") else s.__dict__ for s in items]}
except RPCError as e:
raise map_rpc_error(e)
@app.post("/v1/notebooks/{notebook_id}/sources/url")
async def add_source_url(notebook_id: str, req: SourceAddUrlReq):
client = await get_client()
async with client:
try:
src = await client.sources.add_url(notebook_id, req.url, wait=req.wait)
return {"ok": True, "source": src.model_dump() if hasattr(src, "model_dump") else src.__dict__}
except TypeError:
# some versions may not accept wait=; fall back
try:
src = await client.sources.add_url(notebook_id, req.url)
return {"ok": True, "source": src.model_dump() if hasattr(src, "model_dump") else src.__dict__}
except RPCError as e:
raise map_rpc_error(e)
except RPCError as e:
raise map_rpc_error(e)
@app.post("/v1/notebooks/{notebook_id}/sources/youtube")
async def add_source_youtube(notebook_id: str, req: SourceAddYoutubeReq):
client = await get_client()
async with client:
try:
src = await client.sources.add_youtube(notebook_id, req.url, wait=req.wait)
return {"ok": True, "source": src.model_dump() if hasattr(src, "model_dump") else src.__dict__}
except TypeError:
try:
src = await client.sources.add_youtube(notebook_id, req.url)
return {"ok": True, "source": src.model_dump() if hasattr(src, "model_dump") else src.__dict__}
except RPCError as e:
raise map_rpc_error(e)
except RPCError as e:
raise map_rpc_error(e)
@app.post("/v1/notebooks/{notebook_id}/sources/text")
async def add_source_text(notebook_id: str, req: SourceAddTextReq):
client = await get_client()
async with client:
try:
src = await client.sources.add_text(notebook_id, req.title, req.content)
return {"ok": True, "source": src.model_dump() if hasattr(src, "model_dump") else src.__dict__}
except RPCError as e:
raise map_rpc_error(e)
@app.post("/v1/notebooks/{notebook_id}/sources/file")
async def add_source_file(
notebook_id: str,
upload: UploadFile = File(...),
mime_type: Optional[str] = Form(None),
):
# Save to temp file first
suffix = os.path.splitext(upload.filename or "")[1] or ".bin"
tmp_path = os.path.join(tempfile.gettempdir(), f"nb_{uuid.uuid4().hex}{suffix}")
with open(tmp_path, "wb") as f:
f.write(await upload.read())
client = await get_client()
async with client:
try:
src = await client.sources.add_file(notebook_id, tmp_path, mime_type=mime_type)
return {"ok": True, "source": src.model_dump() if hasattr(src, "model_dump") else src.__dict__}
except RPCError as e:
raise map_rpc_error(e)
finally:
try:
os.remove(tmp_path)
except OSError:
pass
@app.get("/v1/notebooks/{notebook_id}/sources/{source_id}/fulltext")
async def get_source_fulltext(notebook_id: str, source_id: str):
client = await get_client()
async with client:
try:
ft = await client.sources.get_fulltext(notebook_id, source_id)
return {"ok": True, "fulltext": ft.model_dump() if hasattr(ft, "model_dump") else ft.__dict__}
except RPCError as e:
raise map_rpc_error(e)
@app.get("/v1/notebooks/{notebook_id}/sources/{source_id}/guide")
async def get_source_guide(notebook_id: str, source_id: str):
client = await get_client()
async with client:
try:
guide = await client.sources.get_guide(notebook_id, source_id)
return {"ok": True, "guide": guide}
except RPCError as e:
raise map_rpc_error(e)
@app.delete("/v1/notebooks/{notebook_id}/sources/{source_id}")
async def delete_source(notebook_id: str, source_id: str):
client = await get_client()
async with client:
try:
ok = await client.sources.delete(notebook_id, source_id)
return {"ok": True, "deleted": bool(ok)}
except RPCError as e:
raise map_rpc_error(e)
# ----------------------------
# Chat
# ----------------------------
@app.post("/v1/notebooks/{notebook_id}/chat/ask")
async def chat_ask(notebook_id: str, req: ChatAskReq):
client = await get_client()
async with client:
try:
result = await client.chat.ask(notebook_id, req.question)
# result.answer is shown in docs :contentReference[oaicite:5]{index=5}
if hasattr(result, "model_dump"):
return {"ok": True, "result": result.model_dump()}
return {"ok": True, "result": getattr(result, "__dict__", {"answer": getattr(result, "answer", None)})}
except RPCError as e:
raise map_rpc_error(e)
# ----------------------------
# Artifacts: list / generate / poll / download
# ----------------------------
@app.get("/v1/notebooks/{notebook_id}/artifacts")
async def list_artifacts(notebook_id: str, type: Optional[str] = None):
client = await get_client()
async with client:
try:
items = await client.artifacts.list(notebook_id, type=type) if type else await client.artifacts.list(notebook_id)
return {"ok": True, "items": [a.model_dump() if hasattr(a, "model_dump") else a.__dict__ for a in items]}
except RPCError as e:
raise map_rpc_error(e)
@app.post("/v1/notebooks/{notebook_id}/artifacts/generate")
async def generate_artifact(notebook_id: str, req: ArtifactGenerateReq):
client = await get_client()
async with client:
try:
t = req.type
opts = req.options or {}
if t == "audio":
status = await client.artifacts.generate_audio(notebook_id, **opts)
elif t == "video":
status = await client.artifacts.generate_video(notebook_id, **opts)
elif t == "report":
status = await client.artifacts.generate_report(notebook_id, **opts)
elif t == "quiz":
status = await client.artifacts.generate_quiz(notebook_id, **opts)
elif t == "flashcards":
status = await client.artifacts.generate_flashcards(notebook_id, **opts)
elif t == "slide_deck":
status = await client.artifacts.generate_slide_deck(notebook_id, **opts)
elif t == "infographic":
status = await client.artifacts.generate_infographic(notebook_id, **opts)
elif t == "data_table":
status = await client.artifacts.generate_data_table(notebook_id, **opts)
elif t == "mind_map":
# mind_map may return dict directly in docs :contentReference[oaicite:6]{index=6}
out = await client.artifacts.generate_mind_map(notebook_id, **opts)
return {"ok": True, "type": t, "result": out}
else:
raise HTTPException(status_code=400, detail=f"Unsupported artifact type: {t}")
# GenerationStatus commonly contains task_id :contentReference[oaicite:7]{index=7}
payload = status.model_dump() if hasattr(status, "model_dump") else getattr(status, "__dict__", {})
return {"ok": True, "type": t, "status": payload}
except RPCError as e:
raise map_rpc_error(e)
@app.get("/v1/notebooks/{notebook_id}/artifacts/tasks/{task_id}")
async def poll_task(notebook_id: str, task_id: str, wait: bool = False):
client = await get_client()
async with client:
try:
if wait:
status = await client.artifacts.wait_for_completion(notebook_id, task_id)
else:
status = await client.artifacts.poll_status(notebook_id, task_id)
payload = status.model_dump() if hasattr(status, "model_dump") else getattr(status, "__dict__", status)
return {"ok": True, "status": payload}
except RPCError as e:
raise map_rpc_error(e)
@app.get("/v1/notebooks/{notebook_id}/artifacts/download")
async def download_artifact(
notebook_id: str,
type: Literal[
"audio",
"video",
"infographic",
"slide_deck",
"report",
"mind_map",
"data_table",
"quiz",
"flashcards",
],
artifact_id: Optional[str] = None,
output_format: Optional[Literal["json", "markdown", "html"]] = None,
):
"""
Downloads the *first completed* artifact of the given type unless artifact_id is provided.
notebooklm-py provides type-specific download_* methods. :contentReference[oaicite:8]{index=8}
"""
suffix_map = {
"audio": ".mp4",
"video": ".mp4",
"infographic": ".png",
"slide_deck": ".pdf",
"report": ".md",
"mind_map": ".json",
"data_table": ".csv",
"quiz": ".json" if (output_format in (None, "json")) else (".md" if output_format == "markdown" else ".html"),
"flashcards": ".json" if (output_format in (None, "json")) else (".md" if output_format == "markdown" else ".html"),
}
out_path = os.path.join(tempfile.gettempdir(), f"nlm_{uuid.uuid4().hex}{suffix_map[type]}")
client = await get_client()
async with client:
try:
if type == "audio":
await client.artifacts.download_audio(notebook_id, out_path, artifact_id=artifact_id)
elif type == "video":
await client.artifacts.download_video(notebook_id, out_path, artifact_id=artifact_id)
elif type == "infographic":
await client.artifacts.download_infographic(notebook_id, out_path, artifact_id=artifact_id)
elif type == "slide_deck":
await client.artifacts.download_slide_deck(notebook_id, out_path, artifact_id=artifact_id)
elif type == "report":
await client.artifacts.download_report(notebook_id, out_path, artifact_id=artifact_id)
elif type == "mind_map":
await client.artifacts.download_mind_map(notebook_id, out_path, artifact_id=artifact_id)
elif type == "data_table":
await client.artifacts.download_data_table(notebook_id, out_path, artifact_id=artifact_id)
elif type == "quiz":
await client.artifacts.download_quiz(
notebook_id, out_path, artifact_id=artifact_id, output_format=(output_format or "json")
)
elif type == "flashcards":
await client.artifacts.download_flashcards(
notebook_id, out_path, artifact_id=artifact_id, output_format=(output_format or "json")
)
else:
raise HTTPException(status_code=400, detail=f"Unsupported type: {type}")
filename = os.path.basename(out_path)
return FileResponse(out_path, filename=filename)
except RPCError as e:
# Clean up file if partially created
try:
if os.path.exists(out_path):
os.remove(out_path)
except OSError:
pass
raise map_rpc_error(e)