File size: 1,530 Bytes
e4cf227
1a60352
bb10374
 
1a60352
e4cf227
1a60352
bb10374
 
1a60352
e4cf227
1a60352
 
 
e4cf227
1a60352
 
 
e4cf227
1a60352
 
 
 
 
 
 
 
e4cf227
1a60352
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# Custom Imports 
from load_meta_data import ChatBot
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware

# Built-in Imports 
from enum import Enum
import warnings
warnings.filterwarnings('ignore')

# Enum to save the Model Id, which is Constant
class Repo_ID(Enum):
    chat_model: str = "Mohammed-Altaf/medical_chatbot-8bit"

# initializing the application and the chatbot class from the load meta data file
app = FastAPI()
bot = ChatBot()

# middlewares to allow cross orgin communications
app.add_middleware(
    CORSMiddleware,
    allow_origins=['*'], 
    allow_credentials=True, 
    allow_methods=['*'], 
    allow_headers=['*'],
)

# load the model asynchronously on startup and save it into memory 
@app.on_event("startup")
async def startup():
    bot.load_from_hub(Repo_ID.chat_model.value)

@app.get("/")
async def home():
    "Home route for the api"
    return {"messeage":"welcome, Application Successfully Loaded!!"}

# dummy test route
@app.get("/test_response/{query}")
async def test_inference(query:str):
    return query


@app.get("/query/{user_query}")
async def inference_output(user_query: str) -> str:
    """Main query route for the api, which return the response from the inference of the LanguageModel
    
    Keyword arguments:
    user_query -- Input string from the user, which is the question(input) to the Model
    Return: return's the response got from the Language model
    """
    
    global bot
    response = bot.get_response(user_query)
    return response