sha22333 commited on
Commit
adb2a9e
·
verified ·
1 Parent(s): f0fb0f5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +516 -62
app.py CHANGED
@@ -1,70 +1,524 @@
1
  import gradio as gr
2
  from huggingface_hub import InferenceClient
 
 
 
 
 
 
 
 
 
 
3
 
 
 
 
 
 
 
 
4
 
5
- def respond(
6
- message,
7
- history: list[dict[str, str]],
8
- system_message,
9
- max_tokens,
10
- temperature,
11
- top_p,
12
- hf_token: gr.OAuthToken,
13
- ):
14
- """
15
- For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
16
- """
17
- client = InferenceClient(token=hf_token.token, model="openai/gpt-oss-20b")
18
-
19
- messages = [{"role": "system", "content": system_message}]
20
-
21
- messages.extend(history)
22
-
23
- messages.append({"role": "user", "content": message})
24
-
25
- response = ""
26
-
27
- for message in client.chat_completion(
28
- messages,
29
- max_tokens=max_tokens,
30
- stream=True,
31
- temperature=temperature,
32
- top_p=top_p,
33
- ):
34
- choices = message.choices
35
- token = ""
36
- if len(choices) and choices[0].delta.content:
37
- token = choices[0].delta.content
38
-
39
- response += token
40
- yield response
41
-
42
-
43
- """
44
- For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
45
- """
46
- chatbot = gr.ChatInterface(
47
- respond,
48
- type="messages",
49
- additional_inputs=[
50
- gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
51
- gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
52
- gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
53
- gr.Slider(
54
- minimum=0.1,
55
- maximum=1.0,
56
- value=0.95,
57
- step=0.05,
58
- label="Top-p (nucleus sampling)",
59
- ),
60
- ],
61
- )
62
-
63
- with gr.Blocks() as demo:
64
- with gr.Sidebar():
65
- gr.LoginButton()
66
- chatbot.render()
67
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 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3
+ from flask import Flask, request, jsonify, send_from_directory
4
+ from flask_cors import CORS
5
+ import os
6
+ import json
7
+ import logging
8
+ from datetime import datetime
9
+ import sys
10
+ import traceback
11
+ from typing import Dict, List, Any
12
+ import secrets
13
 
14
+ # Try to import the RAG pipeline
15
+ try:
16
+ from rag_pipeline import RAGPipeline
17
+ RAG_AVAILABLE = True
18
+ except ImportError as e:
19
+ logging.warning(f"RAG pipeline import failed: {e}. Running in limited mode.")
20
+ RAG_AVAILABLE = False
21
 
22
+ # Configure logging
23
+ logging.basicConfig(level=logging.INFO)
24
+ logger = logging.getLogger(__name__)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25
 
26
+ app = Flask(__name__, static_folder='build', static_url_path='')
27
+ CORS(app, resources={r"/api/*": {"origins": "*"}})
28
+
29
+ # Configuration
30
+ INDEX_FILE = "pdf_index_enhanced1.pkl"
31
+
32
+ # Model configuration - Load from environment or use defaults
33
+ LLM_TYPE = os.getenv("LLM_TYPE", "deepseek").lower()
34
+
35
+ # Get Hugging Face tokens from environment
36
+ HF_TOKEN = os.getenv("HF_TOKEN", "")
37
+ HF_BACKUP_TOKEN_1 = os.getenv("HF_BACKUP_TOKEN_1", "")
38
+ HF_BACKUP_TOKEN_2 = os.getenv("HF_BACKUP_TOKEN_2", "")
39
+
40
+ # Azure configuration (if using)
41
+ AZURE_KEY = os.getenv("AZURE_KEY", "")
42
+ AZURE_ENDPOINT = os.getenv("AZURE_ENDPOINT", "")
43
+ AZURE_DEPLOYMENT = os.getenv("AZURE_DEPLOYMENT", "gpt-4o-mini")
44
+
45
+ # Session management
46
+ sessions: Dict[str, Dict] = {}
47
+
48
+ # Initialize RAG pipeline
49
+ rag_pipeline = None
50
+
51
+ def initialize_rag_pipeline():
52
+ """Initialize the RAG pipeline with configured model parameters"""
53
+ global rag_pipeline
54
+
55
+ model_params = {
56
+ "llm_type": LLM_TYPE,
57
+ "hf_token": HF_TOKEN,
58
+ "hf_backup_token_1": HF_BACKUP_TOKEN_1,
59
+ "hf_backup_token_2": HF_BACKUP_TOKEN_2,
60
+ }
61
+
62
+ if LLM_TYPE == "azure":
63
+ model_params.update({
64
+ "azure_key": AZURE_KEY,
65
+ "azure_endpoint": AZURE_ENDPOINT,
66
+ "azure_deployment": AZURE_DEPLOYMENT,
67
+ })
68
+ else: # deepseek
69
+ # Use the correct router endpoint
70
+ model_params.update({
71
+ "deepseek_url": "https://router.huggingface.co/v1/chat/completions",
72
+ "deepseek_model": "deepseek-ai/DeepSeek-V3.1",
73
+ })
74
+
75
+ try:
76
+ # Create a dummy PDF folder path (won't be used since we're loading from pickle)
77
+ # The RAGPipeline class expects a pdf_folder parameter, but it won't be used
78
+ # if load_index() is successful
79
+ dummy_pdf_folder = "./dummy_pdfs"
80
+
81
+ rag_pipeline = RAGPipeline(
82
+ pdf_folder=dummy_pdf_folder, # This won't be used since we load from pickle
83
+ index_file=INDEX_FILE,
84
+ model_params=model_params,
85
+ reserve_tokens=8000,
86
+ )
87
+
88
+ # Load existing index
89
+ if rag_pipeline.load_index():
90
+ logger.info(f"✅ Loaded RAG index with {len(rag_pipeline.documents)} chunks")
91
+ else:
92
+ logger.info("❌ No existing index found. You need to build an index first.")
93
+
94
+ return True
95
+ except Exception as e:
96
+ logger.error(f"Failed to initialize RAG pipeline: {e}")
97
+ logger.error(traceback.format_exc())
98
+ return False
99
+
100
+ # Initialize on startup
101
+ initialize_rag_pipeline()
102
+
103
+ def get_session(session_id: str) -> Dict:
104
+ """Get or create a session"""
105
+ if session_id not in sessions:
106
+ sessions[session_id] = {
107
+ "session_id": session_id,
108
+ "created_at": datetime.now().isoformat(),
109
+ "updated_at": datetime.now().isoformat(),
110
+ "messages": [],
111
+ "title": f"Wetland Session {len(sessions) + 1}"
112
+ }
113
+ return sessions[session_id]
114
+
115
+ def update_session_title(session_id: str, first_message: str):
116
+ """Update session title based on first message"""
117
+ if session_id in sessions:
118
+ # Create a short title from first message
119
+ words = first_message.split()[:5]
120
+ title = " ".join(words) + ("..." if len(first_message.split()) > 5 else "")
121
+ sessions[session_id]["title"] = f"🌿 {title}"
122
+ sessions[session_id]["updated_at"] = datetime.now().isoformat()
123
+
124
+ @app.route('/')
125
+ def serve_frontend():
126
+ """Serve the React frontend"""
127
+ try:
128
+ return send_from_directory(app.static_folder, 'index.html')
129
+ except:
130
+ return jsonify({"status": "Frontend not built. Run 'npm run build' first"})
131
+
132
+ @app.route('/api/health', methods=['GET'])
133
+ def health_check():
134
+ """Health check endpoint"""
135
+ try:
136
+ if rag_pipeline is None:
137
+ return jsonify({
138
+ "status": "initializing",
139
+ "llm_type": LLM_TYPE,
140
+ "index_loaded": False
141
+ })
142
+
143
+ # Check if index is loaded
144
+ index_loaded = hasattr(rag_pipeline, 'documents') and len(rag_pipeline.documents) > 0
145
+
146
+ return jsonify({
147
+ "status": "healthy" if index_loaded else "no_index",
148
+ "llm_type": LLM_TYPE,
149
+ "index_loaded": index_loaded,
150
+ "chunks_available": len(rag_pipeline.documents) if index_loaded else 0
151
+ })
152
+ except Exception as e:
153
+ return jsonify({
154
+ "status": "error",
155
+ "error": str(e)
156
+ }), 500
157
+
158
+ @app.route('/api/chat', methods=['POST'])
159
+ def chat():
160
+ """Handle chat messages"""
161
+ try:
162
+ data = request.get_json()
163
+ if not data:
164
+ return jsonify({"error": "No JSON data provided"}), 400
165
+
166
+ message = data.get('message', '').strip()
167
+ session_id = data.get('session_id', f'session-{secrets.token_hex(8)}')
168
+
169
+ if not message:
170
+ return jsonify({"error": "Message is required"}), 400
171
+
172
+ if rag_pipeline is None:
173
+ return jsonify({"error": "RAG pipeline not initialized"}), 500
174
+
175
+ # Check if index is loaded
176
+ if not hasattr(rag_pipeline, 'documents') or len(rag_pipeline.documents) == 0:
177
+ return jsonify({
178
+ "error": "No index loaded. Please rebuild the index first.",
179
+ "response": "I don't have any documents loaded yet. Please rebuild the index from the admin panel."
180
+ }), 400
181
+
182
+ # Get or create session
183
+ session = get_session(session_id)
184
+
185
+ # Update session title if first message
186
+ if len(session['messages']) == 0:
187
+ update_session_title(session_id, message)
188
+
189
+ # Add user message to session
190
+ user_msg = {
191
+ "id": len(session['messages']),
192
+ "role": "user",
193
+ "content": message,
194
+ "timestamp": datetime.now().isoformat()
195
+ }
196
+ session['messages'].append(user_msg)
197
+
198
+ logger.info(f"Processing query: {message[:100]}...")
199
+
200
+ # Query RAG pipeline
201
+ answer, docs = rag_pipeline.query(message, top_k=8)
202
+
203
+ # Format sources
204
+ sources = []
205
+ for i, doc in enumerate(docs, 1):
206
+ sources.append({
207
+ "rank": i,
208
+ "filename": doc.metadata.get('source', 'Unknown'),
209
+ "page": doc.metadata.get('page', 'N/A'),
210
+ "type": doc.metadata.get('type', 'text'),
211
+ "content": doc.page_content[:300] + "..." if len(doc.page_content) > 300 else doc.page_content,
212
+ "full_content": doc.page_content,
213
+ "metadata": doc.metadata
214
+ })
215
+
216
+ # Add assistant response to session
217
+ assistant_msg = {
218
+ "id": len(session['messages']),
219
+ "role": "assistant",
220
+ "content": answer,
221
+ "sources": sources,
222
+ "timestamp": datetime.now().isoformat()
223
+ }
224
+ session['messages'].append(assistant_msg)
225
+
226
+ # Update session timestamp
227
+ session['updated_at'] = datetime.now().isoformat()
228
+
229
+ # Get conversation stats
230
+ stats = rag_pipeline.get_conversation_stats()
231
+
232
+ return jsonify({
233
+ "response": answer,
234
+ "sources": sources,
235
+ "session_id": session_id,
236
+ "retrieval_stats": {
237
+ "retrieved_chunks": len(docs),
238
+ "total_chunks_available": len(rag_pipeline.documents),
239
+ "conversation_history_tokens": stats.get('history_tokens', 0)
240
+ }
241
+ })
242
+
243
+ except Exception as e:
244
+ logger.error(f"Chat error: {e}")
245
+ logger.error(traceback.format_exc())
246
+ return jsonify({
247
+ "error": str(e),
248
+ "response": "Sorry, I encountered an error processing your request."
249
+ }), 500
250
+
251
+ @app.route('/api/clear-history', methods=['POST'])
252
+ def clear_history():
253
+ """Clear conversation history for a session"""
254
+ try:
255
+ data = request.get_json()
256
+ session_id = data.get('session_id', '')
257
+
258
+ if not session_id:
259
+ return jsonify({"error": "Session ID is required"}), 400
260
+
261
+ if rag_pipeline:
262
+ rag_pipeline.clear_conversation()
263
+
264
+ # Reset session
265
+ if session_id in sessions:
266
+ # Keep only the session metadata, clear messages
267
+ sessions[session_id]['messages'] = []
268
+ sessions[session_id]['updated_at'] = datetime.now().isoformat()
269
+
270
+ return jsonify({
271
+ "status": "cleared",
272
+ "session_id": session_id
273
+ })
274
+
275
+ except Exception as e:
276
+ logger.error(f"Clear history error: {e}")
277
+ return jsonify({"error": str(e)}), 500
278
+
279
+ @app.route('/api/models', methods=['GET'])
280
+ def get_models():
281
+ """Get available models"""
282
+ try:
283
+ # For now, return configured model
284
+ models = [
285
+ {
286
+ "key": "wetland-ai",
287
+ "name": "Wetland Conservation AI",
288
+ "type": "wetland",
289
+ "is_current": True
290
+ },
291
+ {
292
+ "key": "aquatic-research",
293
+ "name": "Aquatic Research Model",
294
+ "type": "research",
295
+ "is_current": False
296
+ }
297
+ ]
298
+
299
+ return jsonify({
300
+ "available_models": models,
301
+ "current_model": "wetland-ai",
302
+ "llm_backend": LLM_TYPE
303
+ })
304
+
305
+ except Exception as e:
306
+ logger.error(f"Get models error: {e}")
307
+ return jsonify({"error": str(e)}), 500
308
+
309
+ @app.route('/api/switch-model', methods=['POST'])
310
+ def switch_model():
311
+ """Switch between models (placeholder for future implementation)"""
312
+ try:
313
+ data = request.get_json()
314
+ model_key = data.get('model', '')
315
+
316
+ if not model_key:
317
+ return jsonify({"error": "Model key is required"}), 400
318
+
319
+ # For now, just log the request
320
+ logger.info(f"Model switch requested to: {model_key}")
321
+
322
+ return jsonify({
323
+ "status": "switched",
324
+ "model": model_key,
325
+ "model_info": {
326
+ "name": "Wetland Conservation AI",
327
+ "type": "wetland"
328
+ }
329
+ })
330
+
331
+ except Exception as e:
332
+ logger.error(f"Switch model error: {e}")
333
+ return jsonify({"error": str(e)}), 500
334
+
335
+ @app.route('/api/stats', methods=['GET'])
336
+ def get_stats():
337
+ """Get system statistics"""
338
+ try:
339
+ if rag_pipeline is None:
340
+ return jsonify({
341
+ "status": "initializing",
342
+ "index": {"total_chunks": 0},
343
+ "sessions": {"total": len(sessions)}
344
+ })
345
+
346
+ # Get RAG pipeline stats
347
+ pipeline_stats = rag_pipeline.get_stats() if rag_pipeline else {}
348
+
349
+ # Get conversation stats
350
+ conv_stats = rag_pipeline.get_conversation_stats() if rag_pipeline else {}
351
+
352
+ # Check if index is loaded
353
+ index_loaded = hasattr(rag_pipeline, 'documents') and len(rag_pipeline.documents) > 0
354
+
355
+ return jsonify({
356
+ "status": "healthy" if index_loaded else "no_index",
357
+ "index": {
358
+ "total_chunks": pipeline_stats.get('total_chunks', 0),
359
+ "content_types": pipeline_stats.get('content_types', {}),
360
+ "loaded": index_loaded
361
+ },
362
+ "conversation": conv_stats,
363
+ "sessions": {
364
+ "total": len(sessions),
365
+ "active": len([s for s in sessions.values() if s['messages']])
366
+ },
367
+ "llm_backend": LLM_TYPE
368
+ })
369
+
370
+ except Exception as e:
371
+ logger.error(f"Get stats error: {e}")
372
+ return jsonify({"error": str(e)}), 500
373
+
374
+ @app.route('/api/sources', methods=['GET'])
375
+ def get_sources():
376
+ """Get list of uploaded documents/sources"""
377
+ try:
378
+ # Get sources from RAG pipeline if available
379
+ rag_sources = []
380
+ if rag_pipeline and hasattr(rag_pipeline, 'documents'):
381
+ # Get unique sources
382
+ sources_set = set()
383
+ for doc in rag_pipeline.documents:
384
+ source = doc.metadata.get('source', 'Unknown')
385
+ if source:
386
+ sources_set.add(source)
387
+ rag_sources = list(sources_set)
388
+
389
+ return jsonify({
390
+ "sources": rag_sources,
391
+ "total_sources": len(rag_sources)
392
+ })
393
+
394
+ except Exception as e:
395
+ logger.error(f"Get sources error: {e}")
396
+ return jsonify({"error": str(e)}), 500
397
+
398
+ @app.route('/api/sessions', methods=['GET'])
399
+ def get_sessions():
400
+ """Get all chat sessions"""
401
+ try:
402
+ session_list = []
403
+ for session_id, session_data in sessions.items():
404
+ session_list.append({
405
+ "session_id": session_id,
406
+ "title": session_data.get('title', 'Untitled Session'),
407
+ "created_at": session_data.get('created_at'),
408
+ "updated_at": session_data.get('updated_at'),
409
+ "message_count": len(session_data.get('messages', [])),
410
+ "preview": session_data.get('messages', [])[0]['content'][:100] + "..."
411
+ if session_data.get('messages') else ""
412
+ })
413
+
414
+ # Sort by updated_at (most recent first)
415
+ session_list.sort(key=lambda x: x['updated_at'], reverse=True)
416
+
417
+ return jsonify({
418
+ "sessions": session_list,
419
+ "total": len(session_list)
420
+ })
421
+
422
+ except Exception as e:
423
+ logger.error(f"Get sessions error: {e}")
424
+ return jsonify({"error": str(e)}), 500
425
+
426
+ @app.route('/api/session/<session_id>', methods=['GET'])
427
+ def get_session_data(session_id):
428
+ """Get specific session data"""
429
+ try:
430
+ session = get_session(session_id)
431
+
432
+ return jsonify({
433
+ "session_id": session_id,
434
+ "title": session.get('title', 'Untitled Session'),
435
+ "created_at": session.get('created_at'),
436
+ "updated_at": session.get('updated_at'),
437
+ "messages": session.get('messages', []),
438
+ "message_count": len(session.get('messages', []))
439
+ })
440
+
441
+ except Exception as e:
442
+ logger.error(f"Get session error: {e}")
443
+ return jsonify({"error": str(e)}), 500
444
+
445
+ @app.route('/api/session/<session_id>', methods=['DELETE'])
446
+ def delete_session(session_id):
447
+ """Delete a session"""
448
+ try:
449
+ if session_id in sessions:
450
+ del sessions[session_id]
451
+
452
+ return jsonify({
453
+ "status": "deleted",
454
+ "session_id": session_id
455
+ })
456
+
457
+ except Exception as e:
458
+ logger.error(f"Delete session error: {e}")
459
+ return jsonify({"error": str(e)}), 500
460
+
461
+ @app.route('/api/rebuild-index', methods=['POST'])
462
+ def rebuild_index():
463
+ """Rebuild the RAG index (admin endpoint) - DISABLED since no PDF folder"""
464
+ try:
465
+ return jsonify({
466
+ "status": "error",
467
+ "message": "Rebuild index is disabled. The index is loaded from pickle file only."
468
+ }), 400
469
+
470
+ except Exception as e:
471
+ logger.error(f"Rebuild index error: {e}")
472
+ return jsonify({"error": str(e)}), 500
473
+
474
+ @app.route('/api/debug/chunks', methods=['GET'])
475
+ def debug_chunks():
476
+ """Debug endpoint to view chunks"""
477
+ try:
478
+ if rag_pipeline is None:
479
+ return jsonify({"error": "RAG pipeline not initialized"}), 500
480
+
481
+ source = request.args.get('source', '')
482
+ limit = int(request.args.get('limit', 10))
483
+
484
+ chunks = []
485
+ for doc in rag_pipeline.documents[:limit]:
486
+ if source and doc.metadata.get('source') != source:
487
+ continue
488
+
489
+ chunks.append({
490
+ "metadata": doc.metadata,
491
+ "content_preview": doc.page_content[:200] + "..." if len(doc.page_content) > 200 else doc.page_content,
492
+ "content_length": len(doc.page_content)
493
+ })
494
+
495
+ return jsonify({
496
+ "chunks": chunks,
497
+ "total_chunks": len(rag_pipeline.documents),
498
+ "showing": len(chunks)
499
+ })
500
+
501
+ except Exception as e:
502
+ logger.error(f"Debug chunks error: {e}")
503
+ return jsonify({"error": str(e)}), 500
504
+
505
+ # Serve React static files
506
+ @app.route('/<path:path>')
507
+ def serve_static(path):
508
+ try:
509
+ return send_from_directory(app.static_folder, path)
510
+ except:
511
+ return jsonify({"error": "File not found"}), 404
512
+
513
+ if __name__ == '__main__':
514
+ # Start Flask server
515
+ port = int(os.getenv("PORT", 5000))
516
+
517
+ # Check if index file exists
518
+ if not os.path.exists(INDEX_FILE):
519
+ logger.warning(f"Index file {INDEX_FILE} not found. The RAG pipeline will be in no_index state.")
520
+
521
+ app.run(host='0.0.0.0', port=port, debug=True)
522
 
523
  if __name__ == "__main__":
524
  demo.launch()