rairo commited on
Commit
6d840b9
·
verified ·
1 Parent(s): 84f8480

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +20 -26
main.py CHANGED
@@ -339,20 +339,19 @@ def generate_epiphany():
339
 
340
  @app.route('/api/epiphany/theia', methods=['POST'])
341
  def theia_sweep():
342
- """Standalone Theia Mode: Bounding Box Annotations via Code Execution."""
343
  logger.info(">>> THEIA SWEEP INITIATED")
344
  uid = verify_token(request.headers.get('Authorization'))
345
  if not uid: return jsonify({'error': 'Unauthorized'}), 401
346
 
347
- # 1. Defend against missing parameters (Fixed 400 logic)
348
  epiphany_id = request.form.get('epiphanyId')
349
- if not epiphany_id:
350
- logger.error("Theia Sweep failed: epiphanyId missing from form.")
351
- return jsonify({'error': 'epiphanyId is required.'}), 400
352
 
353
- if 'image' not in request.files:
354
- logger.error("Theia Sweep failed: Image file missing.")
355
- return jsonify({'error': 'image file is required.'}), 400
 
 
356
 
357
  # 2. Check Sparks
358
  user_ref = db_ref.child(f'users/{uid}')
@@ -360,27 +359,21 @@ def theia_sweep():
360
  if user_data.get('credits', 0) < 4:
361
  return jsonify({'error': 'Need 4 Sparks for a Theia Sweep.'}), 402
362
 
363
- # 3. Fetch Context from the Epiphany
364
- epiphany_context = db_ref.child(f'epiphanies/{epiphany_id}').get() or {}
365
- subject = epiphany_context.get('subject', 'Complex System')
366
-
367
  image_file = request.files['image']
368
- image_bytes = image_file.read()
369
-
370
  sweep_prompt = f"""
371
- Activate Theia Mode for: {subject}.
372
- Using Python Code Execution, perform a spatial deconstruction of this image.
373
- Identify every functional, biological, or structural component.
374
- For each component detected, return a JSON object:
375
- 1. label: The name of the part.
376
  2. coordinates: [ymin, xmin, ymax, xmax] (normalized 0-1000).
377
- 3. micro_epiphany: A 20-word Feynman-style secret about this part's role.
378
-
379
- Return a JSON LIST ONLY.
380
  """
381
 
382
  try:
383
- pil_image = Image.open(io.BytesIO(image_bytes)).convert('RGB')
384
  res = client.models.generate_content(
385
  model=ATHENA_FLASH,
386
  contents=[sweep_prompt, pil_image],
@@ -395,13 +388,14 @@ def theia_sweep():
395
  raw_json = re.search(r'```json\n(.*?)\n```', raw_json, re.DOTALL).group(1)
396
 
397
  annotations = json.loads(raw_json)
398
- # Charge 4 sparks for Code Execution
 
 
399
  user_ref.update({'credits': user_data.get('credits', 0) - 4})
400
 
401
- logger.info(f"THEIA SUCCESS: {len(annotations)} components annotated.")
402
  return jsonify({"annotations": annotations}), 200
403
  except Exception as e:
404
- logger.error(f"Theia Sweep Engine Error: {e}")
405
  return jsonify({'error': str(e)}), 500
406
 
407
  @app.route('/api/epiphany/deep-dive', methods=['POST'])
 
339
 
340
  @app.route('/api/epiphany/theia', methods=['POST'])
341
  def theia_sweep():
342
+ """Standalone Theia Mode: Bounding Box Annotations with DB Persistence."""
343
  logger.info(">>> THEIA SWEEP INITIATED")
344
  uid = verify_token(request.headers.get('Authorization'))
345
  if not uid: return jsonify({'error': 'Unauthorized'}), 401
346
 
 
347
  epiphany_id = request.form.get('epiphanyId')
348
+ if not epiphany_id: return jsonify({'error': 'epiphanyId is required.'}), 400
 
 
349
 
350
+ # 1. Check if annotations already exist to prevent double-charging
351
+ epiphany_ref = db_ref.child(f'epiphanies/{epiphany_id}')
352
+ existing_data = epiphany_ref.get() or {}
353
+ if 'annotations' in existing_data:
354
+ return jsonify({"annotations": existing_data['annotations'], "status": "already_stored"}), 200
355
 
356
  # 2. Check Sparks
357
  user_ref = db_ref.child(f'users/{uid}')
 
359
  if user_data.get('credits', 0) < 4:
360
  return jsonify({'error': 'Need 4 Sparks for a Theia Sweep.'}), 402
361
 
 
 
 
 
362
  image_file = request.files['image']
363
+ subject = existing_data.get('subject', 'Complex System')
364
+
365
  sweep_prompt = f"""
366
+ Theia Mode Activation: {subject}.
367
+ Use Python Code Execution for spatial deconstruction.
368
+ Identify every functional component. For each return:
369
+ 1. label: name of the part.
 
370
  2. coordinates: [ymin, xmin, ymax, xmax] (normalized 0-1000).
371
+ 3. micro_epiphany: 20-word Feynman secret.
372
+ Return JSON list ONLY.
 
373
  """
374
 
375
  try:
376
+ pil_image = Image.open(io.BytesIO(image_file.read())).convert('RGB')
377
  res = client.models.generate_content(
378
  model=ATHENA_FLASH,
379
  contents=[sweep_prompt, pil_image],
 
388
  raw_json = re.search(r'```json\n(.*?)\n```', raw_json, re.DOTALL).group(1)
389
 
390
  annotations = json.loads(raw_json)
391
+
392
+ # 3. STORE IN DB & CHARGE
393
+ epiphany_ref.update({"annotations": annotations})
394
  user_ref.update({'credits': user_data.get('credits', 0) - 4})
395
 
 
396
  return jsonify({"annotations": annotations}), 200
397
  except Exception as e:
398
+ logger.error(f"Theia Sweep Error: {e}")
399
  return jsonify({'error': str(e)}), 500
400
 
401
  @app.route('/api/epiphany/deep-dive', methods=['POST'])