Deploy Script commited on
Commit
4d264ff
Β·
1 Parent(s): 758fc31

Deploy PuppyCompanion FastAPI 2025-06-02 10:08:59

Browse files
Files changed (2) hide show
  1. README.md +1 -88
  2. deploy_hf.sh.backup +263 -0
README.md CHANGED
@@ -1,6 +1,3 @@
1
- # Fichier README.md pour Hugging Face Spaces
2
- # Γ€ placer Γ  la racine de votre Space
3
-
4
  ---
5
  title: PuppyCompanion
6
  emoji: 🐢
@@ -9,92 +6,8 @@ colorTo: purple
9
  sdk: docker
10
  pinned: false
11
  license: mit
12
- app_port: 7860
13
  ---
14
 
15
  # PuppyCompanion 🐢
16
 
17
- An intelligent AI assistant specialized in puppy care and training, powered by advanced RAG (Retrieval-Augmented Generation) technology and a modern FastAPI backend.
18
-
19
- ## Features
20
-
21
- - **πŸ• Specialized Knowledge Base**: Expert information from professional canine care resources
22
- - **🧠 Smart Agent Workflow**: LangGraph-powered decision making with tool selection
23
- - **πŸ” Dual Search System**:
24
- - RAG system for specialized puppy knowledge from preprocessed chunks
25
- - Web search integration via Tavily for up-to-date information
26
- - **πŸ“± Modern Mobile-like Interface**: Responsive web interface with real-time updates
27
- - **πŸ”§ Real-time Debug Console**: Watch the AI's decision process in real-time
28
- - **⚑ FastAPI Backend**: High-performance async API with WebSocket support
29
- - **πŸ“Š Detailed Source Attribution**: Shows exactly which knowledge sources were used
30
-
31
- ## How it Works
32
-
33
- 1. **Question Analysis**: The agent workflow analyzes your question and selects appropriate tools
34
- 2. **RAG Search**: For dog-related questions, searches the specialized knowledge base using Qdrant vector database
35
- 3. **Quality Evaluation**: Determines if the RAG response is comprehensive enough
36
- 4. **Web Search Fallback**: If needed, uses Tavily to search for additional current information
37
- 5. **Response Generation**: Combines knowledge sources to provide a comprehensive answer
38
- 6. **Real-time Updates**: All processing steps are visible in the debug console via WebSocket
39
-
40
- ## Usage
41
-
42
- Simply ask any question about puppy care, training, behavior, or health. Examples:
43
- - "How do I house train my 8-week-old puppy?"
44
- - "What vaccination schedule should I follow?"
45
- - "How to stop my puppy from biting furniture?"
46
- - "Best foods for a growing German Shepherd puppy?"
47
-
48
- ## Technology Stack
49
-
50
- - **FastAPI**: Modern async web framework for the API backend
51
- - **LangGraph**: Agent workflow orchestration and tool selection
52
- - **OpenAI GPT-4**: Language model for natural language processing
53
- - **Qdrant**: Vector database for document embeddings and similarity search
54
- - **OpenAI Embeddings**: Text embeddings for semantic search
55
- - **Tavily**: Web search integration for real-time information
56
- - **WebSocket**: Real-time communication for debug console
57
- - **Docker**: Containerized deployment for HuggingFace Spaces
58
-
59
- ## Configuration
60
-
61
- The application requires these environment variables:
62
-
63
- ### Required
64
- - `OPENAI_API_KEY`: Your OpenAI API key for LLM processing and embeddings
65
-
66
- ### Optional
67
- - `TAVILY_API_KEY`: For web search functionality (highly recommended)
68
-
69
- **⚠️ Important**: Configure these as secrets in your HuggingFace Space settings:
70
- 1. Go to your space settings
71
- 2. Add the API keys in the "Repository secrets" section
72
- 3. Never commit API keys to your repository
73
-
74
- ## Knowledge Base
75
-
76
- The application uses a preprocessed knowledge base of professional puppy care resources:
77
- - **File**: `all_books_preprocessed_chunks.json` (2.5MB)
78
- - **Content**: Chunked and processed expert knowledge about puppy training, health, and behavior
79
- - **Vector Store**: Automatically indexed in Qdrant on first startup
80
-
81
- ## API Endpoints
82
-
83
- - `GET /`: Main web interface
84
- - `POST /chat`: Chat API endpoint for programmatic access
85
- - `WebSocket /ws`: Real-time debug console connection
86
- - `GET /health`: Application health check
87
-
88
- ## Development
89
-
90
- The application features:
91
- - **Async Architecture**: Full async/await support for optimal performance
92
- - **Persistent Vector Store**: Qdrant storage persists between restarts
93
- - **Connection Management**: WebSocket connection handling for multiple users
94
- - **Error Handling**: Comprehensive error handling and logging
95
- - **Health Monitoring**: Built-in health checks and monitoring
96
-
97
- ---
98
-
99
- *Transform the chaos of new puppy ownership into confidence with AI-powered expert assistance!* 🐾# Updated Lun 2 jui 2025 10:00:55 CEST
100
- # Force rebuild 2025-06-02 10:01:51
 
 
 
 
1
  ---
2
  title: PuppyCompanion
3
  emoji: 🐢
 
6
  sdk: docker
7
  pinned: false
8
  license: mit
 
9
  ---
10
 
11
  # PuppyCompanion 🐢
12
 
13
+ AI-powered puppy care assistant!
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
deploy_hf.sh.backup ADDED
@@ -0,0 +1,263 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/bin/bash
2
+
3
+ # HuggingFace deployment script for PuppyCompanion FastAPI
4
+ # Usage: ./deploy_hf.sh [--space-name SPACE_NAME]
5
+
6
+ set -e
7
+
8
+ # Colors
9
+ GREEN='\033[0;32m'
10
+ YELLOW='\033[1;33m'
11
+ RED='\033[0;31m'
12
+ BLUE='\033[0;34m'
13
+ NC='\033[0m'
14
+
15
+ log() {
16
+ echo -e "${GREEN}[DEPLOY]${NC} $1"
17
+ }
18
+
19
+ warn() {
20
+ echo -e "${YELLOW}[WARN]${NC} $1"
21
+ }
22
+
23
+ error() {
24
+ echo -e "${RED}[ERROR]${NC} $1"
25
+ }
26
+
27
+ info() {
28
+ echo -e "${BLUE}[INFO]${NC} $1"
29
+ }
30
+
31
+ # Default configuration
32
+ SPACE_NAME="puppycompanion-v3"
33
+ APP_DIR="."
34
+ HF_USERNAME="JTh34"
35
+
36
+ # Parse arguments
37
+ while [[ $# -gt 0 ]]; do
38
+ case $1 in
39
+ --space-name)
40
+ SPACE_NAME="$2"
41
+ shift 2
42
+ ;;
43
+ *)
44
+ error "Unknown option: $1"
45
+ exit 1
46
+ ;;
47
+ esac
48
+ done
49
+
50
+ # Ask for space name if not provided (with default)
51
+ if [ -z "$SPACE_NAME" ]; then
52
+ read -p "HuggingFace space name [puppycompanion-v3]: " SPACE_NAME
53
+ SPACE_NAME=${SPACE_NAME:-puppycompanion-v3}
54
+ fi
55
+
56
+ if [ -z "$SPACE_NAME" ]; then
57
+ error "Space name required"
58
+ exit 1
59
+ fi
60
+
61
+ # Check that the app directory exists
62
+ if [ ! -d "$APP_DIR" ]; then
63
+ error "Application directory not found: $APP_DIR"
64
+ exit 1
65
+ fi
66
+
67
+ cd "$APP_DIR"
68
+
69
+ log "Preparing deployment for FastAPI space: $HF_USERNAME/$SPACE_NAME"
70
+
71
+ # Pre-deployment checks
72
+ log "Pre-deployment checks..."
73
+
74
+ # Check required files for FastAPI application
75
+ REQUIRED_FILES=(
76
+ "main.py"
77
+ "Dockerfile"
78
+ "README.MD"
79
+ "all_books_preprocessed_chunks.json"
80
+ "rag_system.py"
81
+ "agent_workflow.py"
82
+ "embedding_models.py"
83
+ "books_config.json"
84
+ "static/index.html"
85
+ )
86
+
87
+ for file in "${REQUIRED_FILES[@]}"; do
88
+ if [ ! -f "$file" ]; then
89
+ error "Missing required file: $file"
90
+ exit 1
91
+ fi
92
+ done
93
+
94
+ log "βœ… All required files found"
95
+
96
+ # Check that API keys are not in files
97
+ ENV_FILES=(".env" ".env.local" ".env.prod" ".env.development" ".env.staging")
98
+ for env_file in "${ENV_FILES[@]}"; do
99
+ if [ -f "$env_file" ]; then
100
+ if grep -q "sk-\|OPENAI_API_KEY\|TAVILY_API_KEY" "$env_file" 2>/dev/null; then
101
+ warn "API keys detected in $env_file - file will be excluded from deployment"
102
+ warn "Make sure to configure your secrets on HuggingFace Spaces"
103
+ fi
104
+ fi
105
+ done
106
+
107
+ # Check for PDF files that should not be deployed
108
+ if ls data/*.pdf >/dev/null 2>&1; then
109
+ warn "PDF files detected in data/ directory"
110
+ warn "These will be excluded from deployment for security"
111
+ fi
112
+
113
+ # Validate chunks file size
114
+ if [ -f "all_books_preprocessed_chunks.json" ]; then
115
+ FILE_SIZE=$(stat -f%z "all_books_preprocessed_chunks.json" 2>/dev/null || stat -c%s "all_books_preprocessed_chunks.json" 2>/dev/null)
116
+ FILE_SIZE_MB=$((FILE_SIZE / 1024 / 1024))
117
+ log "Chunks file size: ${FILE_SIZE_MB}MB"
118
+ if [ $FILE_SIZE_MB -gt 100 ]; then
119
+ warn "Large chunks file detected (${FILE_SIZE_MB}MB)"
120
+ warn "This may cause slower deployment and startup times"
121
+ fi
122
+ fi
123
+
124
+ # Create requirements.txt from pyproject.toml if needed and requirements.txt doesn't exist
125
+ if [ ! -f "requirements.txt" ] && [ -f "pyproject.toml" ]; then
126
+ log "Generating requirements.txt from pyproject.toml..."
127
+ if command -v pip-compile &> /dev/null; then
128
+ pip-compile pyproject.toml
129
+ else
130
+ warn "pip-compile not found. Basic requirements.txt generation..."
131
+ # Basic extraction of dependencies from pyproject.toml
132
+ if command -v python &> /dev/null; then
133
+ python -c "
134
+ import tomllib
135
+ with open('pyproject.toml', 'rb') as f:
136
+ data = tomllib.load(f)
137
+ deps = data.get('project', {}).get('dependencies', [])
138
+ with open('requirements.txt', 'w') as f:
139
+ for dep in deps:
140
+ f.write(dep + '\n')
141
+ " 2>/dev/null || {
142
+ warn "Could not generate requirements.txt automatically"
143
+ warn "Please create the requirements.txt file manually"
144
+ exit 1
145
+ }
146
+ fi
147
+ fi
148
+ elif [ -f "requirements.txt" ]; then
149
+ log "βœ… requirements.txt found"
150
+ fi
151
+
152
+ # Validate Dockerfile for FastAPI
153
+ if grep -q "main.py" Dockerfile && grep -q "7860" Dockerfile; then
154
+ log "βœ… Dockerfile configured for FastAPI"
155
+ else
156
+ warn "Dockerfile may not be properly configured for FastAPI deployment"
157
+ fi
158
+
159
+ # Check SSH connection to HuggingFace
160
+ log "Checking HuggingFace SSH connection..."
161
+ if ! ssh -T [email protected] -o ConnectTimeout=10 -o BatchMode=yes 2>/dev/null; then
162
+ warn "SSH connection to HuggingFace failed"
163
+ info "Check your SSH configuration or use: ssh-keygen -t ed25519 -C '[email protected]'"
164
+ info "Then add the public key to your HuggingFace profile"
165
+
166
+ # Fallback to HTTPS if available
167
+ if command -v huggingface-cli &> /dev/null && huggingface-cli whoami &> /dev/null; then
168
+ warn "Using HTTPS as fallback..."
169
+ USE_SSH=false
170
+ else
171
+ error "No authentication method available"
172
+ exit 1
173
+ fi
174
+ else
175
+ log "βœ… HuggingFace SSH connection OK"
176
+ USE_SSH=true
177
+ fi
178
+
179
+ # Create the space if it does not exist (requires huggingface-cli)
180
+ if command -v huggingface-cli &> /dev/null; then
181
+ log "Creating/updating HuggingFace space..."
182
+ huggingface-cli repo create "$SPACE_NAME" --type space --space_sdk docker 2>/dev/null || true
183
+ else
184
+ warn "huggingface-cli not available, make sure the space exists"
185
+ fi
186
+
187
+ # Clone or update the repo
188
+ TEMP_DIR="/tmp/hf_deploy_$$"
189
+ log "Cloning repository..."
190
+
191
+ if [ "$USE_SSH" = true ]; then
192
+ # Use SSH
193
+ git clone "[email protected]:spaces/$HF_USERNAME/$SPACE_NAME" "$TEMP_DIR" || {
194
+ error "SSH clone failed. Check username and space name."
195
+ exit 1
196
+ }
197
+ else
198
+ # Use HTTPS
199
+ git clone "https://huggingface.co/spaces/$HF_USERNAME/$SPACE_NAME" "$TEMP_DIR" || {
200
+ error "HTTPS clone failed. Check username and space name."
201
+ exit 1
202
+ }
203
+ fi
204
+
205
+ # Copy files with FastAPI-specific exclusions
206
+ log "Copying FastAPI application files..."
207
+ rsync -av \
208
+ --exclude='.git' \
209
+ --exclude='venv_*' \
210
+ --exclude='.env*' \
211
+ --exclude='__pycache__' \
212
+ --exclude='.chainlit' \
213
+ --exclude='*.pdf' \
214
+ --exclude='*.PDF' \
215
+ --exclude='data/*.pdf' \
216
+ --exclude='data/*.PDF' \
217
+ --exclude='.DS_Store' \
218
+ --exclude='*.log' \
219
+ --exclude='qdrant_storage' \
220
+ --exclude='deploy_hf.sh' \
221
+ --exclude='.gitattributes' \
222
+ --exclude='uv.lock' \
223
+ --exclude='document_loader_preproc.py' \
224
+ ./ "$TEMP_DIR/"
225
+
226
+ # Go to temp directory
227
+ cd "$TEMP_DIR"
228
+
229
+ # Configure Git if needed
230
+ git config user.email "[email protected]" || true
231
+ git config user.name "Deploy Script" || true
232
+
233
+ # Verify critical files are present
234
+ log "Verifying deployment files..."
235
+ CRITICAL_FILES=("main.py" "Dockerfile" "all_books_preprocessed_chunks.json")
236
+ for file in "${CRITICAL_FILES[@]}"; do
237
+ if [ ! -f "$file" ]; then
238
+ error "Critical file missing after copy: $file"
239
+ exit 1
240
+ fi
241
+ done
242
+
243
+ log "βœ… All critical files verified"
244
+
245
+ # Add and commit
246
+ log "Committing changes..."
247
+ git add .
248
+ git commit -m "Deploy PuppyCompanion FastAPI $(date '+%Y-%m-%d %H:%M:%S')" || {
249
+ warn "No changes detected"
250
+ }
251
+
252
+ # Push to HuggingFace
253
+ log "Pushing to HuggingFace..."
254
+ git push
255
+
256
+ # Clean up
257
+ cd - > /dev/null
258
+ rm -rf "$TEMP_DIR"
259
+
260
+ log "πŸš€ FastAPI deployment completed successfully!"
261
+ info "Your app will be available at: https://huggingface.co/spaces/$HF_USERNAME/$SPACE_NAME"
262
+ info "Deployment may take a few minutes to build and start..."
263
+ info "Check the logs on HuggingFace Spaces for any issues"