|
|
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 |
|
|
|
|
|
|
|
|
from sentiment_analysis import SentimentAnalysis |
|
|
|
|
|
app = FastAPI() |
|
|
|
|
|
|
|
|
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=["*"], |
|
|
) |
|
|
|
|
|
|
|
|
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) |
|
|
|