dwfwfwfwf commited on
Commit
eddcfcf
·
verified ·
1 Parent(s): 3143d2f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -1
app.py CHANGED
@@ -3,6 +3,7 @@ import json
3
  import logging
4
  import time
5
  from gradio_client import Client
 
6
 
7
  app = Flask(__name__)
8
 
@@ -12,7 +13,37 @@ logging.basicConfig(level=logging.INFO,
12
  logger = logging.getLogger(__name__)
13
 
14
  # Initialize JARVIS client
15
- jarvis = Client("hadadrjt/ai")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16
 
17
  # Define available models
18
  models = [
@@ -53,6 +84,10 @@ def chat_completions():
53
  if not authenticate_request(request):
54
  return jsonify({"error": {"message": "Invalid API key", "code": "invalid_api_key"}}), 401
55
 
 
 
 
 
56
  data = request.json
57
  messages = data.get("messages", [])
58
  model = data.get("model", "JARVIS: 2.1.2")
@@ -104,6 +139,9 @@ def chat_completions():
104
  def list_models():
105
  if not authenticate_request(request):
106
  return jsonify({"error": {"message": "Invalid API key", "code": "invalid_api_key"}}), 401
 
 
 
107
 
108
  return jsonify({"data": [{"id": model} for model in models], "object": "list"})
109
 
 
3
  import logging
4
  import time
5
  from gradio_client import Client
6
+ from json.decoder import JSONDecodeError # Import JSONDecodeError
7
 
8
  app = Flask(__name__)
9
 
 
13
  logger = logging.getLogger(__name__)
14
 
15
  # Initialize JARVIS client
16
+ jarvis = None # Initialize as None
17
+
18
+ def initialize_jarvis_client(max_retries=3, retry_delay=5):
19
+ global jarvis # Use the global jarvis variable
20
+ for attempt in range(max_retries):
21
+ try:
22
+ jarvis = Client("hadadrjt/ai")
23
+ logger.info("JARVIS client initialized successfully.")
24
+ return True # Indicate success
25
+ except JSONDecodeError as e: # Catch JSONDecodeError specifically
26
+ logger.warning(f"Attempt {attempt + 1}/{max_retries}: JSONDecodeError during JARVIS client initialization: {e}")
27
+ if attempt < max_retries - 1:
28
+ time.sleep(retry_delay) # Wait before retrying
29
+ else:
30
+ logger.error("Max retries reached. JARVIS client initialization failed due to JSONDecodeError.")
31
+ return False # Indicate failure
32
+ except Exception as e: # Catch other exceptions as well
33
+ logger.error(f"Attempt {attempt + 1}/{max_retries}: Error during JARVIS client initialization: {e}")
34
+ if attempt < max_retries - 1:
35
+ time.sleep(retry_delay)
36
+ else:
37
+ logger.error("Max retries reached. JARVIS client initialization failed due to general exception.")
38
+ return False # Indicate failure
39
+ return False # Return False if loop completes without success
40
+
41
+
42
+ if not initialize_jarvis_client(): # Initialize client with retry mechanism
43
+ print("Failed to initialize JARVIS client after multiple retries. API will likely not function correctly.")
44
+ # It's up to you if you want to exit here or let the Flask app start but potentially fail on requests.
45
+ # For now, let's continue and handle potential None jarvis later.
46
+
47
 
48
  # Define available models
49
  models = [
 
84
  if not authenticate_request(request):
85
  return jsonify({"error": {"message": "Invalid API key", "code": "invalid_api_key"}}), 401
86
 
87
+ if jarvis is None: # Check if jarvis client is initialized
88
+ return jsonify({"error": {"message": "JARVIS client failed to initialize. API not available.", "code": "jarvis_not_initialized"}}), 500
89
+
90
+
91
  data = request.json
92
  messages = data.get("messages", [])
93
  model = data.get("model", "JARVIS: 2.1.2")
 
139
  def list_models():
140
  if not authenticate_request(request):
141
  return jsonify({"error": {"message": "Invalid API key", "code": "invalid_api_key"}}), 401
142
+ if jarvis is None: # Check if jarvis client is initialized
143
+ return jsonify({"error": {"message": "JARVIS client failed to initialize. API not available.", "code": "jarvis_not_initialized"}}), 500
144
+
145
 
146
  return jsonify({"data": [{"id": model} for model in models], "object": "list"})
147