CORVO-AI commited on
Commit
de2fd30
·
verified ·
1 Parent(s): d1cd13b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +51 -6
app.py CHANGED
@@ -92,22 +92,67 @@ def chat():
92
  token = request_data.get("token") if request_data else None
93
  message = request_data.get("message") if request_data else None
94
 
 
95
  if not token:
96
- return jsonify({"error": "No token provided in the request."}), 400
97
  if not message:
98
- return jsonify({"error": "No message provided in the request."}), 400
99
 
100
  # Check if the token is valid
101
  if not is_token_valid(token):
102
- return jsonify({"error": "You must subscribe to continue."}), 403
 
 
 
 
 
 
103
 
104
  # Generate AI response using Brain Inc API
105
  ai_response = generate_ai_response(message)
106
- return jsonify({"response": ai_response})
 
 
 
 
 
 
 
 
 
 
 
107
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
108
  except Exception as e:
109
- # Catch any unexpected errors
110
- return jsonify({"error": "An error occurred", "details": str(e)}), 500
111
 
112
 
113
  if __name__ == "__main__":
 
92
  token = request_data.get("token") if request_data else None
93
  message = request_data.get("message") if request_data else None
94
 
95
+ # Error Handling for Missing Parameters
96
  if not token:
97
+ return jsonify({"error": "Token Not Provided", "details": "Please provide a valid token in the request body."}), 400
98
  if not message:
99
+ return jsonify({"error": "Message Not Provided", "details": "Please provide a message in the request body."}), 400
100
 
101
  # Check if the token is valid
102
  if not is_token_valid(token):
103
+ # Enhanced Error Response for Token Validation
104
+ if token_expired(token):
105
+ return jsonify({"error": "Token Expired", "details": "Your token has expired. Please renew your subscription to continue."}), 403
106
+ elif token_not_found(token):
107
+ return jsonify({"error": "Token Not Found", "details": "The provided token does not exist. Please check your token or subscribe to the service."}), 403
108
+ else:
109
+ return jsonify({"error": "Invalid Token", "details": "The provided token is invalid. Please check your token or contact support."}), 403
110
 
111
  # Generate AI response using Brain Inc API
112
  ai_response = generate_ai_response(message)
113
+ if ai_response == "Error generating response":
114
+ return jsonify({"error": "AI Response Generation Failed", "details": "Failed to generate a response from the AI service. Please try again later."}), 500
115
+ else:
116
+ return jsonify({"response": ai_response})
117
+
118
+ except requests.exceptions.RequestException as e:
119
+ # Catch API Request Exceptions
120
+ return jsonify({"error": "API Request Error", "details": str(e)}), 500
121
+ except Exception as e:
122
+ # Catch Any Unexpected Errors
123
+ return jsonify({"error": "An Unexpected Error Occurred", "details": str(e)}), 500
124
+
125
 
126
+ # Helper functions for more specific token error handling
127
+ def token_expired(token):
128
+ try:
129
+ response = requests.get(SEPTIMIUS_JSON_URL)
130
+ if response.status_code == 200:
131
+ json_data = response.json()
132
+ if json_data.get("success"):
133
+ for entry in json_data["data"]:
134
+ if entry.get("token") == token:
135
+ expired_date = entry.get("expired_date")
136
+ if expired_date and datetime.fromisoformat(expired_date) <= datetime.now():
137
+ return True
138
+ except Exception as e:
139
+ print(f"Error checking token expiration: {e}")
140
+ return False
141
+
142
+
143
+ def token_not_found(token):
144
+ try:
145
+ response = requests.get(SEPTIMIUS_JSON_URL)
146
+ if response.status_code == 200:
147
+ json_data = response.json()
148
+ if json_data.get("success"):
149
+ for entry in json_data["data"]:
150
+ if entry.get("token") == token:
151
+ return False # Token found
152
+ return True # Token not found in the loop
153
  except Exception as e:
154
+ print(f"Error checking token existence: {e}")
155
+ return True # Assume not found if an error occurs
156
 
157
 
158
  if __name__ == "__main__":