-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathREST_API.py
More file actions
43 lines (33 loc) · 1.43 KB
/
REST_API.py
File metadata and controls
43 lines (33 loc) · 1.43 KB
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
from fastapi import FastAPI
from fastapi.responses import HTMLResponse, JSONResponse
from fastapi.staticfiles import StaticFiles
from pydantic import BaseModel
import pickle
import re
# Create a FastAPI instance
app=FastAPI()
# Serve static files (HTML and CSS)
app.mount('/static', StaticFiles(directory='static'), name='static')
# Load the language detection model from the model directory
model=pickle.load(open('model/language_detection_model.pkl','rb'))
# Define a Pydantic model for the text input
class Text(BaseModel):
text:str
# Define a GET endpoint at the root route ("/") that returns the UI HTML file
@app.get('/', response_class=HTMLResponse)
async def root():
with open('static/ui.html', 'r') as file:
return file.read()
# Define a POST endpoint at the /predict route that takes an instance of Text
# and returns the predicted language
@app.post('/predict', response_class=JSONResponse)
async def predict_sentence_language(text_input:Text):
# Extract the text from the Text instance and remove special characters
text = [re.sub(r'[!@#$(),\n"%^\*\?\:;~`0-9\.\[\]\+\-\'=£]', '', text_input.text.lower())]
# Make a prediction using the loaded model
prediction = model.predict(text)
# Return the predicted language (1 for Italian and 0 for Non-Italian)
return {"prediction": prediction.tolist()[0]}
if __name__ == '__main__':
import uvicorn
uvicorn.run(app, host="127.0.0.1", port=5000)