Backend-Frontend Connection Setup Guide :
Overview
This guide will help you connect the FastAPI backend to the React frontend for the Study Buddy application.
Prerequisites
Python 3.8+ installed
Node.js 16+ installed
A Google Gemini API key (Get one here)
Setup Instructions
1. Backend Setup
Install Python Dependencies
pip install -r requirements.txtThis will install:
FastAPI
Uvicorn (ASGI server)
Google Generative AI
Transformers (for emotion detection)
Sentence Transformers (for embeddings)
PDF and DOCX processing libraries
Other dependencies
Configure Environment Variables
Copy the example environment file:
copy .env.example .envEdit
.envand add your Gemini API key:GEMINI_API_KEY=your_actual_api_key_here
Run the Backend
python study_buddy_backend.pyThe backend will start on http://localhost:8000
You should see:
"Gemini client configured successfully!"
Server running on http://0.0.0.0:8000
2. Frontend Setup
Install Node Dependencies
npm installRun the Frontend
In a new terminal:
npm run dev:clientThe frontend will start on http://localhost:5173 (or another port if 5173 is taken)
3. Testing the Connection
Open your browser to the frontend URL (e.g.,
http://localhost:5173)Type a message in the chat interface
The frontend will send the message to
http://localhost:8000/api/chatYou should receive an AI-generated response with emotion detection
Available API Endpoints
Chat Endpoint
URL:
POST /api/chatPurpose: Send a chat message and get an AI response with emotion detection
Payload:
{ "user_id": 1, "session_id": "session-123", "message": "Help me with calculus", "personality_mode": "1", "use_notes": false }
Notes Upload
URL:
POST /api/notes/uploadPurpose: Upload PDF, DOCX, or text files for later retrieval
Content-Type:
multipart/form-data
Ask About Notes
URL:
POST /api/notes/askPurpose: Ask questions about uploaded notes
Payload:
{ "user_id": 1, "session_id": "session-123", "question": "What are the key concepts in chapter 3?", "personality_mode": "3" }
Personality Modes
URL:
GET /api/personality-modesPurpose: Get available AI personality modes
Health Check
URL:
GET /healthPurpose: Verify the backend is running
Personality Modes
The Cheerleader (mode "1"): Enthusiastic, motivating, uses emojis
The Peer (mode "2"): Casual, friendly, relatable
The Mentor (mode "3"): Wise, structured, uses Socratic method
Emotion Detection
The backend automatically detects emotions in user messages:
joy: Student is excited
sadness: Student seems down
anger: Student is frustrated
fear: Student is anxious
love: Student is appreciative
surprise: Student is confused
The AI adjusts its tone and response based on the detected emotion.
Troubleshooting
Backend Issues
Error: "GEMINI_API_KEY is not set"
Make sure you've created a
.envfile with your API key
Port 8000 already in use
Change the port in
study_buddy_backend.py:uvicorn.run(app, host="0.0.0.0", port=8001, reload=True)
Module not found errors
Run
pip install -r requirements.txtagainEnsure you're using the correct Python environment
Frontend Issues
API request fails / Network error
Verify the backend is running on http://localhost:8000
Check the browser console for CORS errors
The backend already has CORS enabled for all origins in development
Connection refused
Make sure both frontend and backend are running
Check that the API_BASE in
client/src/lib/queryClient.tsmatches your backend URL
CORS Issues
The backend is configured with:
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)For production, update allow_origins to your deployed frontend domain.
Development Workflow
Start Backend:
python study_buddy_backend.py(Terminal 1)Start Frontend:
npm run dev:client(Terminal 2)Make changes: The backend auto-reloads with
reload=TrueTest: Use the chat interface or test endpoints with curl/Postman
Example Test with curl
curl -X POST http://localhost:8000/api/chat ^
-H "Content-Type: application/json" ^
-d "{\"user_id\": 1, \"session_id\": \"test\", \"message\": \"Hello!\", \"personality_mode\": \"1\"}"Expected response:
{
"reply": "Hey there! 🎉 ...",
"emotion": "joy",
"session_id": "test"
}Next Steps
Add user authentication
Persist conversations to a database
Deploy to production (Heroku, Railway, etc.)
Add file upload functionality to the frontend UI
Implement real-time typing indicators
Add support for more file types
Last updated