HYPERXD commited on
Commit
7f5c2f3
Β·
1 Parent(s): 65f95d4
.env DELETED
@@ -1 +0,0 @@
1
- GROQ_API_KEY="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"
 
 
ARCHITECTURE_DIAGRAM.md DELETED
@@ -1,358 +0,0 @@
1
- # CogniChat Architecture & File Flow in HuggingFace Spaces
2
-
3
- ## πŸ—οΈ System Architecture
4
-
5
- ```
6
- β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
7
- β”‚ HuggingFace Space Environment β”‚
8
- β”‚ β”‚
9
- β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚
10
- β”‚ β”‚ Docker Container β”‚ β”‚
11
- β”‚ β”‚ β”‚ β”‚
12
- β”‚ β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ β”‚
13
- β”‚ β”‚ β”‚ Flask Application (app.py) β”‚ β”‚ β”‚
14
- β”‚ β”‚ β”‚ β”‚ β”‚ β”‚
15
- β”‚ β”‚ β”‚ Port: 7860 (HF requirement) β”‚ β”‚ β”‚
16
- β”‚ β”‚ β”‚ User: appuser (non-root) β”‚ β”‚ β”‚
17
- β”‚ β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚ β”‚
18
- β”‚ β”‚ β”‚ β”‚
19
- β”‚ β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ β”‚
20
- β”‚ β”‚ β”‚ File System Layout β”‚ β”‚ β”‚
21
- β”‚ β”‚ β”‚ β”‚ β”‚ β”‚
22
- β”‚ β”‚ β”‚ /app/ (Read-only, app code) β”‚ β”‚ β”‚
23
- β”‚ β”‚ β”‚ β”œβ”€β”€ app.py β”‚ β”‚ β”‚
24
- β”‚ β”‚ β”‚ β”œβ”€β”€ rag_processor.py β”‚ β”‚ β”‚
25
- β”‚ β”‚ β”‚ β”œβ”€β”€ templates/ β”‚ β”‚ β”‚
26
- β”‚ β”‚ β”‚ └── ... β”‚ β”‚ β”‚
27
- β”‚ β”‚ β”‚ β”‚ β”‚ β”‚
28
- β”‚ β”‚ β”‚ /tmp/ (Writable, ephemeral) β”‚ β”‚ β”‚
29
- β”‚ β”‚ β”‚ β”œβ”€β”€ uploads/ βœ… YOUR UPLOAD FOLDER β”‚ β”‚ β”‚
30
- β”‚ β”‚ β”‚ └── hf_cache/ (Model cache fallback) β”‚ β”‚ β”‚
31
- β”‚ β”‚ β”‚ β”‚ β”‚ β”‚
32
- β”‚ β”‚ β”‚ /home/appuser/ (Writable, user home) β”‚ β”‚ β”‚
33
- β”‚ β”‚ β”‚ └── .cache/ β”‚ β”‚ β”‚
34
- β”‚ β”‚ β”‚ β”œβ”€β”€ huggingface/ (Model cache) β”‚ β”‚ β”‚
35
- β”‚ β”‚ β”‚ β”œβ”€β”€ transformers/ β”‚ β”‚ β”‚
36
- β”‚ β”‚ β”‚ └── sentence_transformers/ β”‚ β”‚ β”‚
37
- β”‚ β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚ β”‚
38
- β”‚ β”‚ β”‚ β”‚
39
- β”‚ β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ β”‚
40
- β”‚ β”‚ β”‚ Environment Variables β”‚ β”‚ β”‚
41
- β”‚ β”‚ β”‚ β”‚ β”‚ β”‚
42
- β”‚ β”‚ β”‚ SPACE_ID: set by HF β”‚ β”‚ β”‚
43
- β”‚ β”‚ β”‚ GROQ_API_KEY: set in Secrets β”‚ β”‚ β”‚
44
- β”‚ β”‚ β”‚ HF_HOME: /home/appuser/.cache/... β”‚ β”‚ β”‚
45
- β”‚ β”‚ β”‚ PORT: 7860 β”‚ β”‚ β”‚
46
- β”‚ β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚ β”‚
47
- β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚
48
- β”‚ β”‚
49
- β”‚ Storage: β”‚
50
- β”‚ β”œβ”€ Ephemeral (50GB free): /tmp/, containers, app files β”‚
51
- β”‚ └─ Optional Persistent ($5-100/mo): /data/ (NOT NEEDED) β”‚
52
- β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
53
- ```
54
-
55
- ## πŸ“€ File Upload Flow
56
-
57
- ```
58
- β”Œβ”€β”€β”€οΏ½οΏ½β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
59
- β”‚ Browser β”‚
60
- β”‚ (User UI) β”‚
61
- β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”˜
62
- β”‚ 1. User uploads PDF
63
- β”‚ FormData with file
64
- ↓
65
- β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
66
- β”‚ Frontend (index.html) β”‚
67
- β”‚ β”‚
68
- β”‚ uploadAndProcessFiles() β”‚
69
- β”‚ β”œβ”€ Create FormData β”‚
70
- β”‚ β”œβ”€ POST to /upload β”‚
71
- β”‚ └─ Handle response β”‚
72
- β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
73
- β”‚ 2. POST /upload
74
- β”‚ multipart/form-data
75
- ↓
76
- β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
77
- β”‚ Backend (app.py) β”‚
78
- β”‚ β”‚
79
- β”‚ @app.route('/upload') β”‚
80
- β”‚ β”œβ”€ Detect HF Spaces environment β”‚
81
- β”‚ β”‚ └─ Upload folder = /tmp/uploads β”‚ βœ… WRITABLE!
82
- β”‚ β”‚ β”‚
83
- β”‚ β”œβ”€ Save file to disk β”‚
84
- β”‚ β”‚ file.save(filepath) β”‚
85
- β”‚ β”‚ └─ /tmp/uploads/document.pdf β”‚
86
- β”‚ β”‚ β”‚
87
- β”‚ β”œβ”€ Process file β”‚
88
- β”‚ β”‚ β”œβ”€ Load with fallback methods β”‚
89
- β”‚ β”‚ β”œβ”€ Split into chunks β”‚
90
- β”‚ β”‚ └─ Create embeddings β”‚
91
- β”‚ β”‚ β”‚
92
- β”‚ β”œβ”€ Create RAG chain β”‚
93
- β”‚ β”‚ β”œβ”€ Vector store (FAISS) β”‚
94
- β”‚ β”‚ β”œβ”€ BM25 retriever β”‚
95
- β”‚ β”‚ └─ Store in memory (rag_chains) β”‚
96
- β”‚ β”‚ β”‚
97
- β”‚ └─ Generate session ID β”‚
98
- β”‚ └─ Return to frontend β”‚
99
- β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
100
- β”‚ 3. Response with session_id
101
- β”‚ { status: 'success',
102
- β”‚ session_id: '<uuid>' }
103
- ↓
104
- β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
105
- β”‚ Frontend (index.html) β”‚
106
- β”‚ β”‚
107
- β”‚ β”œβ”€ Store session_id β”‚
108
- β”‚ β”‚ └─ sessionStorage β”‚ βœ… PERSISTS!
109
- β”‚ β”‚ β”‚
110
- β”‚ β”œβ”€ Show chat interface β”‚
111
- β”‚ └─ Ready for questions β”‚
112
- β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
113
- ```
114
-
115
- ## πŸ’¬ Chat Request Flow
116
-
117
- ```
118
- β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
119
- β”‚ Browser β”‚
120
- β”‚ β”‚
121
- β”‚ User types β”‚
122
- β”‚ question β”‚
123
- β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”˜
124
- β”‚ 1. Submit question
125
- ↓
126
- β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
127
- β”‚ Frontend (index.html) β”‚
128
- β”‚ β”‚
129
- β”‚ chatForm.submit() β”‚
130
- β”‚ β”œβ”€ Get question β”‚
131
- β”‚ β”œβ”€ Get session_id β”‚ βœ… From sessionStorage
132
- β”‚ β”‚ └─ sessionStorage.getItem β”‚
133
- β”‚ β”‚ β”‚
134
- β”‚ └─ POST to /chat β”‚
135
- β”‚ { question: "...", β”‚
136
- β”‚ session_id: "<uuid>" } β”‚
137
- β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
138
- β”‚ 2. POST /chat
139
- β”‚ with session_id in body
140
- ↓
141
- β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
142
- β”‚ Backend (app.py) β”‚
143
- β”‚ β”‚
144
- β”‚ @app.route('/chat') β”‚
145
- β”‚ β”œβ”€ Get session_id β”‚
146
- β”‚ β”‚ └─ session.get() or data.get() β”‚ βœ… DUAL SOURCE!
147
- β”‚ β”‚ β”‚
148
- β”‚ β”œβ”€ Validate session exists β”‚
149
- β”‚ β”‚ └─ Check rag_chains[session_id] β”‚
150
- β”‚ β”‚ β”‚
151
- β”‚ β”œβ”€ Invoke RAG chain β”‚
152
- β”‚ β”‚ β”œβ”€ Rewrite query β”‚
153
- β”‚ β”‚ β”œβ”€ Retrieve context β”‚
154
- β”‚ β”‚ β”‚ └─ From /tmp/uploads/... β”‚
155
- β”‚ β”‚ β”œβ”€ Generate answer (Groq API) β”‚
156
- β”‚ β”‚ └─ Refine response β”‚
157
- β”‚ β”‚ β”‚
158
- β”‚ └─ Stream response β”‚
159
- β”‚ └─ Generator function β”‚
160
- β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
161
- β”‚ 3. Stream response
162
- β”‚ text/plain chunks
163
- ↓
164
- ��────────────────────────────────┐
165
- β”‚ Frontend (index.html) β”‚
166
- β”‚ β”‚
167
- β”‚ β”œβ”€ Read stream β”‚
168
- β”‚ β”œβ”€ Parse markdown β”‚
169
- β”‚ β”œβ”€ Update UI β”‚
170
- β”‚ └─ Add TTS controls β”‚
171
- β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
172
- ```
173
-
174
- ## πŸ”„ Session Management
175
-
176
- ```
177
- β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
178
- β”‚ Session Lifecycle β”‚
179
- β”‚ β”‚
180
- β”‚ Upload ─────────────────────────────────────┐ β”‚
181
- β”‚ β”‚ β”‚ β”‚
182
- β”‚ β”œβ”€ Generate UUID β”‚ β”‚
183
- β”‚ β”‚ session_id = str(uuid.uuid4()) β”‚ β”‚
184
- β”‚ β”‚ β”‚ β”‚
185
- β”‚ β”œβ”€ Store in Flask session β”‚ β”‚
186
- β”‚ β”‚ session['session_id'] = session_id β”‚ β”‚
187
- β”‚ β”‚ β”‚ β”‚
188
- β”‚ β”œβ”€ Store RAG chain in memory β”‚ β”‚
189
- β”‚ β”‚ rag_chains[session_id] = chain β”‚ β”‚
190
- β”‚ β”‚ β”‚ β”‚
191
- β”‚ └─ Return to frontend ──────────────────┐ β”‚ β”‚
192
- β”‚ β”‚ β”‚ β”‚
193
- β”‚ Frontend ←──────────────────────────────── β”‚ β”‚ β”‚
194
- β”‚ β”‚ β”‚ β”‚ β”‚
195
- β”‚ β”œβ”€ Store in sessionStorage β”‚ β”‚ β”‚
196
- β”‚ β”‚ sessionStorage.setItem(...) β”‚ β”‚ β”‚
197
- β”‚ β”‚ β”‚ β”‚ β”‚
198
- β”‚ └─ Include in all chat requests β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚ β”‚
199
- β”‚ β”‚ β”‚
200
- β”‚ Chat β†β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚
201
- β”‚ β”‚ β”‚
202
- β”‚ β”œβ”€ Receive session_id from: β”‚
203
- β”‚ β”‚ β”œβ”€ Flask session cookie (may fail in HF) β”‚
204
- β”‚ β”‚ └─ Request body (reliable!) βœ… β”‚
205
- β”‚ β”‚ β”‚
206
- β”‚ β”œβ”€ Look up RAG chain β”‚
207
- β”‚ β”‚ chain = rag_chains[session_id] β”‚
208
- β”‚ β”‚ β”‚
209
- β”‚ └─ Use chain for answer β”‚
210
- β”‚ β”‚
211
- β”‚ Memory (In-Process) β”‚
212
- β”‚ rag_chains = { β”‚
213
- β”‚ '<uuid-1>': <RAG chain>, β”‚
214
- β”‚ '<uuid-2>': <RAG chain>, β”‚
215
- β”‚ ... β”‚
216
- β”‚ } β”‚
217
- β”‚ β”‚
218
- β”‚ message_histories = { β”‚
219
- β”‚ '<uuid-1>': <Chat history>, β”‚
220
- β”‚ '<uuid-2>': <Chat history>, β”‚
221
- β”‚ ... β”‚
222
- β”‚ } β”‚
223
- β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
224
- ```
225
-
226
- ## πŸ—„οΈ Storage Comparison
227
-
228
- ```
229
- β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
230
- β”‚ Storage Options β”‚
231
- β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
232
- β”‚ Location β”‚ Ephemeral /tmp/ β”‚ Persistent β”‚ Dataset Repoβ”‚
233
- β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
234
- β”‚ Size β”‚ 50GB β”‚ 20GB-1TB β”‚ Unlimited β”‚
235
- β”‚ Cost β”‚ FREE βœ… β”‚ $5-100/mo β”‚ FREE β”‚
236
- β”‚ Writable β”‚ YES βœ… β”‚ YES β”‚ Via API β”‚
237
- β”‚ Survives β”‚ NO (lost on β”‚ YES β”‚ YES β”‚
238
- β”‚ restart β”‚ restart) β”‚ β”‚ β”‚
239
- β”‚ Use for β”‚ Temp uploads βœ… β”‚ Model cache β”‚ Long-term β”‚
240
- β”‚ β”‚ Session data β”‚ User data β”‚ datasets β”‚
241
- β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
242
- β”‚ YOUR CHOICE β”‚ βœ… YES β”‚ ❌ NO β”‚ ❌ NO β”‚
243
- β”‚ β”‚ (Perfect fit!) β”‚ (Overkill) β”‚ (Complex) β”‚
244
- β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
245
- ```
246
-
247
- ## 🎯 Key Points
248
-
249
- ### βœ… What Works
250
-
251
- 1. **File Upload Path**: `/tmp/uploads`
252
- - Always writable in HF Spaces
253
- - 50GB free storage
254
- - No permissions issues
255
-
256
- 2. **Session Management**: Dual-source approach
257
- - Flask session (primary, may fail)
258
- - Request body (fallback, reliable)
259
- - sessionStorage (client-side persistence)
260
-
261
- 3. **Environment Detection**:
262
- ```python
263
- is_hf_spaces = bool(os.getenv("SPACE_ID"))
264
- ```
265
-
266
- 4. **Fallback Mechanisms**:
267
- - Upload folder fallback to /tmp
268
- - Cache directory fallback
269
- - PDF loading fallbacks (3 methods)
270
-
271
- ### ❌ What You Don't Need
272
-
273
- 1. **Persistent Storage** ($5-100/mo)
274
- - Your files are temporary
275
- - RAG chains are in-memory
276
- - Sessions don't need to survive restarts
277
-
278
- 2. **Dataset Storage**
279
- - Too complex for temporary uploads
280
- - Adds unnecessary latency
281
- - Your current approach is simpler
282
-
283
- 3. **External Storage** (S3, DB, etc.)
284
- - Overkill for your use case
285
- - Would require API calls
286
- - Current in-memory is faster
287
-
288
- ## πŸš€ Performance Characteristics
289
-
290
- ```
291
- β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
292
- β”‚ Performance Profile β”‚
293
- β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
294
- β”‚ File Upload β”‚ Fast (~100ms for typical PDF) β”‚
295
- β”‚ File Save to /tmp β”‚ Very Fast (~10ms SSD) β”‚
296
- β”‚ PDF Processing β”‚ Medium (seconds, depends on size)β”‚
297
- β”‚ RAG Chain Creation β”‚ Fast (~1-2 seconds) β”‚
298
- β”‚ Chat Response β”‚ Fast (streaming, <1s to start) β”‚
299
- β”‚ Memory Usage β”‚ Low (one chain per session) β”‚
300
- β”‚ Disk Usage β”‚ Low (files deleted after process)β”‚
301
- β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
302
- ```
303
-
304
- ## πŸ“Š Troubleshooting Decision Tree
305
-
306
- ```
307
- User reports issue
308
- β”‚
309
- β”œβ”€ Upload fails?
310
- β”‚ β”œβ”€ Check: upload_folder_writable in /debug
311
- β”‚ β”‚ └─ False? β†’ Space using old code, rebuild
312
- β”‚ β”‚ True? β†’ Check file size (max 100MB)
313
- β”‚ └─ Check logs for: "βœ“ Successfully saved file"
314
- β”‚
315
- β”œβ”€ Chat fails with 400?
316
- β”‚ β”œβ”€ Check: Session ID in browser console
317
- β”‚ β”‚ └─ Missing? β†’ Clear cache, re-upload
318
- β”‚ β”œβ”€ Check: Session ID in Space logs
319
- β”‚ β”‚ └─ Mismatch? β†’ Frontend not storing correctly
320
- β”‚ └─ Check: rag_chains has session
321
- β”‚ └─ Missing? β†’ Chain creation failed
322
- β”‚
323
- β”œβ”€ GROQ API errors?
324
- β”‚ β”œβ”€ Check: /debug shows groq_api_key_set: true
325
- β”‚ β”‚ └─ False? β†’ Add key to Space secrets
326
- β”‚ └─ Check: Groq Console for rate limits
327
- β”‚
328
- └─ Space won't build?
329
- β”œβ”€ Check: Dockerfile syntax
330
- β”œβ”€ Check: requirements.txt dependencies
331
- └─ Check: Space logs for build errors
332
- ```
333
-
334
- ## πŸŽ“ Best Practices Summary
335
-
336
- ### DO βœ…
337
-
338
- - Use `/tmp/` for temporary files
339
- - Return session_id in responses
340
- - Store session_id in sessionStorage
341
- - Accept session_id from request body
342
- - Set GROQ_API_KEY as Space secret
343
- - Use CPU Basic (free tier)
344
- - Implement error handling
345
- - Add session cleanup
346
-
347
- ### DON'T ❌
348
-
349
- - Use persistent storage unless truly needed
350
- - Rely only on Flask session cookies
351
- - Store files in /app/ (read-only)
352
- - Hardcode API keys
353
- - Keep sessions forever
354
- - Use relative paths for uploads
355
-
356
- ---
357
-
358
- **Your current implementation follows all best practices!** 🌟
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
DEPLOYMENT.md DELETED
@@ -1,100 +0,0 @@
1
- # Hugging Face Spaces Deployment Instructions
2
-
3
- ## Prerequisites
4
-
5
- 1. **Hugging Face Account**: Sign up at [https://huggingface.co](https://huggingface.co)
6
- 2. **Groq API Key**: Get your API key from [https://console.groq.com/](https://console.groq.com/)
7
-
8
- ## Deployment Steps
9
-
10
- ### Method 1: Direct Upload (Recommended)
11
-
12
- 1. **Create a New Space**:
13
- - Go to [https://huggingface.co/new-space](https://huggingface.co/new-space)
14
- - Choose a name for your space (e.g., `cognichat`)
15
- - Select `Docker` as the SDK
16
- - Set visibility (Public or Private)
17
- - Click "Create Space"
18
-
19
- 2. **Upload Files**:
20
- - Upload all files from this directory to your space
21
- - Make sure to include: `Dockerfile`, `requirements.txt`, `packages.txt`, `app.py`, `rag_processor.py`, `templates/`, etc.
22
-
23
- 3. **Set Environment Variables**:
24
- - In your space settings, go to "Variables and secrets"
25
- - Add a new secret: `GROQ_API_KEY` with your Groq API key
26
-
27
- 4. **Wait for Build**:
28
- - Your space will automatically build and deploy
29
- - This may take 5-10 minutes for the first build
30
-
31
- ### Method 2: Git Repository
32
-
33
- 1. **Clone Your Space**:
34
- ```bash
35
- git clone https://huggingface.co/spaces/YOUR_USERNAME/YOUR_SPACE_NAME
36
- cd YOUR_SPACE_NAME
37
- ```
38
-
39
- 2. **Copy Files**:
40
- ```bash
41
- cp -r /path/to/cognichat/* .
42
- ```
43
-
44
- 3. **Commit and Push**:
45
- ```bash
46
- git add .
47
- git commit -m "Initial commit: CogniChat application"
48
- git push
49
- ```
50
-
51
- 4. **Set Environment Variables** (same as Method 1, step 3)
52
-
53
- ## Hardware Configuration
54
-
55
- - **Recommended**: CPU Basic (free tier)
56
- - **Memory**: Should work with default allocation
57
- - **Disk**: Default allocation should be sufficient
58
-
59
- ## Important Notes
60
-
61
- 1. **Port Configuration**: The app is configured to use port 7860 (HF Spaces requirement)
62
- 2. **File Uploads**: Temporary files are stored in `/uploads` directory
63
- 3. **Environment**: Make sure `GROQ_API_KEY` is set in your space secrets
64
- 4. **Build Time**: Initial Docker build may take 10-15 minutes
65
- 5. **File Size Limits**: HF Spaces has file upload limits (typically 100MB per file)
66
-
67
- ## Troubleshooting
68
-
69
- ### Common Issues:
70
-
71
- 1. **Build Failures**:
72
- - Check logs in your space for specific error messages
73
- - Ensure all dependencies in `requirements.txt` are compatible
74
-
75
- 2. **API Key Issues**:
76
- - Verify your Groq API key is correctly set in space secrets
77
- - Check if the API key has sufficient credits
78
-
79
- 3. **Memory Issues**:
80
- - Consider upgrading to CPU Upgrade if you encounter OOM errors
81
- - Reduce chunk size in document processing if needed
82
-
83
- 4. **File Upload Issues**:
84
- - Large PDFs may timeout during processing
85
- - Consider implementing file size limits
86
-
87
- ## Monitoring
88
-
89
- - Check space logs for runtime issues
90
- - Monitor space usage in HF dashboard
91
- - Set up alerts for API usage if needed
92
-
93
- ## Updating Your Space
94
-
95
- To update your deployed application:
96
-
97
- 1. Make changes to your code
98
- 2. Push to your space repository (if using git method)
99
- 3. Or re-upload files through the web interface
100
- 4. Your space will automatically rebuild and redeploy
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
HF_SPACES_CHECKLIST.md DELETED
@@ -1,141 +0,0 @@
1
- # πŸš€ Hugging Face Spaces Deployment Checklist
2
-
3
- ## βœ… Files Created/Modified for HF Spaces
4
-
5
- ### Core Configuration Files
6
- - [x] `Dockerfile` - Optimized for CPU Basic hardware
7
- - [x] `README.md` - Updated with HF Spaces metadata
8
- - [x] `requirements.txt` - Pinned versions for stability
9
- - [x] `packages.txt` - System dependencies (libfaiss-dev)
10
- - [x] `.env.example` - Example environment variables
11
- - [x] `uploads/.gitkeep` - Ensures uploads directory exists
12
-
13
- ### Application Updates
14
- - [x] `app.py` - Modified to use port 7860 and host 0.0.0.0
15
- - [x] Added `/health` endpoint for monitoring
16
- - [x] Proper error handling for Docker environment
17
-
18
- ### Documentation
19
- - [x] `DEPLOYMENT.md` - Complete deployment instructions
20
- - [x] This checklist file
21
-
22
- ## πŸ”§ Pre-Deployment Setup
23
-
24
- ### 1. Obtain Groq API Key
25
- - [ ] Sign up at [Groq Console](https://console.groq.com/)
26
- - [ ] Generate API key
27
- - [ ] Save API key securely
28
-
29
- ### 2. Prepare Hugging Face Account
30
- - [ ] Create account at [Hugging Face](https://huggingface.co)
31
- - [ ] Verify email address
32
-
33
- ## πŸš€ Deployment Steps
34
-
35
- ### Option A: Web Interface (Recommended)
36
- 1. [ ] Go to [New Space](https://huggingface.co/new-space)
37
- 2. [ ] Choose name (e.g., "cognichat")
38
- 3. [ ] Select "Docker" SDK
39
- 4. [ ] Set visibility (Public/Private)
40
- 5. [ ] Click "Create Space"
41
- 6. [ ] Upload all project files
42
- 7. [ ] In Settings β†’ Variables and secrets:
43
- - [ ] Add secret: `GROQ_API_KEY` = `your_api_key`
44
- 8. [ ] Wait for build (10-15 minutes)
45
-
46
- ### Option B: Git Repository
47
- 1. [ ] Clone your space: `git clone https://huggingface.co/spaces/USERNAME/SPACE_NAME`
48
- 2. [ ] Copy files: `cp -r /path/to/cognichat/* .`
49
- 3. [ ] Commit and push: `git add . && git commit -m "Deploy CogniChat" && git push`
50
- 4. [ ] Set environment variables in web interface
51
-
52
- ## βš™οΈ Configuration Settings
53
-
54
- ### Hardware
55
- - [x] **Recommended**: CPU Basic (Free)
56
- - [x] **Alternative**: CPU Upgrade if needed
57
- - [x] **Memory**: Default allocation sufficient
58
- - [x] **Storage**: Default allocation sufficient
59
-
60
- ### Environment Variables Required
61
- - [ ] `GROQ_API_KEY` - Your Groq API key
62
-
63
- ### Environment Variables Optional
64
- - [ ] `PORT` - Defaults to 7860 (HF standard)
65
-
66
- ## πŸ§ͺ Testing Checklist
67
-
68
- ### Pre-deployment (Local)
69
- - [ ] Test with `docker build -t cognichat .`
70
- - [ ] Test with `docker run -p 7860:7860 -e GROQ_API_KEY=your_key cognichat`
71
- - [ ] Verify app loads at `http://localhost:7860`
72
- - [ ] Test file upload functionality
73
- - [ ] Test chat functionality
74
- - [ ] Test TTS functionality
75
-
76
- ### Post-deployment (HF Spaces)
77
- - [ ] Space builds successfully
78
- - [ ] App loads without errors
79
- - [ ] Health check responds: `/health`
80
- - [ ] File upload works
81
- - [ ] Document processing works
82
- - [ ] Chat responses stream properly
83
- - [ ] TTS audio generation works
84
- - [ ] No memory/timeout issues
85
-
86
- ## πŸ” Monitoring & Maintenance
87
-
88
- ### Regular Checks
89
- - [ ] Monitor space logs for errors
90
- - [ ] Check Groq API usage/limits
91
- - [ ] Monitor space resource usage
92
- - [ ] Test with different document types
93
-
94
- ### Performance Optimization
95
- - [ ] Monitor build times
96
- - [ ] Check response times
97
- - [ ] Monitor memory usage
98
- - [ ] Optimize document chunk sizes if needed
99
-
100
- ## 🚨 Troubleshooting Common Issues
101
-
102
- ### Build Issues
103
- - [ ] Check all files uploaded correctly
104
- - [ ] Verify Dockerfile syntax
105
- - [ ] Check requirements.txt dependencies
106
- - [ ] Review build logs for specific errors
107
-
108
- ### Runtime Issues
109
- - [ ] Verify GROQ_API_KEY is set correctly
110
- - [ ] Check API key has sufficient credits
111
- - [ ] Monitor memory usage (upgrade to CPU Upgrade if needed)
112
- - [ ] Check document size limits
113
-
114
- ### Upload Issues
115
- - [ ] Verify uploads directory permissions
116
- - [ ] Check file size limits (100MB default)
117
- - [ ] Test with smaller files first
118
- - [ ] Monitor processing times
119
-
120
- ## πŸ“ž Support Resources
121
-
122
- - [HF Spaces Documentation](https://huggingface.co/docs/hub/spaces)
123
- - [Docker Documentation](https://docs.docker.com/)
124
- - [Flask Documentation](https://flask.palletsprojects.com/)
125
- - [LangChain Documentation](https://docs.langchain.com/)
126
- - [Groq API Documentation](https://console.groq.com/docs)
127
-
128
- ---
129
-
130
- ## πŸŽ‰ Final Notes
131
-
132
- Your CogniChat application is now ready for deployment to Hugging Face Spaces! The configuration has been optimized for:
133
-
134
- - βœ… CPU Basic hardware compatibility
135
- - βœ… Docker containerization
136
- - βœ… Proper security (non-root user)
137
- - βœ… Health monitoring
138
- - βœ… Resource efficiency
139
- - βœ… Error handling
140
-
141
- Good luck with your deployment! πŸš€
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
HF_SPACES_FILE_STORAGE_GUIDE.md DELETED
@@ -1,423 +0,0 @@
1
- # HuggingFace Spaces File Storage & Access Solutions
2
-
3
- ## 🎯 Problem Summary
4
-
5
- Your CogniChat application needs to:
6
- 1. **Upload files** (PDFs, documents) from users
7
- 2. **Store them temporarily** during processing
8
- 3. **Access them** for RAG chain creation
9
- 4. **Persist session data** across requests
10
-
11
- ## πŸ“Š Current Implementation Analysis
12
-
13
- ### βœ… What's Already Fixed (Good News!)
14
-
15
- Your application is **already configured correctly** for HF Spaces:
16
-
17
- ```python
18
- # app.py - Lines 40-52
19
- is_hf_spaces = bool(os.getenv("SPACE_ID") or os.getenv("SPACES_ZERO_GPU"))
20
- if is_hf_spaces:
21
- app.config['UPLOAD_FOLDER'] = '/tmp/uploads' # βœ… CORRECT!
22
- else:
23
- app.config['UPLOAD_FOLDER'] = 'uploads'
24
-
25
- # Fallback mechanism is also in place
26
- try:
27
- os.makedirs(app.config['UPLOAD_FOLDER'], exist_ok=True)
28
- print(f"βœ“ Upload folder ready: {app.config['UPLOAD_FOLDER']}")
29
- except Exception as e:
30
- app.config['UPLOAD_FOLDER'] = '/tmp/uploads' # βœ… Good fallback
31
- ```
32
-
33
- **Why `/tmp/uploads` is correct:**
34
- - βœ… `/tmp` directory is **always writable** in HF Spaces
35
- - βœ… Ephemeral storage is **free** and sufficient for temporary file uploads
36
- - βœ… Your files don't need to persist beyond the session
37
- - βœ… 50GB ephemeral storage is available for free
38
-
39
- ### ⚠️ Potential Session Management Issue
40
-
41
- The real issue appears to be **Flask session persistence**, not file storage:
42
-
43
- ```python
44
- # Current flow in app.py
45
- session['session_id'] = session_id # Flask session cookie
46
- return jsonify({
47
- 'session_id': session_id # Also return in response βœ…
48
- })
49
- ```
50
-
51
- **HF Spaces considerations:**
52
- - Flask sessions use **signed cookies** (should work)
53
- - Your frontend now stores `session_id` in `sessionStorage` βœ…
54
- - Backend accepts session_id from **both sources** βœ…
55
-
56
- ## πŸ” Storage Options in HuggingFace Spaces
57
-
58
- ### Option 1: Ephemeral Storage (FREE - Current Approach) βœ…
59
-
60
- **What you're using now:**
61
- - **Location**: `/tmp/` directory
62
- - **Size**: 50GB
63
- - **Persistence**: Lost on Space restart
64
- - **Cost**: FREE
65
- - **Best for**: Temporary file uploads, session-based data
66
-
67
- **Your implementation:**
68
- ```python
69
- app.config['UPLOAD_FOLDER'] = '/tmp/uploads'
70
- ```
71
-
72
- βœ… **This is PERFECT for your use case** because:
73
- - File uploads are temporary (only needed during chat session)
74
- - RAG chain creation happens immediately after upload
75
- - No need to store files permanently
76
- - Free tier is sufficient
77
-
78
- ### Option 2: Persistent Storage (/data) - NOT NEEDED
79
-
80
- **When to use:**
81
- - Files need to survive Space restarts
82
- - Long-term data storage required
83
- - Model cache persistence
84
- - **Cost**: $5-$100/month
85
-
86
- **How to implement** (if needed in future):
87
- ```python
88
- # Not necessary for your app, but here's how:
89
- if is_hf_spaces:
90
- app.config['UPLOAD_FOLDER'] = '/data/uploads' # Persistent
91
- os.environ['HF_HOME'] = '/data/.huggingface' # Cache models here
92
- ```
93
-
94
- **Why you DON'T need this:**
95
- - ❌ Costs money ($5/month minimum)
96
- - ❌ Your files are temporary
97
- - ❌ RAG chains are in-memory
98
- - βœ… You're already using ephemeral storage correctly
99
-
100
- ### Option 3: Dataset Storage - OVERKILL
101
-
102
- **What it is:**
103
- - Use HF Dataset repo as file storage
104
- - Programmatic uploads with `huggingface_hub`
105
- - Git LFS for large files
106
-
107
- **Why you don't need it:**
108
- - ❌ Complex implementation
109
- - ❌ Not designed for temporary uploads
110
- - ❌ Adds latency
111
- - βœ… Your current approach is simpler and better
112
-
113
- ## πŸ› οΈ Recommended Solutions
114
-
115
- ### Solution 1: Verify Current Setup (Most Likely)
116
-
117
- Your file storage is **already working correctly**. The issue is likely session management:
118
-
119
- **Checklist:**
120
- ```bash
121
- # 1. Check if upload folder is writable
122
- curl https://YOUR-SPACE-URL/debug
123
-
124
- # Expected output:
125
- {
126
- "upload_folder": "/tmp/uploads",
127
- "upload_folder_writable": true, # βœ… Should be true
128
- "groq_api_key_set": true,
129
- "sessions_count": X,
130
- "hf_spaces_detected": true
131
- }
132
- ```
133
-
134
- **Test upload:**
135
- 1. Upload a file in HF Space
136
- 2. Check logs for: `βœ“ Successfully saved file: /tmp/uploads/filename.pdf`
137
- 3. If you see this, **file storage is working!**
138
-
139
- ### Solution 2: Session Persistence Fix (Already Implemented!)
140
-
141
- Your recent changes **should have fixed this**:
142
-
143
- **Frontend (index.html):**
144
- ```javascript
145
- // Store session ID
146
- if (result.session_id) {
147
- sessionId = result.session_id;
148
- sessionStorage.setItem('cognichat_session_id', sessionId);
149
- }
150
-
151
- // Send session ID in chat requests
152
- const requestBody = { question: question };
153
- if (sessionId) {
154
- requestBody.session_id = sessionId;
155
- }
156
- ```
157
-
158
- **Backend (app.py):**
159
- ```python
160
- # Accept session_id from multiple sources
161
- session_id = session.get('session_id') or data.get('session_id')
162
- ```
163
-
164
- ### Solution 3: Enhanced Debugging
165
-
166
- Add this endpoint to diagnose issues:
167
-
168
- ```python
169
- @app.route('/upload-test', methods=['POST'])
170
- def upload_test():
171
- """Test file upload capabilities."""
172
- try:
173
- # Create test file
174
- test_file = os.path.join(app.config['UPLOAD_FOLDER'], 'test.txt')
175
- with open(test_file, 'w') as f:
176
- f.write('test content')
177
-
178
- # Verify it's readable
179
- with open(test_file, 'r') as f:
180
- content = f.read()
181
-
182
- # Clean up
183
- os.remove(test_file)
184
-
185
- return jsonify({
186
- 'status': 'success',
187
- 'upload_folder': app.config['UPLOAD_FOLDER'],
188
- 'writable': True,
189
- 'readable': True,
190
- 'message': 'File upload system working correctly'
191
- })
192
- except Exception as e:
193
- return jsonify({
194
- 'status': 'error',
195
- 'error': str(e),
196
- 'upload_folder': app.config['UPLOAD_FOLDER']
197
- }), 500
198
- ```
199
-
200
- ## πŸš€ Deployment Checklist for HF Spaces
201
-
202
- ### Pre-Deployment βœ…
203
-
204
- - [x] Upload folder set to `/tmp/uploads`
205
- - [x] Environment detection working (`SPACE_ID` check)
206
- - [x] Fallback mechanisms in place
207
- - [x] Session ID returned in upload response
208
- - [x] Frontend stores and sends session_id
209
- - [x] Backend accepts session_id from request body
210
-
211
- ### In HF Spaces Settings βš™οΈ
212
-
213
- 1. **Environment Variables/Secrets:**
214
- - [ ] `GROQ_API_KEY` set as a secret
215
-
216
- 2. **Hardware:**
217
- - [ ] CPU Basic (free tier) - sufficient for your app
218
-
219
- 3. **Storage:**
220
- - [ ] Use **default ephemeral storage** (no upgrade needed!)
221
-
222
- ### Testing in HF Space πŸ§ͺ
223
-
224
- ```bash
225
- # 1. Check debug endpoint
226
- curl https://YOUR-SPACE/debug
227
-
228
- # 2. Test file upload
229
- # Upload a small PDF through the UI
230
- # Check Space logs for:
231
- # βœ“ Upload folder ready: /tmp/uploads
232
- # βœ“ Successfully saved file: /tmp/uploads/test.pdf
233
- # βœ“ Successfully processed: test.pdf
234
-
235
- # 3. Test chat functionality
236
- # Ask a question after upload
237
- # Check logs for:
238
- # βœ“ Session ID found: <uuid>
239
- # βœ“ RAG chain found for session: <uuid>
240
- ```
241
-
242
- ## πŸ› Troubleshooting Guide
243
-
244
- ### Issue: "Permission denied" when saving files
245
-
246
- **Diagnosis:**
247
- ```python
248
- # Check in /debug endpoint:
249
- "upload_folder_writable": false # ❌ Problem!
250
- ```
251
-
252
- **Solution:**
253
- ```python
254
- # Should automatically fallback, but verify:
255
- if is_hf_spaces:
256
- app.config['UPLOAD_FOLDER'] = '/tmp/uploads' # Must be /tmp
257
- ```
258
-
259
- ### Issue: "Session not found" errors
260
-
261
- **Diagnosis:**
262
- ```python
263
- # In chat request logs:
264
- "Session ID from Flask session: None" # ❌
265
- "Session ID from request body: <uuid>" # βœ… This should work!
266
- "Final session ID used: <uuid>" # βœ…
267
- "Available RAG chains: [<uuid>]" # βœ…
268
- ```
269
-
270
- **Solution:**
271
- Your frontend **already handles this**:
272
- ```javascript
273
- // Sends session_id in request body
274
- if (sessionId) {
275
- requestBody.session_id = sessionId;
276
- }
277
- ```
278
-
279
- ### Issue: Files upload but chat fails
280
-
281
- **Symptoms:**
282
- - Upload returns 200 OK
283
- - Chat returns 400 Bad Request
284
- - Logs show: "Session ID <uuid> not found in RAG chains"
285
-
286
- **Root Cause:**
287
- Session ID mismatch between upload and chat
288
-
289
- **Solution:**
290
- 1. Clear browser's sessionStorage
291
- 2. Upload a new file
292
- 3. Check console for: "Stored session ID from upload: <uuid>"
293
- 4. Verify same UUID is sent in chat request
294
-
295
- ## πŸ“ˆ Performance Optimization Tips
296
-
297
- ### 1. Model Caching (Already Optimized!)
298
-
299
- Your code already handles this:
300
-
301
- ```python
302
- # Cache directories set correctly
303
- cache_base = os.path.expanduser("~/.cache") if os.path.expanduser("~") != "~" else "/tmp/hf_cache"
304
- os.environ['HF_HOME'] = f'{cache_base}/huggingface'
305
- ```
306
-
307
- **For faster restarts** (optional, costs money):
308
- ```python
309
- # Only if you subscribe to persistent storage:
310
- if is_hf_spaces and os.path.exists('/data'):
311
- os.environ['HF_HOME'] = '/data/.huggingface' # Persist model cache
312
- ```
313
-
314
- ### 2. File Cleanup (Recommended Addition)
315
-
316
- Add automatic cleanup to prevent disk space issues:
317
-
318
- ```python
319
- import atexit
320
- import shutil
321
-
322
- def cleanup_uploads():
323
- """Clean up temporary upload files on shutdown."""
324
- try:
325
- if os.path.exists(app.config['UPLOAD_FOLDER']):
326
- shutil.rmtree(app.config['UPLOAD_FOLDER'])
327
- print(f"βœ“ Cleaned up upload folder: {app.config['UPLOAD_FOLDER']}")
328
- except Exception as e:
329
- print(f"Warning: Could not clean up uploads: {e}")
330
-
331
- # Register cleanup on exit
332
- atexit.register(cleanup_uploads)
333
- ```
334
-
335
- ### 3. Session Management Optimization
336
-
337
- Add session timeout and cleanup:
338
-
339
- ```python
340
- from datetime import datetime, timedelta
341
-
342
- # Track session creation times
343
- session_timestamps = {}
344
-
345
- def cleanup_old_sessions():
346
- """Remove sessions older than 1 hour."""
347
- cutoff = datetime.now() - timedelta(hours=1)
348
- old_sessions = [
349
- sid for sid, timestamp in session_timestamps.items()
350
- if timestamp < cutoff
351
- ]
352
-
353
- for sid in old_sessions:
354
- if sid in rag_chains:
355
- del rag_chains[sid]
356
- if sid in message_histories:
357
- del message_histories[sid]
358
- if sid in session_timestamps:
359
- del session_timestamps[sid]
360
-
361
- if old_sessions:
362
- print(f"βœ“ Cleaned up {len(old_sessions)} old sessions")
363
-
364
- # Call periodically or on upload
365
- ```
366
-
367
- ## πŸ’‘ Best Practices Summary
368
-
369
- ### DO βœ…
370
-
371
- 1. **Use `/tmp/uploads`** for ephemeral file storage (free!)
372
- 2. **Return session_id** in upload response
373
- 3. **Store session_id** in browser sessionStorage
374
- 4. **Send session_id** in chat request body
375
- 5. **Accept session_id** from multiple sources
376
- 6. **Clean up old sessions** to free memory
377
- 7. **Set GROQ_API_KEY** as HF Space secret
378
-
379
- ### DON'T ❌
380
-
381
- 1. ❌ Use persistent storage unless truly needed (costs money)
382
- 2. ❌ Store files in current directory (read-only in Spaces)
383
- 3. ❌ Rely only on Flask session cookies
384
- 4. ❌ Keep all sessions forever (memory leak)
385
- 5. ❌ Hardcode API keys in code
386
-
387
- ## πŸŽ‰ Conclusion
388
-
389
- **Your current implementation is CORRECT!**
390
-
391
- You're using:
392
- - βœ… `/tmp/uploads` for temporary storage (free, writable)
393
- - βœ… Session ID in response + sessionStorage
394
- - βœ… Dual session ID sources (Flask + request body)
395
- - βœ… Proper HF Spaces environment detection
396
-
397
- **If you're still seeing issues**, it's likely:
398
- 1. Browser cache (clear sessionStorage)
399
- 2. Session ID not being stored correctly in frontend
400
- 3. GROQ API key not set in HF Spaces secrets
401
-
402
- **Test with:**
403
- ```bash
404
- # Visit your Space's debug endpoint
405
- https://YOUR-SPACE-URL/debug
406
-
407
- # Check for:
408
- "upload_folder_writable": true
409
- "groq_api_key_set": true
410
- "hf_spaces_detected": true
411
- ```
412
-
413
- **Your app should work perfectly in HF Spaces without any storage upgrades!** πŸš€
414
-
415
- ---
416
-
417
- ## πŸ“š Additional Resources
418
-
419
- - [HF Spaces Storage Docs](https://huggingface.co/docs/hub/spaces-storage)
420
- - [HF Spaces Docker Guide](https://huggingface.co/docs/hub/spaces-sdks-docker)
421
- - [Flask Session Management](https://flask.palletsprojects.com/en/2.3.x/quickstart/#sessions)
422
- - [Your DEPLOYMENT.md](./DEPLOYMENT.md)
423
- - [Your HF_SPACES_CHECKLIST.md](./HF_SPACES_CHECKLIST.md)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
PERMISSION_FIXES.md DELETED
@@ -1,142 +0,0 @@
1
- # CogniChat Permission Issues Fix Summary
2
-
3
- ## Problem Analysis
4
- The application was failing with permission errors when trying to create cache directories for HuggingFace models:
5
- ```
6
- PermissionError: [Errno 13] Permission denied: '/app/.cache/sentence_transformers/models--sentence-transformers--all-miniLM-L6-v2'
7
- ```
8
-
9
- ## Root Causes
10
- 1. **Docker Container Permissions**: The application was running as root but trying to create directories that later needed write access
11
- 2. **Cache Directory Location**: Using `/app/.cache` which had restrictive permissions
12
- 3. **Model Loading**: No fallback mechanism when primary cache locations failed
13
-
14
- ## Implemented Fixes
15
-
16
- ### 1. Dockerfile Improvements
17
- - **Non-root User**: Created `appuser` with UID 1000 to run the application securely
18
- - **Cache Location**: Moved cache to user home directory (`/home/appuser/.cache/`)
19
- - **Proper Permissions**: Set ownership and permissions correctly during build
20
- - **Environment Variables**: Updated to point to user-writable locations
21
-
22
- ### 2. Application Code Enhancements (app.py)
23
- - **Dynamic Cache Detection**: Uses `expanduser()` to detect home directory or fallback to `/tmp`
24
- - **Robust Error Handling**: Multiple fallback strategies for model loading
25
- - **Permission Handling**: Creates directories with proper permissions (755/777)
26
- - **Fallback Directories**: Uses `/tmp` if home directory cache fails
27
-
28
- ### 3. Environment Variable Management
29
- - **HF_HOME**: `/home/appuser/.cache/huggingface`
30
- - **HF_HUB_CACHE**: `/home/appuser/.cache/huggingface/hub`
31
- - **TRANSFORMERS_CACHE**: `/home/appuser/.cache/transformers`
32
- - **SENTENCE_TRANSFORMERS_HOME**: `/home/appuser/.cache/sentence_transformers`
33
-
34
- ### 4. Model Loading Strategy
35
- ```python
36
- # Primary attempt with full model name
37
- model_name="sentence-transformers/all-miniLM-L6-v2"
38
-
39
- # Fallback 1: Short name
40
- model_name="all-miniLM-L6-v2"
41
-
42
- # Fallback 2: No cache specification
43
- # Let the model use default cache location
44
- ```
45
-
46
- ## Key Changes Made
47
-
48
- ### Dockerfile
49
- ```dockerfile
50
- # Before: Running as root with /app/.cache
51
- # After: Running as appuser with /home/appuser/.cache
52
- RUN useradd --create-home --shell /bin/bash --uid 1000 appuser
53
- # ... proper ownership and permissions setup
54
- USER appuser
55
- ```
56
-
57
- ### app.py
58
- ```python
59
- # Before: Fixed /app/.cache paths
60
- # After: Dynamic cache detection
61
- cache_base = os.path.expanduser("~/.cache") if os.path.expanduser("~") != "~" else "/tmp/hf_cache"
62
- ```
63
-
64
- ## Verification Steps
65
- 1. **Build Docker Image**: `docker build -t cognichat .`
66
- 2. **Run Container**: `docker run -p 7860:7860 cognichat`
67
- 3. **Check Logs**: Verify no permission errors during startup
68
- 4. **Test Upload**: Upload documents to verify model loading works
69
- 5. **Test Chat**: Ensure RAG chain functions properly
70
-
71
- ## Additional Benefits
72
- - **Security**: Application now runs as non-root user
73
- - **Portability**: Works in various environments (Docker, local, cloud)
74
- - **Reliability**: Multiple fallback mechanisms prevent startup failures
75
- - **Debugging**: Better error messages and logging
76
-
77
- ## Additional Fix: Missing BM25 Dependency
78
-
79
- ### Problem
80
- ```
81
- Error: Failed to process files: Could not import rank_bm25, please install with `pip install rank_bm25`.
82
- ```
83
-
84
- ### Solution
85
- Added missing dependency to `requirements.txt`:
86
- ```
87
- rank-bm25==0.2.2
88
- ```
89
-
90
- The BM25Retriever component requires the `rank_bm25` package for implementing the BM25 ranking algorithm used in hybrid search.
91
-
92
- ## Additional Fix: Corrupted PDF Handling
93
-
94
- ### Problem
95
- ```
96
- Error: Failed to process files: Unable to find 'endstream' marker for obj starting at 55599.
97
- ```
98
-
99
- This error occurs when PDFs are corrupted or malformed, causing PyPDFLoader to fail.
100
-
101
- ### Solution
102
- Implemented a robust multi-method PDF processing system:
103
-
104
- 1. **Enhanced Error Handling**: Added specific error messages for different PDF issues
105
- 2. **Multiple PDF Parsers**: Added fallback methods using PyMuPDF and pdfplumber
106
- 3. **Graceful Degradation**: Files that fail to process are reported with specific error messages
107
- 4. **Partial Success**: Successfully processed files are still used even if some files fail
108
-
109
- #### New Dependencies Added:
110
- ```
111
- pymupdf==1.23.26 # Alternative PDF parser (handles corrupted files better)
112
- pdfplumber==0.10.3 # Additional PDF parsing method
113
- ```
114
-
115
- #### Custom PDF Loader Function:
116
- - **Method 1**: PyPDFLoader (primary method)
117
- - **Method 2**: PyMuPDF/fitz (handles corrupted PDFs better)
118
- - **Method 3**: pdfplumber (alternative extraction method)
119
-
120
- #### Enhanced User Experience:
121
- - Clear error messages for specific PDF issues (corruption, password protection, etc.)
122
- - Partial processing success (some files work, others fail gracefully)
123
- - Detailed feedback on which files succeeded/failed and why
124
-
125
- ## Files Modified
126
- - `Dockerfile` - Container configuration and permissions
127
- - `app.py` - Cache handling, model loading, and robust PDF processing
128
- - `requirements.txt` - Added dependencies: rank-bm25, pymupdf, pdfplumber
129
- - `test_dependencies.py` - Created for testing all dependencies and PDF handling
130
- - `PERMISSION_FIXES.md` - This comprehensive documentation
131
- - `README.md` - Updated setup instructions and troubleshooting
132
-
133
- ## Summary of All Fixes
134
-
135
- βœ… **Permission Issues**: Fixed Docker cache directory permissions
136
- βœ… **Security**: Application runs as non-root user
137
- βœ… **Missing Dependencies**: Added rank-bm25 for BM25 retrieval
138
- βœ… **PDF Corruption**: Multi-method PDF processing with fallbacks
139
- βœ… **Error Handling**: Graceful degradation and detailed error messages
140
- βœ… **User Experience**: Clear feedback on file processing success/failure
141
-
142
- The fixes ensure the application can handle various deployment scenarios while maintaining security best practices, providing robust error recovery, handling corrupted files gracefully, and including all required dependencies.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
QUICK_FIX_FILE_ACCESS.md DELETED
@@ -1,302 +0,0 @@
1
- # 🎯 QUICK FIX SUMMARY: File Access in HuggingFace Spaces
2
-
3
- ## βœ… GOOD NEWS: Your Implementation is Already Correct!
4
-
5
- After indexing your entire codebase and analyzing HuggingFace Spaces documentation, **your file access implementation is perfect for HF Spaces**.
6
-
7
- ## πŸ“Š What I Found
8
-
9
- ### Current Implementation (app.py lines 40-52):
10
-
11
- ```python
12
- is_hf_spaces = bool(os.getenv("SPACE_ID") or os.getenv("SPACES_ZERO_GPU"))
13
- if is_hf_spaces:
14
- app.config['UPLOAD_FOLDER'] = '/tmp/uploads' # βœ… PERFECT!
15
- else:
16
- app.config['UPLOAD_FOLDER'] = 'uploads'
17
-
18
- # With fallback mechanism
19
- try:
20
- os.makedirs(app.config['UPLOAD_FOLDER'], exist_ok=True)
21
- except Exception as e:
22
- app.config['UPLOAD_FOLDER'] = '/tmp/uploads' # βœ… Good fallback
23
- ```
24
-
25
- **This is EXACTLY the right approach!** ✨
26
-
27
- ## πŸ” Why Your Implementation is Correct
28
-
29
- ### βœ… Using `/tmp/uploads` (Ephemeral Storage)
30
-
31
- **HuggingFace Spaces provides:**
32
- - **50GB free ephemeral storage** in `/tmp/` directory
33
- - Always writable, no permissions needed
34
- - Perfect for temporary file uploads
35
- - **No cost** - completely free!
36
-
37
- **Your use case:**
38
- - Upload PDFs/documents
39
- - Process them immediately
40
- - Create RAG chain (in-memory)
41
- - Files don't need to persist
42
-
43
- **Result:** `/tmp/uploads` is the **optimal solution** for your needs!
44
-
45
- ### ❌ What You DON'T Need
46
-
47
- **Persistent Storage (/data)**
48
- - Costs $5-$100/month
49
- - Survives Space restarts
50
- - NOT needed because:
51
- - Your files are temporary
52
- - RAG chains are in-memory
53
- - Each session is independent
54
-
55
- ## πŸ”₯ The Real Issue (If Any)
56
-
57
- Based on your conversation history, the issue isn't file access - it's **session management**.
58
-
59
- ### What We Fixed:
60
-
61
- 1. **Frontend (index.html)** - Now stores and sends session_id:
62
- ```javascript
63
- // Stores session ID from upload
64
- if (result.session_id) {
65
- sessionId = result.session_id;
66
- sessionStorage.setItem('cognichat_session_id', sessionId);
67
- }
68
-
69
- // Sends it in chat requests
70
- const requestBody = { question: question };
71
- if (sessionId) {
72
- requestBody.session_id = sessionId;
73
- }
74
- ```
75
-
76
- 2. **Backend (app.py)** - Accepts session_id from multiple sources:
77
- ```python
78
- # Try Flask session first, fallback to request body
79
- session_id = session.get('session_id') or data.get('session_id')
80
- ```
81
-
82
- ## πŸš€ Deployment Checklist
83
-
84
- ### Before Deploying to HF Spaces:
85
-
86
- - [x] Upload folder configured for `/tmp/uploads`
87
- - [x] Environment detection working
88
- - [x] Session ID management implemented
89
- - [x] Error handling and fallbacks in place
90
- - [x] No duplicate HTML content
91
-
92
- ### In HuggingFace Spaces:
93
-
94
- 1. **Create Space**
95
- - Go to https://huggingface.co/new-space
96
- - Select **Docker** as SDK
97
- - Choose a name
98
-
99
- 2. **Upload Files**
100
- - Upload all project files
101
- - Ensure `Dockerfile`, `requirements.txt`, `packages.txt` included
102
-
103
- 3. **Set Secret**
104
- - Settings β†’ Repository Secrets
105
- - Add: `GROQ_API_KEY` = your_actual_key
106
-
107
- 4. **Wait for Build**
108
- - Initial build: 10-15 minutes
109
- - Space will auto-restart
110
-
111
- 5. **Test**
112
- ```bash
113
- # Check debug endpoint
114
- https://YOUR-SPACE/debug
115
-
116
- # Should show:
117
- {
118
- "upload_folder": "/tmp/uploads",
119
- "upload_folder_writable": true,
120
- "groq_api_key_set": true,
121
- "hf_spaces_detected": true
122
- }
123
- ```
124
-
125
- ## πŸ› If You Still See Issues
126
-
127
- ### Issue: "Permission denied" when saving files
128
-
129
- **Check:**
130
- ```bash
131
- curl https://YOUR-SPACE/debug
132
- ```
133
-
134
- **Look for:**
135
- ```json
136
- {
137
- "upload_folder": "/tmp/uploads", // βœ… Should be /tmp
138
- "upload_folder_writable": true // βœ… Should be true
139
- }
140
- ```
141
-
142
- **If false:**
143
- - Your Space might be using old code
144
- - Rebuild: Settings β†’ Factory Reboot
145
-
146
- ### Issue: "Session not found" in chat
147
-
148
- **Check browser console:**
149
- ```javascript
150
- // Should see:
151
- "Stored session ID from upload: <uuid>"
152
- "Including session ID in chat request: <uuid>"
153
- ```
154
-
155
- **If missing:**
156
- 1. Clear browser cache and sessionStorage
157
- 2. Upload a new file
158
- 3. Check that index.html has no duplicates
159
- 4. Verify session_id in upload response
160
-
161
- ### Issue: Chat returns 400
162
-
163
- **Check Space logs:**
164
- ```
165
- Session ID from Flask session: None // OK, expected in HF Spaces
166
- Session ID from request body: <uuid> // βœ… This should work
167
- Final session ID used: <uuid> // βœ…
168
- Available RAG chains: [<uuid>] // βœ… Must match
169
- ```
170
-
171
- **If UUIDs don't match:**
172
- - Frontend isn't storing/sending session_id correctly
173
- - Check index.html for duplicate functions
174
- - Verify sessionStorage is working in browser
175
-
176
- ## πŸ“ˆ Performance Tips
177
-
178
- ### 1. Model Cache (Optional)
179
-
180
- If you want faster cold starts (costs money):
181
-
182
- ```python
183
- # Subscribe to persistent storage in HF Space
184
- # Then in app.py:
185
- if is_hf_spaces and os.path.exists('/data'):
186
- os.environ['HF_HOME'] = '/data/.huggingface'
187
- ```
188
-
189
- **Cost:** $5/month for 20GB persistent storage
190
- **Benefit:** Models cached between restarts
191
- **Needed?** Only if cold starts are too slow
192
-
193
- ### 2. Session Cleanup (Recommended)
194
-
195
- Add to app.py to prevent memory leaks:
196
-
197
- ```python
198
- from datetime import datetime, timedelta
199
-
200
- session_timestamps = {}
201
-
202
- def cleanup_old_sessions():
203
- """Remove sessions older than 1 hour."""
204
- cutoff = datetime.now() - timedelta(hours=1)
205
- old_sessions = [
206
- sid for sid, timestamp in session_timestamps.items()
207
- if timestamp < cutoff
208
- ]
209
- for sid in old_sessions:
210
- if sid in rag_chains:
211
- del rag_chains[sid]
212
- if sid in message_histories:
213
- del message_histories[sid]
214
-
215
- # Call in upload endpoint after creating new chain
216
- cleanup_old_sessions()
217
- ```
218
-
219
- ## πŸ“š Key Documentation References
220
-
221
- From HuggingFace official docs:
222
-
223
- 1. **Ephemeral Storage (What you're using):**
224
- - Location: `/tmp/` directory
225
- - Size: 50GB
226
- - Cost: FREE
227
- - Persistence: Lost on restart (perfect for your use case)
228
-
229
- 2. **Persistent Storage (Not needed):**
230
- - Location: `/data/` directory
231
- - Cost: $5-$100/month
232
- - Use when: Files must survive restarts
233
-
234
- 3. **Docker Spaces:**
235
- - Must expose port 7860 βœ… (you have this)
236
- - Run as non-root user βœ… (you have this)
237
- - Use environment variables for secrets βœ… (you have this)
238
-
239
- ## ✨ Final Verdict
240
-
241
- **Your file access implementation is production-ready!**
242
-
243
- ### What's Working:
244
- - βœ… Correct upload directory (`/tmp/uploads`)
245
- - βœ… Environment detection
246
- - βœ… Fallback mechanisms
247
- - βœ… Error handling
248
- - βœ… Session management (after recent fixes)
249
-
250
- ### What to Deploy:
251
- 1. All files as-is
252
- 2. Set `GROQ_API_KEY` in Space secrets
253
- 3. Use CPU Basic (free tier)
254
- 4. No storage upgrades needed
255
-
256
- ### Expected Behavior:
257
- 1. User uploads PDF β†’ Saved to `/tmp/uploads/` βœ…
258
- 2. File processed β†’ RAG chain created βœ…
259
- 3. Session ID stored β†’ Frontend + backend βœ…
260
- 4. Chat works β†’ Streaming responses βœ…
261
-
262
- **Your app should work perfectly in HF Spaces without any changes!** πŸŽ‰
263
-
264
- ---
265
-
266
- ## πŸ†˜ Still Having Issues?
267
-
268
- If you're still experiencing problems after deployment:
269
-
270
- 1. **Check Space Logs:**
271
- - Settings β†’ Logs
272
- - Look for: "βœ“ Upload folder ready: /tmp/uploads"
273
- - Check for any error messages
274
-
275
- 2. **Test Debug Endpoint:**
276
- ```bash
277
- curl https://YOUR-SPACE/debug
278
- ```
279
-
280
- 3. **Verify GROQ API Key:**
281
- - Settings β†’ Repository Secrets
282
- - Ensure `GROQ_API_KEY` is set correctly
283
- - Check your Groq Console for API limits
284
-
285
- 4. **Browser Console:**
286
- - Open DevTools β†’ Console
287
- - Look for session ID logging
288
- - Check Network tab for 400/500 errors
289
-
290
- 5. **Run Verification Script:**
291
- ```bash
292
- python3 verify_hf_spaces_ready.py
293
- ```
294
-
295
- ## πŸ“ž Additional Resources
296
-
297
- - **Full Guide:** `HF_SPACES_FILE_STORAGE_GUIDE.md` (comprehensive)
298
- - **Deployment:** `DEPLOYMENT.md` (step-by-step)
299
- - **Checklist:** `HF_SPACES_CHECKLIST.md` (pre-flight)
300
- - **Verification:** `verify_hf_spaces_ready.py` (automated checks)
301
-
302
- **Questions?** Your implementation is solid - deploy with confidence! πŸš€
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
README.md CHANGED
@@ -139,4 +139,4 @@ python app.py
139
  - `HF_HOME`: HuggingFace cache directory
140
  - `PORT`: Application port (default: 7860)
141
 
142
- Developed by Ritesh and Alish
 
139
  - `HF_HOME`: HuggingFace cache directory
140
  - `PORT`: Application port (default: 7860)
141
 
142
+ Developed by [Ritesh](https://github.com/RautRitesh) and [Alish-0x](https://github.com/Alish-0x)
readme.md DELETED
@@ -1,136 +0,0 @@
1
-
2
- ***
3
-
4
- # CogniChat ✨
5
-
6
- What if you could chat with your documents? CogniChat is a smart, conversational document reader that transforms static files into interactive study partners.
7
-
8
- <img src="Document_reader.gif" width="400" alt="Document reader in action">
9
-
10
- ***
11
-
12
- ## 🎯 Problem
13
-
14
- Students and lifelong learners know the frustration: searching through hundreds of pages for that one key definition or concept before a test. Scrolling, skimming, and Ctrl+F-ing can feel slow, inefficient, and outdated. There should be a smarter way to interact with learning materials.
15
-
16
- ***
17
-
18
- ## πŸš€ Solution
19
-
20
- CogniChat is the study tool that enables you to instantly chat with your documents. Powered by Retrieval-Augmented Generation (RAG), CogniChat lets you ask plain-language questions, get instant summaries, and extract just the information you needβ€”directly from your files. Study smarter, not harder.
21
-
22
- ***
23
-
24
- ## ✨ Key Features
25
-
26
- - Multi-Format Support: Upload PDFs, DOCX, TXT files, and images.
27
- - Conversational Q&A: Ask anything in plain English and receive precise answers from your document.
28
- - Instant Summaries: Get document overviews in seconds.
29
- - Modern UI: Gemini-inspired, intuitive, and clean user interface.
30
- - Streaming Responses: Answers stream in real time for that ChatGPT-like feel.
31
- - Secure & Private: Documents are only processed within your sessionβ€”never stored long term.
32
-
33
- ***
34
-
35
- ## 🧠 How CogniChat Works (RAG Architecture)
36
-
37
- When you chat with a document, CogniChat’s RAG pipeline operates like this:
38
-
39
- ```mermaid
40
- graph TD
41
- A[User asks a question] --> B{Embed Question}
42
- B --> C[FAISS Vector Store]
43
- C -- "Find similar context" --> D[Retrieve Relevant Chunks]
44
- A --> E{Prompt Template}
45
- D --> E
46
- E -- "Augmented Prompt (Context + Question)" --> F[Groq LLM]
47
- F --> G[✨ Final Answer]
48
- ```
49
-
50
- - **1. User Asks a Question:** Enter your question in the chat.
51
- - **2. Embed Question:** The question is turned into a vector.
52
- - **3. Vector Search:** This vector searches the FAISS database for the most relevant document chunks.
53
- - **4. Retrieve Context:** Chunks similar to your question are retrieved.
54
- - **5. Augment Prompt:** Question + context become a rich prompt for the AI.
55
- - **6. LLM Generation:** The Groq LLM generates a context-aware answer.
56
-
57
- ***
58
-
59
- ## πŸ› οΈ Tech Stack
60
-
61
- | Category | Technology |
62
- |----------------|--------------------------|
63
- | Backend | Python (Flask) |
64
- | AI Core | LangChain, FAISS |
65
- | LLM & API | Groq API |
66
- | Frontend | Modern Gemini-inspired UI|
67
- | Database | In-memory FAISS store |
68
-
69
- ***
70
-
71
- ## βš™οΈ Getting Started
72
-
73
- Follow these steps to run CogniChat locally:
74
-
75
- ### Prerequisites
76
-
77
- - Python 3.10 or later
78
- - Groq API key
79
-
80
- ### Installation
81
-
82
- 1. **Clone the repository:**
83
- ```bash
84
- git clone https://github.com/your-username/cognichat.git
85
- cd cognichat
86
- ```
87
- 2. **Create & activate virtual environment:**
88
- - Windows:
89
- ```bash
90
- python -m venv venv
91
- venv\Scripts\activate
92
- ```
93
- - macOS / Linux:
94
- ```bash
95
- python3 -m venv venv
96
- source venv/bin/activate
97
- ```
98
- 3. **Install dependencies:**
99
- ```bash
100
- pip install -r requirements.txt
101
- ```
102
- 4. **Set up environment variables:**
103
- - Create a `.env` file in the project root:
104
- ```
105
- GROQ_API_KEY="your_api_key_here"
106
- ```
107
-
108
- ### Running the Application
109
-
110
- Start the Flask server:
111
- ```bash
112
- python app.py
113
- ```
114
- Visit [http://127.0.0.1:5001](http://127.0.0.1:5001) in your browser to begin.
115
-
116
- ***
117
-
118
- ## πŸ’‘ Roadmap
119
-
120
- - [ ] Session-based chat history
121
- - [ ] Talk to multiple documents at once
122
- - [ ] More formats: .pptx, .csv, etc.
123
- - [ ] User authentication to persist sessions
124
-
125
- ***
126
-
127
- ## πŸ“„ License
128
-
129
- MIT License. See [LICENSE](LICENSE) for details.
130
-
131
- ***
132
-
133
- ## πŸ™ Acknowledgements
134
-
135
- - The creators of Flask, LangChain, and Groq
136
- - The open-source community whose contributions make this project possible