File size: 1,135 Bytes
c38c542
dde26ac
c38c542
 
 
3e26431
 
 
 
 
c38c542
 
420d681
c38c542
 
 
 
 
 
 
 
420d681
c38c542
 
420d681
c38c542
 
3e26431
 
 
 
bb2c44a
dde26ac
 
 
c38c542
dde26ac
 
 
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
import pickle
import logging 
from sklearn.feature_extraction.text import TfidVectorizer
from sklearn.pipeline import Pipeline
from sklearn.native_bayes import MultinomialNB
import uvicorn
from fastapi import FastAPI

app = FastAPI()

strings = set() # Set to store all input strings

def predict(input_text: str):
    # Add the new input string to the set of strings
    strings.add(input_text)
    # Train a new model using all strings in the set
    model = Pipeline([
        ('vectorizer', TfidVectorizer()),
        ('classifier', MultinomialNB())
    ])
    model.fit(list(strings), list(strings))

    # Make a prediction on the new input string
    prediction = model.predict([input_text])[0]

    return {"prediction": prediction}
    
# Here you can do things such as load your models

@app.get("/")
def read_root(input_text):
    logging.info("Received request with input_text: %s", input_text)
    try:
        result = predict(input_text)
        logging.info("Prediction made: %s", result)
        return result
    except Exception as e:
        logging.error("An error occured: %s", e)
        return {"error": str(e)}