from fastapi import FastAPI, UploadFile, File, Form, Request
from fastapi.responses import HTMLResponse
from fastapi.staticfiles import StaticFiles
from fastapi.templating import Jinja2Templates
import shutil, os
from detectface import predict_emotion_image, predict_emotion_webcam
from TEXT import detect_emotion
from recommendation import recommend_songs_by_emotion
app = FastAPI()
if method == "upload" and file:
# Save uploaded image to static/uploads
file_path = f"static/uploads/{file.filename}"
with open(file_path, "wb") as buffer:
shutil.copyfileobj(file.file, buffer)
emotion = predict_emotion_image(file_path)
image_url = f"/{file_path}" # For displaying in HTML
elif method == "webcam":
emotion = predict_emotion_webcam()
image_url = "/static/uploads/temp.jpg" if os.path.exists("static/uploads/temp.jpg") else None
else:
emotion = "No input detected"
# Get song recommendations based on emotion
if emotion and emotion != "No input detected":
songs = recommend_songs_by_emotion(emotion,n=count)
else:
songs = []
return templates.TemplateResponse(
"emotion.html",
{"request": request, "emotion": emotion, "image_url": image_url, "songs": songs}
@app.post("/predict_sentiment/", response_class=HTMLResponse)
async def predict_sentiment(request: Request, text: str = Form(...), count: int = Form(5)):
sentiment = detect_emotion(text)
songs = recommend_songs_by_emotion(sentiment,n=count)
return templates.TemplateResponse("sentiment.html", {"request": request, "sentiment":
sentiment, "songs": songs})