File size: 3,543 Bytes
3ad6f3d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
598105d
3ad6f3d
598105d
 
d96f0e7
598105d
3ad6f3d
598105d
 
3ad6f3d
598105d
d96f0e7
13a5ece
598105d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3ad6f3d
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
"""
Hugging Face Spaces entry point for HackRx 6.0
This file is specifically for Hugging Face Spaces deployment
"""

import os
import sys
from pathlib import Path

# Add the current directory to Python path
sys.path.append(str(Path(__file__).parent))

# Set environment variables for Hugging Face cache
os.environ.setdefault("HF_HOME", "/code/.cache/huggingface")
os.environ.setdefault("TRANSFORMERS_CACHE", "/code/.cache/huggingface")
os.environ.setdefault("HF_HUB_CACHE", "/code/.cache/huggingface")

# Create cache directory if it doesn't exist
cache_dir = "/code/.cache/huggingface"
try:
    os.makedirs(cache_dir, exist_ok=True)
except Exception as e:
    print(f"Warning: Could not create cache directory: {e}")

# Import the main app and initialize services
try:
    from main import app as main_app, lifespan
    from main import get_query_service, process_query, process_query_legacy, process_query_detailed, analyze_document, summarize_document
    
    print("βœ… Successfully imported main.py app and services")
    
    # Use the main app directly
    app = main_app
    
    print("βœ… Using main.py app with proper service initialization")
    
except Exception as e:
    print(f"❌ Failed to import main.py app: {e}")
    print("Creating fallback app...")
    
    # Create fallback app
    from fastapi import FastAPI, Request
    app = FastAPI(
        title="HackRx 6.0 - Intelligent Query-Retrieval System",
        description="LLM-Powered Intelligent Query-Retrieval System for HackRx 6.0",
        version="1.0.0"
    )
    
    # Add middleware to log all requests
    @app.middleware("http")
    async def log_requests(request: Request, call_next):
        print(f"πŸ” Request: {request.method} {request.url.path}")
        response = await call_next(request)
        print(f"πŸ“€ Response: {response.status_code}")
        return response
    
    # Add guaranteed working endpoints
    @app.get("/")
    async def root():
        print("🎯 Root endpoint called")
        return {
            "message": "HackRx 6.0 - Intelligent Query-Retrieval System",
            "version": "1.0.0",
            "status": "fallback_mode",
            "note": "App is running in fallback mode due to initialization issues",
            "error": str(e),
            "endpoints": {
                "main": "api/v1/hackrx/run",
                "legacy": "hackrx/run", 
                "detailed": "api/v1/hackrx/run/detailed",
                "health": "health",
                "docs": "docs",
                "test": "test"
            }
        }
    
    @app.get("/health")
    async def health_check():
        print("πŸ₯ Health endpoint called")
        return {
            "status": "fallback",
            "message": "App is running in fallback mode",
            "services": {
                "query_service": False,
                "embedding_service": False,
                "llm_service": False,
                "pinecone_service": False
            },
            "error": str(e)
        }
    
    @app.get("/test")
    async def test_endpoint():
        print("πŸ§ͺ Test endpoint called")
        return {
            "message": "Test endpoint working!",
            "status": "success",
            "app_loaded": False,
            "mode": "fallback",
            "error": str(e)
        }

# For Hugging Face Spaces, we need to expose the app directly
if __name__ == "__main__":
    import uvicorn
    print("πŸš€ Starting HackRx 6.0 on Hugging Face Spaces...")
    uvicorn.run(app, host="0.0.0.0", port=7860)