-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
83 lines (62 loc) · 2.19 KB
/
main.py
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
import asyncio
from contextlib import asynccontextmanager
from concurrent.futures import ThreadPoolExecutor
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel
from mangum import Mangum
from inference.Initialize import Intialize
from inference.Gemini import Query_from_gemini
from inference.Groq import Query_from_groq
# import uvicorn
executor = ThreadPoolExecutor(max_workers=2)
# co, groq, index defined at the module level to store the initialized objects
co = None
groq = None
index = None
@asynccontextmanager
async def lifespan(app: FastAPI):
# To modify the same global variables we are using the global keyword
global co, groq, index
# Modify the global variables
co, index, groq = await Intialize()
print("Initialized", co, index, groq)
yield
# Clean up
print("Cleaned up")
app = FastAPI(lifespan=lifespan)
origins = [
"http://localhost.tiangolo.com",
"https://localhost.tiangolo.com",
"http://localhost:3000",
"http://localhost:8080",
"http://localhost:5173",
"https://mind-stride-react.vercel.app"
]
app.add_middleware(
CORSMiddleware,
allow_origins=origins,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
class UserQuery(BaseModel):
user: str
@app.post("/groq_inference")
async def get_groq_inference(query: UserQuery):
loop = asyncio.get_event_loop() # Get the current event loop
# Run Query_from_groq in a separate thread and await the result
response = await loop.run_in_executor(executor, Query_from_groq, query.user, co, groq, index)
return {"output": response}
@app.post("/gemini_inference")
async def get_gemini_inference(query: UserQuery):
loop = asyncio.get_event_loop() # Get the current event loop
# Run Query_from_gemini in a separate thread and await the result
response = await loop.run_in_executor(executor, Query_from_gemini, query.user, co, index)
# sending modified co, index directly with the user query
response = Query_from_gemini(query.user, co, index)
return {"output": response}
# Create the Mangum handler
handler = Mangum(app)
# if __name__ == "__main__":
# uvicorn.run(app)