CORVO-AI commited on
Commit
b48e6ab
·
verified ·
1 Parent(s): 73f8a69

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +3 -32
app.py CHANGED
@@ -118,7 +118,7 @@ def chat_with_assistant(user_input, chat_history, bot_id, workspace_id):
118
  {
119
  "type": "image_url",
120
  "image_url": {
121
- "url": chat_history.get("image_url", "") # User-provided image URL
122
  }
123
  }
124
  ]
@@ -134,42 +134,27 @@ def chat_with_assistant(user_input, chat_history, bot_id, workspace_id):
134
 
135
  botpress_url = "https://api.botpress.cloud/v1/cognitive/chat-gpt/query"
136
 
137
- # Attempt to send the request
138
  try:
139
  response = requests.post(botpress_url, json=payload, headers=headers)
140
 
141
- # If successful (200)
142
  if response.status_code == 200:
143
  data = response.json()
144
  assistant_content = data.get('choices', [{}])[0].get('message', {}).get('content', '')
145
  return assistant_content, bot_id, workspace_id
146
-
147
- # If we get a 403, it could be because the bot/workspace IDs are invalid/expired
148
  elif response.status_code == 403:
149
  raise Exception("Invalid or expired bot ID.")
150
-
151
- # Other errors
152
  else:
153
  return f"Error {response.status_code}: {response.text}", bot_id, workspace_id
154
-
155
  except Exception as e:
156
- # Handle exceptions (e.g., invalid/expired bot ID)
157
  if "Invalid or expired bot ID" in str(e):
158
- # Attempt to delete old IDs if they exist
159
  if bot_id and workspace_id:
160
  delete_bot(bot_id, workspace_id)
161
  delete_workspace(workspace_id)
162
-
163
- # Create fresh IDs
164
  new_workspace = create_workspace()
165
  new_bot = create_bot(new_workspace)
166
  if not new_workspace or not new_bot:
167
  return "Failed to regenerate workspace or bot IDs.", None, None
168
-
169
- # Update headers with the new bot ID
170
  headers["x-bot-id"] = new_bot
171
-
172
- # Retry
173
  retry_response = requests.post(botpress_url, json=payload, headers=headers)
174
  if retry_response.status_code == 200:
175
  data = retry_response.json()
@@ -178,13 +163,9 @@ def chat_with_assistant(user_input, chat_history, bot_id, workspace_id):
178
  else:
179
  return f"Error {retry_response.status_code}: {retry_response.text}", new_bot, new_workspace
180
  else:
181
- # Other exceptions
182
  return f"Unexpected error: {str(e)}", bot_id, workspace_id
183
 
184
-
185
- # -------------------------------------------------------------------
186
  # Flask Endpoint
187
- # -------------------------------------------------------------------
188
  @app.route("/chat", methods=["POST"])
189
  def chat_endpoint():
190
  """
@@ -205,24 +186,17 @@ def chat_endpoint():
205
  user_input = data.get("user_input", "")
206
  image_url = data.get("image_url", "") # Optional image URL
207
 
208
- # Construct chat history with text and image
209
- chat_history = {
210
- "image_url": image_url # Pass the image URL to the payload
211
- }
212
-
213
  # If we don't yet have a workspace or bot, create them
214
  if not GLOBAL_WORKSPACE_ID or not GLOBAL_BOT_ID:
215
  GLOBAL_WORKSPACE_ID = create_workspace()
216
  GLOBAL_BOT_ID = create_bot(GLOBAL_WORKSPACE_ID)
217
-
218
- # If creation failed
219
  if not GLOBAL_WORKSPACE_ID or not GLOBAL_BOT_ID:
220
  return jsonify({"assistant_response": "Could not create workspace or bot."}), 500
221
 
222
  # Call our function that interacts with Botpress GPT-4
223
  assistant_response, updated_bot_id, updated_workspace_id = chat_with_assistant(
224
  user_input,
225
- chat_history,
226
  GLOBAL_BOT_ID,
227
  GLOBAL_WORKSPACE_ID
228
  )
@@ -233,9 +207,6 @@ def chat_endpoint():
233
 
234
  return jsonify({"assistant_response": assistant_response})
235
 
236
-
237
- # -------------------------------------------------------------------
238
- # Run the Flask app (example)
239
- # -------------------------------------------------------------------
240
  if __name__ == "__main__":
241
  app.run(host="0.0.0.0", port=7860, debug=True)
 
118
  {
119
  "type": "image_url",
120
  "image_url": {
121
+ "url": image_url # User-provided image URL
122
  }
123
  }
124
  ]
 
134
 
135
  botpress_url = "https://api.botpress.cloud/v1/cognitive/chat-gpt/query"
136
 
 
137
  try:
138
  response = requests.post(botpress_url, json=payload, headers=headers)
139
 
 
140
  if response.status_code == 200:
141
  data = response.json()
142
  assistant_content = data.get('choices', [{}])[0].get('message', {}).get('content', '')
143
  return assistant_content, bot_id, workspace_id
 
 
144
  elif response.status_code == 403:
145
  raise Exception("Invalid or expired bot ID.")
 
 
146
  else:
147
  return f"Error {response.status_code}: {response.text}", bot_id, workspace_id
 
148
  except Exception as e:
 
149
  if "Invalid or expired bot ID" in str(e):
 
150
  if bot_id and workspace_id:
151
  delete_bot(bot_id, workspace_id)
152
  delete_workspace(workspace_id)
 
 
153
  new_workspace = create_workspace()
154
  new_bot = create_bot(new_workspace)
155
  if not new_workspace or not new_bot:
156
  return "Failed to regenerate workspace or bot IDs.", None, None
 
 
157
  headers["x-bot-id"] = new_bot
 
 
158
  retry_response = requests.post(botpress_url, json=payload, headers=headers)
159
  if retry_response.status_code == 200:
160
  data = retry_response.json()
 
163
  else:
164
  return f"Error {retry_response.status_code}: {retry_response.text}", new_bot, new_workspace
165
  else:
 
166
  return f"Unexpected error: {str(e)}", bot_id, workspace_id
167
 
 
 
168
  # Flask Endpoint
 
169
  @app.route("/chat", methods=["POST"])
170
  def chat_endpoint():
171
  """
 
186
  user_input = data.get("user_input", "")
187
  image_url = data.get("image_url", "") # Optional image URL
188
 
 
 
 
 
 
189
  # If we don't yet have a workspace or bot, create them
190
  if not GLOBAL_WORKSPACE_ID or not GLOBAL_BOT_ID:
191
  GLOBAL_WORKSPACE_ID = create_workspace()
192
  GLOBAL_BOT_ID = create_bot(GLOBAL_WORKSPACE_ID)
 
 
193
  if not GLOBAL_WORKSPACE_ID or not GLOBAL_BOT_ID:
194
  return jsonify({"assistant_response": "Could not create workspace or bot."}), 500
195
 
196
  # Call our function that interacts with Botpress GPT-4
197
  assistant_response, updated_bot_id, updated_workspace_id = chat_with_assistant(
198
  user_input,
199
+ image_url,
200
  GLOBAL_BOT_ID,
201
  GLOBAL_WORKSPACE_ID
202
  )
 
207
 
208
  return jsonify({"assistant_response": assistant_response})
209
 
210
+ # Run the Flask app
 
 
 
211
  if __name__ == "__main__":
212
  app.run(host="0.0.0.0", port=7860, debug=True)