File size: 2,454 Bytes
6709e9b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# ============================================
# BACKEND: api/start_server.py
# ============================================
# Simple launcher with QR code and network info
# Use this instead of 'python main.py' for better UX

import socket
import qrcode
import sys
import os


def get_local_ip():
    """Get WiFi IP address"""
    try:
        s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        s.connect(("8.8.8.8", 80))
        ip = s.getsockname()[0]
        s.close()
        return ip
    except Exception:
        return "127.0.0.1"


def generate_qr_code(url):
    """Generate QR code in terminal"""
    try:
        qr = qrcode.QRCode(
            version=1,
            error_correction=qrcode.constants.ERROR_CORRECT_L,
            box_size=1,
            border=1,
        )
        qr.add_data(url)
        qr.make(fit=True)
        qr.print_ascii(invert=True)
    except Exception:
        print("⚠️ QR code generation failed (optional feature)")


def main():
    """Start server with visual feedback"""
    local_ip = get_local_ip()
import os
port = int(os.environ.get("PORT", 7860))    url = f"http://{local_ip}:{port}"

    # Display banner
    print("\n" + "=" * 60)
    print("πŸ₯­ MANGO DISEASE DETECTION API")
    print("=" * 60)
    print(f"\nπŸ“‘ Network Configuration:")
    print(f"   IP Address: {local_ip}")
    print(f"   Port: {port}")
    print(f"\n🌐 Access URLs:")
    print(f"   β€’ Direct IP:  {url}")
    print(f"   β€’ mDNS Name:  http://mango-api.local:{port}")
    print(f"   β€’ Localhost:  http://localhost:{port}")
    print(f"\nπŸ“– API Documentation:")
    print(f"   {url}/docs")

    # QR Code (optional - for phone access)
    print(f"\nπŸ“± QR Code (Optional - Scan to get IP):")
    print("─" * 40)
    generate_qr_code(url)
    print("─" * 40)

    print(f"\nβœ… Starting server...")
    print(f"πŸ’‘ Tip: Frontend will auto-discover this API")
    print(f"πŸ›‘ Press CTRL+C to stop")
    print("=" * 60 + "\n")

    # Start uvicorn
    try:
        import uvicorn
        from main import app

        uvicorn.run(app, host="0.0.0.0", port=port)

    except KeyboardInterrupt:
        print("\n\nπŸ›‘ Server stopped by user")
        sys.exit(0)
    except Exception as e:
        print(f"\n❌ Error: {e}")
        print("πŸ’‘ Make sure all dependencies are installed:")
        print("   pip install -r requirements.txt")
        sys.exit(1)


if __name__ == "__main__":
    main()