Sameer-Handsome173 commited on
Commit
31e3146
Β·
verified Β·
1 Parent(s): 2d99efe

Create main.py

Browse files
Files changed (1) hide show
  1. main.py +238 -0
main.py ADDED
@@ -0,0 +1,238 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ from fastapi import FastAPI, UploadFile, File
3
+ from fastapi.middleware.cors import CORSMiddleware
4
+ from fastapi.responses import JSONResponse
5
+
6
+ # Import both services
7
+ try:
8
+ from split import (
9
+ app as ingestion_app,
10
+ ingest_pdf,
11
+ get_stats as get_ingestion_stats
12
+ )
13
+ print("βœ… Loaded ingestion service (split.py)")
14
+ except Exception as e:
15
+ print(f"⚠️ Warning: Could not load ingestion service: {e}")
16
+ ingestion_app = None
17
+
18
+ try:
19
+ from query_service import (
20
+ app as query_app,
21
+ query_rag,
22
+ query_with_details,
23
+ get_stats as get_query_stats
24
+ )
25
+ print("βœ… Loaded query service (query_service.py)")
26
+ except Exception as e:
27
+ print(f"⚠️ Warning: Could not load query service: {e}")
28
+ query_app = None
29
+
30
+
31
+ # Create main application
32
+ app = FastAPI(
33
+ title="πŸš€ Multimodal RAG - Combined Service",
34
+ description="Unified service for PDF ingestion and multimodal querying",
35
+ version="1.0.0",
36
+ docs_url="/docs",
37
+ redoc_url="/redoc"
38
+ )
39
+
40
+ # Add CORS middleware
41
+ app.add_middleware(
42
+ CORSMiddleware,
43
+ allow_origins=["*"],
44
+ allow_credentials=True,
45
+ allow_methods=["*"],
46
+ allow_headers=["*"],
47
+ )
48
+
49
+
50
+ # ==================== HOME & HEALTH ENDPOINTS ====================
51
+
52
+ @app.get("/")
53
+ async def home():
54
+ """Main endpoint with service information"""
55
+ return {
56
+ "message": "βœ… Multimodal RAG Service is running",
57
+ "status": "healthy",
58
+ "services": {
59
+ "ingestion": "available" if ingestion_app else "unavailable",
60
+ "query": "available" if query_app else "unavailable"
61
+ },
62
+ "endpoints": {
63
+ "ingestion": {
64
+ "ingest_pdf": "POST /ingest",
65
+ "ingestion_stats": "GET /ingest/stats"
66
+ },
67
+ "query": {
68
+ "query": "POST /query?question=YOUR_QUESTION&k=5",
69
+ "query_detailed": "POST /query/details?question=YOUR_QUESTION&k=5",
70
+ "query_stats": "GET /query/stats"
71
+ },
72
+ "documentation": {
73
+ "swagger": "/docs",
74
+ "redoc": "/redoc"
75
+ }
76
+ },
77
+ "features": [
78
+ "PDF text extraction",
79
+ "Table extraction",
80
+ "Image extraction",
81
+ "Multimodal summarization",
82
+ "Vector similarity search",
83
+ "Context-aware answering"
84
+ ]
85
+ }
86
+
87
+
88
+ @app.get("/health")
89
+ async def health_check():
90
+ """Health check endpoint"""
91
+ return {
92
+ "status": "healthy",
93
+ "services": {
94
+ "ingestion": ingestion_app is not None,
95
+ "query": query_app is not None
96
+ }
97
+ }
98
+
99
+
100
+ # ==================== INGESTION ENDPOINTS ====================
101
+
102
+ @app.post("/ingest")
103
+ async def ingest_document(file: UploadFile = File(...)):
104
+ """
105
+ Upload and ingest a PDF document
106
+
107
+ This endpoint processes PDFs and extracts:
108
+ - Text content
109
+ - Tables
110
+ - Images
111
+
112
+ All content is summarized and stored in the vectorstore.
113
+ """
114
+ if ingestion_app is None:
115
+ return JSONResponse(
116
+ status_code=503,
117
+ content={"error": "Ingestion service not available"}
118
+ )
119
+
120
+ return await ingest_pdf(file)
121
+
122
+
123
+ @app.get("/ingest/stats")
124
+ async def ingestion_stats():
125
+ """Get ingestion service statistics"""
126
+ if ingestion_app is None:
127
+ return JSONResponse(
128
+ status_code=503,
129
+ content={"error": "Ingestion service not available"}
130
+ )
131
+
132
+ return get_ingestion_stats()
133
+
134
+
135
+ # ==================== QUERY ENDPOINTS ====================
136
+
137
+ @app.post("/query")
138
+ async def query_documents(question: str, k: int = 5):
139
+ """
140
+ Query the RAG system
141
+
142
+ Args:
143
+ question: The question to ask
144
+ k: Number of documents to retrieve (default: 5)
145
+
146
+ Returns:
147
+ Answer based on retrieved documents
148
+ """
149
+ if query_app is None:
150
+ return JSONResponse(
151
+ status_code=503,
152
+ content={"error": "Query service not available"}
153
+ )
154
+
155
+ return await query_rag(question, k)
156
+
157
+
158
+ @app.post("/query/details")
159
+ async def query_documents_detailed(question: str, k: int = 5):
160
+ """
161
+ Query with detailed document information
162
+
163
+ Args:
164
+ question: The question to ask
165
+ k: Number of documents to retrieve (default: 5)
166
+
167
+ Returns:
168
+ Answer with detailed information about retrieved documents
169
+ """
170
+ if query_app is None:
171
+ return JSONResponse(
172
+ status_code=503,
173
+ content={"error": "Query service not available"}
174
+ )
175
+
176
+ return await query_with_details(question, k)
177
+
178
+
179
+ @app.get("/query/stats")
180
+ async def query_stats():
181
+ """Get query service statistics"""
182
+ if query_app is None:
183
+ return JSONResponse(
184
+ status_code=503,
185
+ content={"error": "Query service not available"}
186
+ )
187
+
188
+ return get_query_stats()
189
+
190
+
191
+ # ==================== COMBINED STATS ENDPOINT ====================
192
+
193
+ @app.get("/stats")
194
+ async def combined_stats():
195
+ """Get combined statistics from both services"""
196
+ stats = {
197
+ "service": "combined",
198
+ "status": "healthy"
199
+ }
200
+
201
+ try:
202
+ if query_app:
203
+ query_stats_data = get_query_stats()
204
+ stats["vectorstore"] = query_stats_data
205
+ except Exception as e:
206
+ stats["vectorstore_error"] = str(e)
207
+
208
+ return stats
209
+
210
+
211
+
212
+
213
+ @app.on_event("startup")
214
+ async def startup_event():
215
+ """Run on application startup"""
216
+ print("\n" + "="*50)
217
+ print("πŸš€ Multimodal RAG Service Starting...")
218
+ print("="*50)
219
+
220
+ if ingestion_app:
221
+ print("βœ… Ingestion service: READY")
222
+ else:
223
+ print("⚠️ Ingestion service: NOT LOADED")
224
+
225
+ if query_app:
226
+ print("βœ… Query service: READY")
227
+ else:
228
+ print("⚠️ Query service: NOT LOADED")
229
+
230
+ print("="*50)
231
+ print("πŸ“‘ Service available at: http://0.0.0.0:7860")
232
+ print("πŸ“š API Documentation: http://0.0.0.0:7860/docs")
233
+ print("="*50 + "\n")
234
+
235
+
236
+ if __name__ == "__main__":
237
+ import uvicorn
238
+ uvicorn.run(app, host="0.0.0.0", port=7860)