Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -12,6 +12,7 @@ load_dotenv()
|
|
12 |
openai.api_key = os.getenv("OPENAI_API_KEY")
|
13 |
|
14 |
app = FastAPI()
|
|
|
15 |
app.add_middleware(
|
16 |
CORSMiddleware,
|
17 |
allow_origins=["*"],
|
@@ -20,26 +21,41 @@ app.add_middleware(
|
|
20 |
allow_headers=["*"],
|
21 |
)
|
22 |
|
|
|
23 |
notebooks = {}
|
24 |
|
25 |
class Query(BaseModel):
|
26 |
question: str
|
27 |
notebook_id: str
|
28 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
@app.post("/ask")
|
30 |
def ask(query: Query):
|
31 |
nb = notebooks.get(query.notebook_id)
|
32 |
if not nb:
|
33 |
return {"answer": "Notebook not found."}
|
|
|
34 |
question_embedding = openai.Embedding.create(
|
35 |
input=[query.question],
|
36 |
model="text-embedding-ada-002"
|
37 |
)["data"][0]["embedding"]
|
|
|
38 |
if len(nb["texts"]) == 0:
|
39 |
return {"answer": "No documents indexed in this notebook."}
|
|
|
40 |
D, I = nb["index"].search(np.array([question_embedding]).astype("float32"), k=3)
|
41 |
context = "\n\n".join([f"[{i+1}] {nb['texts'][i]}" for i in I[0]])
|
42 |
citation_refs = [nb['citations'][i] for i in I[0]]
|
|
|
43 |
response = openai.ChatCompletion.create(
|
44 |
model="gpt-4",
|
45 |
messages=[
|
@@ -53,14 +69,23 @@ def ask(query: Query):
|
|
53 |
@app.post("/upload-pdf")
|
54 |
def upload_pdf(notebook_id: str = Form(...), file: UploadFile = File(...)):
|
55 |
if notebook_id not in notebooks:
|
56 |
-
notebooks[notebook_id] = {
|
|
|
|
|
|
|
|
|
|
|
57 |
nb = notebooks[notebook_id]
|
58 |
reader = PdfReader(file.file)
|
59 |
for i, page in enumerate(reader.pages):
|
60 |
content = page.extract_text()
|
61 |
if content:
|
62 |
-
embedding = openai.Embedding.create(
|
|
|
|
|
|
|
63 |
nb["index"].add(np.array([embedding]).astype("float32"))
|
64 |
nb["texts"].append(content)
|
65 |
nb["citations"].append(f"{file.filename}, page {i+1}")
|
|
|
66 |
return {"status": f"{file.filename} uploaded and parsed"}
|
|
|
12 |
openai.api_key = os.getenv("OPENAI_API_KEY")
|
13 |
|
14 |
app = FastAPI()
|
15 |
+
|
16 |
app.add_middleware(
|
17 |
CORSMiddleware,
|
18 |
allow_origins=["*"],
|
|
|
21 |
allow_headers=["*"],
|
22 |
)
|
23 |
|
24 |
+
# Store documents and vectors per notebook
|
25 |
notebooks = {}
|
26 |
|
27 |
class Query(BaseModel):
|
28 |
question: str
|
29 |
notebook_id: str
|
30 |
|
31 |
+
@app.get("/")
|
32 |
+
def read_root():
|
33 |
+
return {
|
34 |
+
"message": "✅ NotebookLM OpenAI Backend is running!",
|
35 |
+
"endpoints": {
|
36 |
+
"/upload-pdf": "POST a PDF file with notebook_id",
|
37 |
+
"/ask": "POST question + notebook_id to get answer"
|
38 |
+
}
|
39 |
+
}
|
40 |
+
|
41 |
@app.post("/ask")
|
42 |
def ask(query: Query):
|
43 |
nb = notebooks.get(query.notebook_id)
|
44 |
if not nb:
|
45 |
return {"answer": "Notebook not found."}
|
46 |
+
|
47 |
question_embedding = openai.Embedding.create(
|
48 |
input=[query.question],
|
49 |
model="text-embedding-ada-002"
|
50 |
)["data"][0]["embedding"]
|
51 |
+
|
52 |
if len(nb["texts"]) == 0:
|
53 |
return {"answer": "No documents indexed in this notebook."}
|
54 |
+
|
55 |
D, I = nb["index"].search(np.array([question_embedding]).astype("float32"), k=3)
|
56 |
context = "\n\n".join([f"[{i+1}] {nb['texts'][i]}" for i in I[0]])
|
57 |
citation_refs = [nb['citations'][i] for i in I[0]]
|
58 |
+
|
59 |
response = openai.ChatCompletion.create(
|
60 |
model="gpt-4",
|
61 |
messages=[
|
|
|
69 |
@app.post("/upload-pdf")
|
70 |
def upload_pdf(notebook_id: str = Form(...), file: UploadFile = File(...)):
|
71 |
if notebook_id not in notebooks:
|
72 |
+
notebooks[notebook_id] = {
|
73 |
+
"index": faiss.IndexFlatL2(1536),
|
74 |
+
"texts": [],
|
75 |
+
"citations": []
|
76 |
+
}
|
77 |
+
|
78 |
nb = notebooks[notebook_id]
|
79 |
reader = PdfReader(file.file)
|
80 |
for i, page in enumerate(reader.pages):
|
81 |
content = page.extract_text()
|
82 |
if content:
|
83 |
+
embedding = openai.Embedding.create(
|
84 |
+
input=[content],
|
85 |
+
model="text-embedding-ada-002"
|
86 |
+
)["data"][0]["embedding"]
|
87 |
nb["index"].add(np.array([embedding]).astype("float32"))
|
88 |
nb["texts"].append(content)
|
89 |
nb["citations"].append(f"{file.filename}, page {i+1}")
|
90 |
+
|
91 |
return {"status": f"{file.filename} uploaded and parsed"}
|