File size: 1,858 Bytes
b290d37
 
 
08637a1
b290d37
08637a1
b290d37
08637a1
 
 
 
 
 
0dc9d36
08637a1
 
b290d37
5ad91e4
 
08637a1
5ad91e4
08637a1
 
5ad91e4
08637a1
 
5ad91e4
b290d37
08637a1
 
 
b290d37
 
08637a1
b290d37
 
 
 
08637a1
b290d37
 
08637a1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
48
49
50
51
52
53
54
55
56
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
from BeatSync import ask_question
from fastapi.middleware.cors import CORSMiddleware  # <-- IMPORT THIS

app = FastAPI()

# ====================================================================================
# FIX: Add CORS middleware to allow requests from your frontend.
# This block tells your Hugging Face server to accept API calls
# from your Render frontend.
# ====================================================================================
origins = [
    "https://beatsync-vx53.onrender.com",  # Your deployed frontend
    "http://localhost:5173",         # For local development
]

app.add_middleware(
    CORSMiddleware,
    allow_origins=origins,
    allow_credentials=True,
    allow_methods=["*"],  # Allows all methods (GET, POST, etc.)
    allow_headers=["*"],  # Allows all headers
)
# ====================================================================================


class ChatRequest(BaseModel):
    question: str
    history: list = []


class ChatResponse(BaseModel):
    answer: str


@app.get("/")
def read_root():
    return {"message": "BeatSync Chatbot is running"}


@app.post("/", response_model=ChatResponse)
def get_chat_response(request: ChatRequest):
    """
    Handles a user's chat message, processes it with the RAG chain,
    and returns the model's response.
    """
    try:
        # Pass the question and the history to the stateless RAG chain function
        answer = ask_question(question=request.question, history=request.history)
        return ChatResponse(answer=answer)
    except Exception as e:
        # Log the exception for debugging
        print(f"An error occurred: {e}")
        # Return a user-friendly error message
        raise HTTPException(status_code=500, detail="An internal error occurred in the chatbot.")