Spaces:
Sleeping
Sleeping
yuvrajsingh6
commited on
Commit
·
2d41a13
1
Parent(s):
c5b5cc8
Add Hugging Face deployment configuration
Browse files- DEPLOY_HUGGINGFACE.md +143 -0
- Dockerfile +51 -0
- README_HF.md +27 -0
- backend/.env.example +2 -0
- backend/requirements.txt +5 -0
- frontend-next/src/lib/api.js +3 -2
DEPLOY_HUGGINGFACE.md
ADDED
|
@@ -0,0 +1,143 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Deploying to Hugging Face Spaces
|
| 2 |
+
|
| 3 |
+
This guide will help you deploy the Analytical Finance Chatbot to Hugging Face Spaces.
|
| 4 |
+
|
| 5 |
+
## Prerequisites
|
| 6 |
+
|
| 7 |
+
1. **Hugging Face Account**: Sign up at [huggingface.co](https://huggingface.co)
|
| 8 |
+
2. **Groq API Key**: Get one from [console.groq.com](https://console.groq.com)
|
| 9 |
+
|
| 10 |
+
## Step 1: Create a New Space
|
| 11 |
+
|
| 12 |
+
1. Go to [huggingface.co/spaces](https://huggingface.co/spaces)
|
| 13 |
+
2. Click **"Create new Space"**
|
| 14 |
+
3. Fill in the details:
|
| 15 |
+
- **Space name**: `analytical-finance-chatbot` (or your choice)
|
| 16 |
+
- **License**: MIT
|
| 17 |
+
- **SDK**: Docker
|
| 18 |
+
- **Hardware**: CPU basic (free tier)
|
| 19 |
+
4. Click **"Create Space"**
|
| 20 |
+
|
| 21 |
+
## Step 2: Push Your Code
|
| 22 |
+
|
| 23 |
+
### Option A: Using Git (Recommended)
|
| 24 |
+
|
| 25 |
+
```bash
|
| 26 |
+
# Add Hugging Face remote
|
| 27 |
+
git remote add hf https://huggingface.co/spaces/YOUR_USERNAME/analytical-finance-chatbot
|
| 28 |
+
|
| 29 |
+
# Rename README for Hugging Face
|
| 30 |
+
mv README.md README_GITHUB.md
|
| 31 |
+
mv README_HF.md README.md
|
| 32 |
+
|
| 33 |
+
# Commit the Hugging Face config
|
| 34 |
+
git add README.md Dockerfile backend/requirements.txt backend/.env.example
|
| 35 |
+
git commit -m "Add Hugging Face deployment configuration"
|
| 36 |
+
|
| 37 |
+
# Push to Hugging Face
|
| 38 |
+
git push hf main
|
| 39 |
+
```
|
| 40 |
+
|
| 41 |
+
### Option B: Using Hugging Face Web Interface
|
| 42 |
+
|
| 43 |
+
1. Go to your Space's **"Files"** tab
|
| 44 |
+
2. Click **"Add file"** → **"Upload files"**
|
| 45 |
+
3. Upload these files:
|
| 46 |
+
- `README.md` (rename README_HF.md to README.md)
|
| 47 |
+
- `Dockerfile`
|
| 48 |
+
- All `backend/` files
|
| 49 |
+
- All `frontend-next/` files
|
| 50 |
+
4. Commit the changes
|
| 51 |
+
|
| 52 |
+
## Step 3: Configure Secrets
|
| 53 |
+
|
| 54 |
+
1. In your Space, go to **"Settings"** tab
|
| 55 |
+
2. Scroll to **"Repository secrets"**
|
| 56 |
+
3. Add a new secret:
|
| 57 |
+
- **Name**: `GROQ_API_KEY`
|
| 58 |
+
- **Value**: Your Groq API key
|
| 59 |
+
4. Click **"Add secret"**
|
| 60 |
+
|
| 61 |
+
## Step 4: Update API URL in Frontend
|
| 62 |
+
|
| 63 |
+
Before deploying, update the API URL in `frontend-next/src/lib/api.js`:
|
| 64 |
+
|
| 65 |
+
```javascript
|
| 66 |
+
// Change from:
|
| 67 |
+
const API_BASE_URL = 'http://localhost:8000';
|
| 68 |
+
|
| 69 |
+
// To:
|
| 70 |
+
const API_BASE_URL = process.env.NEXT_PUBLIC_API_URL || 'http://localhost:8000';
|
| 71 |
+
```
|
| 72 |
+
|
| 73 |
+
Then set the environment variable in Hugging Face:
|
| 74 |
+
- **Name**: `NEXT_PUBLIC_API_URL`
|
| 75 |
+
- **Value**: `http://localhost:8000` (backend runs on same container)
|
| 76 |
+
|
| 77 |
+
## Step 5: Wait for Build
|
| 78 |
+
|
| 79 |
+
1. Hugging Face will automatically build your Docker image
|
| 80 |
+
2. This may take 5-10 minutes
|
| 81 |
+
3. Watch the build logs in the **"Logs"** tab
|
| 82 |
+
4. Once complete, your app will be live!
|
| 83 |
+
|
| 84 |
+
## Step 6: Access Your App
|
| 85 |
+
|
| 86 |
+
Your app will be available at:
|
| 87 |
+
```
|
| 88 |
+
https://huggingface.co/spaces/YOUR_USERNAME/analytical-finance-chatbot
|
| 89 |
+
```
|
| 90 |
+
|
| 91 |
+
## Troubleshooting
|
| 92 |
+
|
| 93 |
+
### Build Fails
|
| 94 |
+
|
| 95 |
+
**Check logs** in the "Logs" tab. Common issues:
|
| 96 |
+
- Missing dependencies in `requirements.txt`
|
| 97 |
+
- Incorrect Dockerfile syntax
|
| 98 |
+
- Port conflicts (ensure using 7860)
|
| 99 |
+
|
| 100 |
+
### App Doesn't Load
|
| 101 |
+
|
| 102 |
+
1. Check if both services started (backend + frontend)
|
| 103 |
+
2. Verify `GROQ_API_KEY` is set in secrets
|
| 104 |
+
3. Check browser console for errors
|
| 105 |
+
|
| 106 |
+
### API Errors
|
| 107 |
+
|
| 108 |
+
1. Ensure backend is running on port 8000
|
| 109 |
+
2. Verify frontend is connecting to correct API URL
|
| 110 |
+
3. Check CORS settings in `backend/main.py`
|
| 111 |
+
|
| 112 |
+
## Updating Your Space
|
| 113 |
+
|
| 114 |
+
To update your deployed app:
|
| 115 |
+
|
| 116 |
+
```bash
|
| 117 |
+
# Make changes locally
|
| 118 |
+
git add .
|
| 119 |
+
git commit -m "Update: description of changes"
|
| 120 |
+
|
| 121 |
+
# Push to Hugging Face
|
| 122 |
+
git push hf main
|
| 123 |
+
```
|
| 124 |
+
|
| 125 |
+
Hugging Face will automatically rebuild and redeploy.
|
| 126 |
+
|
| 127 |
+
## Cost & Limits
|
| 128 |
+
|
| 129 |
+
- **Free tier**: CPU basic (sufficient for demo)
|
| 130 |
+
- **Upgrade**: For production, consider upgrading to GPU or better CPU
|
| 131 |
+
- **Persistent storage**: Conversations are stored in memory (lost on restart)
|
| 132 |
+
|
| 133 |
+
## Next Steps
|
| 134 |
+
|
| 135 |
+
Consider adding:
|
| 136 |
+
- Persistent database (PostgreSQL on external service)
|
| 137 |
+
- Authentication
|
| 138 |
+
- Rate limiting
|
| 139 |
+
- Analytics
|
| 140 |
+
|
| 141 |
+
---
|
| 142 |
+
|
| 143 |
+
**Need help?** Check [Hugging Face Spaces documentation](https://huggingface.co/docs/hub/spaces)
|
Dockerfile
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Multi-stage build for production
|
| 2 |
+
|
| 3 |
+
# Stage 1: Build Frontend
|
| 4 |
+
FROM node:18-alpine AS frontend-builder
|
| 5 |
+
|
| 6 |
+
WORKDIR /app/frontend
|
| 7 |
+
|
| 8 |
+
# Copy frontend files
|
| 9 |
+
COPY frontend-next/package*.json ./
|
| 10 |
+
RUN npm ci
|
| 11 |
+
|
| 12 |
+
COPY frontend-next/ ./
|
| 13 |
+
RUN npm run build
|
| 14 |
+
|
| 15 |
+
# Stage 2: Backend + Serve Frontend
|
| 16 |
+
FROM python:3.10-slim
|
| 17 |
+
|
| 18 |
+
WORKDIR /app
|
| 19 |
+
|
| 20 |
+
# Install system dependencies
|
| 21 |
+
RUN apt-get update && apt-get install -y \
|
| 22 |
+
curl \
|
| 23 |
+
&& rm -rf /var/lib/apt/lists/*
|
| 24 |
+
|
| 25 |
+
# Copy backend files
|
| 26 |
+
COPY backend/requirements.txt ./backend/
|
| 27 |
+
RUN pip install --no-cache-dir -r backend/requirements.txt
|
| 28 |
+
|
| 29 |
+
COPY backend/ ./backend/
|
| 30 |
+
|
| 31 |
+
# Copy built frontend from previous stage
|
| 32 |
+
COPY --from=frontend-builder /app/frontend/.next ./frontend/.next
|
| 33 |
+
COPY --from=frontend-builder /app/frontend/public ./frontend/public
|
| 34 |
+
COPY --from=frontend-builder /app/frontend/package*.json ./frontend/
|
| 35 |
+
COPY --from=frontend-builder /app/frontend/node_modules ./frontend/node_modules
|
| 36 |
+
|
| 37 |
+
# Create startup script
|
| 38 |
+
RUN echo '#!/bin/bash\n\
|
| 39 |
+
cd /app/backend && uvicorn main:app --host 0.0.0.0 --port 8000 &\n\
|
| 40 |
+
cd /app/frontend && npm start -- -p 7860\n\
|
| 41 |
+
wait' > /app/start.sh && chmod +x /app/start.sh
|
| 42 |
+
|
| 43 |
+
# Expose port
|
| 44 |
+
EXPOSE 7860
|
| 45 |
+
|
| 46 |
+
# Set environment variables
|
| 47 |
+
ENV PYTHONUNBUFFERED=1
|
| 48 |
+
ENV NODE_ENV=production
|
| 49 |
+
|
| 50 |
+
# Start both services
|
| 51 |
+
CMD ["/app/start.sh"]
|
README_HF.md
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
title: Analytical Finance Chatbot
|
| 3 |
+
emoji: 💹
|
| 4 |
+
colorFrom: black
|
| 5 |
+
colorTo: gray
|
| 6 |
+
sdk: docker
|
| 7 |
+
pinned: false
|
| 8 |
+
license: mit
|
| 9 |
+
app_port: 7860
|
| 10 |
+
---
|
| 11 |
+
|
| 12 |
+
# Analytical Finance Chatbot
|
| 13 |
+
|
| 14 |
+
An AI-powered financial data analysis chatbot with an OpenAI-inspired interface.
|
| 15 |
+
|
| 16 |
+
## Features
|
| 17 |
+
- Real-time streaming responses
|
| 18 |
+
- Financial data analysis (holdings & trades)
|
| 19 |
+
- Clean, modern UI with dark mode
|
| 20 |
+
- Markdown table rendering
|
| 21 |
+
|
| 22 |
+
## Tech Stack
|
| 23 |
+
- Backend: FastAPI + Groq AI
|
| 24 |
+
- Frontend: Next.js + Tailwind CSS
|
| 25 |
+
- Deployment: Docker
|
| 26 |
+
|
| 27 |
+
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
backend/.env.example
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
GROQ_API_KEY=your_groq_api_key_here
|
| 2 |
+
DATA_DIR=data
|
backend/requirements.txt
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
fastapi==0.115.0
|
| 2 |
+
uvicorn[standard]==0.32.0
|
| 3 |
+
pandas==2.2.3
|
| 4 |
+
groq==0.11.0
|
| 5 |
+
python-dotenv==1.0.1
|
frontend-next/src/lib/api.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
| 1 |
// Basic API client for the analytical backend
|
| 2 |
-
|
| 3 |
-
|
|
|
|
| 4 |
|
| 5 |
export const api = {
|
| 6 |
// Get all conversations
|
|
|
|
| 1 |
// Basic API client for the analytical backend
|
| 2 |
+
const API_BASE_URL = typeof window !== 'undefined' && window.location.hostname !== 'localhost'
|
| 3 |
+
? `${window.location.protocol}//${window.location.hostname}:8000`
|
| 4 |
+
: 'http://localhost:8000';
|
| 5 |
|
| 6 |
export const api = {
|
| 7 |
// Get all conversations
|