Fraser commited on
Commit
2b91d4c
·
1 Parent(s): 251ab1b
Files changed (1) hide show
  1. app.py +65 -76
app.py CHANGED
@@ -816,86 +816,75 @@ async def generate_piclet(image, hf_token: str) -> dict:
816
  # Get user profile (creates if doesn't exist)
817
  user_profile = PicletDiscoveryService.get_or_create_user_profile(user_info)
818
 
819
- # Extract image from Gradio input - save to temp file to pass to API calls
820
- # Gradio cleans up temp files, so we save to our own temp file
821
- from PIL import Image as PILImage
822
- import shutil
823
-
824
- image_path = image if isinstance(image, str) else image.name if hasattr(image, 'name') else str(image)
825
-
826
- # Save to a temporary file that we control
827
- with tempfile.NamedTemporaryFile(mode='wb', suffix='.jpg', delete=False) as f:
828
- temp_image_path = f.name
829
- img = PILImage.open(image_path)
830
- img.save(temp_image_path, format='JPEG')
831
-
832
- try:
833
- # Step 1: Generate caption
834
- print("Step 1/5: Generating image caption...")
835
- caption = await PicletGeneratorService.generate_enhanced_caption(temp_image_path, hf_token)
836
-
837
- # Step 2: Generate concept
838
- print("Step 2/5: Generating Piclet concept...")
839
- concept_data = await PicletGeneratorService.generate_piclet_concept(caption, hf_token)
840
-
841
- object_name = concept_data['objectName']
842
- attributes = concept_data['attributes']
843
- stats = concept_data['stats']
844
- image_prompt = concept_data['imagePrompt']
845
- concept_text = concept_data['concept']
846
-
847
- # Step 3: Generate image
848
- print("Step 3/5: Generating Piclet image...")
849
- image_result = await PicletGeneratorService.generate_piclet_image(
850
- image_prompt,
851
- stats['tier'],
852
- hf_token
853
- )
854
-
855
- # Step 4: Check for canonical/variation
856
- print("Step 4/5: Checking for existing canonical...")
857
- existing_data = PicletDiscoveryService.load_piclet_data(object_name)
858
-
859
- discovery_status = 'new'
860
- canonical_id = None
861
- scan_count = 1
862
 
863
- if existing_data:
864
- # Check if this is an exact canonical match (no attributes)
865
- if not attributes or len(attributes) == 0:
866
- discovery_status = 'existing'
867
- canonical_id = existing_data['canonical']['typeId']
868
- # Increment scan count
869
- existing_data['canonical']['scanCount'] = existing_data['canonical'].get('scanCount', 0) + 1
870
- scan_count = existing_data['canonical']['scanCount']
871
- PicletDiscoveryService.save_piclet_data(object_name, existing_data)
872
- else:
873
- # Check for matching variation
874
- variations = existing_data.get('variations', [])
875
- matched_variation = None
876
-
877
- for variation in variations:
878
- var_attrs = set(variation.get('attributes', []))
879
- search_attrs = set(attributes)
880
- overlap = len(var_attrs.intersection(search_attrs))
881
-
882
- if overlap >= len(search_attrs) * 0.5:
883
- matched_variation = variation
884
- discovery_status = 'existing'
885
- canonical_id = existing_data['canonical']['typeId']
886
- # Increment variation scan count
887
- variation['scanCount'] = variation.get('scanCount', 0) + 1
888
- scan_count = variation['scanCount']
889
- PicletDiscoveryService.save_piclet_data(object_name, existing_data)
890
- break
891
 
892
- if not matched_variation:
893
- discovery_status = 'variation'
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
894
  canonical_id = existing_data['canonical']['typeId']
 
 
 
 
 
 
 
 
 
895
 
896
- # Step 5: Save new discovery if needed
897
- print("Step 5/5: Saving to dataset...")
898
- if discovery_status == 'new':
899
  # Create new canonical
900
  type_id = f"{PicletDiscoveryService.normalize_object_name(object_name)}_canonical"
901
  canonical_data = {
 
816
  # Get user profile (creates if doesn't exist)
817
  user_profile = PicletDiscoveryService.get_or_create_user_profile(user_info)
818
 
819
+ # Extract image path from Gradio input (already a filepath string)
820
+ image_path = image if isinstance(image, str) else str(image)
821
+
822
+ # Step 1: Generate caption
823
+ print("Step 1/5: Generating image caption...")
824
+ caption = await PicletGeneratorService.generate_enhanced_caption(image_path, hf_token)
825
+
826
+ # Step 2: Generate concept
827
+ print("Step 2/5: Generating Piclet concept...")
828
+ concept_data = await PicletGeneratorService.generate_piclet_concept(caption, hf_token)
829
+
830
+ object_name = concept_data['objectName']
831
+ attributes = concept_data['attributes']
832
+ stats = concept_data['stats']
833
+ image_prompt = concept_data['imagePrompt']
834
+ concept_text = concept_data['concept']
835
+
836
+ # Step 3: Generate image
837
+ print("Step 3/5: Generating Piclet image...")
838
+ image_result = await PicletGeneratorService.generate_piclet_image(
839
+ image_prompt,
840
+ stats['tier'],
841
+ hf_token
842
+ )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
843
 
844
+ # Step 4: Check for canonical/variation
845
+ print("Step 4/5: Checking for existing canonical...")
846
+ existing_data = PicletDiscoveryService.load_piclet_data(object_name)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
847
 
848
+ discovery_status = 'new'
849
+ canonical_id = None
850
+ scan_count = 1
851
+
852
+ if existing_data:
853
+ # Check if this is an exact canonical match (no attributes)
854
+ if not attributes or len(attributes) == 0:
855
+ discovery_status = 'existing'
856
+ canonical_id = existing_data['canonical']['typeId']
857
+ # Increment scan count
858
+ existing_data['canonical']['scanCount'] = existing_data['canonical'].get('scanCount', 0) + 1
859
+ scan_count = existing_data['canonical']['scanCount']
860
+ PicletDiscoveryService.save_piclet_data(object_name, existing_data)
861
+ else:
862
+ # Check for matching variation
863
+ variations = existing_data.get('variations', [])
864
+ matched_variation = None
865
+
866
+ for variation in variations:
867
+ var_attrs = set(variation.get('attributes', []))
868
+ search_attrs = set(attributes)
869
+ overlap = len(var_attrs.intersection(search_attrs))
870
+
871
+ if overlap >= len(search_attrs) * 0.5:
872
+ matched_variation = variation
873
+ discovery_status = 'existing'
874
  canonical_id = existing_data['canonical']['typeId']
875
+ # Increment variation scan count
876
+ variation['scanCount'] = variation.get('scanCount', 0) + 1
877
+ scan_count = variation['scanCount']
878
+ PicletDiscoveryService.save_piclet_data(object_name, existing_data)
879
+ break
880
+
881
+ if not matched_variation:
882
+ discovery_status = 'variation'
883
+ canonical_id = existing_data['canonical']['typeId']
884
 
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 = {