ParthSadaria commited on
Commit
9b57c30
·
verified ·
1 Parent(s): ad8074f

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +121 -11
main.py CHANGED
@@ -55,6 +55,7 @@ def get_env_vars():
55
  'secret_api_endpoint': os.getenv('SECRET_API_ENDPOINT'),
56
  'secret_api_endpoint_2': os.getenv('SECRET_API_ENDPOINT_2'),
57
  'secret_api_endpoint_3': os.getenv('SECRET_API_ENDPOINT_3'),
 
58
  'mistral_api': "https://api.mistral.ai",
59
  'mistral_key': os.getenv('MISTRAL_KEY'),
60
  'image_endpoint': os.getenv("IMAGE_ENDPOINT"),
@@ -74,6 +75,25 @@ mistral_models = {
74
  "codestral-latest"
75
  }
76
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
77
  alternate_models = {
78
  "gpt-4o-mini",
79
  "deepseek-v3",
@@ -341,6 +361,9 @@ async def get_completion(payload: Payload, request: Request, authenticated: bool
341
  custom_headers = {
342
  "Authorization": f"Bearer {env_vars['mistral_key']}"
343
  }
 
 
 
344
  elif model_to_use in alternate_models:
345
  endpoint = env_vars['secret_api_endpoint_2']
346
  custom_headers = {}
@@ -348,7 +371,6 @@ async def get_completion(payload: Payload, request: Request, authenticated: bool
348
  endpoint = env_vars['secret_api_endpoint']
349
  custom_headers = {}
350
 
351
- # Get a scraper from the pool
352
  scraper = get_scraper()
353
 
354
  async def stream_generator(payload_dict):
@@ -790,6 +812,11 @@ async def startup_event():
790
  available_model_ids = load_model_ids("models.json")
791
  print(f"Loaded {len(available_model_ids)} model IDs")
792
 
 
 
 
 
 
793
  # Preload scrapers
794
  for _ in range(MAX_SCRAPERS):
795
  scraper_pool.append(cloudscraper.create_scraper())
@@ -804,22 +831,105 @@ async def startup_event():
804
  missing_vars.append('SECRET_API_ENDPOINT_2')
805
  if not env_vars['secret_api_endpoint_3']:
806
  missing_vars.append('SECRET_API_ENDPOINT_3')
 
 
 
 
 
 
 
 
807
 
808
  if missing_vars:
809
- print(f"WARNING: The following required environment variables are missing: {', '.join(missing_vars)}")
 
 
 
 
 
 
 
 
 
 
 
 
810
 
811
- print("API started successfully with high-performance optimizations")
 
 
 
 
 
812
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
813
  if __name__ == "__main__":
814
  import uvicorn
 
 
 
 
815
  uvicorn.run(
816
- app,
817
  host="0.0.0.0",
818
- port=7860,
819
- workers=4, # Multiple workers for better CPU utilization
820
- loop="uvloop", # Use uvloop for faster async operations
821
- http="httptools", # Faster HTTP parsing
822
- log_level="warning", # Reduce logging overhead
823
- limit_concurrency=100, # Limit concurrent connections
824
- timeout_keep_alive=5 # Reduce idle connection time
825
  )
 
55
  'secret_api_endpoint': os.getenv('SECRET_API_ENDPOINT'),
56
  'secret_api_endpoint_2': os.getenv('SECRET_API_ENDPOINT_2'),
57
  'secret_api_endpoint_3': os.getenv('SECRET_API_ENDPOINT_3'),
58
+ 'secret_api_endpoint_4': "https://text.pollinations.ai/openai",
59
  'mistral_api': "https://api.mistral.ai",
60
  'mistral_key': os.getenv('MISTRAL_KEY'),
61
  'image_endpoint': os.getenv("IMAGE_ENDPOINT"),
 
75
  "codestral-latest"
76
  }
77
 
78
+ pollinations_models = {
79
+ "openai",
80
+ "openai-large",
81
+ "openai-reasoning",
82
+ "qwen-coder",
83
+ "llama",
84
+ "mistral",
85
+ "searchgpt",
86
+ "deepseek",
87
+ "claude-hybridspace",
88
+ "deepseek-r1",
89
+ "deepseek-reasoner",
90
+ "llamalight",
91
+ "gemini",
92
+ "gemini-thinking",
93
+ "hormoz",
94
+ "llama-scaleway"
95
+ }
96
+
97
  alternate_models = {
98
  "gpt-4o-mini",
99
  "deepseek-v3",
 
361
  custom_headers = {
362
  "Authorization": f"Bearer {env_vars['mistral_key']}"
363
  }
364
+ elif model_to_use in pollinations_models:
365
+ endpoint = env_vars['secret_api_endpoint_4']
366
+ custom_headers = {}
367
  elif model_to_use in alternate_models:
368
  endpoint = env_vars['secret_api_endpoint_2']
369
  custom_headers = {}
 
371
  endpoint = env_vars['secret_api_endpoint']
372
  custom_headers = {}
373
 
 
374
  scraper = get_scraper()
375
 
376
  async def stream_generator(payload_dict):
 
812
  available_model_ids = load_model_ids("models.json")
813
  print(f"Loaded {len(available_model_ids)} model IDs")
814
 
815
+ # Add all pollinations models to available_model_ids
816
+ available_model_ids.extend(list(pollinations_models))
817
+ available_model_ids = list(set(available_model_ids)) # Remove duplicates
818
+ print(f"Added Pollinations models. Total available models: {len(available_model_ids)}")
819
+
820
  # Preload scrapers
821
  for _ in range(MAX_SCRAPERS):
822
  scraper_pool.append(cloudscraper.create_scraper())
 
831
  missing_vars.append('SECRET_API_ENDPOINT_2')
832
  if not env_vars['secret_api_endpoint_3']:
833
  missing_vars.append('SECRET_API_ENDPOINT_3')
834
+ if not env_vars['secret_api_endpoint_4']:
835
+ missing_vars.append('SECRET_API_ENDPOINT_4')
836
+ if not env_vars['mistral_api'] and any(model in mistral_models for model in available_model_ids):
837
+ missing_vars.append('MISTRAL_API')
838
+ if not env_vars['mistral_key'] and any(model in mistral_models for model in available_model_ids):
839
+ missing_vars.append('MISTRAL_KEY')
840
+ if not env_vars['image_endpoint']:
841
+ missing_vars.append('IMAGE_ENDPOINT')
842
 
843
  if missing_vars:
844
+ print(f"WARNING: The following environment variables are missing: {', '.join(missing_vars)}")
845
+ print("Some functionality may be limited.")
846
+
847
+ print("Server started successfully!")
848
+
849
+ @app.on_event("shutdown")
850
+ async def shutdown_event():
851
+ # Close the httpx client
852
+ client = get_async_client()
853
+ await client.aclose()
854
+
855
+ # Clear scraper pool
856
+ scraper_pool.clear()
857
 
858
+ # Persist usage data
859
+ usage_tracker.save_data()
860
+
861
+ print("Server shutdown complete!")
862
+
863
+ # Server maintenance endpoint
864
 
865
+ # Health check endpoint
866
+ @app.get("/health")
867
+ async def health_check():
868
+ """Health check endpoint for monitoring"""
869
+ env_vars = get_env_vars()
870
+ missing_critical_vars = []
871
+
872
+ # Check critical environment variables
873
+ if not env_vars['api_keys'] or env_vars['api_keys'] == ['']:
874
+ missing_critical_vars.append('API_KEYS')
875
+ if not env_vars['secret_api_endpoint']:
876
+ missing_critical_vars.append('SECRET_API_ENDPOINT')
877
+
878
+ # Check if models are loaded
879
+ models_loaded = len(available_model_ids) > 0
880
+
881
+ status = "healthy"
882
+ if missing_critical_vars or not models_loaded:
883
+ status = "degraded"
884
+
885
+ return {
886
+ "status": status,
887
+ "timestamp": datetime.datetime.utcnow().isoformat(),
888
+ "uptime": time.time() - usage_tracker.start_time,
889
+ "models_loaded": models_loaded,
890
+ "model_count": len(available_model_ids),
891
+ "issues": {
892
+ "missing_env_vars": missing_critical_vars,
893
+ "models_available": models_loaded
894
+ }
895
+ }
896
+
897
+ # Error handlers
898
+ @app.exception_handler(HTTPException)
899
+ async def http_exception_handler(request, exc):
900
+ """Format HTTP exceptions in a consistent way"""
901
+ return JSONResponse(
902
+ status_code=exc.status_code,
903
+ content={"error": exc.detail}
904
+ )
905
+
906
+ @app.exception_handler(Exception)
907
+ async def general_exception_handler(request, exc):
908
+ """Handle unexpected exceptions gracefully"""
909
+ # Log the error for debugging
910
+ print(f"Unexpected error: {str(exc)}")
911
+
912
+ return JSONResponse(
913
+ status_code=500,
914
+ content={"error": "An unexpected error occurred. Please try again later."}
915
+ )
916
+
917
+ # Static files endpoint for serving CSS, JS, etc.
918
+
919
+ # Documentation
920
+
921
+ # Run the server when executed directly
922
  if __name__ == "__main__":
923
  import uvicorn
924
+
925
+ port = int(os.getenv("PORT", 7860))
926
+
927
+ print(f"Starting Lokiai AI server on port {port}")
928
  uvicorn.run(
929
+ "main:app",
930
  host="0.0.0.0",
931
+ port=port,
932
+ workers=4,
933
+ reload=False,
934
+ log_level="info"
 
 
 
935
  )