File size: 1,263 Bytes
47f370d
 
 
274c132
 
 
 
47f370d
 
 
 
 
 
10dbb77
 
 
47f370d
 
 
 
 
 
 
 
 
 
 
 
274c132
 
 
 
 
 
 
47f370d
 
 
 
 
 
 
 
274c132
 
 
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
from fastapi import FastAPI, HTTPException
from fastapi.responses import RedirectResponse
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import FileResponse
from fastapi.staticfiles import StaticFiles
import uvicorn
import os

# Import the SentimentAnalysis class
from sentiment_analysis import SentimentAnalysis

app = FastAPI()

# Serve static files
app.mount("/app/static", StaticFiles(directory="static"), name="static")

app.add_middleware(
    CORSMiddleware,
    allow_origins=["http://127.0.0.1:5173","http://localhost:5173"],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

# Create a global instance of SentimentAnalysis
classifier = SentimentAnalysis()

@app.get("/")
def root():
    return RedirectResponse(url="/editor/")

@app.get("/editor/")
def get_editor():
    print(os.path.join("static", "editor", "index.html"))
    return FileResponse(os.path.join("static", "editor", "index.html"))

@app.get("/classify")
async def classify(text: str):
    try:
        result = classifier.classify(text)
        return result
    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e)) from e

if __name__ == "__main__":
    uvicorn.run(app, host="0.0.0.0", port=8000)