File size: 1,979 Bytes
bb9c96b
 
 
7a837d4
bb9c96b
 
 
 
7a837d4
bb9c96b
 
 
 
 
 
 
 
 
 
 
7a837d4
 
 
 
bb9c96b
 
7a837d4
bb9c96b
 
 
 
 
 
 
 
 
 
 
7a837d4
 
bb9c96b
7a837d4
bb9c96b
7a837d4
 
 
bb9c96b
 
 
 
 
7a837d4
bb9c96b
7a837d4
bb9c96b
 
 
e0313cc
bb9c96b
7a837d4
52ac50e
 
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
from fastapi import FastAPI, UploadFile, File, HTTPException, Request 
from fastapi.middleware.cors import CORSMiddleware
import os
from rag_engine import RagEngine
from starlette.responses import JSONResponse
from starlette.status import HTTP_400_BAD_REQUEST
from fastapi.responses import StreamingResponse
import asyncio

app = FastAPI()

app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],  # or specify your allowed origins
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],)

UPLOAD_FOLDER = "uploads"
os.makedirs(UPLOAD_FOLDER, exist_ok=True)


rag = RagEngine()

@app.post("/upload")
async def upload_pdf(file: UploadFile = File(...)):
    if not file.filename.endswith(".pdf"):
        raise HTTPException(status_code=HTTP_400_BAD_REQUEST, detail="Only pdf files are supported")

    filename = os.path.basename(file.filename)  # simple sanitization
    filepath = os.path.join(UPLOAD_FOLDER, filename)

    # Save uploaded file to disk
    with open(filepath, "wb") as buffer:
        content = await file.read()
        buffer.write(content)

    try:
        rag.index_pdf(filepath)
    except ValueError as ve:
        raise HTTPException(status_code=HTTP_400_BAD_REQUEST, detail=str(ve))

    return JSONResponse(content={"message": f"file {filename} uploaded and indexed successfully"})



@app.post("/stream")
async def stream_answer(request:Request ):
    data = await request.json()
    question = data.get("question", "")
    print(question)
    if not question.strip():
        raise HTTPException(status_code=400, detail="Empty question")

    async def generate():
        # Assuming rag.ask_question_stream is a generator
        for token in rag.stream_answer(question):
            yield token
            await asyncio.sleep(0)  # yield control to event loop    

    return StreamingResponse((f"data: {token}\n\n" for token in rag.stream_answer(question)),
                         media_type="text/event-stream")