File size: 3,755 Bytes
65879b1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#!/usr/bin/env python3
"""
Development script to run the FastAPI version of OPTEEE
This replaces the original Gradio app for testing
"""

import os
import sys
import subprocess
import threading
import time

def check_dependencies():
    """Check if required dependencies are installed"""
    try:
        import fastapi
        import uvicorn
        print(" FastAPI dependencies found")
        return True
    except ImportError:
        print("❌ FastAPI dependencies not found")
        print("   Please install with: pip install -r requirements-fastapi.txt")
        return False

def check_rag_dependencies():
    """Check if RAG dependencies are available"""
    try:
        import vector_search
        import rag_pipeline
        print(" RAG dependencies found")
        return True
    except ImportError as e:
        print(f"❌ RAG dependencies not found: {e}")
        print("   Make sure vector_search.py and rag_pipeline.py are available")
        return False

def check_vector_store():
    """Check if vector store exists"""
    vector_store_path = "/app/vector_store/faiss.index"
    local_vector_store_path = "vector_store"
    
    if os.path.exists(vector_store_path) or os.path.exists(local_vector_store_path):
        print(" Vector store found")
        return True
    else:
        print("⚠️  Vector store not found")
        print("   The app will warn about this but should still start")
        return True  # Don't fail, just warn

def run_fastapi_server():
    """Run the FastAPI server"""
    print(" Starting FastAPI server...")
    
    try:
        import uvicorn
        from main import app
        
        # Run uvicorn server
        uvicorn.run(
            app,
            host="0.0.0.0", 
            port=7860,
            log_level="info"
        )
    except Exception as e:
        print(f"❌ Failed to start FastAPI server: {e}")
        return False

def run_tests_after_startup():
    """Run tests after server startup"""
    print("⏳ Waiting for server to start...")
    time.sleep(5)  # Wait for server to be ready
    
    print("🧪 Running API tests...")
    try:
        result = subprocess.run([sys.executable, "test_fastapi.py"], 
                              capture_output=True, text=True)
        
        print(result.stdout)
        if result.stderr:
            print("Errors:", result.stderr)
            
        if result.returncode == 0:
            print(" All tests passed!")
        else:
            print("⚠️  Some tests failed")
            
    except Exception as e:
        print(f"❌ Failed to run tests: {e}")

def main():
    """Main development script"""
    print(" OPTEEE FastAPI Development Server")
    print("=" * 50)
    
    # Check dependencies
    if not check_dependencies():
        return False
        
    if not check_rag_dependencies():
        return False
        
    check_vector_store()
    
    print("\n🌐 Starting server on http://localhost:7860")
    print("📚 API documentation will be available at http://localhost:7860/docs")
    print(" Health check: http://localhost:7860/api/health")
    print("\nPress Ctrl+C to stop the server")
    print("=" * 50)
    
    # Start tests in background thread
    if len(sys.argv) > 1 and sys.argv[1] == "--test":
        test_thread = threading.Thread(target=run_tests_after_startup)
        test_thread.daemon = True
        test_thread.start()
    
    try:
        # Run the server
        run_fastapi_server()
    except KeyboardInterrupt:
        print("\n👋 Shutting down server...")
        return True
    except Exception as e:
        print(f"\n❌ Server error: {e}")
        return False

if __name__ == "__main__":
    success = main()
    sys.exit(0 if success else 1)