Spaces:
Sleeping
Sleeping
Download api.py from Afifi00/Flask_n8n_Workflow: direct link, hf CLI and curl.
- Browser
- Download file 9.71 kB
-
https://huggingface.co/spaces/Afifi00/Flask_n8n_Workflow/resolve/main/api.py
- Command line
-
hf download hf://spaces/Afifi00/Flask_n8n_Workflow/api.py
-
curl -L -o api.py https://huggingface.co/spaces/Afifi00/Flask_n8n_Workflow/resolve/main/api.py
9.71 kB
| from flask import Flask, request, jsonify | |
| import os | |
| import json | |
| import time | |
| import logging | |
| from models import ( | |
| load_llm, | |
| generate_workflow_with_llm, | |
| process_documents_for_rag, | |
| check_model_health, | |
| cleanup_resources, | |
| clear_memory | |
| ) | |
| # Setup logging | |
| logging.basicConfig(level=logging.INFO) | |
| logger = logging.getLogger(__name__) | |
| app = Flask(__name__) | |
| # Global variables | |
| model_loaded = False | |
| llm_pipeline = None | |
| model_load_time = None | |
| model_name = None | |
| def initialize_model(): | |
| """Initialize model with comprehensive error handling""" | |
| global model_loaded, llm_pipeline, model_load_time, model_name | |
| try: | |
| logger.info("π Starting model initialization...") | |
| start_time = time.time() | |
| # Clear any existing resources | |
| cleanup_resources() | |
| # Load the model | |
| llm_pipeline = load_llm() | |
| if llm_pipeline is not None: | |
| # Test the model | |
| if check_model_health(llm_pipeline): | |
| model_loaded = True | |
| model_load_time = time.time() - start_time | |
| model_name = getattr(llm_pipeline, 'model', {}).get('config', {}).get('_name_or_path', 'Unknown') | |
| logger.info(f"β Model loaded successfully in {model_load_time:.2f}s") | |
| logger.info(f"β Model: {model_name}") | |
| return True | |
| else: | |
| logger.error("β Model health check failed") | |
| model_loaded = False | |
| return False | |
| else: | |
| logger.error("β Model loading returned None") | |
| model_loaded = False | |
| return False | |
| except Exception as e: | |
| logger.error(f"β Model initialization failed: {e}") | |
| model_loaded = False | |
| llm_pipeline = None | |
| cleanup_resources() | |
| return False | |
| def home(): | |
| """Home endpoint with detailed status""" | |
| return jsonify({ | |
| "message": "n8n Workflow Generator API", | |
| "status": "running", | |
| "model_loaded": model_loaded, | |
| "model_name": model_name, | |
| "load_time": f"{model_load_time:.2f}s" if model_load_time else None, | |
| "version": "2.0", | |
| "endpoints": { | |
| "health": "/health", | |
| "generate": "/generate_workflow", | |
| "model_info": "/model_info", | |
| "reload": "/reload_model" | |
| } | |
| }) | |
| def health_check(): | |
| """Comprehensive health check""" | |
| health_status = { | |
| "status": "healthy" if model_loaded else "unhealthy", | |
| "model_loaded": model_loaded, | |
| "model_name": model_name, | |
| "timestamp": time.time(), | |
| "port": 7860 | |
| } | |
| if model_loaded: | |
| # Quick model test | |
| try: | |
| health_status["model_responsive"] = check_model_health(llm_pipeline) | |
| except Exception as e: | |
| health_status["model_responsive"] = False | |
| health_status["model_error"] = str(e) | |
| return jsonify(health_status) | |
| def model_info(): | |
| """Get detailed model information""" | |
| if not model_loaded: | |
| return jsonify({"error": "Model not loaded"}), 503 | |
| try: | |
| info = { | |
| "model_name": model_name, | |
| "model_loaded": model_loaded, | |
| "load_time": f"{model_load_time:.2f}s" if model_load_time else None, | |
| "device": "cuda" if hasattr(llm_pipeline, 'device') and 'cuda' in str(llm_pipeline.device) else "cpu" | |
| } | |
| return jsonify(info) | |
| except Exception as e: | |
| return jsonify({"error": str(e)}), 500 | |
| def reload_model(): | |
| """Reload the model (useful for debugging)""" | |
| global model_loaded | |
| try: | |
| logger.info("π Manual model reload requested") | |
| model_loaded = False | |
| success = initialize_model() | |
| if success: | |
| return jsonify({ | |
| "success": True, | |
| "message": "Model reloaded successfully", | |
| "model_name": model_name, | |
| "load_time": f"{model_load_time:.2f}s" | |
| }) | |
| else: | |
| return jsonify({ | |
| "success": False, | |
| "message": "Model reload failed" | |
| }), 500 | |
| except Exception as e: | |
| return jsonify({ | |
| "success": False, | |
| "error": str(e) | |
| }), 500 | |
| def generate_workflow(): | |
| """Generate workflow with improved error handling""" | |
| global llm_pipeline | |
| try: | |
| # Check model status | |
| if not model_loaded: | |
| logger.info("π Model not loaded, attempting to initialize...") | |
| if not initialize_model(): | |
| return jsonify({ | |
| "error": "Model failed to load. Please check logs and try again.", | |
| "success": False, | |
| "suggestion": "Try the /reload_model endpoint" | |
| }), 500 | |
| # Validate request | |
| data = request.get_json() | |
| if not data: | |
| return jsonify({ | |
| "error": "No JSON data provided", | |
| "success": False | |
| }), 400 | |
| user_prompt = data.get('prompt', '').strip() | |
| use_rag = data.get('use_rag', False) | |
| raw_docs = data.get('raw_docs', None) | |
| if not user_prompt: | |
| return jsonify({ | |
| "error": "Prompt is required and cannot be empty", | |
| "success": False | |
| }), 400 | |
| logger.info(f"π Processing request: {user_prompt[:100]}...") | |
| # Process RAG if requested | |
| context_block = "" | |
| if use_rag and raw_docs: | |
| try: | |
| logger.info("π Processing documents for RAG...") | |
| context_block = process_documents_for_rag(raw_docs, user_prompt) | |
| if context_block: | |
| logger.info("β RAG context created successfully") | |
| else: | |
| logger.warning("β οΈ RAG processing returned empty context") | |
| except Exception as e: | |
| logger.error(f"β RAG processing failed: {e}") | |
| # Continue without RAG | |
| context_block = "" | |
| # Generate workflow | |
| try: | |
| logger.info("π€ Generating workflow...") | |
| start_time = time.time() | |
| workflow_json, is_json = generate_workflow_with_llm( | |
| llm_pipeline, | |
| user_prompt, | |
| context_block | |
| ) | |
| generation_time = time.time() - start_time | |
| logger.info(f"β Generation completed in {generation_time:.2f}s") | |
| if is_json: | |
| return jsonify({ | |
| "success": True, | |
| "workflow": workflow_json, | |
| "context_used": bool(context_block.strip()), | |
| "generation_time": f"{generation_time:.2f}s", | |
| "model_name": model_name | |
| }) | |
| else: | |
| return jsonify({ | |
| "success": False, | |
| "error": "Failed to generate valid JSON workflow", | |
| "raw_output": workflow_json[:500] + "..." if len(workflow_json) > 500 else workflow_json, | |
| "suggestion": "Try rephrasing your request or check the model logs" | |
| }), 422 | |
| except Exception as e: | |
| logger.error(f"β Generation error: {e}") | |
| return jsonify({ | |
| "success": False, | |
| "error": f"Generation failed: {str(e)}", | |
| "suggestion": "Try a simpler request or reload the model" | |
| }), 500 | |
| except Exception as e: | |
| logger.error(f"β Request processing error: {e}") | |
| return jsonify({ | |
| "success": False, | |
| "error": f"Request processing failed: {str(e)}" | |
| }), 500 | |
| def not_found(error): | |
| return jsonify({ | |
| "error": "Endpoint not found", | |
| "available_endpoints": ["/", "/health", "/generate_workflow", "/model_info", "/reload_model"] | |
| }), 404 | |
| def internal_error(error): | |
| logger.error(f"β Internal server error: {error}") | |
| return jsonify({ | |
| "error": "Internal server error", | |
| "suggestion": "Check server logs for details" | |
| }), 500 | |
| # Initialize model when the app starts | |
| def startup_initialization(): | |
| """Initialize model on startup""" | |
| logger.info("π Starting n8n Workflow Generator API...") | |
| max_retries = 3 | |
| for attempt in range(max_retries): | |
| logger.info(f"π Initialization attempt {attempt + 1}/{max_retries}") | |
| if initialize_model(): | |
| logger.info("β Startup initialization successful") | |
| return True | |
| else: | |
| logger.warning(f"β Initialization attempt {attempt + 1} failed") | |
| if attempt < max_retries - 1: | |
| logger.info("β³ Retrying in 5 seconds...") | |
| time.sleep(5) | |
| logger.error("β All initialization attempts failed") | |
| return False | |
| # Startup initialization | |
| startup_initialization() | |
| if __name__ == '__main__': | |
| # Development mode | |
| logger.info("π§ Running in development mode") | |
| app.run(debug=False, host='0.0.0.0', port=7860, threaded=True) | |
| else: | |
| # Production mode (gunicorn) | |
| logger.info("π Running in production mode") | |
| # Gunicorn will handle the startup | |