sentiment-analysis-oid / app /server_sentiment_analysis.py
santanche's picture
fix (server): static mounted
10dbb77
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)