Fraser commited on
Commit
bfb6a62
·
1 Parent(s): 2b91d4c
Files changed (1) hide show
  1. app.py +81 -95
app.py CHANGED
@@ -885,51 +885,12 @@ async def generate_piclet(image, hf_token: str) -> dict:
885
  # Step 5: Save new discovery if needed
886
  print("Step 5/5: Saving to dataset...")
887
  if discovery_status == 'new':
888
- # Create new canonical
889
- type_id = f"{PicletDiscoveryService.normalize_object_name(object_name)}_canonical"
890
- canonical_data = {
891
- "canonical": {
892
- "objectName": object_name,
893
- "typeId": type_id,
894
- "discoveredBy": user_info['preferred_username'],
895
- "discovererSub": user_info['sub'],
896
- "discovererUsername": user_info['preferred_username'],
897
- "discovererName": user_info.get('name'),
898
- "discovererPicture": user_info.get('picture'),
899
- "discoveredAt": datetime.now().isoformat(),
900
- "scanCount": scan_count,
901
- "picletData": {
902
- "typeId": type_id,
903
- "nickname": stats['name'],
904
- "stats": stats,
905
- "imageUrl": image_result['imageUrl'],
906
- "imageCaption": caption,
907
- "concept": concept_text,
908
- "imagePrompt": image_prompt,
909
- "createdAt": datetime.now().isoformat()
910
- }
911
- },
912
- "variations": []
913
- }
914
- canonical_id = type_id
915
-
916
- PicletDiscoveryService.save_piclet_data(object_name, canonical_data)
917
-
918
- # Update user profile
919
- user_profile["discoveries"].append(type_id)
920
- user_profile["uniqueFinds"] = user_profile.get("uniqueFinds", 0) + 1
921
- user_profile["totalFinds"] = user_profile.get("totalFinds", 0) + 1
922
- user_profile["rarityScore"] = user_profile.get("rarityScore", 0) + 100
923
- PicletDiscoveryService.save_user_data(user_info['sub'], user_profile)
924
-
925
- elif discovery_status == 'variation':
926
- # Create new variation
927
- existing_data = PicletDiscoveryService.load_piclet_data(object_name)
928
- variation_id = f"{PicletDiscoveryService.normalize_object_name(object_name)}_{len(existing_data['variations']) + 1:03d}"
929
-
930
- variation_data = {
931
- "typeId": variation_id,
932
- "attributes": attributes,
933
  "discoveredBy": user_info['preferred_username'],
934
  "discovererSub": user_info['sub'],
935
  "discovererUsername": user_info['preferred_username'],
@@ -938,7 +899,7 @@ async def generate_piclet(image, hf_token: str) -> dict:
938
  "discoveredAt": datetime.now().isoformat(),
939
  "scanCount": scan_count,
940
  "picletData": {
941
- "typeId": variation_id,
942
  "nickname": stats['name'],
943
  "stats": stats,
944
  "imageUrl": image_result['imageUrl'],
@@ -947,65 +908,90 @@ async def generate_piclet(image, hf_token: str) -> dict:
947
  "imagePrompt": image_prompt,
948
  "createdAt": datetime.now().isoformat()
949
  }
950
- }
 
 
 
951
 
952
- existing_data['variations'].append(variation_data)
953
- PicletDiscoveryService.save_piclet_data(object_name, existing_data)
954
 
955
- # Update user profile
956
- user_profile["discoveries"].append(variation_id)
957
- user_profile["totalFinds"] = user_profile.get("totalFinds", 0) + 1
958
- user_profile["rarityScore"] = user_profile.get("rarityScore", 0) + 50
959
- PicletDiscoveryService.save_user_data(user_info['sub'], user_profile)
960
-
961
- # Build complete response
962
- piclet_data = {
963
- "typeId": canonical_id,
964
- "nickname": stats['name'],
965
- "stats": stats,
966
- "imageUrl": image_result['imageUrl'],
967
- "imageCaption": caption,
968
- "concept": concept_text,
969
- "imagePrompt": image_prompt,
970
- "objectName": object_name,
971
  "attributes": attributes,
972
- "discoveryStatus": discovery_status,
 
 
 
 
 
973
  "scanCount": scan_count,
974
- "createdAt": datetime.now().isoformat()
 
 
 
 
 
 
 
 
 
975
  }
976
 
977
- messages = {
978
- 'new': f"Congratulations! You discovered the first {object_name} Piclet!",
979
- 'variation': f"You found a new variation of {object_name}!",
980
- 'existing': f"You encountered a known {object_name} Piclet."
981
- }
982
 
983
- return {
984
- "success": True,
985
- "piclet": piclet_data,
986
- "discoveryStatus": discovery_status,
987
- "canonicalId": canonical_id,
988
- "message": messages.get(discovery_status, "Piclet generated!")
989
- }
990
 
991
- except Exception as e:
992
- print(f"Failed to generate Piclet: {e}")
993
- import traceback
994
- traceback.print_exc()
995
- return {
996
- "success": False,
997
- "error": str(e)
998
- }
999
- finally:
1000
- # Clean up temporary image file
1001
- if os.path.exists(temp_image_path):
1002
- try:
1003
- os.unlink(temp_image_path)
1004
- except:
1005
- pass
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1006
 
1007
  except Exception as e:
1008
- print(f"Failed to process image: {e}")
 
 
1009
  return {
1010
  "success": False,
1011
  "error": str(e)
 
885
  # Step 5: Save new discovery if needed
886
  print("Step 5/5: Saving to dataset...")
887
  if discovery_status == 'new':
888
+ # Create new canonical
889
+ type_id = f"{PicletDiscoveryService.normalize_object_name(object_name)}_canonical"
890
+ canonical_data = {
891
+ "canonical": {
892
+ "objectName": object_name,
893
+ "typeId": type_id,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
894
  "discoveredBy": user_info['preferred_username'],
895
  "discovererSub": user_info['sub'],
896
  "discovererUsername": user_info['preferred_username'],
 
899
  "discoveredAt": datetime.now().isoformat(),
900
  "scanCount": scan_count,
901
  "picletData": {
902
+ "typeId": type_id,
903
  "nickname": stats['name'],
904
  "stats": stats,
905
  "imageUrl": image_result['imageUrl'],
 
908
  "imagePrompt": image_prompt,
909
  "createdAt": datetime.now().isoformat()
910
  }
911
+ },
912
+ "variations": []
913
+ }
914
+ canonical_id = type_id
915
 
916
+ PicletDiscoveryService.save_piclet_data(object_name, canonical_data)
 
917
 
918
+ # Update user profile
919
+ user_profile["discoveries"].append(type_id)
920
+ user_profile["uniqueFinds"] = user_profile.get("uniqueFinds", 0) + 1
921
+ user_profile["totalFinds"] = user_profile.get("totalFinds", 0) + 1
922
+ user_profile["rarityScore"] = user_profile.get("rarityScore", 0) + 100
923
+ PicletDiscoveryService.save_user_data(user_info['sub'], user_profile)
924
+
925
+ elif discovery_status == 'variation':
926
+ # Create new variation
927
+ existing_data = PicletDiscoveryService.load_piclet_data(object_name)
928
+ variation_id = f"{PicletDiscoveryService.normalize_object_name(object_name)}_{len(existing_data['variations']) + 1:03d}"
929
+
930
+ variation_data = {
931
+ "typeId": variation_id,
 
 
932
  "attributes": attributes,
933
+ "discoveredBy": user_info['preferred_username'],
934
+ "discovererSub": user_info['sub'],
935
+ "discovererUsername": user_info['preferred_username'],
936
+ "discovererName": user_info.get('name'),
937
+ "discovererPicture": user_info.get('picture'),
938
+ "discoveredAt": datetime.now().isoformat(),
939
  "scanCount": scan_count,
940
+ "picletData": {
941
+ "typeId": variation_id,
942
+ "nickname": stats['name'],
943
+ "stats": stats,
944
+ "imageUrl": image_result['imageUrl'],
945
+ "imageCaption": caption,
946
+ "concept": concept_text,
947
+ "imagePrompt": image_prompt,
948
+ "createdAt": datetime.now().isoformat()
949
+ }
950
  }
951
 
952
+ existing_data['variations'].append(variation_data)
953
+ PicletDiscoveryService.save_piclet_data(object_name, existing_data)
 
 
 
954
 
955
+ # Update user profile
956
+ user_profile["discoveries"].append(variation_id)
957
+ user_profile["totalFinds"] = user_profile.get("totalFinds", 0) + 1
958
+ user_profile["rarityScore"] = user_profile.get("rarityScore", 0) + 50
959
+ PicletDiscoveryService.save_user_data(user_info['sub'], user_profile)
 
 
960
 
961
+ # Build complete response
962
+ piclet_data = {
963
+ "typeId": canonical_id,
964
+ "nickname": stats['name'],
965
+ "stats": stats,
966
+ "imageUrl": image_result['imageUrl'],
967
+ "imageCaption": caption,
968
+ "concept": concept_text,
969
+ "imagePrompt": image_prompt,
970
+ "objectName": object_name,
971
+ "attributes": attributes,
972
+ "discoveryStatus": discovery_status,
973
+ "scanCount": scan_count,
974
+ "createdAt": datetime.now().isoformat()
975
+ }
976
+
977
+ messages = {
978
+ 'new': f"Congratulations! You discovered the first {object_name} Piclet!",
979
+ 'variation': f"You found a new variation of {object_name}!",
980
+ 'existing': f"You encountered a known {object_name} Piclet."
981
+ }
982
+
983
+ return {
984
+ "success": True,
985
+ "piclet": piclet_data,
986
+ "discoveryStatus": discovery_status,
987
+ "canonicalId": canonical_id,
988
+ "message": messages.get(discovery_status, "Piclet generated!")
989
+ }
990
 
991
  except Exception as e:
992
+ print(f"Failed to generate Piclet: {e}")
993
+ import traceback
994
+ traceback.print_exc()
995
  return {
996
  "success": False,
997
  "error": str(e)