File size: 6,996 Bytes
e413948
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from fastapi import FastAPI, UploadFile, File, HTTPException
from fastapi.responses import StreamingResponse, FileResponse
from fastapi.staticfiles import StaticFiles
from fastapi.middleware.cors import CORSMiddleware
from pathlib import Path
import json
from typing import AsyncGenerator

from app.config import settings
from app.models.schemas import (
    ScrapeRequest, QueryRequest, FinalResponse,
    StatusResponse, SummarizeRequest, SummaryResponse
)
from app.services.rag_service import RAGService
from app.utils.document_processor import DocumentProcessor

app = FastAPI(
    title="Studyson RAG API",
    description="Document QA and Summarization using RAG",
    version="1.0.0"
)

app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

rag_service = RAGService()
doc_processor = DocumentProcessor()

app.mount("/static", StaticFiles(directory="static"), name="static")


@app.get("/")
async def read_root():
    return FileResponse("static/index.html")


@app.post("/upload", response_model=StatusResponse)
async def upload_document(file: UploadFile = File(...)):
    if not doc_processor.validate_file_type(file.filename):
        raise HTTPException(status_code=400, detail="Only PDF files are supported")

    if file.size and file.size > settings.max_file_size:
        raise HTTPException(status_code=400, detail="File size exceeds maximum limit")

    settings.upload_dir.mkdir(exist_ok=True)
    file_path = settings.upload_dir / file.filename

    try:
        with open(file_path, "wb") as buffer:
            content = await file.read()
            buffer.write(content)

        text = await doc_processor.extract_pdf_text(file_path)
        cleaned_text = doc_processor.clean_text(text)

        rag_service.create_index_from_text(cleaned_text, file.filename)

        return StatusResponse(
            status="success",
            message=f"Document '{file.filename}' uploaded and indexed successfully",
            details={
                "filename": file.filename,
                "text_length": len(cleaned_text),
                "indexed_documents": rag_service.get_indexed_documents()
            }
        )

    except Exception as e:
        if file_path.exists():
            file_path.unlink()
        raise HTTPException(status_code=500, detail=f"Error processing document: {str(e)}")


@app.post("/scrape_and_index", response_model=StatusResponse)
async def scrape_and_index(request: ScrapeRequest):
    try:
        title, text = await doc_processor.scrape_url(str(request.url))
        cleaned_text = doc_processor.clean_text(text)

        rag_service.create_index_from_text(cleaned_text, title)

        return StatusResponse(
            status="success",
            message=f"URL content indexed successfully",
            details={
                "url": str(request.url),
                "title": title,
                "text_length": len(cleaned_text),
                "indexed_documents": rag_service.get_indexed_documents()
            }
        )

    except Exception as e:
        raise HTTPException(status_code=500, detail=f"Error scraping URL: {str(e)}")


@app.post("/stream_query")
async def stream_query(request: QueryRequest):
    if not rag_service.has_documents():
        raise HTTPException(status_code=400, detail="No documents indexed. Please upload a document first.")

    async def event_generator() -> AsyncGenerator[str, None]:
        try:
            answer_parts = []

            async for token in rag_service.stream_query(request.question):
                answer_parts.append(token)
                yield f"data: {json.dumps({'token': token})}\n\n"

            full_answer = "".join(answer_parts)

            _, sources = await rag_service.query(request.question)

            final_response = FinalResponse(
                final_answer=full_answer,
                sources=[source.model_dump() for source in sources]
            )

            yield f"data: [DONE]\n\n"
            yield f"data: {json.dumps(final_response.model_dump())}\n\n"

        except Exception as e:
            yield f"data: {json.dumps({'error': str(e)})}\n\n"

    return StreamingResponse(
        event_generator(),
        media_type="text/event-stream",
        headers={
            "Cache-Control": "no-cache",
            "Connection": "keep-alive",
        }
    )


@app.post("/query", response_model=FinalResponse)
async def query(request: QueryRequest):
    if not rag_service.has_documents():
        raise HTTPException(status_code=400, detail="No documents indexed. Please upload a document first.")

    try:
        answer, sources = await rag_service.query(request.question)

        return FinalResponse(
            final_answer=answer,
            sources=sources
        )

    except Exception as e:
        raise HTTPException(status_code=500, detail=f"Error processing query: {str(e)}")


@app.post("/summarize", response_model=SummaryResponse)
async def summarize(request: SummarizeRequest):
    if not rag_service.has_documents():
        raise HTTPException(status_code=400, detail="No documents indexed. Please upload a document first.")

    try:
        summary = await rag_service.summarize(max_length=request.max_length)

        word_count = len(summary.split())

        return SummaryResponse(
            summary=summary,
            word_count=word_count,
            source_documents=rag_service.get_indexed_documents()
        )

    except Exception as e:
        raise HTTPException(status_code=500, detail=f"Error generating summary: {str(e)}")


@app.post("/reset", response_model=StatusResponse)
async def reset_index():
    try:
        rag_service.reset_index()

        # Only try to delete files if uploads directory exists
        if settings.upload_dir.exists():
            for file_path in settings.upload_dir.glob("*"):
                if file_path.is_file():
                    file_path.unlink()

        return StatusResponse(
            status="success",
            message="Index reset successfully. All documents removed."
        )

    except Exception as e:
        raise HTTPException(status_code=500, detail=f"Error resetting index: {str(e)}")


@app.get("/status", response_model=StatusResponse)
async def get_status():
    return StatusResponse(
        status="online",
        message="Studyson RAG API is running",
        details={
            "has_documents": rag_service.has_documents(),
            "indexed_documents": rag_service.get_indexed_documents(),
            "document_count": len(rag_service.get_indexed_documents())
        }
    )


if __name__ == "__main__":
    import uvicorn
    uvicorn.run(app, host=settings.host, port=settings.port)