Bhanushray commited on
Commit
ea85083
·
verified ·
1 Parent(s): 6e2ce37

Update backend/Server.py

Browse files
Files changed (1) hide show
  1. backend/Server.py +146 -71
backend/Server.py CHANGED
@@ -1,71 +1,146 @@
1
- from fastapi import FastAPI, HTTPException, Request
2
- from fastapi.middleware.cors import CORSMiddleware
3
- from rag_engine import GetRagResponse, ExtractStructure
4
- import uvicorn
5
- import os
6
- from dotenv import load_dotenv
7
-
8
- load_dotenv()
9
-
10
- App = FastAPI()
11
-
12
- App.add_middleware(
13
- CORSMiddleware,
14
- allow_origins=["*"],
15
- allow_methods=["*"],
16
- allow_headers=["*"],
17
- )
18
-
19
- @App.get("/")
20
- def HealthCheck():
21
- return {"status": "Online"}
22
-
23
- # --- CHAT ENDPOINT ---
24
- @App.post("/chat")
25
- @App.post("/api/chat")
26
- async def ChatRoute(request: Request):
27
- # 1. Read JSON body flexibly
28
- try:
29
- data = await request.json()
30
- except:
31
- raise HTTPException(status_code=400, detail="Invalid JSON")
32
-
33
- # 2. Look for ANY common key
34
- user_text = data.get("message") or data.get("Msg") or data.get("query") or data.get("question")
35
-
36
- if not user_text:
37
- raise HTTPException(status_code=422, detail="No message found. Please send JSON with 'message' key.")
38
-
39
- # 3. Process
40
- try:
41
- return GetRagResponse(user_text)
42
- except Exception as e:
43
- print(f"Error: {e}")
44
- return {"answer": "Sorry, the backend encountered an error processing your request.", "sources": []}
45
-
46
- # --- EXTRACT ENDPOINT ---
47
- @App.post("/extract")
48
- @App.post("/api/extract")
49
- async def ExtractRoute(request: Request):
50
- # 1. Read JSON
51
- try:
52
- data = await request.json()
53
- except:
54
- raise HTTPException(status_code=400, detail="Invalid JSON")
55
-
56
- # 2. Look for ANY common key
57
- user_text = data.get("message") or data.get("Cmd") or data.get("instruction") or data.get("query")
58
-
59
- if not user_text:
60
- raise HTTPException(status_code=422, detail="No command found. Please send JSON with 'message' key.")
61
-
62
- # 3. Process
63
- try:
64
- return ExtractStructure(user_text)
65
- except Exception as e:
66
- print(f"Error: {e}")
67
- return {"data": [], "sources": []}
68
-
69
- if __name__ == "__main__":
70
- # Listen on all interfaces
71
- uvicorn.run(App, host="0.0.0.0", port=8000)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # from fastapi import FastAPI, HTTPException, Request
2
+ # from fastapi.middleware.cors import CORSMiddleware
3
+ # from rag_engine import GetRagResponse, ExtractStructure
4
+ # import uvicorn
5
+ # import os
6
+ # from dotenv import load_dotenv
7
+
8
+ # load_dotenv()
9
+
10
+ # App = FastAPI()
11
+
12
+ # App.add_middleware(
13
+ # CORSMiddleware,
14
+ # allow_origins=["*"],
15
+ # allow_methods=["*"],
16
+ # allow_headers=["*"],
17
+ # )
18
+
19
+ # @App.get("/")
20
+ # def HealthCheck():
21
+ # return {"status": "Online"}
22
+
23
+ # # --- CHAT ENDPOINT ---
24
+ # @App.post("/chat")
25
+ # @App.post("/api/chat")
26
+ # async def ChatRoute(request: Request):
27
+ # # 1. Read JSON body flexibly
28
+ # try:
29
+ # data = await request.json()
30
+ # except:
31
+ # raise HTTPException(status_code=400, detail="Invalid JSON")
32
+
33
+ # # 2. Look for ANY common key
34
+ # user_text = data.get("message") or data.get("Msg") or data.get("query") or data.get("question")
35
+
36
+ # if not user_text:
37
+ # raise HTTPException(status_code=422, detail="No message found. Please send JSON with 'message' key.")
38
+
39
+ # # 3. Process
40
+ # try:
41
+ # return GetRagResponse(user_text)
42
+ # except Exception as e:
43
+ # print(f"Error: {e}")
44
+ # return {"answer": "Sorry, the backend encountered an error processing your request.", "sources": []}
45
+
46
+ # # --- EXTRACT ENDPOINT ---
47
+ # @App.post("/extract")
48
+ # @App.post("/api/extract")
49
+ # async def ExtractRoute(request: Request):
50
+ # # 1. Read JSON
51
+ # try:
52
+ # data = await request.json()
53
+ # except:
54
+ # raise HTTPException(status_code=400, detail="Invalid JSON")
55
+
56
+ # # 2. Look for ANY common key
57
+ # user_text = data.get("message") or data.get("Cmd") or data.get("instruction") or data.get("query")
58
+
59
+ # if not user_text:
60
+ # raise HTTPException(status_code=422, detail="No command found. Please send JSON with 'message' key.")
61
+
62
+ # # 3. Process
63
+ # try:
64
+ # return ExtractStructure(user_text)
65
+ # except Exception as e:
66
+ # print(f"Error: {e}")
67
+ # return {"data": [], "sources": []}
68
+
69
+ # if __name__ == "__main__":
70
+ # # Listen on all interfaces
71
+ # uvicorn.run(App, host="0.0.0.0", port=8000)
72
+
73
+
74
+
75
+ from fastapi import FastAPI, HTTPException, Request
76
+ from fastapi.middleware.cors import CORSMiddleware
77
+ # 1. IMPORT THE INGESTION LOGIC
78
+ from ingestion import IngestDocuments
79
+ from rag_engine import GetRagResponse, ExtractStructure
80
+ import uvicorn
81
+ import os
82
+ from dotenv import load_dotenv
83
+
84
+ load_dotenv()
85
+
86
+ App = FastAPI()
87
+
88
+ App.add_middleware(
89
+ CORSMiddleware,
90
+ allow_origins=["*"],
91
+ allow_methods=["*"],
92
+ allow_headers=["*"],
93
+ )
94
+
95
+ @App.get("/")
96
+ def HealthCheck():
97
+ return {"status": "Online", "platform": "Hugging Face Spaces"}
98
+
99
+ # --- INGEST ENDPOINT ---
100
+ @App.post("/ingest")
101
+ @App.post("/api/ingest")
102
+ async def TriggerIngest():
103
+ print("--- INGESTION TRIGGERED BY API ---")
104
+ try:
105
+ # This actually runs the code inside ingestion.py
106
+ IngestDocuments()
107
+ return {"status": "Success", "message": "Ingestion process finished. Embeddings loaded."}
108
+ except Exception as e:
109
+ print(f"INGESTION FAILED: {e}")
110
+ return {"status": "Error", "message": str(e)}
111
+
112
+ # --- CHAT ENDPOINT ---
113
+ @App.post("/chat")
114
+ @App.post("/api/chat")
115
+ async def ChatRoute(request: Request):
116
+ try:
117
+ data = await request.json()
118
+ except:
119
+ raise HTTPException(status_code=400, detail="Invalid JSON")
120
+
121
+ user_text = data.get("message") or data.get("Msg") or data.get("query") or data.get("question")
122
+
123
+ if not user_text:
124
+ raise HTTPException(status_code=422, detail="No message found.")
125
+
126
+ return GetRagResponse(user_text)
127
+
128
+ # --- EXTRACT ENDPOINT ---
129
+ @App.post("/extract")
130
+ @App.post("/api/extract")
131
+ async def ExtractRoute(request: Request):
132
+ try:
133
+ data = await request.json()
134
+ except:
135
+ raise HTTPException(status_code=400, detail="Invalid JSON")
136
+
137
+ user_text = data.get("message") or data.get("Cmd") or data.get("instruction") or data.get("query")
138
+
139
+ if not user_text:
140
+ raise HTTPException(status_code=422, detail="No command found.")
141
+
142
+ return ExtractStructure(user_text)
143
+
144
+ if __name__ == "__main__":
145
+ # PORT TO 7860 (REQUIRED FOR HUGGING FACE DOCKER)
146
+ uvicorn.run(App, host="0.0.0.0", port=7860)