Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, UploadFile, File, Form
|
| 2 |
+
from pydantic import BaseModel
|
| 3 |
+
import openai
|
| 4 |
+
import faiss
|
| 5 |
+
import numpy as np
|
| 6 |
+
import os
|
| 7 |
+
from dotenv import load_dotenv
|
| 8 |
+
from fastapi.middleware.cors import CORSMiddleware
|
| 9 |
+
from PyPDF2 import PdfReader
|
| 10 |
+
|
| 11 |
+
load_dotenv()
|
| 12 |
+
openai.api_key = os.getenv("OPENAI_API_KEY")
|
| 13 |
+
|
| 14 |
+
app = FastAPI()
|
| 15 |
+
app.add_middleware(
|
| 16 |
+
CORSMiddleware,
|
| 17 |
+
allow_origins=["*"],
|
| 18 |
+
allow_credentials=True,
|
| 19 |
+
allow_methods=["*"],
|
| 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=[
|
| 46 |
+
{"role": "system", "content": "You are an AI assistant that answers based on uploaded documents. Cite sources using [1], [2], etc."},
|
| 47 |
+
{"role": "user", "content": f"Context:\n{context}\n\nQuestion: {query.question}"}
|
| 48 |
+
],
|
| 49 |
+
temperature=0.3
|
| 50 |
+
)
|
| 51 |
+
return {"answer": response.choices[0].message.content.strip(), "citations": citation_refs}
|
| 52 |
+
|
| 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] = {"index": faiss.IndexFlatL2(1536), "texts": [], "citations": []}
|
| 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(input=[content], model="text-embedding-ada-002")["data"][0]["embedding"]
|
| 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"}
|