Spaces:
Sleeping
Sleeping
maybe fix
Browse files
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
|
| 820 |
-
|
| 821 |
-
|
| 822 |
-
|
| 823 |
-
|
| 824 |
-
|
| 825 |
-
|
| 826 |
-
#
|
| 827 |
-
|
| 828 |
-
|
| 829 |
-
|
| 830 |
-
|
| 831 |
-
|
| 832 |
-
|
| 833 |
-
|
| 834 |
-
|
| 835 |
-
|
| 836 |
-
|
| 837 |
-
|
| 838 |
-
|
| 839 |
-
|
| 840 |
-
|
| 841 |
-
|
| 842 |
-
|
| 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 |
-
|
| 864 |
-
|
| 865 |
-
|
| 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 |
-
|
| 893 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 894 |
canonical_id = existing_data['canonical']['typeId']
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 895 |
|
| 896 |
-
|
| 897 |
-
|
| 898 |
-
|
| 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 = {
|