File size: 995 Bytes
b2e1431
 
 
 
 
 
 
 
 
 
 
 
b49d5c2
 
 
 
b2e1431
 
 
 
 
 
 
 
 
a594fb9
b2e1431
 
a594fb9
b2e1431
 
 
 
 
 
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
from fastapi import FastAPI, Form
import uvicorn

from model import LogisticRegressionModel
from helper import get_llm, classification_modeL_cache, llm_model_cache, prompt

app = FastAPI()

@app.get("/")
async def root():
    return {"message": "Sentiment Analysis API is running."}

@app.get("/logo")
async def load_log():
    return {"message": "hello"}

@app.post("/chat", response_model=str)
async def chat_endpoint(message: str = Form(...)):
    if "model" not in classification_modeL_cache:
        classification_modeL_cache["model"] = LogisticRegressionModel()
    
    if "llm" not in llm_model_cache:
        llm_model_cache["llm"] = get_llm()

    prediction = classification_modeL_cache["model"].predict(message)


    result = llm_model_cache["llm"].invoke(
        prompt.format(text=message, positive_prob=prediction[0][1], negative_prob=prediction[0][0]))

    if result:
        return result.content

if __name__ == "__main__":
    uvicorn.run(app, host="127.0.0.1", port=7861)