Spaces:
Sleeping
Sleeping
| # ============================================ | |
| # 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() |